ios - 如何反转 AAC 音频文件?

标签 ios objective-c avfoundation aac

我目前能够反向录制和播放(多亏了几个例子),但我想让用户能够从表格 View 中选择以前录制的 .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);

}

最佳答案

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

您的 read-backwards-and-write-forwards 方法应该仍然适用于上述更改。

提到的 API 可以将 AAC(除其他外)解码为 LPCM,例如 decoding with AVAudioFiledecoding with ExtAudioFile

注意 示例未读取 AAC,但这无关紧要,因为 API 向您隐藏了详细信息,因此您的代码无需关心源格式是什么。

关于ios - 如何反转 AAC 音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34822766/

相关文章:

ios - Swift 函数调用中的隐式类型

iphone - 是否可以缩短临时构建配置文件过期之前的时间?

ios - iOS LAPolicyDeviceOwnerAuthentication“取消”按钮

ios - 如何更改 Xcode 中的工作区名称?

objective-c - 所有属性都应该声明为 "nonatomic"吗?

objective-c - 阐明 NSLayoutManager 和 NSTextContainer 在垂直布局方面的行为?

ios - 如何更改sendSynchronousRequest:urlRequest,因为已弃用

ios - 为什么我不必在 Swift 中使用 NSBundle 之前导入 Foundation?

ios - 继续错误 : due to uncaught exception 'NSUnknownKeyException'

iphone - 更改 "sounder"的音频音高