/ source / audiolib / src / music.cpp
music.cpp
  1  //-------------------------------------------------------------------------
  2  /*
  3  Copyright (C) 2010-2019 EDuke32 developers and contributors
  4  Copyright (C) 2019 Nuke.YKT
  5  
  6  This file is part of NBlood.
  7  
  8  NBlood is free software; you can redistribute it and/or
  9  modify it under the terms of the GNU General Public License version 2
 10  as published by the Free Software Foundation.
 11  
 12  This program is distributed in the hope that it will be useful,
 13  but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 15  
 16  See the GNU General Public License for more details.
 17  
 18  You should have received a copy of the GNU General Public License
 19  along with this program; if not, write to the Free Software
 20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 21  */
 22  //-------------------------------------------------------------------------
 23  
 24  #include "music.h"
 25  
 26  #include "compat.h"
 27  #include "drivers.h"
 28  #include "midi.h"
 29  #include "multivoc.h"
 30  #include "sndcards.h"
 31  
 32  int MUSIC_ErrorCode = MUSIC_Ok;
 33  
 34  static midifuncs MUSIC_MidiFunctions;
 35  
 36  #define MUSIC_SetErrorCode(status) MUSIC_ErrorCode = (status);
 37  
 38  const char *MUSIC_ErrorString(int const ErrorNumber)
 39  {
 40      switch (ErrorNumber)
 41      {
 42          case MUSIC_Warning:
 43          case MUSIC_Error:       return MUSIC_ErrorString(MUSIC_ErrorCode);
 44          case MUSIC_Ok:          return "Music ok.";
 45          case MUSIC_MidiError:   return "Error playing MIDI file.";
 46          default:                return "Unknown Music error code.";
 47      }
 48  }
 49  
 50  
 51  int MUSIC_Init(int SoundCard)
 52  {
 53      int detected = 0;
 54  
 55      if (SoundCard == ASS_AutoDetect)
 56      {
 57  redetect:
 58          detected++;
 59          SoundCard = ASS_OPL3;
 60      }
 61  
 62  
 63      if (SoundCard < 0 || SoundCard >= ASS_NumSoundCards)
 64      {
 65  failed:
 66          LOG_F(ERROR, "MIDI initialization failed!");
 67          MUSIC_ErrorCode = MUSIC_MidiError;
 68          return MUSIC_Error;
 69      }
 70  
 71      if (!SoundDriver_IsMIDISupported(SoundCard))
 72      {
 73          LOG_F(ERROR, "%s MIDI output not supported!", SoundDriver_GetName(SoundCard));
 74  
 75          if (detected < 2)
 76              goto redetect;
 77  
 78          goto failed;
 79      }
 80  
 81      ASS_MIDISoundDriver = SoundCard;
 82  
 83      VLOG_F(LOG_ASS, "Initializing MIDI driver: %s", SoundDriver_GetName(SoundCard));
 84  
 85      // SoundDriver_MIDI_Init() prints to the same line in the log for the MME driver
 86      int status = SoundDriver_MIDI_Init(&MUSIC_MidiFunctions);
 87  
 88      if (status != MUSIC_Ok)
 89      {
 90          if (detected < 2)
 91              goto redetect;
 92  
 93          goto failed;
 94      }
 95  
 96      MIDI_SetMidiFuncs(&MUSIC_MidiFunctions);
 97  
 98      return MUSIC_Ok;
 99  }
100  
101  
102  int MUSIC_Shutdown(void)
103  {
104      MIDI_StopSong();
105      SoundDriver_MIDI_Shutdown();
106  
107      return MUSIC_Ok;
108  }
109  
110  
111  void MUSIC_SetVolume(int volume) { MIDI_SetVolume(clamp(volume, 0, 255)); }
112  int  MUSIC_GetVolume(void)       { return MIDI_GetVolume(); }
113  void MUSIC_SetLoopFlag(int loop) { MIDI_SetLoopFlag(loop); }
114  void MUSIC_Continue(void)        { MIDI_ContinueSong(); }
115  void MUSIC_Pause(void)           { MIDI_PauseSong(); }
116  
117  int MUSIC_StopSong(void)
118  {
119      MIDI_StopSong();
120      MUSIC_SetErrorCode(MUSIC_Ok);
121      return MUSIC_Ok;
122  }
123  
124  
125  int MUSIC_PlaySong(char *song, int songsize, int loopflag, const char *fn /*= nullptr*/)
126  {
127      UNREFERENCED_PARAMETER(songsize);
128      UNREFERENCED_PARAMETER(fn);
129  
130      MUSIC_SetErrorCode(MUSIC_Ok)
131  
132      if (MIDI_PlaySong(song, loopflag) != MIDI_Ok)
133      {
134          MUSIC_SetErrorCode(MUSIC_MidiError);
135          return MUSIC_Warning;
136      }
137  
138      return MUSIC_Ok;
139  }
140  
141  void MUSIC_SetSongPosition(int measure, int beat, int tick) { MIDI_SetSongPosition(measure, beat, tick); }
142  void MUSIC_GetSongPosition(songposition *pos) { MIDI_GetSongPosition(pos); }
143  
144  void MUSIC_Update(void) {}