java根据音频流或者音频的地址获取分贝的工具类

工具类

java 复制代码
import lombok.extern.slf4j.Slf4j;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

@Slf4j
public class WavUtils {

    public static double getDecibels(String url) {
        try {
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(URI.create(url).toURL());
            return doCal(audioInputStream);
        } catch (UnsupportedAudioFileException | IOException e) {
            return Double.MAX_VALUE;
        }
    }

    public static double getDecibels(InputStream inputStream) {
        try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(bufferedInputStream);
            return doCal(audioInputStream);
        } catch (IOException | UnsupportedAudioFileException e) {
            return Double.MAX_VALUE;
        }
    }

    private static double doCal(AudioInputStream audioInputStream) throws IOException {
        AudioFormat audioFormat = audioInputStream.getFormat();
        if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
            audioFormat = new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    audioFormat.getSampleRate(),
                    audioFormat.getSampleSizeInBits() * 2,
                    audioFormat.getChannels(),
                    audioFormat.getFrameSize() * 2,
                    audioFormat.getFrameRate(),
                    true);
            audioInputStream = AudioSystem.getAudioInputStream(audioFormat, audioInputStream);
        }
        byte[] buffer = new byte[4096];
        int bytesRead;
        double sum = 0;
        int count = 0;
        while ((bytesRead = audioInputStream.read(buffer)) != -1) {
            for (int i = 0; i < bytesRead; i += 2) {
                // 将字节数据转换为 16 位有符号整数
                short sample = (short) ((buffer[i + 1] << 8) | buffer[i]);
                // 计算样本的振幅
                double amplitude = sample / 32768.0;
                // 计算振幅的平方并累加
                sum += amplitude * amplitude;
                count++;
            }
        }
        double rms = Math.sqrt(sum / count);
        double decibels = 20 * Math.log10(rms);
        decibels = Math.abs(decibels);
        log.info("decibels: {}", decibels);
        return decibels;
    }
}

测试类

java 复制代码
@Test
    public void testDb() {

        try (FileInputStream stream = new FileInputStream("/path/to/file")) {
            double decibels = WavUtils.getDecibels(stream);
        } catch (Exception e) {

        }
		double decibels = WavUtils.getDecibels("url");
    }
相关推荐
lwx572801 小时前
探秘InnoDB:搞懂它的内存、线程、磁盘与日志刷盘策略
java·后端
Flynt3 小时前
从Spring Boot 4.0升到4.1,我在Maven和gRPC上栽了跟头
java·spring boot·后端
plainGeekDev4 小时前
Activity 间传值 → Navigation 参数
android·java·kotlin
plainGeekDev4 小时前
onActivityResult → ActivityResult API
android·java·kotlin
Sunia4 小时前
《AgentX 专栏》10-生产部署:3台2C4G云服务器把企业级Agent真正跑起来的完整方案
java·架构
ZhengEnCi5 小时前
J7A-高级Java工程师面试三道灵魂拷问-深度广度与工程素养的终极检验
java·后端
狼爷1 天前
吃透 Java Function 接口,搞定 99% 的 Stream 场景
java·函数式编程
祎雪双十Gy1 天前
从 DataX 的配置加载说起:我用 FastJson2 做了一个轻量级动态配置管理库
java·后端
小锋java12341 天前
分享一套锋哥原创的SpringBoot4+Vue3宠物领养网站系统
java
考虑考虑1 天前
Java实现hmacsha1加密算法
java·后端·java ee