ios – 反转音频文件SwiftObjective-C

ios – 反转音频文件SwiftObjective-C,第1张

概述有没有办法可以反转并导出.m4a音频文件?我找到了一个反转音频轨道 here的解决方案,但它似乎只是在处理.caf文件格式.如果唯一的方法是使用.caf,有没有办法首先将.m4a文件转换为.caf? 更新: 在another post我发现AVAssetReader可用于从音频文件中读取音频样本,但我不知道如何以相反的顺序写回样本.以下代码片段直接来自帖子.任何帮助,将不胜感激.谢谢 + (voi 有没有办法可以反转并导出.m4a音频文件?我找到了一个反转音频轨道 here的解决方案,但它似乎只是在处理.caf文件格式.如果唯一的方法是使用.caf,有没有办法首先将.m4a文件转换为.caf?

更新:
在another post我发现AVAssetReader可用于从音频文件中读取音频样本,但我不知道如何以相反的顺序写回样本.以下代码片段直接来自帖子.任何帮助,将不胜感激.谢谢

+ (voID) reverseAudioTrack: (AVAsset *)audioAsset outputURL: (NSURL *)outputURL {NSError *error;AVAssetReader* reader = [[AVAssetReader alloc] initWithAsset:audioAsset error:&error];if (error) {NSLog(@"%@",error.localizedDescription);}AVAssetTrack* track = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];NSMutableDictionary* audioReadSettings = [NSMutableDictionary dictionary];[audioReadSettings setValue:[NSNumber numberWithInt:kAudioFormatlinearPCM]                     forKey:AVFormatIDKey];AVAssetReaderTrackOutput* readerOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:track outputSettings:audioReadSettings];[reader addOutput:readerOutput];[reader startReading];CMSampleBufferRef sample; //= [readerOutput copyNextSampleBuffer];NSMutableArray *samples = [[NSMutableArray alloc] init];// Get all sampleswhile((sample = [readerOutput copyNextSampleBuffer])) {    [samples addobject:(__brIDge ID)sample];    CFRelease(sample);}// Process samples in reverseAudioChannelLayout acl;bzero(&acl,sizeof(acl));acl.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;AVAssetWriter *writer = [[AVAssetWriter alloc] initWithURL:outputURL                                                   fileType:AVfileTypeAppleM4A                                                      error:&error];if (error) {NSLog(@"%@",error.localizedDescription);}NSDictionary *writerOutputSettings = [ NSDictionary dictionaryWithObjectsAndKeys:                                      [ NSNumber numberWithInt: kAudioFormatAppleLossless ],AVFormatIDKey,[ NSNumber numberWithInt: 16 ],AVEncoderBitDepthHintKey,[ NSNumber numberWithfloat: 44100.0 ],AVSampleRateKey,[ NSNumber numberWithInt: 1 ],AVNumberOfChannelsKey,[ NSData dataWithBytes: &acl length: sizeof( acl ) ],AVChannelLayoutKey,nil ];AVAssetWriterinput *audioWriterinput = [AVAssetWriterinput assetWriterinputWithMediaType:AVMediaTypeAudio outputSettings:writerOutputSettings];[writer addinput:audioWriterinput];[writer startWriting];[writer startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp((__brIDge CMSampleBufferRef)samples[0]) ];// (1) Would it work if I loop in reverse here?for (NSInteger i = 0; i < samples.count; i++) {    CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer((__brIDge CMSampleBufferRef)samples[i]);    CMItemCount numSamplesInBuffer = CMSampleBufferGetNumSamples((__brIDge CMSampleBufferRef)samples[i]);    audiobufferlist audiobufferlist;    CMSampleBufferGetaudiobufferlistWithRetainedBlockBuffer((__brIDge CMSampleBufferRef)samples[i],NulL,&audiobufferlist,sizeof(audiobufferlist),kCMSampleBufferFlag_audiobufferlist_Assure16Bytealignment,&buffer                                                            );    for (int bufferCount = 0; bufferCount < audiobufferlist.mNumberBuffers; bufferCount++) {        SInt16* samples = (SInt16 *)audiobufferlist.mBuffers[bufferCount].mData;        for (int i=0; i < numSamplesInBuffer; i++) {            // amplitude for the sample is samples[i],assuming you have linear pcm to start with            // (2) What should I be doing to write the samples into an audio file?        }    }    CFRelease(buffer);}
解决方法 是的,有一种方法可以处理,然后导出任何有iOS支持的音频文件.

但是,大多数这些格式(mp3命名为1)都是有损和压缩的.您必须首先解压缩数据,应用转换并重新压缩.您将应用于音频信息的大多数转换应该可以在原始PCM级别完成.

结合这两个语句,您可以在几个过程中执行此 *** 作:

>将原始文件转换为符合kAudioFormatlinearPCM的音频文件,如aifF
>处理该临时文件(反转其内容)
>将临时文件转换回原始格式

就像你将一个转换应用于压缩的jpeg图像一样,这个过程也会有所退化.最后的音频最多只会遭受一次压缩循环.

所以这种方法真正的数学答案实际上是没有的.

仅供参考,这里是swift 3中的一些入门代码.需要进一步细化才能跳过文件头.

var outAudiofile:AudiofileID?var pcm = AudioStreamBasicDescription(mSampleRate: 44100.0,mFormatID: kAudioFormatlinearPCM,mFormatFlags: kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger,mBytesPerPacket: 2,mFramesPerPacket: 1,mBytesPerFrame: 2,mChannelsPerFrame: 1,mBitsPerChannel: 16,mReserved: 0)var theErr = AudiofileCreateWithURL(destUrl as CFURL!,kAudiofileaifFType,&pcm,.erasefile,&outAudiofile)if noErr == theErr,let outAudiofile = outAudiofile {    var inAudiofile:AudiofileID?    theErr = AudiofileOpenURL(sourceUrl as! CFURL,.readPermission,&inAudiofile)    if noErr == theErr,let inAudiofile = inAudiofile {        var fileDataSize:UInt64 = 0        var thePropertySize:UInt32 = UInt32(MemoryLayout<UInt64>.strIDe)        theErr = AudiofileGetProperty(inAudiofile,kAudiofilePropertyAudioDataByteCount,&thePropertySize,&fileDataSize)        if( noErr == theErr) {            let dataSize:Int64 = Int64(fileDataSize)            let theData = UnsafeMutableRawPointer.allocate(bytes: Int(dataSize),alignedTo: MemoryLayout<UInt8>.alignment)            var readPoint:Int64 = Int64(dataSize)            var writePoint:Int64 = 0            while( readPoint > 0 )            {                var bytesToRead = UInt32(2)                AudiofileReadBytes( inAudiofile,false,readPoint,&bytesToRead,theData)                AudiofileWriteBytes( outAudiofile,writePoint,theData)                writePoint += 2                readPoint -= 2            }            theData.deallocate(bytes: Int(dataSize),alignedTo: MemoryLayout<UInt8>.alignment)            AudiofileClose(inAudiofile);            AudiofileClose(outAudiofile);        }    }}
总结

以上是内存溢出为你收集整理的ios – 反转音频文件Swift / Objective-C全部内容,希望文章能够帮你解决ios – 反转音频文件Swift / Objective-C所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1031941.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存