Skip to content

Commit

Permalink
- rewrote the ZMusic interface so that it is free of C++ constructs.
Browse files Browse the repository at this point in the history
Now it is ready to put in a DLL.
  • Loading branch information
coelckers committed Jan 2, 2020
1 parent 0d00034 commit d2ca1ea
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 182 deletions.
3 changes: 2 additions & 1 deletion libraries/music_common/fileio.h
Expand Up @@ -379,6 +379,7 @@ class SF2Reader : public FileSystemSoundFontReader
}
};


MusicIO::SoundFontReaderInterface* ClientOpenSoundFont(const char* name, int type);

}

20 changes: 13 additions & 7 deletions libraries/zmusic/mididevices/music_timidity_mididevice.cpp
Expand Up @@ -267,14 +267,20 @@ bool GUS_SetupConfig(const char* args)
if (*args == 0) args = gusConfig.gus_config.c_str();
if (stricmp(gusConfig.loadedConfig.c_str(), args) == 0) return false; // aleady loaded

MusicIO::SoundFontReaderInterface *reader = nullptr;
if (musicCallbacks.OpenSoundFont)
MusicIO::SoundFontReaderInterface* reader = MusicIO::ClientOpenSoundFont(args, SF_GUS | SF_SF2);
if (!reader && MusicIO::fileExists(args))
{
reader = musicCallbacks.OpenSoundFont(args, SF_GUS | SF_SF2);
}
else if (MusicIO::fileExists(args))
{
reader = new MusicIO::FileSystemSoundFontReader(args, true);
auto f = MusicIO::utf8_fopen(args, "rb");
if (f)
{
char test[12] = {};
fread(test, 1, 12, f);
fclose(f);
// If the passed file is an SF2 sound font we need to use the special reader that fakes a config for it.
if (memcmp(test, "RIFF", 4) == 0 && memcmp(test + 8, "sfbk", 4) == 0)
reader = new MusicIO::SF2Reader(args);
}
if (!reader) reader = new MusicIO::FileSystemSoundFontReader(args, true);
}

if (reader == nullptr)
Expand Down
20 changes: 13 additions & 7 deletions libraries/zmusic/mididevices/music_timiditypp_mididevice.cpp
Expand Up @@ -198,14 +198,20 @@ bool Timidity_SetupConfig(const char* args)
if (*args == 0) args = timidityConfig.timidity_config.c_str();
if (stricmp(timidityConfig.loadedConfig.c_str(), args) == 0) return false; // aleady loaded

MusicIO::SoundFontReaderInterface* reader = nullptr;
if (musicCallbacks.OpenSoundFont)
MusicIO::SoundFontReaderInterface* reader = MusicIO::ClientOpenSoundFont(args, SF_GUS | SF_SF2);
if (!reader && MusicIO::fileExists(args))
{
reader = musicCallbacks.OpenSoundFont(args, SF_GUS | SF_SF2);
}
else if (MusicIO::fileExists(args))
{
reader = new MusicIO::FileSystemSoundFontReader(args, true);
auto f = MusicIO::utf8_fopen(args, "rb");
if (f)
{
char test[12] = {};
fread(test, 1, 12, f);
fclose(f);
// If the passed file is an SF2 sound font we need to use the special reader that fakes a config for it.
if (memcmp(test, "RIFF", 4) == 0 && memcmp(test + 8, "sfbk", 4) == 0)
reader = new MusicIO::SF2Reader(args);
}
if (!reader) reader = new MusicIO::FileSystemSoundFontReader(args, true);
}

if (reader == nullptr)
Expand Down
8 changes: 2 additions & 6 deletions libraries/zmusic/mididevices/music_wildmidi_mididevice.cpp
Expand Up @@ -244,12 +244,8 @@ bool WildMidi_SetupConfig(const char* args)
if (*args == 0) args = wildMidiConfig.config.c_str();
if (stricmp(wildMidiConfig.loadedConfig.c_str(), args) == 0) return false; // aleady loaded

MusicIO::SoundFontReaderInterface* reader = nullptr;
if (musicCallbacks.OpenSoundFont)
{
reader = musicCallbacks.OpenSoundFont(args, SF_GUS);
}
else if (MusicIO::fileExists(args))
MusicIO::SoundFontReaderInterface* reader = MusicIO::ClientOpenSoundFont(args, SF_GUS);
if (!reader && MusicIO::fileExists(args))
{
reader = new MusicIO::FileSystemSoundFontReader(args, true);
}
Expand Down
1 change: 1 addition & 0 deletions libraries/zmusic/midisources/midisource_hmi.cpp
Expand Up @@ -37,6 +37,7 @@
#include <algorithm>
#include <assert.h>
#include "midisource.h"
#include "zmusic/zmusic_internal.h"
#include "zmusic/m_swap.h"

// MACROS ------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions libraries/zmusic/midisources/midisource_smf.cpp
Expand Up @@ -38,6 +38,7 @@
// HEADER FILES ------------------------------------------------------------

#include "midisource.h"
#include "zmusic/zmusic_internal.h"

// MACROS ------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion libraries/zmusic/musicformats/music_cd.cpp
Expand Up @@ -31,7 +31,7 @@
**
*/

#include "zmusic/zmusic.h"
#include "zmusic/zmusic_internal.h"
#include "zmusic/musinfo.h"

#ifdef _WIN32
Expand Down
1 change: 1 addition & 0 deletions libraries/zmusic/musicformats/music_stream.cpp
Expand Up @@ -33,6 +33,7 @@
*/

