mp3play.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #if PAL_HAS_MP3
  25. #include "sound.h"
  26. #include "players.h"
  27. #include "resampler.h"
  28. #include "libmad/music_mad.h"
  29. typedef struct tagMP3PLAYER
  30. {
  31. MUSICPLAYER_FUNCTIONS;
  32. mad_data *pMP3;
  33. INT iMusic;
  34. BOOL fLoop;
  35. } MP3PLAYER, *LPMP3PLAYER;
  36. static VOID MP3_Close(
  37. LPMP3PLAYER player
  38. )
  39. {
  40. if (player->pMP3)
  41. {
  42. mad_stop(player->pMP3);
  43. mad_closeFile(player->pMP3);
  44. player->pMP3 = NULL;
  45. }
  46. }
  47. static VOID
  48. MP3_FillBuffer(
  49. VOID *object,
  50. LPBYTE stream,
  51. INT len
  52. )
  53. {
  54. LPMP3PLAYER player = (LPMP3PLAYER)object;
  55. if (player->pMP3) {
  56. player->pMP3->volume = gConfig.iVolume * 3 / 4;
  57. mad_getSamples(player->pMP3, stream, len);
  58. if (!mad_isPlaying(player->pMP3) && player->fLoop)
  59. {
  60. mad_seek(player->pMP3, 0);
  61. mad_start(player->pMP3);
  62. mad_getSamples(player->pMP3, stream, len);
  63. }
  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("%s/mp3/%.2d.mp3", PAL_PREFIX, iNum), SOUND_GetAudioSpec(), gConfig.iResampleQuality)) == NULL)
  102. {
  103. player->pMP3 = mad_openFile(va("%s/MP3/%.2d.MP3", PAL_PREFIX, iNum), SOUND_GetAudioSpec(), 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. LPMUSICPLAYER
  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 (LPMUSICPLAYER)player;
  136. }
  137. else
  138. {
  139. return NULL;
  140. }
  141. }
  142. #endif