midi.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. AUDIO_PlayCDTrack(-1);
  39. native_midi_stop(g_pMidi);
  40. native_midi_freesong(g_pMidi);
  41. g_pMidi = NULL;
  42. g_iMidiCurrent = -1;
  43. if (!AUDIO_MusicEnabled() || iNumRIX <= 0)
  44. {
  45. return;
  46. }
  47. if (gConfig.fIsWIN95)
  48. {
  49. g_pMidi = native_midi_loadsong(va("%s/Musics/%.3d.mid", PAL_PREFIX, iNumRIX));
  50. }
  51. if (!g_pMidi)
  52. {
  53. FILE *fp = NULL;
  54. uint8_t *buf = NULL;
  55. int size;
  56. if ((fp = UTIL_OpenFile("midi.mkf")) != NULL)
  57. {
  58. if ((size = PAL_MKFGetChunkSize(iNumRIX, fp)) > 0 &&
  59. (buf = (uint8_t*)UTIL_malloc(size)))
  60. {
  61. PAL_MKFReadChunk(buf, size, iNumRIX, fp);
  62. }
  63. fclose(fp);
  64. }
  65. if (buf)
  66. {
  67. SDL_RWops *rw = SDL_RWFromConstMem(buf, size);
  68. g_pMidi = native_midi_loadsong_RW(rw);
  69. SDL_RWclose(rw);
  70. free(buf);
  71. }
  72. }
  73. if (g_pMidi)
  74. {
  75. native_midi_start(g_pMidi, fLoop);
  76. g_iMidiCurrent = iNumRIX;
  77. }
  78. #endif
  79. }