语言目标C ++。
我已经使用AudioQueueNewInput设置了一个音频队列,排队了5个缓冲区,每个缓冲区的大小为44100 * 3 * 2(对于3秒钟的音频,在单个通道上具有16位数据)。在回调中,我似乎在传递给AudioQueueInputCallback的缓冲区中收到零。
从我的ViewController.mm文件中:
static void audioQueueInputCallback(void* data,
AudioQueueRef queue,
AudioQueueBufferRef buffer,
const AudioTimeStamp* timpestamp,
uint32 numberOfPacketDescriptions,
const AudioStreamPacketDescription* packetDescriptions){
void* const bytes = buffer->mAudioData;
uint32 size = buffer->mAudioDataByteSize;
uint32 count = 0;
const short* dataBytes = (const short*)bytes;
for(int i = 0; i< size/(sizeof(short));++i){
if(dataBytes[i]!= 0){
++count;
}
}
NSLog(@"---------Non-zero bytes:%u of %u(%lu)(%lu)",count,(unsigned int)size,size/(sizeof(short)),numberOfPacketDescriptions);
AudioQueueLevelMeterState meters[1];
UInt32 dlen = sizeof(meters);
OSStatus Status = AudioQueueGetProperty(queue,kAudioQueueProperty_CurrentLevelMeterDB,meters,&dlen);
NSLog(@"METER STATE. Average power:%f. Peak power:%f.",meters[0].mAveragePower,meters[0].mPeakPower);
AudioQueueEnqueueBuffer(queue, buffer, 0, NULL);
}
事件处理程序:
- (IBAction)startRecording:(id)sender{
OSStatus status;
self.state->recording = YES;
status = AudioQueueNewInput(&(self.state->format), audioQueueInputCallback, (__bridge void*)self, NULL, kCFRunLoopCommonModes, 0, &(self.state->queue));
NSLog(@"AudioQueueNewInput:%d",status);
bufferLength = 3; /* seconds */
int bytes = (int)ceil(bufferLength * self.state->format.mSampleRate) * self.state->format.mBytesPerFrame;
for (int i = 0; i < NUM_BUFFERS; i++) {
AudioQueueAllocateBuffer(self.state->queue, bytes, &(self.state->buffers[i]));
AudioQueueEnqueueBuffer(self.state->queue, self.state->buffers[i], 0, NULL);
}
status = AudioQueueStart(self.state->queue, NULL);
NSLog(@"AudioQueueStart:%d",status);
}
- (IBAction)stopRecording:(id)sender{
AudioFileClose(self.state->outputFile);
self.state->recording = NO;
AudioQueueStop(self.state->queue,false);
AudioQueueDispose(self.state->queue, true);
}