mp3play.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* -*- mode: c++; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. // Modified by Lou Yihua <louyihua@21cn.com>, 2015.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // This program 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 "util.h"
  23. #include "global.h"
  24. #include "palcfg.h"
  25. #if PAL_HAS_MP3
  26. #include "audio.h"
  27. #include "players.h"
  28. #include "resampler.h"
  29. #include "libmad/music_mad.h"
  30. typedef struct tagMP3PLAYER
  31. {
  32. AUDIOPLAYER_COMMONS;
  33. mad_data *pMP3;
  34. INT iMusic;
  35. BOOL fLoop;
  36. } MP3PLAYER, *LPMP3PLAYER;
  37. static VOID MP3_Close(
  38. LPMP3PLAYER player
  39. )
  40. {
  41. if (player->pMP3)
  42. {
  43. mad_stop(player->pMP3);
  44. mad_closeFile(player->pMP3);
  45. player->pMP3 = NULL;
  46. }
  47. }
  48. static VOID
  49. MP3_FillBuffer(
  50. VOID *object,
  51. LPBYTE stream,
  52. INT len
  53. )
  54. {
  55. LPMP3PLAYER player = (LPMP3PLAYER)object;
  56. if (player->pMP3) {
  57. if (!mad_isPlaying(player->pMP3) && player->fLoop)
  58. {
  59. mad_seek(player->pMP3, 0);
  60. mad_start(player->pMP3);
  61. }
  62. if (mad_isPlaying(player->pMP3))
  63. mad_getSamples(player->pMP3, stream, len);
  64. }
  65. }
  66. static VOID
  67. MP3_Shutdown(
  68. VOID *object
  69. )
  70. {
  71. if (object)
  72. {
  73. MP3_Close((LPMP3PLAYER)object);
  74. free(object);
  75. }
  76. }
  77. static BOOL
  78. MP3_Play(
  79. VOID *object,
  80. INT iNum,
  81. BOOL fLoop,
  82. FLOAT flFadeTime
  83. )
  84. {
  85. LPMP3PLAYER player = (LPMP3PLAYER)object;
  86. //
  87. // Check for NULL pointer.
  88. //
  89. if (player == NULL)
  90. {
  91. return FALSE;
  92. }
  93. player->fLoop = fLoop;
  94. if (iNum == player->iMusic)
  95. {
  96. return TRUE;
  97. }
  98. MP3_Close(player);
  99. if (iNum > 0)
  100. {
  101. if ((player->pMP3 = mad_openFile(va("%smp3/%.2d.mp3", gConfig.pszGamePath, iNum), AUDIO_GetDeviceSpec(), gConfig.iResampleQuality)) == NULL)
  102. {
  103. player->pMP3 = mad_openFile(va("%sMP3/%.2d.MP3", gConfig.pszGamePath, iNum), AUDIO_GetDeviceSpec(), gConfig.iResampleQuality);
  104. }
  105. if (player->pMP3)
  106. {
  107. player->iMusic = iNum;
  108. mad_start(player->pMP3);
  109. return TRUE;
  110. }
  111. else
  112. {
  113. return FALSE;
  114. }
  115. }
  116. else
  117. {
  118. return TRUE;
  119. }
  120. }
  121. LPAUDIOPLAYER
  122. MP3_Init(
  123. LPCSTR szFileName
  124. )
  125. {
  126. LPMP3PLAYER player;
  127. if (player = (LPMP3PLAYER)malloc(sizeof(MP3PLAYER)))
  128. {
  129. player->FillBuffer = MP3_FillBuffer;
  130. player->Play = MP3_Play;
  131. player->Shutdown = MP3_Shutdown;
  132. player->pMP3 = NULL;
  133. player->iMusic = -1;
  134. player->fLoop = FALSE;
  135. return (LPAUDIOPLAYER)player;
  136. }
  137. else
  138. {
  139. return NULL;
  140. }
  141. }
  142. #endif