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");
    }
相关推荐
ArabySide1 分钟前
【Spring Boot】基于MyBatis的条件分页
java·spring boot·后端·mybatis
z***y8628 分钟前
后端服务限流配置,Nginx与Spring Cloud Gateway
java·服务器·nginx
ACP广源盛1392462567313 分钟前
GSV2201S(1201S)@ACP#支持嵌入式 MCU 的 DisplayPort 1.4 到 HDMI 2.0 转换器
单片机·嵌入式硬件·电脑·音视频
熙客18 分钟前
Java集合框架概述
java·开发语言
一只会写代码的猫25 分钟前
深度解析 Java、C# 和 C++ 的内存管理机制:自动 vs 手动
java·jvm·算法
我命由我1234528 分钟前
Java 开发 - 简单消息队列实现、主题消息队列实现
java·开发语言·后端·算法·java-ee·消息队列·intellij-idea
float_六七40 分钟前
SQL中=与IS的区别:关键用法解析
java·数据库·sql
rit843249940 分钟前
配置Spring框架以连接SQL Server数据库
java·数据库·spring
Tony_yitao2 小时前
9.华为OD机试真题 - 最长的顺子 - 2024E卷 Java
java·华为od·algorithm
毕设源码-赖学姐3 小时前
【开题答辩全过程】以 非凡物流公司电商物流管理系统的设计与实现为例,包含答辩的问题和答案
java·eclipse