无法听到'噪音'在iOS中编写随机值AudioBuffer时

时间:2015-01-04 04:31:55

标签: ios objective-c audio core-audio

我正在使用EZAudio并尝试将我的softaware生成的一些示例写入此库https://github.com/syedhali/EZAudio提供的AudioBuffer,如示例“通过手动覆盖播放”中所示。

我的代码看起来像这样......

// Completely override the output callback function
- (void)
                 output:(EZOutput *)output
callbackWithActionFlags:(AudioUnitRenderActionFlags *)ioActionFlags
            inTimeStamp:(const AudioTimeStamp *)inTimeStamp
            inBusNumber:(UInt32)inBusNumber
         inNumberFrames:(UInt32)inNumberFrames
                 ioData:(AudioBufferList *)ioData {
    //grab latest sample from sample queue
    if (currentAudioPiece == nil || currentAudioPiece.duration >= currentAudioPieceIndex) {
        self.currentAudioPiece = sampleQueue.dequeue;
    }
    AudioBuffer audioBuffer = ioData->mBuffers[0];


    if (true) {
        for (int i = 0; i < audioBuffer.mDataByteSize; i++) {
            uint8_t rofl[2048];
            arc4random_buf(&rofl, 2048);
            audioBuffer.mData = rofl;
        }

        return;
    }

    //... more code that I'll debug later...'

基本上我无法进行理智检查,即随机播放的一堆内存会产生一些噪音。我认为问题在于“audioBuffer.mData = rofl;”。我对在* *中处理内存感到困惑。

1 个答案:

答案 0 :(得分:2)

jn_pdx 是正确的,您需要复制/填写mData而不是重新分配。

- (void)
                 output:(EZOutput *)output
callbackWithActionFlags:(AudioUnitRenderActionFlags *)ioActionFlags
            inTimeStamp:(const AudioTimeStamp *)inTimeStamp
            inBusNumber:(UInt32)inBusNumber
         inNumberFrames:(UInt32)inNumberFrames
                 ioData:(AudioBufferList *)ioData {
    //grab latest sample from sample queue
    if (currentAudioPiece == nil || currentAudioPiece.duration >= currentAudioPieceIndex) {
        self.currentAudioPiece = sampleQueue.dequeue;
    }
    AudioBuffer audioBuffer = ioData->mBuffers[0];


    if (true) {
        arc4random_buf(audioBuffer.mData, audioBuffer.mDataByteSize);

        return;
    }