Separate MIDI streamer and source creation in the high level code.

This commit is contained in:
Christoph Oelckers 2018-02-23 13:20:07 +01:00
parent 9aa1199902
commit 9a1479ab08
3 changed files with 31 additions and 16 deletions

View file

@ -309,33 +309,42 @@ MusInfo *MusInfo::GetWaveDumper(const char *filename, int rate)
//========================================================================== //==========================================================================
// //
// create a streamer based on MIDI file type // create a source based on MIDI file type
// //
//========================================================================== //==========================================================================
static MIDIStreamer *CreateMIDIStreamer(FileReader &reader, EMidiDevice devtype, EMIDIType miditype, const char *args) static MIDISource *CreateMIDISource(FileReader &reader, EMIDIType miditype)
{ {
MIDISource *source = nullptr; MIDISource *source = nullptr;
switch (miditype) switch (miditype)
{ {
case MIDI_MUS: case MIDI_MUS:
source = new MUSSong2(reader); return new MUSSong2(reader);
case MIDI_MIDI: case MIDI_MIDI:
source = new MIDISong2(reader); return new MIDISong2(reader);
case MIDI_HMI: case MIDI_HMI:
source = new HMISong(reader); return new HMISong(reader);
case MIDI_XMI: case MIDI_XMI:
source = new XMISong(reader); return new XMISong(reader);
default: default:
return NULL; return NULL;
} }
auto streamer = new MIDIStreamer(devtype, args); }
streamer->SetMIDISource(source);
//==========================================================================
//
// create a streamer
//
//==========================================================================
static MIDIStreamer *CreateMIDIStreamer(EMidiDevice devtype, const char *args)
{
auto me = new MIDIStreamer(devtype, args);
return me;
} }
//========================================================================== //==========================================================================
@ -447,6 +456,15 @@ MusInfo *I_RegisterSong (FileReader *reader, MidiDeviceSetting *device)
EMIDIType miditype = IdentifyMIDIType(id, sizeof(id)); EMIDIType miditype = IdentifyMIDIType(id, sizeof(id));
if (miditype != MIDI_NOTMIDI) if (miditype != MIDI_NOTMIDI)
{ {
auto source = CreateMIDISource(*reader, miditype);
if (source == nullptr) return 0;
if (!source->isValid())
{
delete source;
return 0;
}
// fixme: device and streamer need to be handled individually.
EMidiDevice devtype = device == NULL? MDEV_DEFAULT : (EMidiDevice)device->device; EMidiDevice devtype = device == NULL? MDEV_DEFAULT : (EMidiDevice)device->device;
#ifndef _WIN32 #ifndef _WIN32
// non-Windows platforms don't support MDEV_MMAPI so map to MDEV_SNDSYS // non-Windows platforms don't support MDEV_MMAPI so map to MDEV_SNDSYS
@ -455,12 +473,8 @@ MusInfo *I_RegisterSong (FileReader *reader, MidiDeviceSetting *device)
#endif #endif
retry_as_sndsys: retry_as_sndsys:
info = CreateMIDIStreamer(*reader, devtype, miditype, device != NULL? device->args.GetChars() : ""); info = CreateMIDIStreamer(devtype, device != NULL? device->args.GetChars() : "");
if (info != NULL && !info->IsValid())
{
delete info;
info = NULL;
}
if (info == NULL && devtype != MDEV_SNDSYS && snd_mididevice < 0) if (info == NULL && devtype != MDEV_SNDSYS && snd_mididevice < 0)
{ {
devtype = MDEV_SNDSYS; devtype = MDEV_SNDSYS;
@ -469,7 +483,7 @@ retry_as_sndsys:
#ifdef _WIN32 #ifdef _WIN32
if (info == NULL && devtype != MDEV_MMAPI && snd_mididevice >= 0) if (info == NULL && devtype != MDEV_MMAPI && snd_mididevice >= 0)
{ {
info = CreateMIDIStreamer(*reader, MDEV_MMAPI, miditype, ""); info = CreateMIDIStreamer(MDEV_MMAPI, "");
} }
#endif #endif
} }

View file

@ -52,6 +52,7 @@ public:
isLooping = looped; isLooping = looped;
} }
bool isValid() const { return Division > 0; }
int getDivision() const { return Division; } int getDivision() const { return Division; }
int getInitialTempo() const { return InitialTempo; } int getInitialTempo() const { return InitialTempo; }
int getTempo() const { return Tempo; } int getTempo() const { return Tempo; }

View file

@ -146,7 +146,7 @@ bool MIDIStreamer::IsMIDI() const
bool MIDIStreamer::IsValid() const bool MIDIStreamer::IsValid() const
{ {
return source != nullptr && source->getDivision() != 0; return source != nullptr && source->isValid();
} }