midi.c 2.5 KB

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