From d06ec56c2e18a5ad101e1a44db4862f8a732287f Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <coelckers@users.noreply.github.com>
Date: Sun, 29 Sep 2019 22:32:42 +0200
Subject: [PATCH] - a bit of cleanup - moving internal class declarations into
 the sources.

Now i_musicinterns.h doesn't bleed the entire implementation everywhere anymore.
---
 src/sound/music/i_music.cpp                 |  34 +-----
 src/sound/music/i_music.h                   |   1 -
 src/sound/music/i_musicinterns.h            | 122 +-------------------
 src/sound/musicformats/music_cd.cpp         |  42 +++++++
 src/sound/musicformats/music_midistream.cpp |  98 ++++++++++++++++
 5 files changed, 148 insertions(+), 149 deletions(-)

diff --git a/src/sound/music/i_music.cpp b/src/sound/music/i_music.cpp
index 23bf4bbed..f786642ac 100644
--- a/src/sound/music/i_music.cpp
+++ b/src/sound/music/i_music.cpp
@@ -359,10 +359,6 @@ void MusInfo::MusicVolumeChanged()
 {
 }
 
-void MusInfo::GMEDepthChanged(float val)
-{
-}
-
 void MusInfo::ChangeSettingInt(const char *, int)
 {
 }
@@ -386,18 +382,6 @@ MusInfo *MusInfo::GetWaveDumper(const char *filename, int rate)
 }
 
 
-//==========================================================================
-//
-// create a streamer
-//
-//==========================================================================
-
-static MIDIStreamer *CreateMIDIStreamer(EMidiDevice devtype, const char *args)
-{
-	auto me = new MIDIStreamer(devtype, args);
-	return me;
-}
-
 //==========================================================================
 //
 // identify a music lump's type and set up a player for it
@@ -474,15 +458,7 @@ MusInfo *I_RegisterSong (MusicIO::FileInterface *reader, MidiDeviceSetting *devi
 				devtype = MDEV_SNDSYS;
 #endif
 
-			MIDIStreamer* streamer = CreateMIDIStreamer(devtype, device != nullptr ? device->args.GetChars() : "");
-			if (streamer == nullptr)
-			{
-				delete source;
-				reader->close();
-				return nullptr;
-			}
-			streamer->SetMIDISource(source);
-			info = streamer;
+			info = CreateMIDIStreamer(source, devtype, device != nullptr ? device->args.GetChars() : "");
 		}
 
 		// Check for CDDA "format"
@@ -498,7 +474,7 @@ MusInfo *I_RegisterSong (MusicIO::FileInterface *reader, MidiDeviceSetting *devi
 				if (subid == (('C') | (('D') << 8) | (('D') << 16) | (('A') << 24)))
 				{
 					// This is a CDDA file
-					info = new CDDAFile(reader);
+					info = CDDA_OpenSong(reader);
 				}
 			}
 		}
