尝试使用AudioKit将PCM转换为MP3时崩溃

时间:2019-03-07 20:05:02

标签: pcm audiokit lame

我正在尝试将iPhone麦克风的音频实时转换为MP3。

我将其设置为:

        let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16,
                               sampleRate: 44100.0,
                               channels: 1,
                               interleaved: true)

    mic.avAudioUnitOrNode.installTap(onBus: 0, bufferSize: AVAudioFrameCount((format?.sampleRate)!), format: format, block: { (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in

        let audioBuffer : AVAudioBuffer = buffer
        self.audioProcessor?.processBuffer( audioBuffer.mutableAudioBufferList)
    })


-(void)processBuffer: (AudioBufferList*) audioBufferList;
{
    const int PCM_SIZE = 8192;
    const int MP3_SIZE = 8192;
    short int pcm_buffer[PCM_SIZE*2];
    unsigned char mp3_buffer[MP3_SIZE];
    int write = lame_encode_buffer_interleaved(mLame, pcm_buffer,(int*) audioBufferList->mBuffers[0].mData, mp3_buffer, MP3_SIZE);

   //some other stuff
}

但是一旦到达编码部分,我就会崩溃。

编辑:

我停止崩溃了,但是音频质量非常苛刻:

    int size = audioBufferList->mBuffers[0].mDataByteSize / 2;
    unsigned char mp3_buffer[size * 4];
    int write = lame_encode_buffer(mLame, audioBufferList->mBuffers[0].mData, audioBufferList->mBuffers[0].mData, size, mp3_buffer, size*4);

1 个答案:

答案 0 :(得分:1)

源音频和编码器之间的采样率不匹配。