Hopefully fix memory usage during playback

Shuffle around @autoreleasepool blocks, and also add one
to the audio processing code in the playback callback, so
audio memory is released during playback instead of
accumulating.

Fixes #379

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
This commit is contained in:
Christopher Snowhill 2023-10-11 20:11:22 -07:00
parent bc330e75f6
commit 1c95771ed0
No known key found for this signature in database
2 changed files with 62 additions and 62 deletions

View file

@ -93,13 +93,12 @@ void scale_by_volume(float *buffer, size_t count, float volume) {
// when the end of stream is reached. Convert function instead processes what it can, // when the end of stream is reached. Convert function instead processes what it can,
// and returns 0 samples when it has nothing more to process at the end of stream. // and returns 0 samples when it has nothing more to process at the end of stream.
while([self shouldContinue] == YES) { while([self shouldContinue] == YES) {
AudioChunk *chunk = nil;
while(paused) { while(paused) {
usleep(500); usleep(500);
} }
@autoreleasepool { @autoreleasepool {
AudioChunk *chunk = nil;
chunk = [self convert]; chunk = [self convert];
}
if(!chunk) { if(!chunk) {
if([self endOfStream] == YES) { if([self endOfStream] == YES) {
break; break;
@ -108,13 +107,10 @@ void scale_by_volume(float *buffer, size_t count, float volume) {
continue; continue;
} }
} else { } else {
@autoreleasepool {
[self writeChunk:chunk]; [self writeChunk:chunk];
chunk = nil; chunk = nil;
} }
}
if(streamFormatChanged) { if(streamFormatChanged) {
@autoreleasepool {
[self cleanUp]; [self cleanUp];
[self setupWithInputFormat:newInputFormat withInputConfig:newInputChannelConfig outputFormat:self->outputFormat isLossless:rememberedLossless]; [self setupWithInputFormat:newInputFormat withInputConfig:newInputChannelConfig outputFormat:self->outputFormat isLossless:rememberedLossless];
} }

View file

@ -422,6 +422,7 @@ current_device_listener(AudioObjectID inObjectID, UInt32 inNumberAddresses, cons
secondsLatency = 1.0; secondsLatency = 1.0;
while(!stopping) { while(!stopping) {
@autoreleasepool {
if(outputdevicechanged) { if(outputdevicechanged) {
[self updateDeviceFormat]; [self updateDeviceFormat];
outputdevicechanged = NO; outputdevicechanged = NO;
@ -453,6 +454,7 @@ current_device_listener(AudioObjectID inObjectID, UInt32 inNumberAddresses, cons
if([outputController shouldContinue] == NO) { if([outputController shouldContinue] == NO) {
break; break;
} }
}
usleep(5000); usleep(5000);
} }
@ -1038,6 +1040,7 @@ current_device_listener(AudioObjectID inObjectID, UInt32 inNumberAddresses, cons
OutputCoreAudio *_self = (__bridge OutputCoreAudio *)refCon; OutputCoreAudio *_self = (__bridge OutputCoreAudio *)refCon;
int renderedSamples = 0; int renderedSamples = 0;
@autoreleasepool {
while(renderedSamples < frameCount) { while(renderedSamples < frameCount) {
int inputRemain = _self->inputRemain; int inputRemain = _self->inputRemain;
while(!inputRemain) { while(!inputRemain) {
@ -1060,6 +1063,7 @@ current_device_listener(AudioObjectID inObjectID, UInt32 inNumberAddresses, cons
double secondsRendered = (double)renderedSamples / format->mSampleRate; double secondsRendered = (double)renderedSamples / format->mSampleRate;
[_self updateLatency:secondsRendered]; [_self updateLatency:secondsRendered];
}
#ifdef _DEBUG #ifdef _DEBUG
[BadSampleCleaner cleanSamples:(float *)inputData->mBuffers[0].mData [BadSampleCleaner cleanSamples:(float *)inputData->mBuffers[0].mData