feat(audio): 添加单声道转立体声转换功能
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful

- 新增 monoToStereoReader 将单声道 WAV 实时转换为立体声
- PlayWav 自动检测单声道并应用转换管线
- 添加完整的单元测试覆盖转换逻辑
- 整理 import 顺序(goimports)

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
2026-04-09 10:34:45 +08:00
parent 9825a85359
commit 6ac23c28f1
3 changed files with 160 additions and 8 deletions

View File

@@ -7,11 +7,51 @@ import (
"io"
"time"
"github.com/youpy/go-wav"
"github.com/hajimehoshi/go-mp3"
"github.com/youpy/go-wav"
"go.uber.org/zap"
)
// monoToStereoReader 将单声道音频转换为立体声
type monoToStereoReader struct {
src io.Reader
buf []byte
}
func (m *monoToStereoReader) Read(p []byte) (int, error) {
maxSamples := len(p) / 4
if maxSamples == 0 {
return 0, nil
}
// 按需分配缓冲区
if cap(m.buf) < maxSamples*2 {
m.buf = make([]byte, maxSamples*2)
}
// 读取单声道数据
n, err := m.src.Read(m.buf[:maxSamples*2])
if n == 0 {
return 0, err
}
// 单声道→立体声:复制每个样本到左右声道
samples := n / 2
for i := range samples {
base := i * 4
mono := i * 2
p[base] = m.buf[mono] // 左声道低字节
p[base+1] = m.buf[mono+1] // 左声道高字节
p[base+2] = m.buf[mono] // 右声道低字节
p[base+3] = m.buf[mono+1] // 右声道高字节
}
if err == io.EOF {
return samples * 4, io.EOF
}
return samples * 4, nil
}
// PlayWav 播放 WAV 文件(阻塞),直到完成或 context 取消
func PlayWav(ctx context.Context, r io.ReadCloser) error {
// Read the entire file into memory since wav.NewReader needs ReadAt
@@ -33,15 +73,21 @@ func PlayWav(ctx context.Context, r io.ReadCloser) error {
duration, _ := dec.Duration()
sourceRate := int(format.SampleRate)
channels := int(format.NumChannels)
zap.S().Infof("WAV 音频: %d ch, %d Hz, 时长: %v",
format.NumChannels, sourceRate, duration)
zap.S().Infof("WAV 音频: %d ch, %d Hz, 时长: %v", channels, sourceRate, duration)
// 构建处理管线:单声道转换 → 重采样
reader := io.Reader(dec)
if channels == 1 {
zap.S().Infof("单声道转立体声: 1 ch → 2 ch")
reader = &monoToStereoReader{src: dec}
channels = DefaultChannelCount
}
// 需要重采样(使用 Sinc 高质量重采样)
var reader io.Reader = dec
if needsResampling(sourceRate) {
zap.S().Infof("Sinc 重采样: %d Hz → %d Hz", sourceRate, UniversalSampleRate)
reader = newSincResampler(dec, sourceRate, UniversalSampleRate, int(format.NumChannels))
zap.S().Infof("Sinc 重采样: %d Hz → %d Hz, %d ch", sourceRate, UniversalSampleRate, channels)
reader = newSincResampler(reader, sourceRate, UniversalSampleRate, channels)
}
otoCtx, err := initContext()