2019-12-08 11:28:51 +00:00
|
|
|
/*
|
|
|
|
**
|
|
|
|
** music.cpp
|
|
|
|
**
|
2020-04-12 06:07:48 +00:00
|
|
|
** music engine
|
2019-12-08 11:28:51 +00:00
|
|
|
**
|
|
|
|
** Copyright 1999-2016 Randy Heit
|
|
|
|
** Copyright 2002-2016 Christoph Oelckers
|
2019-11-10 23:23:52 +00:00
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-05-23 10:59:03 +00:00
|
|
|
#include <stdexcept>
|
2020-04-12 06:07:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "i_sound.h"
|
2019-11-11 00:01:18 +00:00
|
|
|
#include "i_music.h"
|
2019-11-10 22:58:51 +00:00
|
|
|
#include "printf.h"
|
2020-04-12 06:07:48 +00:00
|
|
|
#include "s_playlist.h"
|
|
|
|
#include "c_dispatch.h"
|
2019-11-10 22:58:51 +00:00
|
|
|
#include "filesystem.h"
|
|
|
|
#include "cmdlib.h"
|
2020-04-12 06:07:48 +00:00
|
|
|
#include "s_music.h"
|
2019-11-10 22:58:51 +00:00
|
|
|
#include "filereadermusicinterface.h"
|
2020-04-12 06:07:48 +00:00
|
|
|
#include <zmusic.h>
|
2019-11-10 22:58:51 +00:00
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
static bool MusicPaused; // whether music is paused
|
|
|
|
MusPlayingInfo mus_playing; // music currently being played
|
|
|
|
static FPlayList PlayList;
|
|
|
|
float relative_volume = 1.f;
|
|
|
|
float saved_relative_volume = 1.0f; // this could be used to implement an ACS FadeMusic function
|
2019-11-11 00:01:18 +00:00
|
|
|
MusicVolumeMap MusicVolumes;
|
2020-04-12 06:07:48 +00:00
|
|
|
MidiDeviceMap MidiDevices;
|
|
|
|
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
|
|
|
|
|
|
|
|
extern float S_GetMusicVolume (const char *music);
|
|
|
|
|
|
|
|
static void S_ActivatePlayList(bool goBack);
|
|
|
|
|
|
|
|
// PRIVATE DATA DEFINITIONS ------------------------------------------------
|
2020-01-27 19:53:41 +00:00
|
|
|
|
2019-11-28 18:35:35 +00:00
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
static FileReader DefaultOpenMusic(const char* fn)
|
2020-01-27 20:39:15 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
// This is the minimum needed to make the music system functional.
|
|
|
|
FileReader fr;
|
|
|
|
fr.OpenFile(fn);
|
|
|
|
return fr;
|
2020-01-27 20:39:15 +00:00
|
|
|
}
|
2020-04-12 06:07:48 +00:00
|
|
|
static MusicCallbacks mus_cb = { nullptr, DefaultOpenMusic };
|
2020-01-27 20:39:15 +00:00
|
|
|
|
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
// PUBLIC DATA DEFINITIONS -------------------------------------------------
|
|
|
|
|
|
|
|
// CODE --------------------------------------------------------------------
|
|
|
|
|
|
|
|
void S_SetMusicCallbacks(MusicCallbacks* cb)
|
2020-01-27 20:39:15 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
mus_cb = *cb;
|
|
|
|
if (mus_cb.OpenMusic == nullptr) mus_cb.OpenMusic = DefaultOpenMusic; // without this we are dead in the water.
|
2020-01-27 20:39:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 20:50:20 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Create a sound system stream for the currently playing song
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
static std::unique_ptr<SoundStream> musicStream;
|
|
|
|
|
|
|
|
static bool FillStream(SoundStream* stream, void* buff, int len, void* userdata)
|
|
|
|
{
|
|
|
|
bool written = ZMusic_FillStream(mus_playing.handle, buff, len);
|
|
|
|
|
|
|
|
if (!written)
|
|
|
|
{
|
|
|
|
memset((char*)buff, 0, len);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void S_CreateStream()
|
|
|
|
{
|
|
|
|
if (!mus_playing.handle) return;
|
2020-02-09 12:26:51 +00:00
|
|
|
SoundStreamInfo fmt;
|
|
|
|
ZMusic_GetStreamInfo(mus_playing.handle, &fmt);
|
|
|
|
if (fmt.mBufferSize > 0) // if buffer size is 0 the library will play the song itself (e.g. Windows system synth.)
|
2019-11-11 20:50:20 +00:00
|
|
|
{
|
|
|
|
int flags = fmt.mNumChannels < 0 ? 0 : SoundStream::Float;
|
|
|
|
if (abs(fmt.mNumChannels) < 2) flags |= SoundStream::Mono;
|
|
|
|
|
|
|
|
musicStream.reset(GSnd->CreateStream(FillStream, fmt.mBufferSize, flags, fmt.mSampleRate, nullptr));
|
|
|
|
if (musicStream) musicStream->Play(true, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void S_PauseStream(bool paused)
|
|
|
|
{
|
|
|
|
if (musicStream) musicStream->SetPaused(paused);
|
|
|
|
}
|
|
|
|
|
|
|
|
void S_StopStream()
|
|
|
|
{
|
|
|
|
if (musicStream)
|
|
|
|
{
|
|
|
|
musicStream->Stop();
|
|
|
|
musicStream.reset();
|
|
|
|
}
|
|
|
|
}
|
2019-11-10 22:58:51 +00:00
|
|
|
|
|
|
|
|
2019-11-10 23:23:52 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// starts playing this song
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2020-02-09 12:26:51 +00:00
|
|
|
static bool S_StartMusicPlaying(ZMusic_MusicStream song, bool loop, float rel_vol, int subsong)
|
2019-11-10 23:23:52 +00:00
|
|
|
{
|
|
|
|
if (rel_vol > 0.f)
|
|
|
|
{
|
|
|
|
float factor = relative_volume / saved_relative_volume;
|
|
|
|
saved_relative_volume = rel_vol;
|
|
|
|
I_SetRelativeVolume(saved_relative_volume * factor);
|
|
|
|
}
|
|
|
|
ZMusic_Stop(song);
|
2020-02-09 12:26:51 +00:00
|
|
|
if (!ZMusic_Start(song, subsong, loop))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-10 23:23:52 +00:00
|
|
|
|
|
|
|
// Notify the sound system of the changed relative volume
|
2020-04-12 06:07:48 +00:00
|
|
|
snd_musicvolume.Callback();
|
2020-02-09 12:26:51 +00:00
|
|
|
return true;
|
2019-11-10 23:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// S_PauseSound
|
|
|
|
//
|
|
|
|
// Stop music and sound effects, during game PAUSE.
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void S_PauseMusic ()
|
|
|
|
{
|
|
|
|
if (mus_playing.handle && !MusicPaused)
|
|
|
|
{
|
|
|
|
ZMusic_Pause(mus_playing.handle);
|
|
|
|
S_PauseStream(true);
|
|
|
|
MusicPaused = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// S_ResumeSound
|
|
|
|
//
|
|
|
|
// Resume music and sound effects, after game PAUSE.
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void S_ResumeMusic ()
|
|
|
|
{
|
|
|
|
if (mus_playing.handle && MusicPaused)
|
|
|
|
{
|
|
|
|
ZMusic_Resume(mus_playing.handle);
|
|
|
|
S_PauseStream(false);
|
|
|
|
MusicPaused = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// S_UpdateSound
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void S_UpdateMusic ()
|
|
|
|
{
|
|
|
|
if (mus_playing.handle != nullptr)
|
|
|
|
{
|
|
|
|
ZMusic_Update(mus_playing.handle);
|
|
|
|
|
|
|
|
// [RH] Update music and/or playlist. IsPlaying() must be called
|
|
|
|
// to attempt to reconnect to broken net streams and to advance the
|
|
|
|
// playlist when the current song finishes.
|
|
|
|
if (!ZMusic_IsPlaying(mus_playing.handle))
|
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
if (PlayList.GetNumSongs())
|
|
|
|
{
|
|
|
|
PlayList.Advance();
|
|
|
|
S_ActivatePlayList(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-11 00:01:18 +00:00
|
|
|
S_StopMusic(true);
|
2019-11-10 23:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 06:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Resets the music player if music playback was paused.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void S_ResetMusic ()
|
|
|
|
{
|
|
|
|
// stop the old music if it has been paused.
|
|
|
|
// This ensures that the new music is started from the beginning
|
|
|
|
// if it's the same as the last one and it has been paused.
|
|
|
|
if (MusicPaused) S_StopMusic(true);
|
|
|
|
|
|
|
|
// start new music for the level
|
|
|
|
MusicPaused = false;
|
2019-11-10 23:23:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
|
2019-11-10 23:23:52 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2020-04-12 06:07:48 +00:00
|
|
|
// S_ActivatePlayList
|
2019-11-10 23:23:52 +00:00
|
|
|
//
|
2020-04-12 06:07:48 +00:00
|
|
|
// Plays the next song in the playlist. If no songs in the playlist can be
|
|
|
|
// played, then it is deleted.
|
2019-11-10 23:23:52 +00:00
|
|
|
//==========================================================================
|
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
void S_ActivatePlayList (bool goBack)
|
2019-11-10 23:23:52 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
int startpos, pos;
|
2019-11-10 23:23:52 +00:00
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
startpos = pos = PlayList.GetPosition ();
|
|
|
|
S_StopMusic (true);
|
|
|
|
while (!S_ChangeMusic (PlayList.GetSong (pos), 0, false, true))
|
2019-11-10 23:23:52 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
pos = goBack ? PlayList.Backup () : PlayList.Advance ();
|
|
|
|
if (pos == startpos)
|
2019-11-10 23:23:52 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
PlayList.Clear();
|
|
|
|
Printf ("Cannot play anything in the playlist.\n");
|
|
|
|
return;
|
|
|
|
}
|
2019-11-10 23:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// S_StartMusic
|
|
|
|
//
|
|
|
|
// Starts some music with the given name.
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool S_StartMusic (const char *m_id)
|
|
|
|
{
|
|
|
|
return S_ChangeMusic (m_id, 0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// S_ChangeMusic
|
|
|
|
//
|
2020-04-12 06:07:48 +00:00
|
|
|
// initiates playback of a song
|
2019-11-10 23:23:52 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-11-10 22:58:51 +00:00
|
|
|
bool S_ChangeMusic(const char* musicname, int order, bool looping, bool force)
|
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
if (nomusic) return false; // skip the entire procedure if music is globally disabled.
|
|
|
|
|
|
|
|
if (!force && PlayList.GetNumSongs())
|
|
|
|
{ // Don't change if a playlist is active
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Do game specific lookup.
|
|
|
|
FString musicname_;
|
|
|
|
if (mus_cb.LookupFileName)
|
|
|
|
{
|
|
|
|
musicname_ = mus_cb.LookupFileName(musicname, order);
|
|
|
|
musicname = musicname_.GetChars();
|
|
|
|
}
|
|
|
|
|
2019-11-10 22:58:51 +00:00
|
|
|
if (musicname == nullptr || musicname[0] == 0)
|
|
|
|
{
|
|
|
|
// Don't choke if the map doesn't have a song attached
|
2019-11-10 23:23:52 +00:00
|
|
|
S_StopMusic (true);
|
2019-11-10 22:58:51 +00:00
|
|
|
mus_playing.name = "";
|
|
|
|
mus_playing.LastSong = "";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mus_playing.name.IsEmpty() &&
|
|
|
|
mus_playing.handle != nullptr &&
|
2020-04-12 06:07:48 +00:00
|
|
|
stricmp(mus_playing.name, musicname) == 0 &&
|
2020-02-09 12:26:51 +00:00
|
|
|
ZMusic_IsLooping(mus_playing.handle) == zmusic_bool(looping))
|
2019-11-10 22:58:51 +00:00
|
|
|
{
|
|
|
|
if (order != mus_playing.baseorder)
|
|
|
|
{
|
|
|
|
if (ZMusic_SetSubsong(mus_playing.handle, order))
|
|
|
|
{
|
|
|
|
mus_playing.baseorder = order;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!ZMusic_IsPlaying(mus_playing.handle))
|
|
|
|
{
|
2020-02-22 15:12:40 +00:00
|
|
|
if (!ZMusic_Start(mus_playing.handle, order, looping))
|
2019-11-10 22:58:51 +00:00
|
|
|
{
|
2020-02-09 12:26:51 +00:00
|
|
|
Printf("Unable to start %s: %s\n", mus_playing.name.GetChars(), ZMusic_GetLastError());
|
2019-11-10 22:58:51 +00:00
|
|
|
}
|
2020-02-09 12:26:51 +00:00
|
|
|
S_CreateStream();
|
2019-11-10 22:58:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int lumpnum = -1;
|
2020-02-09 12:26:51 +00:00
|
|
|
int length = 0;
|
|
|
|
ZMusic_MusicStream handle = nullptr;
|
2020-04-12 06:07:48 +00:00
|
|
|
MidiDeviceSetting* devp = MidiDevices.CheckKey(musicname);
|
2019-11-10 22:58:51 +00:00
|
|
|
|
|
|
|
// Strip off any leading file:// component.
|
|
|
|
if (strncmp(musicname, "file://", 7) == 0)
|
|
|
|
{
|
|
|
|
musicname += 7;
|
|
|
|
}
|
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
// opening the music must be done by the game because it's different depending on the game's file system use.
|
|
|
|
FileReader reader = mus_cb.OpenMusic(musicname);
|
2020-01-27 20:39:15 +00:00
|
|
|
if (!reader.isOpen()) return false;
|
2019-11-10 22:58:51 +00:00
|
|
|
|
|
|
|
// shutdown old music
|
2020-04-12 06:07:48 +00:00
|
|
|
S_StopMusic(true);
|
2019-11-10 22:58:51 +00:00
|
|
|
|
2019-11-28 02:18:58 +00:00
|
|
|
// Just record it if volume is 0 or music was disabled
|
2020-04-12 06:07:48 +00:00
|
|
|
if (snd_musicvolume <= 0 || !mus_enabled)
|
2019-11-10 22:58:51 +00:00
|
|
|
{
|
|
|
|
mus_playing.loop = looping;
|
|
|
|
mus_playing.name = musicname;
|
|
|
|
mus_playing.baseorder = order;
|
|
|
|
mus_playing.LastSong = musicname;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load & register it
|
|
|
|
if (handle != nullptr)
|
|
|
|
{
|
|
|
|
mus_playing.handle = handle;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-09 12:26:51 +00:00
|
|
|
auto mreader = GetMusicReader(reader); // this passes the file reader to the newly created wrapper.
|
2020-04-12 06:07:48 +00:00
|
|
|
mus_playing.handle = ZMusic_OpenSong(mreader, devp ? (EMidiDevice)devp->device : MDEV_DEFAULT, devp ? devp->args.GetChars() : "");
|
2020-02-09 12:26:51 +00:00
|
|
|
if (mus_playing.handle == nullptr)
|
2019-11-10 22:58:51 +00:00
|
|
|
{
|
2020-02-09 12:26:51 +00:00
|
|
|
Printf("Unable to load %s: %s\n", mus_playing.name.GetChars(), ZMusic_GetLastError());
|
2019-11-10 22:58:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mus_playing.loop = looping;
|
|
|
|
mus_playing.name = musicname;
|
|
|
|
mus_playing.baseorder = 0;
|
|
|
|
mus_playing.LastSong = "";
|
|
|
|
|
|
|
|
if (mus_playing.handle != 0)
|
|
|
|
{ // play it
|
2020-02-09 12:26:51 +00:00
|
|
|
auto volp = MusicVolumes.CheckKey(musicname);
|
|
|
|
float vol = volp ? *volp : 1.f;
|
|
|
|
if (!S_StartMusicPlaying(mus_playing.handle, looping, vol, order))
|
2019-11-10 22:58:51 +00:00
|
|
|
{
|
2020-02-09 12:26:51 +00:00
|
|
|
Printf("Unable to start %s: %s\n", mus_playing.name.GetChars(), ZMusic_GetLastError());
|
|
|
|
return false;
|
2019-11-10 22:58:51 +00:00
|
|
|
}
|
2020-02-09 12:26:51 +00:00
|
|
|
S_CreateStream();
|
|
|
|
mus_playing.baseorder = order;
|
2019-11-10 22:58:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-10 23:23:52 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// S_RestartMusic
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void S_RestartMusic ()
|
2019-11-10 22:58:51 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
if (snd_musicvolume <= 0) return;
|
|
|
|
if (!mus_playing.LastSong.IsEmpty() && mus_enabled)
|
2019-11-10 23:23:52 +00:00
|
|
|
{
|
|
|
|
FString song = mus_playing.LastSong;
|
|
|
|
mus_playing.LastSong = "";
|
|
|
|
S_ChangeMusic (song, mus_playing.baseorder, mus_playing.loop, true);
|
|
|
|
}
|
2020-02-25 20:15:25 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
S_StopMusic(true);
|
|
|
|
}
|
2019-11-10 23:23:52 +00:00
|
|
|
}
|
2019-11-10 22:58:51 +00:00
|
|
|
|
2019-11-10 23:23:52 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// S_MIDIDeviceChanged
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
void S_MIDIDeviceChanged(int newdev)
|
|
|
|
{
|
2020-02-09 12:26:51 +00:00
|
|
|
auto song = mus_playing.handle;
|
2019-11-10 23:23:52 +00:00
|
|
|
if (song != nullptr && ZMusic_IsMIDI(song) && ZMusic_IsPlaying(song))
|
|
|
|
{
|
|
|
|
// Reload the song to change the device
|
|
|
|
auto mi = mus_playing;
|
|
|
|
S_StopMusic(true);
|
|
|
|
S_ChangeMusic(mi.name, mi.baseorder, mi.loop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// S_GetMusic
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int S_GetMusic (const char **name)
|
|
|
|
{
|
|
|
|
int order;
|
|
|
|
|
|
|
|
if (mus_playing.name.IsNotEmpty())
|
|
|
|
{
|
|
|
|
*name = mus_playing.name;
|
|
|
|
order = mus_playing.baseorder;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*name = nullptr;
|
|
|
|
order = 0;
|
|
|
|
}
|
|
|
|
return order;
|
2019-11-10 22:58:51 +00:00
|
|
|
}
|
2019-11-10 23:23:52 +00:00
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// S_StopMusic
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void S_StopMusic (bool force)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// [RH] Don't stop if a playlist is active.
|
2020-04-12 06:07:48 +00:00
|
|
|
if ((force || PlayList.GetNumSongs() == 0) && !mus_playing.name.IsEmpty())
|
2019-11-10 23:23:52 +00:00
|
|
|
{
|
|
|
|
if (mus_playing.handle != nullptr)
|
|
|
|
{
|
|
|
|
S_ResumeMusic();
|
|
|
|
S_StopStream();
|
|
|
|
ZMusic_Stop(mus_playing.handle);
|
|
|
|
auto h = mus_playing.handle;
|
|
|
|
mus_playing.handle = nullptr;
|
|
|
|
ZMusic_Close(h);
|
|
|
|
}
|
|
|
|
mus_playing.LastSong = std::move(mus_playing.name);
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 06:07:48 +00:00
|
|
|
catch (const std::runtime_error& )
|
2019-11-10 23:23:52 +00:00
|
|
|
{
|
|
|
|
//Printf("Unable to stop %s: %s\n", mus_playing.name.GetChars(), err.what());
|
|
|
|
if (mus_playing.handle != nullptr)
|
|
|
|
{
|
|
|
|
auto h = mus_playing.handle;
|
|
|
|
mus_playing.handle = nullptr;
|
|
|
|
ZMusic_Close(h);
|
|
|
|
}
|
|
|
|
mus_playing.name = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// CCMD changemus
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
CCMD (changemus)
|
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
if (!nomusic)
|
2019-11-10 23:23:52 +00:00
|
|
|
{
|
|
|
|
if (argv.argc() > 1)
|
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
PlayList.Clear();
|
2019-11-10 23:23:52 +00:00
|
|
|
S_ChangeMusic (argv[1], argv.argc() > 2 ? atoi (argv[2]) : 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *currentmus = mus_playing.name.GetChars();
|
|
|
|
if(currentmus != nullptr && *currentmus != 0)
|
|
|
|
{
|
|
|
|
Printf ("currently playing %s\n", currentmus);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Printf ("no music playing\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Printf("Music is disabled\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// CCMD stopmus
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
CCMD (stopmus)
|
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
PlayList.Clear();
|
2019-11-10 23:23:52 +00:00
|
|
|
S_StopMusic (false);
|
|
|
|
mus_playing.LastSong = ""; // forget the last played song so that it won't get restarted if some volume changes occur
|
|
|
|
}
|
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// CCMD playlist
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
UNSAFE_CCMD (playlist)
|
2019-11-10 22:58:51 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
int argc = argv.argc();
|
|
|
|
|
|
|
|
if (argc < 2 || argc > 3)
|
2019-11-11 23:43:07 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
Printf ("playlist <playlist.m3u> [<position>|shuffle]\n");
|
2019-11-11 23:43:07 +00:00
|
|
|
}
|
2020-04-12 06:07:48 +00:00
|
|
|
else
|
2019-11-11 23:43:07 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
if (!PlayList.ChangeList(argv[1]))
|
|
|
|
{
|
|
|
|
Printf("Could not open " TEXTCOLOR_BOLD "%s" TEXTCOLOR_NORMAL ": %s\n", argv[1], strerror(errno));
|
|
|
|
return;
|
2019-11-11 23:43:07 +00:00
|
|
|
}
|
2020-04-12 06:07:48 +00:00
|
|
|
if (PlayList.GetNumSongs () > 0)
|
2019-11-28 18:35:35 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
if (argc == 3)
|
2019-11-28 18:35:35 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
if (stricmp (argv[2], "shuffle") == 0)
|
|
|
|
{
|
|
|
|
PlayList.Shuffle ();
|
2019-11-28 18:35:35 +00:00
|
|
|
}
|
2020-04-12 06:07:48 +00:00
|
|
|
else
|
2019-11-28 18:35:35 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
PlayList.SetPosition (atoi (argv[2]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
S_ActivatePlayList (false);
|
|
|
|
}
|
2019-11-28 18:35:35 +00:00
|
|
|
}
|
2019-11-10 22:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// CCMD playlistpos
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2019-12-26 12:04:29 +00:00
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
static bool CheckForPlaylist ()
|
2019-11-10 22:58:51 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
if (PlayList.GetNumSongs() == 0)
|
|
|
|
{
|
|
|
|
Printf ("No playlist is playing.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2019-11-10 22:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
CCMD (playlistpos)
|
2019-12-07 17:28:30 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
if (CheckForPlaylist() && argv.argc() > 1)
|
|
|
|
{
|
|
|
|
PlayList.SetPosition (atoi (argv[1]) - 1);
|
|
|
|
S_ActivatePlayList (false);
|
|
|
|
}
|
2019-12-07 17:28:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// CCMD playlistnext
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
CCMD (playlistnext)
|
2019-11-10 22:58:51 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
if (CheckForPlaylist())
|
|
|
|
{
|
|
|
|
PlayList.Advance ();
|
|
|
|
S_ActivatePlayList (false);
|
|
|
|
}
|
2019-11-10 22:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// CCMD playlistprev
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
CCMD (playlistprev)
|
2019-11-28 02:18:58 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
if (CheckForPlaylist())
|
2019-11-28 02:18:58 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
PlayList.Backup ();
|
|
|
|
S_ActivatePlayList (true);
|
2020-02-23 13:03:03 +00:00
|
|
|
}
|
2020-04-12 06:07:48 +00:00
|
|
|
}
|
2019-11-28 02:18:58 +00:00
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// CCMD playliststatus
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2019-11-28 02:18:58 +00:00
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
CCMD (playliststatus)
|
|
|
|
{
|
|
|
|
if (CheckForPlaylist ())
|
|
|
|
{
|
|
|
|
Printf ("Song %d of %d:\n%s\n",
|
|
|
|
PlayList.GetPosition () + 1,
|
|
|
|
PlayList.GetNumSongs (),
|
|
|
|
PlayList.GetSong (PlayList.GetPosition ()));
|
2019-11-28 02:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 06:07:48 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
CCMD(currentmusic)
|
2019-11-28 02:18:58 +00:00
|
|
|
{
|
2020-04-12 06:07:48 +00:00
|
|
|
if (mus_playing.name.IsNotEmpty())
|
|
|
|
{
|
|
|
|
Printf("Currently playing music '%s'\n", mus_playing.name.GetChars());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Printf("Currently no music playing\n");
|
|
|
|
}
|
2019-11-28 02:18:58 +00:00
|
|
|
}
|