midi.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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(UTIL_GetFullPathName(PAL_BUFFER_SIZE_ARGS(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. }