gpt4 book ai didi

ios - 用音频单元录制音频,文件的每个文件都以X秒为单位分割

转载 作者:行者123 更新时间:2023-12-03 02:05:46 25 4
gpt4 key购买 nike

我已经来这几天了。我对框架的音频单元层不是很熟悉。有人可以给我指出一个完整的示例,该示例说明如何让用户以x个间隔即时记录和写入文件。例如,用户按下记录,我想每10秒写入一个文件,在第11秒,它将写入下一个文件,而在第21秒,这是同一件事。因此,当我录制25秒的音频字时,它将产生3个不同的文件。

我已经使用AVCapture尝试过此操作,但它会在中间产生咔嗒声。我已经阅读了它,这是由于读写操作之间的毫秒数引起的。我曾经尝试过Audio Queue Services,但是知道我正在使用的应用程序之后,我需要对音频层进行完全控制。所以我决定选择Audio Unit。

最佳答案

我想我越来越近了……仍然迷路了。我最终使用了Amazing Audio Engine(TAAE)。我现在正在看AEAudioReceiver,我的回调代码看起来像这样。我认为从逻辑上讲是正确的,但我认为它没有正确实现。

当前的任务:以AAC格式记录〜5秒的片段。

尝试:使用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];
}

-----音频 Controller 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;

关于ios - 用音频单元录制音频,文件的每个文件都以X秒为单位分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27893428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com