diff options
author | Wohlstand <Wohlstand@users.noreply.github.com> | 2024-05-24 19:33:44 +0300 |
---|---|---|
committer | Wohlstand <Wohlstand@users.noreply.github.com> | 2024-05-24 19:33:44 +0300 |
commit | b3604289940aa8f2690f79434eb96de9bbad30be (patch) | |
tree | 309b5e1b007eaf5c1fc990258972adb1add06470 /utils/midiplay | |
parent | 9fff2b20c5aed43aac0c94004c13389969f950fe (diff) | |
download | libADLMIDI-b3604289940aa8f2690f79434eb96de9bbad30be.tar.gz libADLMIDI-b3604289940aa8f2690f79434eb96de9bbad30be.tar.bz2 libADLMIDI-b3604289940aa8f2690f79434eb96de9bbad30be.zip |
Serial: Tweaked some of timer code
Diffstat (limited to 'utils/midiplay')
-rw-r--r-- | utils/midiplay/CMakeLists.txt | 1 | ||||
-rw-r--r-- | utils/midiplay/adlmidiplay.cpp | 39 |
2 files changed, 29 insertions, 11 deletions
diff --git a/utils/midiplay/CMakeLists.txt b/utils/midiplay/CMakeLists.txt index 52d0128..36c88b5 100644 --- a/utils/midiplay/CMakeLists.txt +++ b/utils/midiplay/CMakeLists.txt @@ -30,6 +30,7 @@ add_executable(adlmidiplay ${ADLMIDI_PLAY_SRC}) if(USE_SDL2_AUDIO) target_link_libraries(adlmidiplay PRIVATE ADLMIDI_SDL2) + target_compile_definitions(adlmidiplay PRIVATE ADLMIDI_USE_SDL2) elseif(WIN32) target_link_libraries(adlmidiplay PRIVATE winmm) endif() diff --git a/utils/midiplay/adlmidiplay.cpp b/utils/midiplay/adlmidiplay.cpp index 6f690ba..021bf54 100644 --- a/utils/midiplay/adlmidiplay.cpp +++ b/utils/midiplay/adlmidiplay.cpp @@ -36,15 +36,34 @@ #include <stdint.h> #if defined(ADLMIDI_ENABLE_HW_SERIAL) && !defined(OUTPUT_WAVE_ONLY) +# ifdef ADLMIDI_USE_SDL2 +# include <SDL2/SDL_timer.h> +static inline double s_getTime() +{ + return SDL_GetTicks64() / 1000.0; +} + +static inline void s_sleepU(double s) +{ + SDL_Delay((Uint32)(s * 1000)); +} +# else + # include <time.h> # include <unistd.h> # include <assert.h> -static inline long long s_getTime() +static inline double s_getTime() { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); - return t.tv_nsec + (t.tv_sec * 1000000000); + return (t.tv_nsec + (t.tv_sec * 1000000000)) / 1000000000.0; } + +static inline void s_sleepU(double s) +{ + usleep((uint32_t)(s * 1000000)); +} +# endif #endif #ifdef DEBUG_SONG_SWITCHING @@ -795,7 +814,7 @@ static void runHWSerialLoop(ADL_MIDIPlayer *myDevice) { double tick_delay = 0.00000001; double tick_wait = 0.0; - long long timeBegL, timeEndL; + double timeBegL, timeEndL; const double minDelay = 0.005; double eat_delay; bool tickSkip = true; @@ -821,18 +840,16 @@ static void runHWSerialLoop(ADL_MIDIPlayer *myDevice) if(timeEndL < timeBegL) timeEndL = timeBegL; - if(tick_delay < 0.00000001) - tick_delay = 0.00000001; - - eat_delay = (double)(timeEndL - timeBegL) / 1000000000; + eat_delay = timeEndL - timeBegL; tick_wait += tick_delay - eat_delay; if(tick_wait > 0.0) { if(tick_wait < minDelay) { - usleep(tick_wait * 1000000); - tick_wait = 0.0; + if(tick_wait > 0.0) + s_sleepU(tick_wait); + tick_wait -= minDelay; } while(tick_wait > 0.0) @@ -847,10 +864,10 @@ static void runHWSerialLoop(ADL_MIDIPlayer *myDevice) if(timeEndL < timeBegL) timeEndL = timeBegL; - double microDelay = minDelay - ((timeEndL - timeBegL) / 1000000000.0); + double microDelay = minDelay - (timeEndL - timeBegL); if(microDelay > 0.0) - usleep(microDelay * 1000000); + s_sleepU(microDelay); tick_wait -= minDelay; } |