如何播放 opus 解码帧,或将 1 字节 pcm16 拆分为 2 字节 pcm(bitDepthInBytes)?

问题描述

在录制麦克风时,录制的块是原始 PCM8 格式,我可以通过更改 bitDepthInBytes = 2 来发送和播放它而没有任何噪音,但是当我通过网络发送编码的 opus 帧并将它们解码为PCM16,除非我将它们转换为 PCM8,否则我无法播放它们,但它很吵。这是我的代码

const sampleRate = 48000
const channels = 1
....
....

    dec,err := opus.NewDecoder(sampleRate,channels)
    if err != nil {
        fmt.Println(err)
        return
    }

    var frameSizeMs float32 = 20
    frameSize := int(channels * frameSizeMs * sampleRate / 1000)
    pcm := make([]int16,frameSize)

    // (sampleRate int,channelNum int,bitDepthInBytes int,bufferSizeInBytes int)
    context,err := oto.NewContext(sampleRate,channels,1,frameSize*2)
    if err != nil {
        log.Fatal(err)
    }
    player := context.NewPlayer()

    ...
    ...

    _,err := dec.Decode(data,pcm)
    if err != nil {
        fmt.Println(err)
    }

    var mask uint16 = 0x8000


    pcm8 := make([]byte,frameSize)
    for i := 0; i < frameSize; i++ {
        // using this work and play sound but it has noise
        pcm8[i] = byte((uint16(pcm[i]) ^ mask) >> 8)

    }
    
    _,_ = player.Write(pcm8)

解决方法

通过阅读本文,我能够知道如何将您的 PCM 缓冲区格式化为可播放的音频字节 https://github.com/philfrei/AudioCue/blob/master/src/main/java/com/adonax/audiocue/AudioCue.java,这是我使用过的片段:

public static byte[] fromBufferToAudioBytes(byte[] audioBytes,float[] buffer)
{
    for (int i = 0,n = buffer.length; i < n; i++)
    {
        buffer[i] *= 32767;
        
        audioBytes[i*2] = (byte) buffer[i];
        audioBytes[i*2 + 1] = (byte)((int)buffer[i] >> 8 );
    }
    
    return audioBytes;
}

这是我在代码中更新的内容

    pcm8 := make([]byte,frameSize * 2)
    for i := 0; i < frameSize; i++ {
        //pcm[i] *= 32767 // uncomment when pcm array is float insteand of int16
        pcm8[i*2] =  byte(uint16(pcm[i]))
        pcm8[i*2 + 1] = byte(uint16(pcm[i]) >> 8)
    }