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");
    }
相关推荐
【D'accumulation】25 分钟前
典型的MVC设计模式:使用JSP和JavaBean相结合的方式来动态生成网页内容典型的MVC设计模式
java·设计模式·mvc
试行40 分钟前
Android实现自定义下拉列表绑定数据
android·java
茜茜西西CeCe1 小时前
移动技术开发:简单计算器界面
java·gitee·安卓·android-studio·移动技术开发·原生安卓开发
救救孩子把1 小时前
Java基础之IO流
java·开发语言
小菜yh1 小时前
关于Redis
java·数据库·spring boot·redis·spring·缓存
宇卿.1 小时前
Java键盘输入语句
java·开发语言
浅念同学1 小时前
算法.图论-并查集上
java·算法·图论
立志成为coding大牛的菜鸟.1 小时前
力扣1143-最长公共子序列(Java详细题解)
java·算法·leetcode
鱼跃鹰飞1 小时前
Leetcode面试经典150题-130.被围绕的区域
java·算法·leetcode·面试·职场和发展·深度优先
爱上语文2 小时前
Springboot的三层架构
java·开发语言·spring boot·后端·spring