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