midi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 iMidCurrent = -1;
  24. static BOOL fMidLoop = FALSE;
  25. static NativeMidiSong *g_pMid = NULL;
  26. void
  27. MIDI_Play(
  28. int iNumRIX,
  29. BOOL fLoop
  30. )
  31. {
  32. #if PAL_HAS_NATIVEMIDI
  33. if (g_pMid != NULL && iNumRIX == iMidCurrent && native_midi_active())
  34. {
  35. return;
  36. }
  37. AUDIO_PlayCDTrack(-1);
  38. native_midi_freesong(g_pMid);
  39. g_pMid = NULL;
  40. iMidCurrent = -1;
  41. if (!AUDIO_MusicEnabled() || iNumRIX <= 0)
  42. {
  43. return;
  44. }
  45. if (gConfig.fIsWIN95)
  46. {
  47. char filename[1024];
  48. sprintf(filename, "%s/Musics/%.3d.mid", PAL_PREFIX, iNumRIX);
  49. g_pMid = native_midi_loadsong(filename);
  50. if (g_pMid != NULL)
  51. {
  52. native_midi_start(g_pMid);
  53. iMidCurrent = iNumRIX;
  54. fMidLoop = fLoop;
  55. }
  56. }
  57. if (!g_pMid)
  58. {
  59. unsigned char *buf;
  60. int size;
  61. SDL_RWops *rw;
  62. FILE *fp = UTIL_OpenFile("midi.mkf");
  63. if (fp == NULL)
  64. {
  65. return;
  66. }
  67. if (iNumRIX > PAL_MKFGetChunkCount(fp))
  68. {
  69. fclose(fp);
  70. return;
  71. }
  72. size = PAL_MKFGetChunkSize(iNumRIX, fp);
  73. if (size <= 0)
  74. {
  75. fclose(fp);
  76. return;
  77. }
  78. buf = (unsigned char *)UTIL_malloc(size);
  79. PAL_MKFReadChunk((LPBYTE)buf, size, iNumRIX, fp);
  80. fclose(fp);
  81. rw = SDL_RWFromConstMem((const void *)buf, size);
  82. g_pMid = native_midi_loadsong_RW(rw);
  83. if (g_pMid != NULL)
  84. {
  85. native_midi_start(g_pMid);
  86. iMidCurrent = iNumRIX;
  87. fMidLoop = fLoop;
  88. }
  89. SDL_RWclose(rw);
  90. free(buf);
  91. }
  92. #endif
  93. }
  94. void
  95. MIDI_CheckLoop(
  96. void
  97. )
  98. {
  99. #if PAL_HAS_NATIVEMIDI
  100. if (fMidLoop && g_pMid != NULL && !native_midi_active())
  101. {
  102. MIDI_Play(iMidCurrent, TRUE);
  103. }
  104. #endif
  105. }