mp3play.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "sound.h"
  27. #include "players.h"
  28. #include "resampler.h"
  29. #include "libmad/music_mad.h"
  30. typedef struct tagMP3PLAYER
  31. {
  32. MUSICPLAYER_FUNCTIONS;
  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. player->pMP3->volume = gConfig.iVolume * 3 / 4;
  58. mad_getSamples(player->pMP3, stream, len);
  59. if (!mad_isPlaying(player->pMP3) && player->fLoop)
  60. {
  61. mad_seek(player->pMP3, 0);
  62. mad_start(player->pMP3);
  63. mad_getSamples(player->pMP3, stream, len);
  64. }
  65. }
  66. }
  67. static VOID
  68. MP3_Shutdown(
  69. VOID *object
  70. )
  71. {
  72. if (object)
  73. {
  74. MP3_Close((LPMP3PLAYER)object);
  75. free(object);
  76. }
  77. }
  78. static BOOL
  79. MP3_Play(
  80. VOID *object,
  81. INT iNum,
  82. BOOL fLoop,
  83. FLOAT flFadeTime
  84. )
  85. {
  86. LPMP3PLAYER player = (LPMP3PLAYER)object;
  87. //
  88. // Check for NULL pointer.
  89. //
  90. if (player == NULL)
  91. {
  92. return FALSE;
  93. }
  94. player->fLoop = fLoop;
  95. if (iNum == player->iMusic)
  96. {
  97. return TRUE;
  98. }
  99. MP3_Close(player);
  100. if (iNum > 0)
  101. {
  102. if ((player->pMP3 = mad_openFile(va("%smp3/%.2d.mp3", gConfig.pszGamePath, iNum), SOUND_GetAudioSpec(), gConfig.iResampleQuality)) == NULL)
  103. {
  104. player->pMP3 = mad_openFile(va("%sMP3/%.2d.MP3", gConfig.pszGamePath, iNum), SOUND_GetAudioSpec(), gConfig.iResampleQuality);
  105. }
  106. if (player->pMP3)
  107. {
  108. player->iMusic = iNum;
  109. mad_start(player->pMP3);
  110. return TRUE;
  111. }
  112. else
  113. {
  114. return FALSE;
  115. }
  116. }
  117. else
  118. {
  119. return TRUE;
  120. }
  121. }
  122. LPMUSICPLAYER
  123. MP3_Init(
  124. LPCSTR szFileName
  125. )
  126. {
  127. LPMP3PLAYER player;
  128. if (player = (LPMP3PLAYER)malloc(sizeof(MP3PLAYER)))
  129. {
  130. player->FillBuffer = MP3_FillBuffer;
  131. player->Play = MP3_Play;
  132. player->Shutdown = MP3_Shutdown;
  133. player->pMP3 = NULL;
  134. player->iMusic = -1;
  135. player->fLoop = FALSE;
  136. return (LPMUSICPLAYER)player;
  137. }
  138. else
  139. {
  140. return NULL;
  141. }
  142. }
  143. #endif