Skip to content

Instantly share code, notes, and snippets.

@jake-b
Created January 23, 2017 21:38
Show Gist options
  • Save jake-b/085c1976ba578f6a5ec60a720d6594cc to your computer and use it in GitHub Desktop.
Save jake-b/085c1976ba578f6a5ec60a720d6594cc to your computer and use it in GitHub Desktop.
diff --git a/Emulator/Sound/TCoreAudioSoundManager.cp b/Emulator/Sound/TCoreAudioSoundManager.cp
index 9211d4b..193531d 100644
--- a/Emulator/Sound/TCoreAudioSoundManager.cp
+++ b/Emulator/Sound/TCoreAudioSoundManager.cp
@@ -43,6 +43,8 @@
// Einstein.
#include "Emulator/Log/TLog.h"
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+
// -------------------------------------------------------------------------- //
// Constantes
// -------------------------------------------------------------------------- //
@@ -226,12 +228,22 @@ TCoreAudioSoundManager::RenderCallback(
UInt32 inNumberFrames,
AudioBufferList* ioData )
{
- // Ask for more data.
- RaiseOutputInterrupt();
+
+ // Rate limiter - only request bytes from NewtonOS when bytes are needed
+ // Otherewise we exhaust the buffers too quickly and truncate the sounds.
+ mDataMutex->Lock();
+ KUIntPtr bytesInBuffer = mOutputBuffer->AvailableBytes();
+ mDataMutex->Unlock();
+
+ if (bytesInBuffer < kNewtonBufferSize) {
+ // Ask for more data.
+ RaiseOutputInterrupt();
+ }
// Copy data from the circle buffer.
- KUIntPtr amount = inNumberFrames * sizeof( KSInt16 );
- mDataMutex->Lock();
+ // TODO: find possible error in the ringBuffer implementation?
+ KUIntPtr amount = MIN(bytesInBuffer, inNumberFrames * sizeof( KSInt16 ));
+
KUIntPtr available =
mOutputBuffer->Consume(
ioData->mBuffers[0].mData,
@@ -266,7 +278,7 @@ TCoreAudioSoundManager::ScheduleOutput( const KUInt8* inBuffer, KUInt32 inSize )
}
// Ask for more data.
- RaiseOutputInterrupt();
+ //RaiseOutputInterrupt();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment