midi.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. void
  26. MIDI_Play(
  27. int iNumRIX,
  28. BOOL fLoop
  29. )
  30. {
  31. #if PAL_HAS_NATIVEMIDI
  32. if (!native_midi_detect())
  33. return;
  34. if (native_midi_active(g_pMidi) && iNumRIX == g_iMidiCurrent)
  35. {
  36. return;
  37. }
  38. native_midi_stop(g_pMidi);
  39. native_midi_freesong(g_pMidi);
  40. g_pMidi = NULL;
  41. g_iMidiCurrent = -1;
  42. if (!AUDIO_MusicEnabled() || iNumRIX <= 0)
  43. {
  44. return;
  45. }
  46. if (gConfig.fIsWIN95)
  47. {
  48. g_pMidi = native_midi_loadsong(PAL_CombinePath(0, gConfig.pszGamePath, PAL_va(1, "Musics/%.3d.mid", iNumRIX)));
  49. }
  50. if (!g_pMidi)
  51. {
  52. FILE *fp = NULL;
  53. uint8_t *buf = NULL;
  54. int size;
  55. if ((fp = UTIL_OpenFile("midi.mkf")) != NULL)
  56. {
  57. if ((size = PAL_MKFGetChunkSize(iNumRIX, fp)) > 0 &&
  58. (buf = (uint8_t*)UTIL_malloc(size)))
  59. {
  60. PAL_MKFReadChunk(buf, size, iNumRIX, fp);
  61. }
  62. fclose(fp);
  63. }
  64. if (buf)
  65. {
  66. SDL_RWops *rw = SDL_RWFromConstMem(buf, size);
  67. g_pMidi = native_midi_loadsong_RW(rw);
  68. SDL_RWclose(rw);
  69. free(buf);
  70. }
  71. }
  72. if (g_pMidi)
  73. {
  74. native_midi_start(g_pMidi, fLoop);
  75. g_iMidiCurrent = iNumRIX;
  76. }
  77. #endif
  78. }