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

Crash trying to convert PCM to MP3 using AudioKit

我正在尝试将来自 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);

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