从线性 PCM 中提取音频通道
Posted
技术标签:
【中文标题】从线性 PCM 中提取音频通道【英文标题】:Extracting audio channel from Linear PCM 【发布时间】:2011-06-05 13:59:10 【问题描述】:我想从 LPCM 原始文件中提取声道音频,即提取立体声 LPCM 文件的左右声道。 LPCM 为 16 位深度、交错、2 通道、小端。从我收集的字节顺序是 LeftChannel,RightChannel,LeftChannel,RightChannel... 并且由于它是 16 位深度,每个通道将有 2 个字节的样本,对吗?
所以我的问题是,如果我想提取左声道,那么我会获取 0,2,4,6...n*2 地址中的字节吗?而右声道是 1,3,4,...(n*2+1)。
同样在提取音频通道后,我应该将提取通道的格式设置为 16 位深度,1 个通道吗?
提前致谢
这是我目前用来从 AssetReader 中提取 PCM 音频的代码。这段代码可以很好地写入音乐文件而不提取其频道,所以我可能是由格式或其他原因引起的……
NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
[NSNumber numberWithInt:2], AVNumberOfChannelsKey,
// [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey,
[NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
[NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
nil];
NSError *assetError = nil;
AVAssetReader *assetReader = [[AVAssetReader assetReaderWithAsset:songAsset
error:&assetError]
retain];
if (assetError)
NSLog (@"error: %@", assetError);
return;
AVAssetReaderOutput *assetReaderOutput = [[AVAssetReaderAudioMixOutput
assetReaderAudioMixOutputWithAudioTracks:songAsset.tracks
audiosettings: outputSettings]
retain];
if (! [assetReader canAddOutput: assetReaderOutput])
NSLog (@"can't add reader output... die!");
return;
[assetReader addOutput: assetReaderOutput];
NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [dirs objectAtIndex:0];
//CODE TO SPLIT STEREO
[self setupAudioWithFormatMono:kAudioFormatLinearPCM];
NSString *splitExportPath = [[documentsDirectoryPath stringByAppendingPathComponent:@"monoleft.caf"] retain];
if ([[NSFileManager defaultManager] fileExistsAtPath:splitExportPath])
[[NSFileManager defaultManager] removeItemAtPath:splitExportPath error:nil];
AudioFileID mRecordFile;
NSURL *splitExportURL = [NSURL fileURLWithPath:splitExportPath];
OSStatus status = AudioFileCreateWithURL(splitExportURL, kAudioFileCAFType, &_streamFormat, kAudioFileFlags_EraseFile,
&mRecordFile);
NSLog(@"status os %d",status);
[assetReader startReading];
CMSampleBufferRef sampBuffer = [assetReaderOutput copyNextSampleBuffer];
UInt32 countsamp= CMSampleBufferGetNumSamples(sampBuffer);
NSLog(@"number of samples %d",countsamp);
SInt64 countByteBuf = 0;
SInt64 countPacketBuf = 0;
UInt32 numBytesIO = 0;
UInt32 numPacketsIO = 0;
NSMutableData * bufferMono = [NSMutableData new];
while (sampBuffer)
AudioBufferList audioBufferList;
CMBlockBufferRef blockBuffer;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampBuffer, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
for (int y=0; y<audioBufferList.mNumberBuffers; y++)
AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
//frames = audioBuffer.mData;
NSLog(@"the number of channel for buffer number %d is %d",y,audioBuffer.mNumberChannels);
NSLog(@"The buffer size is %d",audioBuffer.mDataByteSize);
//Append mono left to buffer data
for (int i=0; i<audioBuffer.mDataByteSize; i= i+4)
[bufferMono appendBytes:(audioBuffer.mData+i) length:2];
//the number of bytes in the mutable data containing mono audio file
numBytesIO = [bufferMono length];
numPacketsIO = numBytesIO/2;
NSLog(@"numpacketsIO %d",numPacketsIO);
status = AudioFileWritePackets(mRecordFile, NO, numBytesIO, &_packetFormat, countPacketBuf, &numPacketsIO, audioBuffer.mData);
NSLog(@"status for writebyte %d, packets written %d",status,numPacketsIO);
if(numPacketsIO != (numBytesIO/2))
NSLog(@"Something wrong");
assert(0);
countPacketBuf = countPacketBuf + numPacketsIO;
[bufferMono setLength:0];
sampBuffer = [assetReaderOutput copyNextSampleBuffer];
countsamp= CMSampleBufferGetNumSamples(sampBuffer);
NSLog(@"number of samples %d",countsamp);
AudioFileClose(mRecordFile);
[assetReader cancelReading];
[self performSelectorOnMainThread:@selector(updateCompletedSizeLabel:)
withObject:0
waitUntilDone:NO];
使用audiofileservices的输出格式如下:
_streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
_streamFormat.mBitsPerChannel = 16;
_streamFormat.mChannelsPerFrame = 1;
_streamFormat.mBytesPerPacket = 2;
_streamFormat.mBytesPerFrame = 2;// (_streamFormat.mBitsPerChannel / 8) * _streamFormat.mChannelsPerFrame;
_streamFormat.mFramesPerPacket = 1;
_streamFormat.mSampleRate = 44100.0;
_packetFormat.mStartOffset = 0;
_packetFormat.mVariableFramesInPacket = 0;
_packetFormat.mDataByteSize = 2;
【问题讨论】:
【参考方案1】:听起来几乎是正确的 - 您有 16 位深度,这意味着每个样本将占用 2 个字节。这意味着左通道数据将以字节 0,1、4,5、8,9 等为单位。交错意味着样本是交错的,而不是字节。 除此之外,我会尝试一下,看看您的代码是否有任何问题。
同样在提取音频之后 频道,我应该设置格式 提取的通道为 16 位深度 ,1 个频道?
提取后两个通道中只剩下一个,所以是的,这是正确的。
【讨论】:
好吧,我实际上已经尝试过,但结果不正确...我使用了您所说的提取算法,但输出听起来失真...失真,听起来“慢”。 .. 我用于上述算法的代码已放在我的帖子中。我欢迎任何导致它出错的输入 你确定输入的采样率是44100吗?如果你有一个错误可以解释它播放“慢” 是的,我很确定。此外,我已经用整个音频文件作为立体声测试了这种代码。我会尝试调查我猜的格式标志【参考方案2】:我有一个类似的错误,音频听起来“慢”,原因是您将 mChannelsPerFrame 指定为 1,而您有双声道声音。将其设置为 2,它应该会加快播放速度。还要告诉你这样做后输出的“声音”是否正确...... :)
【讨论】:
【参考方案3】:我正在尝试将我的立体声音频拆分为两个单声道文件 (split stereo audio to mono streams on iOS)。我一直在使用您的代码,但似乎无法使其正常工作。你的 setupAudioWithFormatMono 方法的内容是什么?
【讨论】:
以上是关于从线性 PCM 中提取音频通道的主要内容,如果未能解决你的问题,请参考以下文章