如何反转AAC音频文件?

时间:2016-01-16 02:06:19

标签: ios objective-c avfoundation aac

我目前能够反向录制和播放(感谢几个例子),但我想让用户能够从表格视图中选择以前录制的.AAC文件并将其反转。 我已经尝试将输入文件url更改为用户文件的url,但我得到静态或0.0持续时间的音频文件。 AAC文件是否可以进行这种反转?如果是这样,我如何更正我的设置以接受它?

recordedAudioUrl = [NSURL URLWithString:[savedURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog( @"url is %@", [recordedAudioUrl absoluteString]);

flippedAudioUrl = [NSURL URLWithString:[reverseURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog( @"url is %@", [flippedAudioUrl absoluteString]);

AudioFileID outputAudioFile;

AudioStreamBasicDescription myPCMFormat;
myPCMFormat.mSampleRate = 16000.00;
myPCMFormat.mFormatID = kAudioFormatLinearPCM ;
myPCMFormat.mFormatFlags =  kAudioFormatFlagsCanonical;
myPCMFormat.mChannelsPerFrame = 1;
myPCMFormat.mFramesPerPacket = 1;
myPCMFormat.mBitsPerChannel = 16;
myPCMFormat.mBytesPerPacket = 2;
myPCMFormat.mBytesPerFrame = 2;

AudioFileCreateWithURL((__bridge CFURLRef)flippedAudioUrl,
                       kAudioFileCAFType,
                       &myPCMFormat,
                       kAudioFileFlags_EraseFile,
                       &outputAudioFile);

AudioFileID inputAudioFile;
OSStatus theErr = noErr;
UInt64 fileDataSize = 0;

AudioStreamBasicDescription theFileFormat;
UInt32 thePropertySize = sizeof(theFileFormat);

theErr = AudioFileOpenURL((__bridge CFURLRef)recordedAudioUrl, kAudioFileReadPermission, 0, &inputAudioFile);

thePropertySize = sizeof(fileDataSize);
theErr = AudioFileGetProperty(inputAudioFile, kAudioFilePropertyAudioDataByteCount, &thePropertySize, &fileDataSize);

UInt32 dataSize = fileDataSize;
void* theData = malloc(dataSize);

//Read data into buffer
SInt64 readPoint  = dataSize;
UInt32 writePoint = 0;
while( readPoint > 0 )
{
    UInt32 bytesToRead = 2;
    AudioFileReadBytes( inputAudioFile, false, readPoint, &bytesToRead, theData );
    AudioFileWriteBytes( outputAudioFile, false, writePoint, &bytesToRead, theData );

    writePoint += 2;
    readPoint -= 2;
}

free(theData);
AudioFileClose(inputAudioFile);
AudioFileClose(outputAudioFile);

}

2 个答案:

答案 0 :(得分:2)

您的代码正在向后读取原始AAC数据(AudioFileReadBytes甚至不尊重数据包边界,与AudioFileReadPacketData不同),然后将其作为LPCM向前写入。难怪你听到静电。要反转AAC文件,您必须先将其解码为LPCM。建议您将阅读代码从AudioFile切换为ExtendedAudioFileAVAudioFile

您的回读 - 写 - 转发方法仍应适用于上述更改。

所提到的API可以将AAC(以及其他内容)解码为LPCM,例如, decoding with AVAudioFiledecoding with ExtAudioFile

NB 示例不是在阅读AAC,但这并不重要,因为API会隐藏您的详细信息,因此您的代码无需关心源格式是什么。

答案 1 :(得分:0)

您需要做的是:

  1. 将整个输入文件解码为PCM
  2. 反向解码的PCM样本
  3. 将反向PCM样本编码为AAC并保存
  4. 这会给你reverse.aac。

相关问题