Запись звука с помощью Audio Unit с файлами, сегментированными по X секунд каждый

Я занимаюсь этим уже несколько дней. Я не очень хорошо знаком со слоем Audio Unit фреймворка. Может ли кто-нибудь указать мне на какой-нибудь полный пример того, как я могу позволить пользователю записывать, а затем записывать файл на лету с интервалом x. Например, пользователь нажимает запись, каждые 10 секунд я хочу записать в файл, на 11-й секунде он записывает в следующий файл, а на 21-й секунде - то же самое. Поэтому, когда я записываю 25-секундное слово аудио, получается 3 разных файла.

Я пробовал это с AVCapture, но он производил щелчки и хлопки посередине. Я читал об этом, это связано с миллисекундами между операциями чтения и записи. Я пробовал Audio Queue Services, но, зная приложение, над которым я работаю, мне нужен полный контроль над звуковым слоем; поэтому я решил пойти с Audio Unit.


person mobile dev    schedule 11.01.2015    source источник


Ответы (1)


Я думаю, что приближаюсь... все еще довольно потерян. В итоге я использовал The Amazing Audio Engine (TAAE). Я сейчас смотрю на AEAudioReceiver, мой код обратного вызова выглядит так. Я думаю, что логически это правильно, но я не думаю, что это реализовано правильно.

Поставленная задача: записать 5-секундные сегменты в формате AAC.

Попытка: использовать обратный вызов AEAudioReciever и сохранить AudioBufferList в кольцевом буфере. Отслеживайте количество секунд аудио, полученных в классе рекордера; как только он пройдет отметку 5 секунд (может быть немного больше, но не 6 секунд). Вызовите метод Obj-c для записи файла с помощью AEAudioFileWriter.

Итог: Не получилось, записи звучали очень медленно и постоянно много шума; Я слышу часть записи; поэтому я знаю, что некоторые данные есть, но я теряю много данных. Я даже не уверен, как это отладить (я продолжу попытки, но на данный момент довольно потерян).

Еще один пункт это конвертация в AAC, мне сначала записать файл в формате PCM, а потом конвертировать в AAC или можно конвертировать только аудио сегмент в AAC?

Заранее спасибо за помощь!

----- Инициализация циклического буфера -----

//trying to get 5 seconds audio, how do I know what the length is if I don't know the frame size yet? and is that even the right question to ask?
TPCircularBufferInit(&_buffer, 1024 * 256);  

----- Обратный вызов AEAudioReceiver ------

static void receiverCallback(__unsafe_unretained MyAudioRecorder *THIS,
                         __unsafe_unretained AEAudioController *audioController,
                         void *source,
                         const AudioTimeStamp *time,
                         UInt32 frames,
                         AudioBufferList *audio) {
//store the audio into the buffer
TPCircularBufferCopyAudioBufferList(&THIS->_buffer, audio, time, kTPCircularBufferCopyAll, NULL);

//increase the time interval to track by THIS    
THIS.numberOfSecondInCurrentRecording += AEConvertFramesToSeconds(THIS.audioController, frames);

//if number of seconds passed an interval of 5 seconds, than write the last 5 seconds of the buffer to a file
if (THIS.numberOfSecondInCurrentRecording > 5 * THIS->_currentSegment + 1) {

    NSLog(@"Segment %d is full, writing file", THIS->_currentSegment);
    [THIS writeBufferToFile];

   //segment tracking variables
    THIS->_numberOfReceiverLoop = 0;
    THIS.lastTimeStamp = nil;
    THIS->_currentSegment += 1;
} else {
    THIS->_numberOfReceiverLoop += 1;
}

// Do something with 'audio'
if (!THIS.lastTimeStamp) {
    THIS.lastTimeStamp = (AudioTimeStamp *)time;
}
}

---- Запись в файл (метод внутри MyAudioRecorderClass) ----

- (void)writeBufferToFileHandler {

NSString *documentsFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
                             objectAtIndex:0];

NSString *filePath = [documentsFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"Segment_%d.aiff", _currentSegment]];

NSError *error = nil;

//setup audio writer, should the buffer be converted to aac first or save the file than convert; and how the heck do you do that?
AEAudioFileWriter *writeFile = [[AEAudioFileWriter alloc] initWithAudioDescription:_audioController.inputAudioDescription];
[writeFile beginWritingToFileAtPath:filePath fileType:kAudioFileAIFFType error:&error];

if (error) {
    NSLog(@"Error in init. the file: %@", error);
    return;
}

int i = 1;
//loop to write all the AudioBufferLists that is in the Circular Buffer; retrieve the ones based off of the _lastTimeStamp; but I had it in NULL too and worked the same way.
while (1) {

//NSLog(@"Processing buffer file list for segment [%d] and buffer index [%d]", _currentSegment, i);
    i += 1;
    // Discard any buffers with an incompatible format, in the event of a format change

    AudioBufferList *nextBuffer = TPCircularBufferNextBufferList(&_buffer, _lastTimeStamp);
    Float32 *frame = (Float32*) &nextBuffer->mBuffers[0].mData;

    //if buffer runs out, than we are done writing it and exit loop to close the file       
    if ( !nextBuffer ) {
        NSLog(@"Ran out of frames, there were [%d] AudioBufferList", i - 1);
        break;
    }
    //Adding audio using AudioFileWriter, is the length correct?
    OSStatus status = AEAudioFileWriterAddAudio(writeFile, nextBuffer, sizeof(nextBuffer->mBuffers[0].mDataByteSize));
    if (status) {
      NSLog(@"Writing Error? %d", status);
    }

    //consume/clear the buffer
    TPCircularBufferConsumeNextBufferList(&_buffer);
}

//close the file and hope it worked
[writeFile finishWriting];
}

----- Аудиоконтроллер AudioStreamBasicDescription ------

//interleaved16BitStereoAudioDescription
AudioStreamBasicDescription audioDescription;
memset(&audioDescription, 0, sizeof(audioDescription));
audioDescription.mFormatID          = kAudioFormatLinearPCM;
audioDescription.mFormatFlags       = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian;
audioDescription.mChannelsPerFrame  = 2;
audioDescription.mBytesPerPacket    = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
audioDescription.mFramesPerPacket   = 1;
audioDescription.mBytesPerFrame     = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
audioDescription.mBitsPerChannel    = 8 * sizeof(SInt16);
audioDescription.mSampleRate        = 44100.0;
person mobile dev    schedule 15.01.2015