From 6854a509e9d11c5915c2c867ce6ad34527478224 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 15 Oct 2019 00:49:40 +0200 Subject: [PATCH] - Moved all music related synchronization to the top level Most of the synchronization was too deep in the implementation so that it did not guard everything it needed. Now each song has precisely one mutex which must be locked for all access to its internals - this is done in the public ZMusic interface --- libraries/oplsynth/opl_mus_player.cpp | 2 -- libraries/oplsynth/oplsynth/opl_mus_player.h | 2 -- libraries/zmusic/mididevices/mididevice.h | 1 - .../music_fluidsynth_mididevice.cpp | 1 - .../music_softsynth_mididevice.cpp | 2 -- libraries/zmusic/streamsources/music_dumb.cpp | 3 --- libraries/zmusic/streamsources/music_gme.cpp | 3 --- .../zmusic/streamsources/music_libsndfile.cpp | 2 -- libraries/zmusic/zmusic/musinfo.h | 2 ++ libraries/zmusic/zmusic/zmusic.cpp | 23 +++++++++++++++---- 10 files changed, 20 insertions(+), 21 deletions(-) diff --git a/libraries/oplsynth/opl_mus_player.cpp b/libraries/oplsynth/opl_mus_player.cpp index fe771fdd9..9f88538af 100644 --- a/libraries/oplsynth/opl_mus_player.cpp +++ b/libraries/oplsynth/opl_mus_player.cpp @@ -66,7 +66,6 @@ OPLmusicBlock::~OPLmusicBlock() void OPLmusicBlock::ResetChips (int numchips) { - std::lock_guard lock(ChipAccess); io->Reset (); NumChips = io->Init(currentCore, std::min(numchips, 2), FullPan, false); } @@ -255,7 +254,6 @@ bool OPLmusicBlock::ServiceStream (void *buff, int numbytes) memset(buff, 0, numbytes); - std::lock_guard lock(ChipAccess); while (numsamples > 0) { int tick_in = int(NextTickIn); diff --git a/libraries/oplsynth/oplsynth/opl_mus_player.h b/libraries/oplsynth/oplsynth/opl_mus_player.h index 28f09b244..bc0620f88 100644 --- a/libraries/oplsynth/oplsynth/opl_mus_player.h +++ b/libraries/oplsynth/oplsynth/opl_mus_player.h @@ -29,8 +29,6 @@ protected: int currentCore; bool Looping; bool FullPan; - - std::mutex ChipAccess; }; class OPLmusicFile : public OPLmusicBlock diff --git a/libraries/zmusic/mididevices/mididevice.h b/libraries/zmusic/mididevices/mididevice.h index f125e13fb..0021249b2 100644 --- a/libraries/zmusic/mididevices/mididevice.h +++ b/libraries/zmusic/mididevices/mididevice.h @@ -84,7 +84,6 @@ public: SoundStreamInfo GetStreamInfo() const override; protected: - std::mutex CritSec; double Tempo; double Division; double SamplesPerTick; diff --git a/libraries/zmusic/mididevices/music_fluidsynth_mididevice.cpp b/libraries/zmusic/mididevices/music_fluidsynth_mididevice.cpp index 8926ff067..16ffa3ab0 100644 --- a/libraries/zmusic/mididevices/music_fluidsynth_mididevice.cpp +++ b/libraries/zmusic/mididevices/music_fluidsynth_mididevice.cpp @@ -479,7 +479,6 @@ std::string FluidSynthMIDIDevice::GetStats() return "FluidSynth is invalid"; } - std::lock_guard lock(CritSec); int polyphony = fluid_synth_get_polyphony(FluidSynth); int voices = fluid_synth_get_active_voice_count(FluidSynth); double load = fluid_synth_get_cpu_load(FluidSynth); diff --git a/libraries/zmusic/mididevices/music_softsynth_mididevice.cpp b/libraries/zmusic/mididevices/music_softsynth_mididevice.cpp index b346b7d37..8b7ea1f61 100644 --- a/libraries/zmusic/mididevices/music_softsynth_mididevice.cpp +++ b/libraries/zmusic/mididevices/music_softsynth_mididevice.cpp @@ -223,7 +223,6 @@ void SoftSynthMIDIDevice::Stop() int SoftSynthMIDIDevice::StreamOutSync(MidiHeader *header) { - std::lock_guard lock(CritSec); StreamOut(header); return 0; } @@ -374,7 +373,6 @@ bool SoftSynthMIDIDevice::ServiceStream (void *buff, int numbytes) samples1 = samples; memset(buff, 0, numbytes); - std::lock_guard lock(CritSec); while (Events != NULL && numsamples > 0) { double ticky = NextTickIn; diff --git a/libraries/zmusic/streamsources/music_dumb.cpp b/libraries/zmusic/streamsources/music_dumb.cpp index 1fbc78651..e88294fa0 100644 --- a/libraries/zmusic/streamsources/music_dumb.cpp +++ b/libraries/zmusic/streamsources/music_dumb.cpp @@ -86,7 +86,6 @@ protected: size_t written; DUH *duh; DUH_SIGRENDERER *sr; - std::mutex crit_sec; bool open2(long pos); long render(double volume, double delta, long samples, sample_t **buffer); @@ -950,7 +949,6 @@ bool DumbSong::GetData(void *buffer, size_t sizebytes) memset(buffer, 0, sizebytes); return false; } - std::lock_guard lock_(crit_sec); while (sizebytes > 0) { @@ -1070,7 +1068,6 @@ bool DumbSong::SetSubsong(int order) start_order = order; return true; } - std::lock_guard lock(crit_sec); DUH_SIGRENDERER *oldsr = sr; sr = NULL; start_order = order; diff --git a/libraries/zmusic/streamsources/music_gme.cpp b/libraries/zmusic/streamsources/music_gme.cpp index f29870071..a3af553b1 100644 --- a/libraries/zmusic/streamsources/music_gme.cpp +++ b/libraries/zmusic/streamsources/music_gme.cpp @@ -60,7 +60,6 @@ public: SoundStreamInfo GetFormat() override; protected: - std::mutex CritSec; Music_Emu *Emu; gme_info_t *TrackInfo; int SampleRate; @@ -245,7 +244,6 @@ bool GMESong::StartTrack(int track, bool getcritsec) if (getcritsec) { - std::lock_guard lock(CritSec); err = gme_start_track(Emu, track); } else @@ -351,7 +349,6 @@ bool GMESong::GetData(void *buffer, size_t len) { gme_err_t err; - std::lock_guard lock(CritSec); if (gme_track_ended(Emu)) { if (m_Looping) diff --git a/libraries/zmusic/streamsources/music_libsndfile.cpp b/libraries/zmusic/streamsources/music_libsndfile.cpp index d96824323..8b801e284 100644 --- a/libraries/zmusic/streamsources/music_libsndfile.cpp +++ b/libraries/zmusic/streamsources/music_libsndfile.cpp @@ -53,7 +53,6 @@ public: bool GetData(void *buffer, size_t len) override; protected: - std::mutex CritSec; SoundDecoder *Decoder; int Channels; int SampleRate; @@ -440,7 +439,6 @@ std::string SndFileSong::GetStats() bool SndFileSong::GetData(void *vbuff, size_t len) { char *buff = (char*)vbuff; - std::lock_guard lock(CritSec); size_t currentpos = Decoder->getSampleOffset(); size_t framestoread = len / (Channels*2); diff --git a/libraries/zmusic/zmusic/musinfo.h b/libraries/zmusic/zmusic/musinfo.h index 12aa87df1..3b934aab4 100644 --- a/libraries/zmusic/zmusic/musinfo.h +++ b/libraries/zmusic/zmusic/musinfo.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "mididefs.h" // The base music class. Everything is derived from this -------------------- @@ -37,4 +38,5 @@ public: STATE_Paused } m_Status = STATE_Stopped; bool m_Looping = false; + std::mutex CritSec; }; diff --git a/libraries/zmusic/zmusic/zmusic.cpp b/libraries/zmusic/zmusic/zmusic.cpp index 4060ee223..6943517b8 100644 --- a/libraries/zmusic/zmusic/zmusic.cpp +++ b/libraries/zmusic/zmusic/zmusic.cpp @@ -312,10 +312,11 @@ MusInfo *ZMusic_OpenCDSong (int track, int id) // //========================================================================== -bool ZMusic_FillStream(MusInfo* stream, void* buff, int len) +bool ZMusic_FillStream(MusInfo* song, void* buff, int len) { - if (stream == nullptr) return false; - return stream->ServiceStream(buff, len); + if (song == nullptr) return false; + std::lock_guard lock(song->CritSec); + return song->ServiceStream(buff, len); } //========================================================================== @@ -339,36 +340,42 @@ void ZMusic_Start(MusInfo *song, int subsong, bool loop) void ZMusic_Pause(MusInfo *song) { if (!song) return; + std::lock_guard lock(song->CritSec); song->Pause(); } void ZMusic_Resume(MusInfo *song) { if (!song) return; + std::lock_guard lock(song->CritSec); song->Resume(); } void ZMusic_Update(MusInfo *song) { if (!song) return; + std::lock_guard lock(song->CritSec); song->Update(); } bool ZMusic_IsPlaying(MusInfo *song) { if (!song) return false; + std::lock_guard lock(song->CritSec); return song->IsPlaying(); } void ZMusic_Stop(MusInfo *song) { if (!song) return; + std::lock_guard lock(song->CritSec); song->Stop(); } bool ZMusic_SetSubsong(MusInfo *song, int subsong) { if (!song) return false; + std::lock_guard lock(song->CritSec); return song->SetSubsong(subsong); } @@ -387,21 +394,27 @@ bool ZMusic_IsMIDI(MusInfo *song) SoundStreamInfo ZMusic_GetStreamInfo(MusInfo *song) { if (!song) return {}; + std::lock_guard lock(song->CritSec); return song->GetStreamInfo(); } void ZMusic_Close(MusInfo *song) { - if (song) delete song; + if (!song) return; + std::lock_guard lock(song->CritSec); + delete song; } void ZMusic_VolumeChanged(MusInfo *song) { - if (song) song->MusicVolumeChanged(); + if (!song) return; + std::lock_guard lock(song->CritSec); + song->MusicVolumeChanged(); } std::string ZMusic_GetStats(MusInfo *song) { if (!song) return ""; + std::lock_guard lock(song->CritSec); return song->GetStats(); }