#include "zmusic/musinfo.h"
#include "zmusic/zmusic_internal.h"
#include "streamsources/streamsource.h"

class StreamSong : public MusInfo
Expand Down
47 changes: 47 additions & 0 deletions libraries/zmusic/zmusic/configuration.cpp
Expand Up @@ -54,9 +54,56 @@ struct Dummy
MiscConfig miscConfig;
Callbacks musicCallbacks;

class SoundFontWrapperInterface : public MusicIO::SoundFontReaderInterface
{
void* handle;

public:
SoundFontWrapperInterface(void* h)
{
handle = h;
}

struct MusicIO::FileInterface* open_file(const char* fn) override
{
auto rd = musicCallbacks.SF_OpenFile(handle, fn);
if (rd)
{
auto fr = new CustomFileReader(rd);
if (fr) fr->filename = fn? fn : "timidity.cfg";
return fr;
}
else return nullptr;
}
void add_search_path(const char* path) override
{
musicCallbacks.SF_AddToSearchPath(handle, path);
}
void close() override
{
musicCallbacks.SF_Close(handle);
delete this;
}
};

namespace MusicIO {
SoundFontReaderInterface* ClientOpenSoundFont(const char* name, int type)
{
if (!musicCallbacks.OpenSoundFont) return nullptr;
auto iface = musicCallbacks.OpenSoundFont(name, type);
if (!iface) return nullptr;
return new SoundFontWrapperInterface(iface);
}
}


DLL_EXPORT void ZMusic_SetCallbacks(const Callbacks* cb)
{
musicCallbacks = *cb;
// If not all these are set the sound font interface is not usable.
if (!cb->SF_AddToSearchPath || !cb->SF_OpenFile || !cb->SF_Close)
musicCallbacks.OpenSoundFont = nullptr;

}

DLL_EXPORT void ZMusic_SetGenMidi(const uint8_t* data)
Expand Down
55 changes: 0 additions & 55 deletions libraries/zmusic/zmusic/mididefs.h
Expand Up @@ -10,68 +10,13 @@ enum
inline constexpr uint8_t MEVENT_EVENTTYPE(uint32_t x) { return ((uint8_t)((x) >> 24)); }
inline constexpr uint32_t MEVENT_EVENTPARM(uint32_t x) { return ((x) & 0xffffff); }

// These constants must match the corresponding values of the Windows headers
// to avoid readjustment in the native Windows device's playback functions
// and should not be changed.
enum EMidiDeviceClass
{
MIDIDEV_MIDIPORT = 1,
MIDIDEV_SYNTH,
MIDIDEV_SQSYNTH,
MIDIDEV_FMSYNTH,
MIDIDEV_MAPPER,
MIDIDEV_WAVETABLE,
MIDIDEV_SWSYNTH
};

enum EMIDIType
{
MIDI_NOTMIDI,
MIDI_MIDI,
MIDI_HMI,
MIDI_XMI,
MIDI_MUS
};

enum EMidiEvent : uint8_t
{
MEVENT_TEMPO = 1,
MEVENT_NOP = 2,
MEVENT_LONGMSG = 128,
};

enum EMidiDevice
{
MDEV_DEFAULT = -1,
MDEV_MMAPI = 0,
MDEV_OPL = 1,
MDEV_SNDSYS = 2,
MDEV_TIMIDITY = 3,
MDEV_FLUIDSYNTH = 4,
MDEV_GUS = 5,
MDEV_WILDMIDI = 6,
MDEV_ADL = 7,
MDEV_OPN = 8,

MDEV_COUNT
};

enum ESoundFontTypes
{
SF_SF2 = 1,
SF_GUS = 2,
SF_WOPL = 4,
SF_WOPN = 8
};


struct SoundStreamInfo
{
int mBufferSize; // If mBufferSize is 0, the song doesn't use streaming but plays through a different interface.
int mSampleRate;
int mNumChannels; // If mNumChannels is negative, 16 bit integer format is used instead of floating point.
};

#ifndef MAKE_ID
#ifndef __BIG_ENDIAN__
#define MAKE_ID(a,b,c,d) ((uint32_t)((a)|((b)<<8)|((c)<<16)|((d)<<24)))
Expand Down
1 change: 1 addition & 0 deletions libraries/zmusic/zmusic/musinfo.h
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <mutex>
#include "mididefs.h"
#include "zmusic/zmusic_internal.h"

// The base music class. Everything is derived from this --------------------

Expand Down
17 changes: 0 additions & 17 deletions libraries/zmusic/zmusic/zmusic.cpp
Expand Up @@ -319,23 +319,6 @@ DLL_EXPORT ZMusic_MusicStream ZMusic_OpenSongMem(const void* mem, size_t size, E
return ZMusic_OpenSongInternal(mr, device, Args);
}

struct CustomFileReader : public MusicIO::FileInterface
{
ZMusicCustomReader* cr;

CustomFileReader(ZMusicCustomReader* zr) : cr(zr) {}
virtual char* gets(char* buff, int n) { return cr->gets(cr, buff, n); }
virtual long read(void* buff, int32_t size) { return cr->read(cr, buff, size); }
virtual long seek(long offset, int whence) { return cr->seek(cr, offset, whence); }
virtual long tell() { return cr->tell(cr); }
virtual void close()
{
cr->close(cr);
delete this;
}

};

DLL_EXPORT ZMusic_MusicStream ZMusic_OpenSong(ZMusicCustomReader* reader, EMidiDevice device, const char* Args)
{
if (!reader)
Expand Down

0 comments on commit d2ca1ea

Please sign in to comment.