midi.c 2.0 KB

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