midi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef PAL_WIN95
  42. FILE *fp;
  43. unsigned char *buf;
  44. int size;
  45. SDL_RWops *rw;
  46. #else
  47. char filename[1024];
  48. #endif
  49. if (g_pMid != NULL && iNumRIX == iMidCurrent && native_midi_active())
  50. {
  51. return;
  52. }
  53. SOUND_PlayCDA(-1);
  54. native_midi_freesong(g_pMid);
  55. g_pMid = NULL;
  56. iMidCurrent = -1;
  57. if (g_fNoMusic || iNumRIX <= 0)
  58. {
  59. return;
  60. }
  61. #ifdef PAL_WIN95
  62. sprintf(filename, "%s/musics/%.3d.mid", PAL_PREFIX, iNumRIX);
  63. g_pMid = native_midi_loadsong(filename);
  64. if (g_pMid != NULL)
  65. {
  66. native_midi_start(g_pMid);
  67. iMidCurrent = iNumRIX;
  68. fMidLoop = fLoop;
  69. }
  70. #else
  71. fp = UTIL_OpenFile("midi.mkf");
  72. if (fp == NULL)
  73. {
  74. return;
  75. }
  76. if (iNumRIX > PAL_MKFGetChunkCount(fp))
  77. {
  78. fclose(fp);
  79. return;
  80. }
  81. size = PAL_MKFGetChunkSize(iNumRIX, fp);
  82. if (size <= 0)
  83. {
  84. fclose(fp);
  85. return;
  86. }
  87. buf = (unsigned char *)UTIL_malloc(size);
  88. PAL_MKFReadChunk((LPBYTE)buf, size, iNumRIX, fp);
  89. fclose(fp);
  90. rw = SDL_RWFromConstMem((const void *)buf, size);
  91. g_pMid = native_midi_loadsong_RW(rw);
  92. if (g_pMid != NULL)
  93. {
  94. native_midi_start(g_pMid);
  95. iMidCurrent = iNumRIX;
  96. fMidLoop = fLoop;
  97. }
  98. SDL_RWclose(rw);
  99. free(buf);
  100. #endif
  101. }
  102. VOID
  103. MIDI_CheckLoop(
  104. VOID
  105. )
  106. {
  107. if (fMidLoop && g_pMid != NULL && !native_midi_active())
  108. {
  109. MIDI_Play(iMidCurrent, TRUE);
  110. }
  111. }
  112. #endif