diff options
author | Mikkel Krautz <mikkel@krautz.dk> | 2017-09-03 19:15:15 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-03 19:15:15 +0300 |
commit | d8416cc5ab53427b60bb93869163747b76bfa860 (patch) | |
tree | b2be4da51eabfa1ac220aa6e1a723729789b2d45 | |
parent | 4a562284001ca96b33da252853706128899f16a8 (diff) | |
parent | 09bc67dc28a0a9d0a1c127d72438db17e3f13d9b (diff) |
Merge PR #44: Fix a bug with buffer size calculation in the AudioUnit inputCallback
-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; |