@@ -571,7 +547,7 @@ MusInfo *I_RegisterSong (MusicIO::FileInterface *reader, MidiDeviceSetting *devi
 
 MusInfo *I_RegisterCDSong (int track, int id)
 {
-	MusInfo *info = new CDSong (track, id);
+	MusInfo *info = CD_OpenSong (track, id);
 
 	if (info && !info->IsValid ())
 	{
@@ -804,9 +780,7 @@ UNSAFE_CCMD (writewave)
 		if (dev == MDEV_DEFAULT && snd_mididevice >= 0) dev = MDEV_FLUIDSYNTH;	// The Windows system synth cannot dump a wave.
 		try
 		{
-			MIDIStreamer streamer(dev, argv.argc() < 6 ? nullptr : argv[6]);
-			streamer.SetMIDISource(source);
-			streamer.DumpWave(argv[2], argv.argc() < 4 ? 0 : (int)strtol(argv[3], nullptr, 10), argv.argc() < 5 ? 0 : (int)strtol(argv[4], nullptr, 10));
+			MIDIDumpWave(source, dev, argv.argc() < 6 ? nullptr : argv[6], argv[2], argv.argc() < 4 ? 0 : (int)strtol(argv[3], nullptr, 10), argv.argc() < 5 ? 0 : (int)strtol(argv[4], nullptr, 10));
 		}
 		catch (const std::runtime_error& err)
 		{
diff --git a/src/sound/music/i_music.h b/src/sound/music/i_music.h
index 4258031d9..31cf33093 100644
--- a/src/sound/music/i_music.h
+++ b/src/sound/music/i_music.h
@@ -82,7 +82,6 @@ public:
 	virtual void ChangeSettingInt(const char *setting, int value);			// FluidSynth settings
 	virtual void ChangeSettingNum(const char *setting, double value);		// "
 	virtual void ChangeSettingString(const char *setting, const char *value);	// "
-	virtual void GMEDepthChanged(float val);
 
 	void Start(bool loop, float rel_vol = -1.f, int subsong = 0);
 
diff --git a/src/sound/music/i_musicinterns.h b/src/sound/music/i_musicinterns.h
index a07e43509..709e8e4c5 100644
--- a/src/sound/music/i_musicinterns.h
+++ b/src/sound/music/i_musicinterns.h
@@ -19,123 +19,6 @@ class MIDIDevice;
 class OPLmusicFile;
 
 
-extern FluidConfig fluidConfig;
-extern OPLConfig oplConfig;
-extern OpnConfig opnConfig;
-extern GUSConfig gusConfig;
-extern TimidityConfig timidityConfig;
-extern WildMidiConfig wildMidiConfig;
-extern DumbConfig dumbConfig;
-
-
-class MIDIStreamer;
-
-// Base class for streaming MUS and MIDI files ------------------------------
-
-class MIDIStreamer : public MusInfo
-{
-public:
-	MIDIStreamer(EMidiDevice type, const char *args);
-	~MIDIStreamer();
-
-	void MusicVolumeChanged() override;
-	void Play(bool looping, int subsong) override;
-	void Pause() override;
-	void Resume() override;
-	void Stop() override;
-	bool IsPlaying() override;
-	bool IsMIDI() const override;
-	bool IsValid() const override;
-	bool SetSubsong(int subsong) override;
-	void Update() override;
-	FString GetStats() override;
-	void ChangeSettingInt(const char *setting, int value) override;
-	void ChangeSettingNum(const char *setting, double value) override;
-	void ChangeSettingString(const char *setting, const char *value) override;
-	int ServiceEvent();
-	void SetMIDISource(MIDISource *_source);
-
-	int GetDeviceType() const override;
-
-	bool DumpWave(const char *filename, int subsong, int samplerate);
-	static bool FillStream(SoundStream* stream, void* buff, int len, void* userdata);
-
-
-protected:
-	MIDIStreamer(const char *dumpname, EMidiDevice type);
-
-	void OutputVolume (uint32_t volume);
-	int FillBuffer(int buffer_num, int max_events, uint32_t max_time);
-	int FillStopBuffer(int buffer_num);
-	uint32_t *WriteStopNotes(uint32_t *events);
-	int VolumeControllerChange(int channel, int volume);
-	void SetTempo(int new_tempo);
-	void Precache();
-	void StartPlayback();
-	bool InitPlayback();
-
-	//void SetMidiSynth(MIDIDevice *synth);
-	
-	
-	static EMidiDevice SelectMIDIDevice(EMidiDevice devtype);
-	MIDIDevice *CreateMIDIDevice(EMidiDevice devtype, int samplerate);
-
-	static void Callback(void *userdata);
-
-	enum
-	{
-		SONG_MORE,
-		SONG_DONE,
-		SONG_ERROR
-	};
-
-	std::unique_ptr<MIDIDevice> MIDI;
-	uint32_t Events[2][MAX_MIDI_EVENTS*3];
-	MidiHeader Buffer[2];
-	int BufferNum;
-	int EndQueued;
-	bool VolumeChanged;
-	bool Restarting;
-	bool InitialPlayback;
-	uint32_t NewVolume;
-	uint32_t Volume;
-	EMidiDevice DeviceType;
-	bool CallbackIsThreaded;
-	int LoopLimit;
-	FString Args;
-	std::unique_ptr<MIDISource> source;
-	std::unique_ptr<SoundStream> Stream;
-};
-
-// CD track/disk played through the multimedia system -----------------------
-
-class CDSong : public MusInfo
-{
-public:
-	CDSong (int track, int id);
-	~CDSong ();
-	void Play (bool looping, int subsong);
-	void Pause ();
-	void Resume ();
-	void Stop ();
-	bool IsPlaying ();
-	bool IsValid () const { return m_Inited; }
-
-protected:
-	CDSong () : m_Inited(false) {}
-
-	int m_Track;
-	bool m_Inited;
-};
-
-// CD track on a specific disk played through the multimedia system ---------
-
-class CDDAFile : public CDSong
-{
-public:
-	CDDAFile (MusicIO::FileInterface *reader);
-};
-
 // Data interface
 
 // Module played via foo_dumb -----------------------------------------------
@@ -145,8 +28,11 @@ class StreamSource;
 // stream song ------------------------------------------
 
 MusInfo *OpenStreamSong(StreamSource *source);
-
 const char *GME_CheckFormat(uint32_t header);
+MusInfo* CDDA_OpenSong(MusicIO::FileInterface* reader);
+MusInfo* CD_OpenSong(int track, int id);
+MusInfo* CreateMIDIStreamer(MIDISource *source, EMidiDevice devtype, const char* args);
+void MIDIDumpWave(MIDISource* source, EMidiDevice devtype, const char* devarg, const char* outname, int subsong, int samplerate);
 
 // --------------------------------------------------------------------------
 
diff --git a/src/sound/musicformats/music_cd.cpp b/src/sound/musicformats/music_cd.cpp
index 734549965..1ec8e82b4 100644
--- a/src/sound/musicformats/music_cd.cpp
+++ b/src/sound/musicformats/music_cd.cpp
@@ -34,6 +34,38 @@
 #include "i_musicinterns.h"
 #include "i_cd.h"
 
+
+// CD track/disk played through the multimedia system -----------------------
+
+class CDSong : public MusInfo
+{
+public:
+	CDSong(int track, int id);
+	~CDSong();
+	void Play(bool looping, int subsong);
+	void Pause();
+	void Resume();
+	void Stop();
+	bool IsPlaying();
+	bool IsValid() const { return m_Inited; }
+
+protected:
+	CDSong() : m_Inited(false) {}
+
+	int m_Track;
+	bool m_Inited;
+};
+
+// CD track on a specific disk played through the multimedia system ---------
+
+class CDDAFile : public CDSong
+{
+public:
+	CDDAFile(MusicIO::FileInterface* reader);
+};
+
+
+
 void CDSong::Play (bool looping, int subsong)
 {
 	m_Status = STATE_Stopped;
@@ -146,3 +178,13 @@ CDDAFile::CDDAFile (MusicIO::FileInterface* reader)
 		}
 	}
 }
+
+MusInfo* CD_OpenSong(int track, int id)
+{
+	return new CDSong(track, id);
+}
+
+MusInfo* CDDA_OpenSong(MusicIO::FileInterface* reader)
+{
+	return new CDDAFile(reader);
+}
\ No newline at end of file
diff --git a/src/sound/musicformats/music_midistream.cpp b/src/sound/musicformats/music_midistream.cpp
index adedd44fc..eea775621 100644
--- a/src/sound/musicformats/music_midistream.cpp
+++ b/src/sound/musicformats/music_midistream.cpp
@@ -68,6 +68,84 @@ extern unsigned mididevice;
 
 // PRIVATE DATA DEFINITIONS ------------------------------------------------
 
+// Base class for streaming MUS and MIDI files ------------------------------
+
+class MIDIStreamer : public MusInfo
+{
+public:
+	MIDIStreamer(EMidiDevice type, const char* args);
+	~MIDIStreamer();
+
+	void MusicVolumeChanged() override;
+	void Play(bool looping, int subsong) override;
+	void Pause() override;
+	void Resume() override;
+	void Stop() override;
+	bool IsPlaying() override;
+	bool IsMIDI() const override;
+	bool IsValid() const override;
+	bool SetSubsong(int subsong) override;
+	void Update() override;
+	FString GetStats() override;
+	void ChangeSettingInt(const char* setting, int value) override;
+	void ChangeSettingNum(const char* setting, double value) override;
+	void ChangeSettingString(const char* setting, const char* value) override;
+	int ServiceEvent();
+	void SetMIDISource(MIDISource* _source);
+
+	int GetDeviceType() const override;
+
+	bool DumpWave(const char* filename, int subsong, int samplerate);
+	static bool FillStream(SoundStream* stream, void* buff, int len, void* userdata);
+
+
+protected:
+	MIDIStreamer(const char* dumpname, EMidiDevice type);
+
+	void OutputVolume(uint32_t volume);
+	int FillBuffer(int buffer_num, int max_events, uint32_t max_time);
+	int FillStopBuffer(int buffer_num);
+	uint32_t* WriteStopNotes(uint32_t* events);
+	int VolumeControllerChange(int channel, int volume);
+	void SetTempo(int new_tempo);
+	void Precache();
+	void StartPlayback();
+	bool InitPlayback();
+
+	//void SetMidiSynth(MIDIDevice *synth);
+
+
+	static EMidiDevice SelectMIDIDevice(EMidiDevice devtype);
+	MIDIDevice* CreateMIDIDevice(EMidiDevice devtype, int samplerate);
+
+	static void Callback(void* userdata);
+
+	enum
+	{
+		SONG_MORE,
+		SONG_DONE,
+		SONG_ERROR
+	};
+
+	std::unique_ptr<MIDIDevice> MIDI;
+	uint32_t Events[2][MAX_MIDI_EVENTS * 3];
+	MidiHeader Buffer[2];
+	int BufferNum;
+	int EndQueued;
+	bool VolumeChanged;
+	bool Restarting;
+	bool InitialPlayback;
+	uint32_t NewVolume;
+	uint32_t Volume;
+	EMidiDevice DeviceType;
+	bool CallbackIsThreaded;
+	int LoopLimit;
+	FString Args;
+	std::unique_ptr<MIDISource> source;
+	std::unique_ptr<SoundStream> Stream;
+};
+
+
 // PUBLIC DATA DEFINITIONS -------------------------------------------------
 
 // CODE --------------------------------------------------------------------
@@ -934,3 +1012,23 @@ bool MIDIStreamer::FillStream(SoundStream* stream, void* buff, int len, void* us
 	SoftSynthMIDIDevice* device = (SoftSynthMIDIDevice*)userdata;
 	return device->ServiceStream(buff, len);
 }
+
+//==========================================================================
+//
+// create a streamer
+//
+//==========================================================================
+
+MusInfo* CreateMIDIStreamer(MIDISource *source, EMidiDevice devtype, const char* args)
+{
+	auto me = new MIDIStreamer(devtype, args);
+	me->SetMIDISource(source);
+	return me;
+}
+
+void MIDIDumpWave(MIDISource* source, EMidiDevice devtype, const char *devarg, const char *outname, int subsong, int samplerate)
+{
+	MIDIStreamer me(devtype, devarg);
+	me.SetMIDISource(source);
+	me.DumpWave(outname, subsong, samplerate);
+}
\ No newline at end of file