mirror of
https://github.com/ZDoom/gzdoom.git
synced 2024-11-29 15:22:08 +00:00
- 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
This commit is contained in:
parent
dc32c2148a
commit
6854a509e9
10 changed files with 20 additions and 21 deletions
|
@ -66,7 +66,6 @@ OPLmusicBlock::~OPLmusicBlock()
|
||||||
|
|
||||||
void OPLmusicBlock::ResetChips (int numchips)
|
void OPLmusicBlock::ResetChips (int numchips)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(ChipAccess);
|
|
||||||
io->Reset ();
|
io->Reset ();
|
||||||
NumChips = io->Init(currentCore, std::min(numchips, 2), FullPan, false);
|
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);
|
memset(buff, 0, numbytes);
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(ChipAccess);
|
|
||||||
while (numsamples > 0)
|
while (numsamples > 0)
|
||||||
{
|
{
|
||||||
int tick_in = int(NextTickIn);
|
int tick_in = int(NextTickIn);
|
||||||
|
|
|
@ -29,8 +29,6 @@ protected:
|
||||||
int currentCore;
|
int currentCore;
|
||||||
bool Looping;
|
bool Looping;
|
||||||
bool FullPan;
|
bool FullPan;
|
||||||
|
|
||||||
std::mutex ChipAccess;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class OPLmusicFile : public OPLmusicBlock
|
class OPLmusicFile : public OPLmusicBlock
|
||||||
|
|
|
@ -84,7 +84,6 @@ public:
|
||||||
SoundStreamInfo GetStreamInfo() const override;
|
SoundStreamInfo GetStreamInfo() const override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::mutex CritSec;
|
|
||||||
double Tempo;
|
double Tempo;
|
||||||
double Division;
|
double Division;
|
||||||
double SamplesPerTick;
|
double SamplesPerTick;
|
||||||
|
|
|
@ -479,7 +479,6 @@ std::string FluidSynthMIDIDevice::GetStats()
|
||||||
return "FluidSynth is invalid";
|
return "FluidSynth is invalid";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(CritSec);
|
|
||||||
int polyphony = fluid_synth_get_polyphony(FluidSynth);
|
int polyphony = fluid_synth_get_polyphony(FluidSynth);
|
||||||
int voices = fluid_synth_get_active_voice_count(FluidSynth);
|
int voices = fluid_synth_get_active_voice_count(FluidSynth);
|
||||||
double load = fluid_synth_get_cpu_load(FluidSynth);
|
double load = fluid_synth_get_cpu_load(FluidSynth);
|
||||||
|
|
|
@ -223,7 +223,6 @@ void SoftSynthMIDIDevice::Stop()
|
||||||
|
|
||||||
int SoftSynthMIDIDevice::StreamOutSync(MidiHeader *header)
|
int SoftSynthMIDIDevice::StreamOutSync(MidiHeader *header)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(CritSec);
|
|
||||||
StreamOut(header);
|
StreamOut(header);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -374,7 +373,6 @@ bool SoftSynthMIDIDevice::ServiceStream (void *buff, int numbytes)
|
||||||
samples1 = samples;
|
samples1 = samples;
|
||||||
memset(buff, 0, numbytes);
|
memset(buff, 0, numbytes);
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(CritSec);
|
|
||||||
while (Events != NULL && numsamples > 0)
|
while (Events != NULL && numsamples > 0)
|
||||||
{
|
{
|
||||||
double ticky = NextTickIn;
|
double ticky = NextTickIn;
|
||||||
|
|
|
@ -86,7 +86,6 @@ protected:
|
||||||
size_t written;
|
size_t written;
|
||||||
DUH *duh;
|
DUH *duh;
|
||||||
DUH_SIGRENDERER *sr;
|
DUH_SIGRENDERER *sr;
|
||||||
std::mutex crit_sec;
|
|
||||||
|
|
||||||
bool open2(long pos);
|
bool open2(long pos);
|
||||||
long render(double volume, double delta, long samples, sample_t **buffer);
|
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);
|
memset(buffer, 0, sizebytes);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::lock_guard<std::mutex> lock_(crit_sec);
|
|
||||||
|
|
||||||
while (sizebytes > 0)
|
while (sizebytes > 0)
|
||||||
{
|
{
|
||||||
|
@ -1070,7 +1068,6 @@ bool DumbSong::SetSubsong(int order)
|
||||||
start_order = order;
|
start_order = order;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
std::lock_guard<std::mutex> lock(crit_sec);
|
|
||||||
DUH_SIGRENDERER *oldsr = sr;
|
DUH_SIGRENDERER *oldsr = sr;
|
||||||
sr = NULL;
|
sr = NULL;
|
||||||
start_order = order;
|
start_order = order;
|
||||||
|
|
|
@ -60,7 +60,6 @@ public:
|
||||||
SoundStreamInfo GetFormat() override;
|
SoundStreamInfo GetFormat() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::mutex CritSec;
|
|
||||||
Music_Emu *Emu;
|
Music_Emu *Emu;
|
||||||
gme_info_t *TrackInfo;
|
gme_info_t *TrackInfo;
|
||||||
int SampleRate;
|
int SampleRate;
|
||||||
|
@ -245,7 +244,6 @@ bool GMESong::StartTrack(int track, bool getcritsec)
|
||||||
|
|
||||||
if (getcritsec)
|
if (getcritsec)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(CritSec);
|
|
||||||
err = gme_start_track(Emu, track);
|
err = gme_start_track(Emu, track);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -351,7 +349,6 @@ bool GMESong::GetData(void *buffer, size_t len)
|
||||||
{
|
{
|
||||||
gme_err_t err;
|
gme_err_t err;
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(CritSec);
|
|
||||||
if (gme_track_ended(Emu))
|
if (gme_track_ended(Emu))
|
||||||
{
|
{
|
||||||
if (m_Looping)
|
if (m_Looping)
|
||||||
|
|
|
@ -53,7 +53,6 @@ public:
|
||||||
bool GetData(void *buffer, size_t len) override;
|
bool GetData(void *buffer, size_t len) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::mutex CritSec;
|
|
||||||
SoundDecoder *Decoder;
|
SoundDecoder *Decoder;
|
||||||
int Channels;
|
int Channels;
|
||||||
int SampleRate;
|
int SampleRate;
|
||||||
|
@ -440,7 +439,6 @@ std::string SndFileSong::GetStats()
|
||||||
bool SndFileSong::GetData(void *vbuff, size_t len)
|
bool SndFileSong::GetData(void *vbuff, size_t len)
|
||||||
{
|
{
|
||||||
char *buff = (char*)vbuff;
|
char *buff = (char*)vbuff;
|
||||||
std::lock_guard<std::mutex> lock(CritSec);
|
|
||||||
|
|
||||||
size_t currentpos = Decoder->getSampleOffset();
|
size_t currentpos = Decoder->getSampleOffset();
|
||||||
size_t framestoread = len / (Channels*2);
|
size_t framestoread = len / (Channels*2);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <mutex>
|
||||||
#include "mididefs.h"
|
#include "mididefs.h"
|
||||||
|
|
||||||
// The base music class. Everything is derived from this --------------------
|
// The base music class. Everything is derived from this --------------------
|
||||||
|
@ -37,4 +38,5 @@ public:
|
||||||
STATE_Paused
|
STATE_Paused
|
||||||
} m_Status = STATE_Stopped;
|
} m_Status = STATE_Stopped;
|
||||||
bool m_Looping = false;
|
bool m_Looping = false;
|
||||||
|
std::mutex CritSec;
|
||||||
};
|
};
|
||||||
|
|
|
@ -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;
|
if (song == nullptr) return false;
|
||||||
return stream->ServiceStream(buff, len);
|
std::lock_guard<std::mutex> 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)
|
void ZMusic_Pause(MusInfo *song)
|
||||||
{
|
{
|
||||||
if (!song) return;
|
if (!song) return;
|
||||||
|
std::lock_guard<std::mutex> lock(song->CritSec);
|
||||||
song->Pause();
|
song->Pause();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZMusic_Resume(MusInfo *song)
|
void ZMusic_Resume(MusInfo *song)
|
||||||
{
|
{
|
||||||
if (!song) return;
|
if (!song) return;
|
||||||
|
std::lock_guard<std::mutex> lock(song->CritSec);
|
||||||
song->Resume();
|
song->Resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZMusic_Update(MusInfo *song)
|
void ZMusic_Update(MusInfo *song)
|
||||||
{
|
{
|
||||||
if (!song) return;
|
if (!song) return;
|
||||||
|
std::lock_guard<std::mutex> lock(song->CritSec);
|
||||||
song->Update();
|
song->Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ZMusic_IsPlaying(MusInfo *song)
|
bool ZMusic_IsPlaying(MusInfo *song)
|
||||||
{
|
{
|
||||||
if (!song) return false;
|
if (!song) return false;
|
||||||
|
std::lock_guard<std::mutex> lock(song->CritSec);
|
||||||
return song->IsPlaying();
|
return song->IsPlaying();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZMusic_Stop(MusInfo *song)
|
void ZMusic_Stop(MusInfo *song)
|
||||||
{
|
{
|
||||||
if (!song) return;
|
if (!song) return;
|
||||||
|
std::lock_guard<std::mutex> lock(song->CritSec);
|
||||||
song->Stop();
|
song->Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ZMusic_SetSubsong(MusInfo *song, int subsong)
|
bool ZMusic_SetSubsong(MusInfo *song, int subsong)
|
||||||
{
|
{
|
||||||
if (!song) return false;
|
if (!song) return false;
|
||||||
|
std::lock_guard<std::mutex> lock(song->CritSec);
|
||||||
return song->SetSubsong(subsong);
|
return song->SetSubsong(subsong);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -387,21 +394,27 @@ bool ZMusic_IsMIDI(MusInfo *song)
|
||||||
SoundStreamInfo ZMusic_GetStreamInfo(MusInfo *song)
|
SoundStreamInfo ZMusic_GetStreamInfo(MusInfo *song)
|
||||||
{
|
{
|
||||||
if (!song) return {};
|
if (!song) return {};
|
||||||
|
std::lock_guard<std::mutex> lock(song->CritSec);
|
||||||
return song->GetStreamInfo();
|
return song->GetStreamInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZMusic_Close(MusInfo *song)
|
void ZMusic_Close(MusInfo *song)
|
||||||
{
|
{
|
||||||
if (song) delete song;
|
if (!song) return;
|
||||||
|
std::lock_guard<std::mutex> lock(song->CritSec);
|
||||||
|
delete song;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ZMusic_VolumeChanged(MusInfo *song)
|
void ZMusic_VolumeChanged(MusInfo *song)
|
||||||
{
|
{
|
||||||
if (song) song->MusicVolumeChanged();
|
if (!song) return;
|
||||||
|
std::lock_guard<std::mutex> lock(song->CritSec);
|
||||||
|
song->MusicVolumeChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ZMusic_GetStats(MusInfo *song)
|
std::string ZMusic_GetStats(MusInfo *song)
|
||||||
{
|
{
|
||||||
if (!song) return "";
|
if (!song) return "";
|
||||||
|
std::lock_guard<std::mutex> lock(song->CritSec);
|
||||||
return song->GetStats();
|
return song->GetStats();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue