开发者

Objective-c/IOS: What's the simplest way to play an audio file backwards

开发者 https://www.devze.com 2023-04-04 04:23 出处:网络
I\'ve been struggling with this one for quite a while now, and there are no code 开发者_如何学Cexamples of it being done on the net. Can anyone help me?

I've been struggling with this one for quite a while now, and there are no code 开发者_如何学Cexamples of it being done on the net. Can anyone help me?

My app uses AVFoundation to record the audio. @16 bit depth , 2 channels , WAV

I can access the bytes of the audio, I just don't know how to reverse it.


In wave data samples are interleaved. This means the data is organised like this.

Sample 1 Left | Sample 1 Right | Sample 2 Left | Sample 2 right ... Sample n Left | Sample n right

As each sample is 16-bits (2 bytes) the first 2 channel sample (ie for both left and right) is 4 bytes in size.

This way you know that the last sample in a block of wave data is as follows:

wavDataSize - 4

You can then load each sample at a time by copying it into a different buffer by starting at the end of the recording and reading backwards. When you get to the start of the wave data you have reversed the wave data and playing will be reversed.

Edit: If you want easy ways to read wave files on iOS check out the Audio File Services Reference.

Edit 2:

readPoint  = waveDataSize;
writePoint = 0;
while( readPoint > 0 )
{
    readPoint -= 4;
    Uint32 bytesToRead = 4;
    Uint32 sample;
    AudioFileReadBytes( inFile, false, maxData, &bytesToRead &sample );
    AudioFileWriteBytes( outFile, false, writePoint, &bytesToRead, &sample );

    writePoint += 4;
}


Assuming a single-chunk WAV file, try memmapping the file, and copying the samples out in reverse order, starting from the end of the file, into Audio Queue or RemoteIO buffers, during the callbacks, while using one of those APIs to play audio. Stop copying before you get to the WAV/RIFF header (commonly the 1st 44 bytes).


To reverse a audio, why not use the currentPlaybackRate in MPMediaPlayback (https://developer.apple.com/library/ios/#DOCUMENTATION/MediaPlayer/Reference/MPMediaPlayback_protocol/Reference/Reference.html)

0

精彩评论

暂无评论...
验证码 换一张
取 消