Язык Цель 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);
}