Skip to content

Commit eb99747

Browse files
author
dsward2
committed
In AudioMonitor2 convertBuffer(), some changes were made to improve performance on older, slower Macs, especially when resampling to a higher rate (NOAA weather radio, for example). The audio should now play more smoothly with fewer skips and dropped packets. Overall CPU usage for the main app and all NSTasks looks reasonably good now.
1 parent 2334b81 commit eb99747

2 files changed

Lines changed: 19 additions & 20 deletions

File tree

AudioMonitor/AudioMonitor/AudioMonitor2.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,6 @@ void * runInputBufferOnThread(void * ptr)
207207

208208
fprintf(stderr, "AudioMonitor2 runInputBufferOnThread - produce bytes failed, bytesAvailableCount = %d\n", bytesAvailableCount);
209209
}
210-
else
211-
{
212-
if (audioConverterThreadID == 0)
213-
{
214-
//createAudioConverterThread();
215-
}
216-
}
217210
}
218211

219212
free(rtlsdrBuffer);
@@ -468,7 +461,8 @@ void convertBuffer(void * inputBufferPtr, unsigned int dataLength)
468461
void * convertedDataPtr = audioConverterOutputBufferList.mBuffers[0].mData;
469462

470463
int32_t space;
471-
void *ptr = TPCircularBufferHead(&audioConverterCircularBuffer, &space); // for fprintf to stderr below
464+
void * headPtr = TPCircularBufferHead(&audioConverterCircularBuffer, &space); // for fprintf to stderr below
465+
int64_t bufferFilledSize = (int64_t)headPtr - (int64_t)inputBufferPtr;
472466

473467
fwrite(convertedDataPtr, 1, convertedDataLength, stdout); // write resampled audio to stdout, can be piped to sox, etc.
474468

@@ -477,24 +471,29 @@ void convertBuffer(void * inputBufferPtr, unsigned int dataLength)
477471
if (produceBytesResult == false)
478472
{
479473
// TODO: We are here to avoid buffer overrun, is TPCircularBufferConsume for audioConverterCircularBuffer getting missed somewhere?
480-
481-
// clear buffer and try again (not recommended practice)
482-
TPCircularBufferClear(&(audioConverterCircularBuffer));
483474

475+
// clear buffer and try again (not the recommended practice)
476+
477+
usleep(500000);
478+
479+
TPCircularBufferClear(&(audioConverterCircularBuffer));
480+
481+
//fprintf(stderr, "AudioMonitor2 convertBuffer TPCircularBufferClear and try again, convertedDataLength = %d, space = %d, head = %p\n", convertedDataLength, space, headPtr);
482+
484483
produceBytesResult = TPCircularBufferProduceBytes(&audioConverterCircularBuffer, convertedDataPtr, convertedDataLength);
485484

486485
if (produceBytesResult == false)
487486
{
488487
// If we get here, packets will be dropped
489-
490-
fprintf(stderr, "AudioMonitor convertBuffer Produce convertedDataLength = %d, space = %d, head = %p\n", convertedDataLength, space, ptr);
491-
fprintf(stderr, "AudioMonitor convertBuffer - produce bytes failed, convertedDataLength = %d\n", convertedDataLength);
488+
489+
fprintf(stderr, "AudioMonitor2 convertBuffer failed, drop packet, convertedDataLength = %d, space = %d, head = %p, bufferFilledSize = %lld\n", convertedDataLength, space, headPtr, bufferFilledSize);
492490
}
493491
}
494-
495-
if (audioQueueThreadID == 0)
492+
else
496493
{
497-
//createAudioQueueThread();
494+
TPCircularBufferClear(&(audioConverterCircularBuffer));
495+
496+
usleep(5000);
498497
}
499498
}
500499

LocalRadio/Base.lproj/MainMenu.xib

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ Gw
13671367
</textField>
13681368
</subviews>
13691369
</view>
1370-
<point key="canvasLocation" x="167" y="-15"/>
1370+
<point key="canvasLocation" x="167" y="5"/>
13711371
</window>
13721372
<window title="LocalRadio" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" restorable="NO" releasedWhenClosed="NO" showsToolbarButton="NO" animationBehavior="default" id="QvC-M9-y7g" userLabel="Main Window">
13731373
<windowStyleMask key="styleMask" titled="YES" miniaturizable="YES"/>
@@ -2081,7 +2081,7 @@ Gw
20812081
</tabView>
20822082
</subviews>
20832083
</view>
2084-
<point key="canvasLocation" x="-474" y="223.5"/>
2084+
<point key="canvasLocation" x="-466" y="589"/>
20852085
</window>
20862086
<window title="Custom Tasks" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" frameAutosaveName="" animationBehavior="default" id="JXF-jt-XNF" userLabel="Custom Tasks Window">
20872087
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
@@ -2584,7 +2584,7 @@ Gw
25842584
<constraint firstAttribute="trailing" secondItem="T3O-Vc-C1s" secondAttribute="trailing" id="mT0-CP-d7p"/>
25852585
</constraints>
25862586
</view>
2587-
<point key="canvasLocation" x="85" y="-497"/>
2587+
<point key="canvasLocation" x="-489" y="12"/>
25882588
</window>
25892589
</objects>
25902590
</document>

0 commit comments

Comments
 (0)