midi.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #if PAL_HAS_NATIVEMIDI
  24. static INT iMidCurrent = -1;
  25. static BOOL fMidLoop = FALSE;
  26. static NativeMidiSong *g_pMid = NULL;
  27. VOID
  28. MIDI_Play(
  29. INT iNumRIX,
  30. BOOL fLoop
  31. )
  32. /*++
  33. Purpose:
  34. Start playing the specified music in MIDI format.
  35. Parameters:
  36. [IN] iNumRIX - number of the music. 0 to stop playing current music.
  37. [IN] fLoop - Whether the music should be looped or not.
  38. Return value:
  39. None.
  40. --*/
  41. {
  42. if (g_pMid != NULL && iNumRIX == iMidCurrent && native_midi_active())
  43. {
  44. return;
  45. }
  46. AUDIO_PlayCDTrack(-1);
  47. native_midi_freesong(g_pMid);
  48. g_pMid = NULL;
  49. iMidCurrent = -1;
  50. if (!AUDIO_MusicEnabled() || iNumRIX <= 0)
  51. {
  52. return;
  53. }
  54. if (gConfig.fIsWIN95)
  55. {
  56. char filename[1024];
  57. sprintf(filename, "%s/musics/%.3d.mid", PAL_PREFIX, iNumRIX);
  58. g_pMid = native_midi_loadsong(filename);
  59. if (g_pMid != NULL)
  60. {
  61. native_midi_start(g_pMid);
  62. iMidCurrent = iNumRIX;
  63. fMidLoop = fLoop;
  64. }
  65. }
  66. if (!g_pMid)
  67. {
  68. unsigned char *buf;
  69. int size;
  70. SDL_RWops *rw;
  71. FILE *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. }
  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