2

In my application I have the bytes of an audio file stored in NSMutableData like so:

NSMutableData *data = [NSMutableData dataWithBytes:byteData length:len];

I can then later play this using

player = [[AVAudioPlayer alloc] initWithData:data error:&error];

My question is what code would reverse the bytes in the data object, so that when played the sound is backwards?

Any help is very much appreciated.

By the way, the byteData of the audio is obtained using:

    NSMutableData *wave =[NSMutableData dataWithContentsOfURL:url];

NSUInteger len = [wave length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [wave bytes], len);
  • 1
    if you want to reverse the audio, you can't just reverse the data, unless of course it were unencoded 8-bit/sample mono which it won't be. – Grady Player Sep 09 '11 at 17:02
  • Need to know the format: how many bytes per sample. – zaph Sep 09 '11 at 17:21
  • Well the sample rate is 44100.0, and the format is wav – DaveSmith122 Sep 09 '11 at 17:27
  • 3
    wav can be 8 or 16 bit PCM. A little math given a file size, number of channels, sampling rate and length in seconds should provide the bits/bytes per sample. But it is more complicated, there is a file format to wav, the PCM portions must be determined and they need to be reversed. Best to get a hex editor and figure things out from a wav format document, one is: http://www.sonicspot.com/guide/wavefiles.html – zaph Sep 09 '11 at 17:54
  • Mine is 16 bit PCM, and wow this is turning out to be impossible for me (a newbie, as you can probably tell). Something so simple like reversing audio playback should be easy. – DaveSmith122 Sep 09 '11 at 19:37
  • So what's the easiest way to reverse audio then, how would you do it? – DaveSmith122 Sep 10 '11 at 16:15

1 Answers1

1

DaveSmith122 is right. You cannot just reverse the byte data. I have achieved the same effect using CoreAudio and AudioUnits. Use ExtFileReader C API to read the file into lPCM buffers and then you can reverse the buffers as needed.

rage
  • 1,447
  • 16
  • 16