From 732ccbbb26b5933e94da523466552c7b89c9d957 Mon Sep 17 00:00:00 2001 From: Karl Blomster Date: Thu, 7 May 2009 14:47:36 +0000 Subject: [PATCH] The buffer length/wanted latency in the dsound2 audio player are now tweakable in config.dat; the options are called "audio dsound buffer latency" and "audio dsound buffer length" respectively. Defaults to 100/5 respectively (should work for the people who used to get buffer errors); for better responsiveness try 80/4. Originally committed to SVN as r2905. --- aegisub/src/audio_player_dsound2.cpp | 34 +++++++++++++++++++--------- aegisub/src/audio_player_dsound2.h | 4 ++++ aegisub/src/options.cpp | 3 +++ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/aegisub/src/audio_player_dsound2.cpp b/aegisub/src/audio_player_dsound2.cpp index 7a40cb43e..192726c32 100644 --- a/aegisub/src/audio_player_dsound2.cpp +++ b/aegisub/src/audio_player_dsound2.cpp @@ -48,6 +48,7 @@ #include "utils.h" #include "main.h" #include "frame_main.h" +#include "options.h" #include "audio_player_dsound2.h" @@ -131,12 +132,15 @@ class DirectSoundPlayer2Thread { int64_t start_frame; int64_t end_frame; + int wanted_latency; + int buffer_length; + DWORD last_playback_restart; AudioProvider *provider; public: - DirectSoundPlayer2Thread(AudioProvider *provider); + DirectSoundPlayer2Thread(AudioProvider *provider, int WantedLatency, int BufferLength); ~DirectSoundPlayer2Thread(); void Play(int64_t start, int64_t count); @@ -159,11 +163,6 @@ unsigned int __stdcall DirectSoundPlayer2Thread::ThreadProc(void *parameter) } -#define WANTED_LATENCY 80 -#define BUFFER_LENGTH 4 -// The buffer will hold BUFFER_LENGTH times WANTED_LATENCY milliseconds of audio - - void DirectSoundPlayer2Thread::Run() { #define REPORT_ERROR(msg) { error_message = _T("DirectSoundPlayer2Thread: ") _T(msg); SetEvent(error_happened); return; } @@ -194,7 +193,7 @@ void DirectSoundPlayer2Thread::Run() waveFormat.cbSize = sizeof(waveFormat); // And the buffer itself - int aim = waveFormat.nAvgBytesPerSec * (WANTED_LATENCY*BUFFER_LENGTH)/1000; + int aim = waveFormat.nAvgBytesPerSec * (wanted_latency*buffer_length)/1000; int min = DSBSIZE_MIN; int max = DSBSIZE_MAX; DWORD bufSize = MIN(MAX(min,aim),max); // size of entier playback buffer @@ -239,7 +238,7 @@ void DirectSoundPlayer2Thread::Run() while (running) { - DWORD wait_result = WaitForMultipleObjects(sizeof(events_to_wait)/sizeof(HANDLE), events_to_wait, FALSE, WANTED_LATENCY); + DWORD wait_result = WaitForMultipleObjects(sizeof(events_to_wait)/sizeof(HANDLE), events_to_wait, FALSE, wanted_latency); switch (wait_result) { @@ -475,7 +474,7 @@ void DirectSoundPlayer2Thread::CheckError() } -DirectSoundPlayer2Thread::DirectSoundPlayer2Thread(AudioProvider *provider) +DirectSoundPlayer2Thread::DirectSoundPlayer2Thread(AudioProvider *provider, int _WantedLatency, int _BufferLength) { event_start_playback = CreateEvent(0, FALSE, FALSE, 0); event_stop_playback = CreateEvent(0, FALSE, FALSE, 0); @@ -492,6 +491,9 @@ DirectSoundPlayer2Thread::DirectSoundPlayer2Thread(AudioProvider *provider) start_frame = 0; end_frame = 0; + wanted_latency = _WantedLatency; + buffer_length = _BufferLength; + this->provider = provider; thread_handle = (HANDLE)_beginthreadex(0, 0, ThreadProc, this, 0, 0); @@ -613,6 +615,16 @@ double DirectSoundPlayer2Thread::GetVolume() DirectSoundPlayer2::DirectSoundPlayer2() { thread = 0; + + // The buffer will hold BufferLength times WantedLatency milliseconds of audio + WantedLatency = Options.AsInt(_T("Audio dsound buffer latency")); + BufferLength = Options.AsInt(_T("Audio dsound buffer length")); + + // sanity checking + if (WantedLatency <= 0) + WantedLatency = 100; + if (BufferLength <= 0) + BufferLength = 5; } @@ -628,7 +640,7 @@ void DirectSoundPlayer2::OpenStream() try { - thread = new DirectSoundPlayer2Thread(GetProvider()); + thread = new DirectSoundPlayer2Thread(GetProvider(), WantedLatency, BufferLength); } catch (const wxChar *msg) { @@ -661,7 +673,7 @@ void DirectSoundPlayer2::SetProvider(AudioProvider *provider) if (thread && provider != GetProvider()) { delete thread; - thread = new DirectSoundPlayer2Thread(provider); + thread = new DirectSoundPlayer2Thread(provider, WantedLatency, BufferLength); } AudioPlayer::SetProvider(provider); diff --git a/aegisub/src/audio_player_dsound2.h b/aegisub/src/audio_player_dsound2.h index d098af8b9..b939b3251 100644 --- a/aegisub/src/audio_player_dsound2.h +++ b/aegisub/src/audio_player_dsound2.h @@ -45,6 +45,10 @@ class DirectSoundPlayer2Thread; class DirectSoundPlayer2 : public AudioPlayer { DirectSoundPlayer2Thread *thread; +protected: + int WantedLatency; + int BufferLength; + public: DirectSoundPlayer2(); ~DirectSoundPlayer2(); diff --git a/aegisub/src/options.cpp b/aegisub/src/options.cpp index 8c0814af8..b70197f60 100644 --- a/aegisub/src/options.cpp +++ b/aegisub/src/options.cpp @@ -219,6 +219,9 @@ void OptionsManager::LoadDefaults(bool onlyDefaults,bool doOverride) { SetInt(_T("Audio Spectrum Cutoff"),0); SetInt(_T("Audio Spectrum Quality"),1); SetInt(_T("Audio Spectrum Memory Max"),128); // megabytes + // and this one could do with just reiniting the audio player + SetInt(_T("Audio dsound buffer latency"), 100); + SetInt(_T("Audio dsound buffer length"), 5); // Automation // The path changes only take effect when a script is (re)loaded but Automatic should be good enough, it certainly doesn't warrart a restart