Skip to content

Commit

Permalink
In AudioMonitor2 convertBuffer(), some changes were made to improve p…
Browse files Browse the repository at this point in the history
…erformance 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.
  • Loading branch information
dsward2 committed Sep 9, 2018
1 parent 2334b81 commit eb99747
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
33 changes: 16 additions & 17 deletions AudioMonitor/AudioMonitor/AudioMonitor2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,6 @@ void * runInputBufferOnThread(void * ptr)

fprintf(stderr, "AudioMonitor2 runInputBufferOnThread - produce bytes failed, bytesAvailableCount = %d\n", bytesAvailableCount);
}
else
{
if (audioConverterThreadID == 0)
{
//createAudioConverterThread();
}
}
}

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

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

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

Expand All @@ -477,24 +471,29 @@ void convertBuffer(void * inputBufferPtr, unsigned int dataLength)
if (produceBytesResult == false)
{
// TODO: We are here to avoid buffer overrun, is TPCircularBufferConsume for audioConverterCircularBuffer getting missed somewhere?

// clear buffer and try again (not recommended practice)
TPCircularBufferClear(&(audioConverterCircularBuffer));

// clear buffer and try again (not the recommended practice)

usleep(500000);

TPCircularBufferClear(&(audioConverterCircularBuffer));

//fprintf(stderr, "AudioMonitor2 convertBuffer TPCircularBufferClear and try again, convertedDataLength = %d, space = %d, head = %p\n", convertedDataLength, space, headPtr);

produceBytesResult = TPCircularBufferProduceBytes(&audioConverterCircularBuffer, convertedDataPtr, convertedDataLength);

if (produceBytesResult == false)
{
// If we get here, packets will be dropped

fprintf(stderr, "AudioMonitor convertBuffer Produce convertedDataLength = %d, space = %d, head = %p\n", convertedDataLength, space, ptr);
fprintf(stderr, "AudioMonitor convertBuffer - produce bytes failed, convertedDataLength = %d\n", convertedDataLength);

fprintf(stderr, "AudioMonitor2 convertBuffer failed, drop packet, convertedDataLength = %d, space = %d, head = %p, bufferFilledSize = %lld\n", convertedDataLength, space, headPtr, bufferFilledSize);
}
}

if (audioQueueThreadID == 0)
else
{
//createAudioQueueThread();
TPCircularBufferClear(&(audioConverterCircularBuffer));

usleep(5000);
}
}

Expand Down
6 changes: 3 additions & 3 deletions LocalRadio/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ Gw
</textField>
</subviews>
</view>
<point key="canvasLocation" x="167" y="-15"/>
<point key="canvasLocation" x="167" y="5"/>
</window>
<window title="LocalRadio" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" restorable="NO" releasedWhenClosed="NO" showsToolbarButton="NO" animationBehavior="default" id="QvC-M9-y7g" userLabel="Main Window">
<windowStyleMask key="styleMask" titled="YES" miniaturizable="YES"/>
Expand Down Expand Up @@ -2081,7 +2081,7 @@ Gw
</tabView>
</subviews>
</view>
<point key="canvasLocation" x="-474" y="223.5"/>
<point key="canvasLocation" x="-466" y="589"/>
</window>
<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">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
Expand Down Expand Up @@ -2584,7 +2584,7 @@ Gw
<constraint firstAttribute="trailing" secondItem="T3O-Vc-C1s" secondAttribute="trailing" id="mT0-CP-d7p"/>
</constraints>
</view>
<point key="canvasLocation" x="85" y="-497"/>
<point key="canvasLocation" x="-489" y="12"/>
</window>
</objects>
</document>

0 comments on commit eb99747

Please sign in to comment.