midi.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Copyright (c) 2011-2017, SDLPAL development team.
  5. // All rights reserved.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // SDLPAL is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. //
  22. #include "main.h"
  23. static int g_iMidiCurrent = -1;
  24. static NativeMidiSong *g_pMidi = NULL;
  25. static int g_iMidiVolume = PAL_MAX_VOLUME;
  26. void
  27. MIDI_SetVolume(
  28. int iVolume
  29. )
  30. {
  31. #if PAL_HAS_NATIVEMIDI
  32. g_iMidiVolume = iVolume;
  33. if (g_pMidi)
  34. {
  35. native_midi_setvolume(g_pMidi, iVolume * 127 / PAL_MAX_VOLUME);
  36. }
  37. #endif
  38. }
  39. void
  40. MIDI_Play(
  41. int iNumRIX,
  42. BOOL fLoop
  43. )
  44. {
  45. #if PAL_HAS_NATIVEMIDI
  46. if (!native_midi_detect())
  47. return;
  48. if (native_midi_active(g_pMidi) && iNumRIX == g_iMidiCurrent)
  49. {
  50. return;
  51. }
  52. native_midi_stop(g_pMidi);
  53. native_midi_freesong(g_pMidi);
  54. g_pMidi = NULL;
  55. g_iMidiCurrent = -1;
  56. if (!AUDIO_MusicEnabled() || iNumRIX <= 0)
  57. {
  58. return;
  59. }
  60. if (gConfig.fIsWIN95)
  61. {
  62. g_pMidi = native_midi_loadsong(UTIL_GetFullPathName(PAL_BUFFER_SIZE_ARGS(0), gConfig.pszGamePath, PAL_va(1, "Musics/%.3d.mid", iNumRIX)));
  63. }
  64. if (!g_pMidi)
  65. {
  66. FILE *fp = NULL;
  67. uint8_t *buf = NULL;
  68. int size;
  69. if ((fp = UTIL_OpenFile("midi.mkf")) != NULL)
  70. {
  71. if ((size = PAL_MKFGetChunkSize(iNumRIX, fp)) > 0 &&
  72. (buf = (uint8_t*)UTIL_malloc(size)))
  73. {
  74. PAL_MKFReadChunk(buf, size, iNumRIX, fp);
  75. }
  76. fclose(fp);
  77. }
  78. if (buf)
  79. {
  80. SDL_RWops *rw = SDL_RWFromConstMem(buf, size);
  81. g_pMidi = native_midi_loadsong_RW(rw);
  82. SDL_RWclose(rw);
  83. free(buf);
  84. }
  85. }
  86. if (g_pMidi)
  87. {
  88. MIDI_SetVolume(g_iMidiVolume);
  89. native_midi_start(g_pMidi, fLoop);
  90. g_iMidiCurrent = iNumRIX;
  91. }
  92. #endif
  93. }