diff options
author | Emilio Pavia <emilio.pavia@gmail.com> | 2015-11-26 13:04:42 +0300 |
---|---|---|
committer | Emilio Pavia <emilio.pavia@gmail.com> | 2015-11-26 13:09:23 +0300 |
commit | 09bc67dc28a0a9d0a1c127d72438db17e3f13d9b (patch) | |
tree | 054ce847c3887c4c69016e2e0686c63f88624b87 | |
parent | abc36e730251139104889203826a192797230692 (diff) |
Fix a bug with buffer size calculation in the AudioUnit inputCallback
This commit fix an error in the formula used to check if the buffer is too small.
Moreover the AudioUnitRender function modifies the mDataByteSize members with the actual read bytes count. This makes the buffer continuosly reallocated even if not necessary. This commit reset the mDataByteSize member to the actual buffer size.
-rw-r--r-- | src/MKMacAudioDevice.m | 9 | ||||
-rw-r--r-- | src/MKVoiceProcessingDevice.m | 9 | ||||
-rw-r--r-- | src/MKiOSAudioDevice.m | 9 |
3 files changed, 24 insertions, 3 deletions
diff --git a/src/MKMacAudioDevice.m b/src/MKMacAudioDevice.m index 24e7e22..2be75d1 100644 --- a/src/MKMacAudioDevice.m +++ b/src/MKMacAudioDevice.m @@ -45,7 +45,7 @@ static OSStatus inputCallback(void *udata, AudioUnitRenderActionFlags *flags, co b->mData = calloc(1, b->mDataByteSize); } - if (dev->_recordBufList.mBuffers->mDataByteSize < (nframes/dev->_recordSampleSize)) { + if (dev->_recordBufList.mBuffers->mDataByteSize < (dev->_recordSampleSize * nframes)) { NSLog(@"MKMacAudioDevice: Buffer too small. Allocating more space."); AudioBuffer *b = dev->_recordBufList.mBuffers; free(b->mData); @@ -53,11 +53,18 @@ static OSStatus inputCallback(void *udata, AudioUnitRenderActionFlags *flags, co b->mData = calloc(1, b->mDataByteSize); } + /* + AudioUnitRender modifies the mDataByteSize members with the + actual read bytes count. We need to write it back otherwise + we'll reallocate the buffer even if not needed. + */ + UInt32 dataByteSize = dev->_buflist.mBuffers->mDataByteSize; err = AudioUnitRender(dev->_recordAudioUnit, flags, ts, busnum, nframes, &dev->_recordBufList); if (err != noErr) { NSLog(@"MKMacAudioDevice: AudioUnitRender failed. err = %ld", (unsigned long)err); return err; } + dev->_buflist.mBuffers->mDataByteSize = dataByteSize; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; short *buf = (short *) dev->_recordBufList.mBuffers->mData; diff --git a/src/MKVoiceProcessingDevice.m b/src/MKVoiceProcessingDevice.m index 069fcaf..9105dab 100644 --- a/src/MKVoiceProcessingDevice.m +++ b/src/MKVoiceProcessingDevice.m @@ -53,7 +53,7 @@ static OSStatus inputCallback(void *udata, AudioUnitRenderActionFlags *flags, co b->mData = calloc(1, b->mDataByteSize); } - if (dev->_buflist.mBuffers->mDataByteSize < (nframes/dev->_micSampleSize)) { + if (dev->_buflist.mBuffers->mDataByteSize < (dev->_micSampleSize * nframes)) { NSLog(@"MKVoiceProcessingDevice: Buffer too small. Allocating more space."); AudioBuffer *b = dev->_buflist.mBuffers; free(b->mData); @@ -61,11 +61,18 @@ static OSStatus inputCallback(void *udata, AudioUnitRenderActionFlags *flags, co b->mData = calloc(1, b->mDataByteSize); } + /* + AudioUnitRender modifies the mDataByteSize members with the + actual read bytes count. We need to write it back otherwise + we'll reallocate the buffer even if not needed. + */ + UInt32 dataByteSize = dev->_buflist.mBuffers->mDataByteSize; err = AudioUnitRender(dev->_audioUnit, flags, ts, busnum, nframes, &dev->_buflist); if (err != noErr) { NSLog(@"MKVoiceProcessingDevice: AudioUnitRender failed. err = %li", (long int)err); return err; } + dev->_buflist.mBuffers->mDataByteSize = dataByteSize; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; short *buf = (short *) dev->_buflist.mBuffers->mData; diff --git a/src/MKiOSAudioDevice.m b/src/MKiOSAudioDevice.m index 1d5aca8..1124ce4 100644 --- a/src/MKiOSAudioDevice.m +++ b/src/MKiOSAudioDevice.m @@ -38,7 +38,7 @@ static OSStatus inputCallback(void *udata, AudioUnitRenderActionFlags *flags, co b->mData = calloc(1, b->mDataByteSize); } - if (dev->_buflist.mBuffers->mDataByteSize < (nframes/dev->_micSampleSize)) { + if (dev->_buflist.mBuffers->mDataByteSize < (dev->_micSampleSize * nframes)) { NSLog(@"MKiOSAudioDevice: Buffer too small. Allocating more space."); AudioBuffer *b = dev->_buflist.mBuffers; free(b->mData); @@ -46,6 +46,12 @@ static OSStatus inputCallback(void *udata, AudioUnitRenderActionFlags *flags, co b->mData = calloc(1, b->mDataByteSize); } + /* + AudioUnitRender modifies the mDataByteSize members with the + actual read bytes count. We need to write it back otherwise + we'll reallocate the buffer even if not needed. + */ + UInt32 dataByteSize = dev->_buflist.mBuffers->mDataByteSize; err = AudioUnitRender(dev->_audioUnit, flags, ts, busnum, nframes, &dev->_buflist); if (err != noErr) { #ifndef TARGET_IPHONE_SIMULATOR @@ -53,6 +59,7 @@ static OSStatus inputCallback(void *udata, AudioUnitRenderActionFlags *flags, co #endif return err; } + dev->_buflist.mBuffers->mDataByteSize = dataByteSize; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; short *buf = (short *) dev->_buflist.mBuffers->mData; |