diff --git a/src/sound/i_music.cpp b/src/sound/i_music.cpp index 0eedfbdc8..93802c8e8 100644 --- a/src/sound/i_music.cpp +++ b/src/sound/i_music.cpp @@ -830,3 +830,51 @@ CCMD (writewave) Printf ("Usage: writewave [sample rate]"); } } + +//========================================================================== +// +// CCMD writemidi +// +// If the currently playing song is a MIDI variant, write it to disk. +// If successful, the current song will restart, since MIDI file generation +// involves a simulated playthrough of the song. +// +//========================================================================== + +CCMD (writemidi) +{ + if (argv.argc() != 2) + { + Printf("Usage: writemidi "); + return; + } + if (currSong == NULL) + { + Printf("No song is currently playing.\n"); + return; + } + if (!currSong->IsMIDI()) + { + Printf("Current song is not MIDI-based.\n"); + return; + } + + TArray midi; + FILE *f; + bool success; + + static_cast(currSong)->CreateSMF(midi, 1); + f = fopen(argv[1], "wb"); + if (f == NULL) + { + Printf("Could not open %s.\n", argv[1]); + return; + } + success = (fwrite(&midi[0], 1, midi.Size(), f) == (size_t)midi.Size()); + fclose (f); + + if (!success) + { + Printf("Could not write to music file.\n"); + } +} diff --git a/src/sound/i_musicinterns.h b/src/sound/i_musicinterns.h index 8aeaca028..37cbfc6b6 100644 --- a/src/sound/i_musicinterns.h +++ b/src/sound/i_musicinterns.h @@ -425,7 +425,7 @@ public: void FluidSettingInt(const char *setting, int value); void FluidSettingNum(const char *setting, double value); void FluidSettingStr(const char *setting, const char *value); - void CreateSMF(TArray &file); + void CreateSMF(TArray &file, int looplimit=0); protected: MIDIStreamer(const char *dumpname, EMidiDevice type); diff --git a/src/sound/music_midistream.cpp b/src/sound/music_midistream.cpp index c8c8ab08e..d760c72b8 100644 --- a/src/sound/music_midistream.cpp +++ b/src/sound/music_midistream.cpp @@ -1062,14 +1062,14 @@ void MIDIStreamer::Precache() // //========================================================================== -void MIDIStreamer::CreateSMF(TArray &file) +void MIDIStreamer::CreateSMF(TArray &file, int looplimit) { DWORD delay = 0; BYTE running_status = 0; // Always create songs aimed at GM devices. CheckCaps(MOD_MIDIPORT); - LoopLimit = EXPORT_LOOP_LIMIT; + LoopLimit = looplimit <= 0 ? EXPORT_LOOP_LIMIT : looplimit; DoRestart(); Tempo = InitialTempo;