midi.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Copyright (c) 2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  3. // All rights reserved.
  4. //
  5. // This file is part of SDLPAL.
  6. //
  7. // SDLPAL is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. //
  20. #include "main.h"
  21. #if !defined (CYGWIN) && !defined (DINGOO) && !defined (GEKKO) && !defined (GPH)
  22. static INT iMidCurrent = -1;
  23. static BOOL fMidLoop = FALSE;
  24. static NativeMidiSong *g_pMid = NULL;
  25. VOID
  26. MIDI_Play(
  27. INT iNumRIX,
  28. BOOL fLoop
  29. )
  30. /*++
  31. Purpose:
  32. Start playing the specified music in MIDI format.
  33. Parameters:
  34. [IN] iNumRIX - number of the music. 0 to stop playing current music.
  35. [IN] fLoop - Whether the music should be looped or not.
  36. Return value:
  37. None.
  38. --*/
  39. {
  40. FILE *fp;
  41. unsigned char *buf;
  42. int size;
  43. SDL_RWops *rw;
  44. if (g_pMid != NULL && iNumRIX == iMidCurrent && native_midi_active())
  45. {
  46. return;
  47. }
  48. SOUND_PlayCDA(-1);
  49. native_midi_freesong(g_pMid);
  50. g_pMid = NULL;
  51. iMidCurrent = -1;
  52. if (g_fNoMusic || iNumRIX <= 0)
  53. {
  54. return;
  55. }
  56. fp = fopen(PAL_PREFIX "midi.mkf", "rb");
  57. if (fp == NULL)
  58. {
  59. return;
  60. }
  61. if (iNumRIX > PAL_MKFGetChunkCount(fp))
  62. {
  63. fclose(fp);
  64. return;
  65. }
  66. size = PAL_MKFGetChunkSize(iNumRIX, fp);
  67. if (size <= 0)
  68. {
  69. fclose(fp);
  70. return;
  71. }
  72. buf = (unsigned char *)UTIL_malloc(size);
  73. PAL_MKFReadChunk((LPBYTE)buf, size, iNumRIX, fp);
  74. fclose(fp);
  75. rw = SDL_RWFromConstMem((const void *)buf, size);
  76. g_pMid = native_midi_loadsong_RW(rw);
  77. if (g_pMid != NULL)
  78. {
  79. native_midi_start(g_pMid);
  80. iMidCurrent = iNumRIX;
  81. fMidLoop = fLoop;
  82. }
  83. SDL_RWclose(rw);
  84. free(buf);
  85. }
  86. VOID
  87. MIDI_CheckLoop(
  88. VOID
  89. )
  90. {
  91. if (fMidLoop && g_pMid != NULL && !native_midi_active())
  92. {
  93. MIDI_Play(iMidCurrent, TRUE);
  94. }
  95. }
  96. #endif