我已使用AudioBufferList
功能将音频文件读入ExtAudioFileRead
。
这是音频的ASBD:
AudioStreamBasicDescription importFormat;
importFormat.mFormatID = kAudioFormatLinearPCM;
importFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
importFormat.mBytesPerPacket = 4;
importFormat.mFramesPerPacket = 1;
importFormat.mBytesPerFrame = 4;
importFormat.mChannelsPerFrame = 2;
importFormat.mBitsPerChannel = 16;
importFormat.mSampleRate = [[AVAudioSession sharedInstance] sampleRate];
所以我们得到了两个通道的交错音频,每个通道有16位符号int
AudioBufferList
init:
UInt32 *audioData = (UInt32 *) calloc (totalFramesInFile, sizeof (UInt32));
AudioBufferList *bufferList;
bufferList = (AudioBufferList *) malloc (sizeof (AudioBufferList));
// buffers amount is 1 because audio is interleaved
bufferList->mNumberBuffers = 1;
bufferList->mBuffers[0].mNumberChannels = 2;
bufferList->mBuffers[0].mDataByteSize = totalFramesInFile * sizeof(UInt32);
bufferList->mBuffers[0].mData = audioData;
阅读缓冲区:
CheckError(ExtAudioFileRead (
audioFileObject,
&numberOfPacketsToRead,
bufferList), "error ExtAudioFileRead");
audioFileObject
是ExtAudioFileRef
的实例,它是在代码中启动的,我没有在此处粘贴以节省空间。
我想要完成的是修改渲染回调中的音频样本。
OSStatus MyCallback (void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData){
ViewController *view = (__bridge ViewController *) inRefCon;
soundStruct *soundStruct = (soundStruct *) &view->mys;
SInt64 frameTotalForSound = soundStruct->frameCount;
soundStruct->isPlaying = true;
UInt32 *audioData = soundStruct->audioData;
UInt32 sampleNumber = soundStruct->sampleNumber;
for( int i = 0; i < ioData->mNumberBuffers; i++){
AudioBuffer buffer = ioData->mBuffers[i];
UInt32 *frameBuffer = buffer.mData;
for(UInt32 frame = 0; frame < inNumberFrames; frame++) {
// here I fill the buffer with my audio data.
// i need to get left and right channel samples
// from audioData[sampleNumber], modify them
// and write into frameBuffer
frameBuffer[frame] = audioData[sampleNumber];
sampleNumber++;
if(sampleNumber > frameTotalForSound) {
soundStruct->isPlaying = false;
AudioOutputUnitStop(soundStruct->outputUnit);
}
}
}
soundStruct->sampleNumber = sampleNumber;
return noErr;
}
是否可以从UInt32音频数据阵列中获取Sint16左右声道样本?
答案 0 :(得分:1)
让this.wrongLocation.add(graphics);
和audioData
都为frameBuffer
s:
SInt16
您的缓冲区大小计算应为SInt16 *audioData;
// ...
SInt16 *frameBuffer;
soundStruct`或添加类型转换。
然后您可以像这样访问交错的样本:
n * 2 * sizeof(SInt16) and you'll either need to change
答案 1 :(得分:1)
@Rhythmic Fistman,非常感谢 - 它有所帮助。
我无法设置frameBuffer
以此方式工作。声音在输出处失真。我想这是因为AudioUnit需要一帧中的两个通道数据。或许还有其他解释。
这是我修改的代码,希望它能帮到某人:
audioData
init:
SInt16 *audioData = (SInt16 *) malloc (sizeof(SInt16) * totalFramesInFile * 2);
修改了渲染回调:
OSStatus MyCallback (void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
ViewController *view = (__bridge ViewController *) inRefCon;
soundStruct *soundStruct = (soundStruct *) &view->mys;
SInt64 frameTotalForSound = soundStruct->frameCount;
soundStruct->isPlaying = true;
SInt16 *audioData = soundStruct->audioData;
UInt32 sampleNumber = soundStruct->sampleNumber;
for( int i = 0; i < ioData->mNumberBuffers; i++){
AudioBuffer buffer = ioData->mBuffers[i];
SInt16 *frameBuffer = (SInt16*) ioData->mBuffers[0].mData;
for(UInt32 frame = 0; frame < inNumberFrames * 2; frame+=2) {
/* .. some samples modification code .. */
// left channel
frameBuffer[frame] = audioData[sampleNumber];
// right channel
frameBuffer[frame + 1] = audioData[sampleNumber + 1];
sampleNumber +=2;
if(sampleNumber > frameTotalForSound * 2) {
soundStruct->isPlaying = false;
AudioOutputUnitStop(soundStruct->outputUnit);
}
}
}
soundStruct->sampleNumber = sampleNumber;
return noErr;
}