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");
    }
相关推荐
FreemanGordon22 分钟前
Java volatile 关键字
java
北京_宏哥24 分钟前
《手把手教你》系列基础篇(九十三)-java+ selenium自动化测试-框架设计基础-POM设计模式实现-上篇(详解教程)
java·前端·selenium
北京_宏哥31 分钟前
《手把手教你》系列基础篇(九十二)-java+ selenium自动化测试-框架设计基础-POM设计模式简介(详解教程)
java·selenium·前端工程化
当归102441 分钟前
微服务与消息队列RabbitMQ
java·微服务
weixin_4862814542 分钟前
webRTC实现一对一通话视频流程
音视频·webrtc
Lx35243 分钟前
《从头开始学java,一天一个知识点》之:循环结构:for与while循环的使用场景
java·后端
Cache技术分享1 小时前
15. Java 如何声明一个变量来引用数组
java·前端
雷渊1 小时前
深入分析理解mysql的MVCC
java·数据库·面试
知其然亦知其所以然1 小时前
Java 高级面试题:Lock 到底比 synchronized 强在哪?
java·后端·面试
风象南1 小时前
Spring Boot 的 20个实用技巧
java·spring boot