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");
    }
相关推荐
Brookty几秒前
网络通信核心:四元组、socket与IO机制详解
java·网络通信·网络入门
佩奇大王11 分钟前
P159 摆动序列
java·开发语言·算法
计算机学姐13 分钟前
基于SpringBoot的网吧管理系统
java·spring boot·后端·spring·tomcat·intellij-idea·mybatis
Boop_wu15 分钟前
[Java EE 进阶] SpringBoot 配置文件全解析:properties 与 yml 的使用(1)
java·spring boot·spring·java-ee
我不是秋秋16 分钟前
软件开发项目各角色关系解析:产品/前后端/测试如何高效协作?
java·算法·面试·职场和发展·哈希算法
青衫客3618 分钟前
浅谈 Java 后端对象映射:从 JSON → VO → Entity 的原理与实践
java·json
大阿明9 小时前
Spring Boot(快速上手)
java·spring boot·后端
bearpping9 小时前
Java进阶,时间与日期,包装类,正则表达式
java
邵奈一9 小时前
清明纪念·时光信笺——项目运行指南
java·实战·项目
sunwenjian8869 小时前
Java进阶——IO 流
java·开发语言·python