音频解码及如何在Java实现

本人并不干这个,但是被迫下水了解了一下这个,稍微做了一下整理。再就是感觉现在网上以及ChatGPT在这方面给出的答案太水了,在此开辟一篇。无意放出代码,这里只介绍一些可能重要的点。

本来以为有了ChatGPT写这些就没有必要了,现在看起来还是不太可能。

  • 一是知识稍微旧了一点,新一点的没有,比如github上近一年更新的优秀项目100%是不会出现在解决方案中的。追求能用是不影响的,但是想找最好的有点难。
  • 二是方案缺少出处,扩展溯源困难。
  • 三是多方案比较困难,不易从中选取出最好的方案。
  • 四是幻觉太多,验证成本太高。

1. 解码

这里将解码定义为将有损/无损的(压缩)格式,比如 mp3/aac 等等转换到 pcm/wav 这种原始数据的格式的操作,供下游使用。

原始数据可以理解为 采样率 × 位深度 × 通道数 × 音频时长 的数据,不加其它处理。

2. 基本参数

来自 javax.sound.sampled.AudioFormat,先要理解记住这些才能够有良好掌控

  • encoding -- the audio encoding technique

  • sampleRate -- the number of samples per second

  • sampleSizeInBits -- the number of bits in each sample

  • channels -- the number of channels (1 for mono, 2 for stereo, and so on)

  • frameSize -- the number of bytes in each frame

  • frameRate -- the number of frames per second

  • bigEndian -- indicates whether the data for a single sample is stored in big-endian byte order

3. FFmpeg

A complete, cross-platform solution to record, convert and stream audio and video.

可能是最强的功能非常齐全,离线格式转换命令行就用它了,但是要注意ffmpeg在做转换时有不少参数直接默认了,使用代码转换可能有各种对不上(所以需要对参数的理解)。

常见音频编码都是确定性编码,不要自欺欺人觉得大小不一致还是正常的,得每一个byte都一样才正常。

4. javax.sound.sampled

  • 支持PCM/WAV之类的原始音频格式
  • service provider: 在实现之后能够自动发现和支持更多格式

5. hendriks73/ffsampledsp

有了1-4的铺垫之后就只需要一个优秀项目来完成剩余的工作了,目前看起来FFSampledSP是最佳选择(之一,服务器的)

FFSampledSP is an implementation of the javax.sound.sampled service provider interfaces based on FFmpeg, a complete, cross-platform solution to record, convert and stream audio and video. FFSampledSP is part of the SampledSP collection of javax.sound.sampled libraries.

  • 相当于引入了ffmepg的能力,在已有的库中实现得是相当好的了。

  • 解码流的时候需要调试参数,可以先校验是否能转换

    java 复制代码
    AudioInputStream fileStream = AudioSystem.getAudioInputStream(file);
    AudioFormat sourceFormat = fileStream.getFormat();
    log.info("in audio format: {}", aacFormat);
    AudioFormat targetFormat = new AudioFormat();
    AudioSystem.isConversionSupported(sourceFormat, targetFormat); // 需要为true
  • 引入依赖即可,并无太多需要注意的。也就是知识要得挺多,使用起来反而没什么。

相关推荐
从琳开始98922 分钟前
网络原理入门(二)------OSI七层模型?TCP/IP五层(四层)模型原来是这样的
java·网络·tcp/ip·java-ee·php
文丑颜不良啊34 分钟前
LeetCode1861~1880题目解析
java·leetcode
Flynn Liang36 分钟前
AgentScope Java 深度解析:ReActAgent、HarnessAgent 与多智能体编排——JVM 上的自治 Agent 底座
java·大模型·架构设计·ai agent·spring ai·langchain4j·agentscope
Flynn Liang1 小时前
上线后怎么排查 AI 调用?Spring AI Observability:token/延迟/成本埋点与全链路追踪
java·人工智能·spring·监控·可观测性·observability·springai
文丑颜不良啊1 小时前
LeetCode1921~1940题目解析
java·leetcode
Tt有了t1 小时前
设计模式速查
java·python·设计模式
Thomas.Sir1 小时前
第15课:SpringBoot连接池详解:HikariCP配置、调优、监控,4.x连接池新特性
java·spring boot
VimorphTech2 小时前
每批开 4 个线程,为什么限制不住 TTS 的总并发?
java·spring boot·spring
Pniubi2 小时前
Java为什么要分堆和栈
java·开发语言
杨凯凡2 小时前
Spring Boot 缓存一致性怎么保证?@Cacheable、@CacheEvict 与事务提交时机实战
java·spring boot·spring cache·后端开发·缓存一致性