2011-01-06 27 views
5

Tôi đang sử dụng đơn vị âm thanh để ghi âm giọng nói của mình. Trong hàm gọi lại của tôi, tôi đang nhận dữ liệu trong AudioBufferList. Tôi phải lưu trữ bộ đệm này trong một tập tin.EXC_BAD_ACCESS từ ExtAudioFileWriteAsync

Vấn đề là; Khi tôi cố gắng lưu trữ bộ đệm trong một tệp .caf, ứng dụng bị lỗi. Dưới đây là mã.

AudioStreamBasicDescription audioFormat; 
audioFormat.mSampleRate   = 44100.00; 
audioFormat.mFormatID   = kAudioFormatLinearPCM; 
audioFormat.mFormatFlags  = kAudioFormatFlagsCanonical; 
audioFormat.mBytesPerPacket  = 2; 
audioFormat.mFramesPerPacket = 1; 
audioFormat.mBytesPerFrame  = 2; 
audioFormat.mChannelsPerFrame = 1; 
audioFormat.mBitsPerChannel  = 16; 
audioFormat.mReserved   = 0; 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
if(![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory]) 
    [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"audio.caf"]; 
if(![[NSFileManager defaultManager] fileExistsAtPath:path]) 
    NSLog(@"file not exist."); 
NSLog(@"Path : %@", path); 
THIS->url = [NSURL fileURLWithPath:[path retain]]; 
err = ExtAudioFileCreateWithURL((CFURLRef)THIS->url, kAudioFileCAFType, &audioFormat, NULL, kAudioFileFlags_EraseFile, &THIS->fOutputAudioFile); 
if(err != noErr){ 
    printf("create with url: error %d\n", (int)err); 
} 
THIS->state = 2; 
err = ExtAudioFileSetProperty(THIS->fOutputAudioFile, kExtAudioFileProperty_ClientDataFormat, (UInt32)sizeof(audioFormat), &audioFormat); 
if(err != noErr){ 
    printf("file set property: error %d\n", (int)err); 
} 
err = ExtAudioFileWriteAsync(THIS->fOutputAudioFile, 0, NULL); 
if(err != noErr){ 
    printf("write async: error %d\n", (int)err); 
    return err; 
} 
[pool release]; 

err = ExtAudioFileWriteAsync(THIS->fOutputAudioFile, ioData->mNumberBuffers, ioData); 
if(err != noErr){ 
    printf("file write async: error %d\n", (int)err); 
    return err; 
} 

LỖI:

Program received signal: “EXC_BAD_ACCESS”.
#0 0x30d04bb2 in memmove() #1 0x33ba1d1e in AudioRingBuffer::Store() #2 0x33c2269e in ExtAudioFile::WriteFramesAsync() #3 0x33c278c8 in ExtAudioFileWriteAsync() #4 0x00002e74 in PerformThru (inRefCon=0x14d9f0, ioActionFlags=0x2ffe77d4, inTimeStamp=0xa896fc, inBusNumber=0, inNumberFrames=256, ioData=0x14f680) at /Alex/project/ProStudio/TestAudioUnit/Classes/TestAudioUnitAppDelegate.mm:399 #5 0x33b70896 in AUInputElement::PullInput() #6 0x33b794a0 in AUInputFormatConverter2::InputProc() #7 0x33b5560e in AudioConverterChain::CallInputProc() #8 0x33b5555e in AudioConverterChain::FillBufferFromInputProc() #9 0x33b5537c in BufferedAudioConverter::GetInputBytes() #10 0x33b79390 in CBRConverter::RenderOutput() #11 0x33b5505a in BufferedAudioConverter::FillBuffer() #12 0x33b55362 in BufferedAudioConverter::GetInputBytes() #13 0x33b79390 in CBRConverter::RenderOutput() #14 0x33b5505a in BufferedAudioConverter::FillBuffer() #15 0x33b551ae in AudioConverterChain::RenderOutput() #16 0x33b5505a in BufferedAudioConverter::FillBuffer() #17 0x33b54e2a in AudioConverterFillComplexBuffer() #18 0x33b78f58 in AUConverterBase::RenderBus() #19 0x33c08eea in AURemoteIO::RenderBus() #20 0x33b5666e in AUBase::DoRender() #21 0x33c09698 in AURemoteIO::PerformIO() #22 0x33c09962 in AURIOCallbackReceiver_PerformIO() #23 0x33c02448 in _XPerformIO() #24 0x33b71bea in mshMIGPerform() #25 0x33bd7de0 in MSHMIGDispatchMessage() #26 0x33c0ebae in AURemoteIO::IOThread::Entry() #27 0x33b4a1d8 in CAPThread::Entry() #28 0x30d7d88c in _pthread_start() #29 0x30d72a90 in thread_start()

Xin giúp một số một để khắc phục vấn đề này.

+0

Tôi đã cố gắng tạo tệp bằng cách sử dụng ExtAudioFileCreateWithURL, tôi không nhận được bất kỳ lỗi nào (ý tôi là mã lỗi là 0). Nhưng ExtAudioFileRef có giá trị null (như 0xfd000). Tại sao nó cho giá trị null? – jfalexvijay

Trả lời

0

Tôi đã nhận được giải pháp cho vấn đề này;

Vấn đề là; Tôi đang đặt AudioStreamBasicDescription cho đơn vị âm thanh và cũng là ExtAudioFileCreateWithURL. Nó đã có một số thiết lập khác nhau. Tôi đã thay đổi như nhau. Bây giờ nó đang làm việc tốt.

Trước ASBD là;


audioFormat.mSampleRate  = 44100.00; 
audioFormat.mFormatID   = kAudioFormatLinearPCM; 
audioFormat.mFormatFlags  = kAudioFormatFlagsCanonical | (kAudioUnitSampleFractionBits << kLinearPCMFormatFlagsSampleFractionShift) | kAudioFormatFlagIsNonInterleaved; 
audioFormat.mChannelsPerFrame = 2; 
audioFormat.mFramesPerPacket = 1; 
audioFormat.mBitsPerChannel = 8 * sizeof(AudioUnitSampleType); 
audioFormat.mBytesPerPacket = sizeof(AudioUnitSampleType); 
audioFormat.mBytesPerFrame  = sizeof(AudioUnitSampleType);; 
audioFormat.mReserved   = 0; 

bây giờ tôi đang sử dụng ASBD sau;


AudioStreamBasicDescription audioFormat; 
audioFormat.mSampleRate   = 44100.00; 
audioFormat.mFormatID   = kAudioFormatLinearPCM; 
audioFormat.mFormatFlags  = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked; 
audioFormat.mFramesPerPacket = 1; 
audioFormat.mChannelsPerFrame = 1; 
audioFormat.mBitsPerChannel  = 16; 
audioFormat.mBytesPerPacket  = 2; 
audioFormat.mBytesPerFrame  = 2; 
audioFormat.mReserved   = 0; 
Các vấn đề liên quan