mp3play.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* -*- mode: c++; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2015, Lou Yihua <louyihua@21cn.com>.
  4. // All rights reserved.
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. //
  19. #include "util.h"
  20. #include "global.h"
  21. #if PAL_HAS_MP3
  22. #include "sound.h"
  23. #include "players.h"
  24. #include "resampler.h"
  25. #include "libmad/music_mad.h"
  26. typedef struct tagMP3PLAYER
  27. {
  28. MUSICPLAYER_FUNCTIONS;
  29. mad_data *pMP3;
  30. INT iMusic;
  31. BOOL fLoop;
  32. } MP3PLAYER, *LPMP3PLAYER;
  33. static VOID MP3_Close(
  34. LPMP3PLAYER player
  35. )
  36. {
  37. if (player->pMP3)
  38. {
  39. mad_stop(player->pMP3);
  40. mad_closeFile(player->pMP3);
  41. player->pMP3 = NULL;
  42. }
  43. }
  44. static VOID
  45. MP3_FillBuffer(
  46. VOID *object,
  47. LPBYTE stream,
  48. INT len
  49. )
  50. {
  51. LPMP3PLAYER player = (LPMP3PLAYER)object;
  52. if (player->pMP3) {
  53. player->pMP3->volume = gpGlobals->iVolume * 3 / 4;
  54. mad_getSamples(player->pMP3, stream, len);
  55. if (!mad_isPlaying(player->pMP3) && player->fLoop)
  56. {
  57. mad_seek(player->pMP3, 0);
  58. mad_start(player->pMP3);
  59. mad_getSamples(player->pMP3, stream, len);
  60. }
  61. }
  62. }
  63. static VOID
  64. MP3_Shutdown(
  65. VOID *object
  66. )
  67. {
  68. if (object)
  69. {
  70. MP3_Close((LPMP3PLAYER)object);
  71. free(object);
  72. }
  73. }
  74. static BOOL
  75. MP3_Play(
  76. VOID *object,
  77. INT iNum,
  78. BOOL fLoop,
  79. FLOAT flFadeTime
  80. )
  81. {
  82. LPMP3PLAYER player = (LPMP3PLAYER)object;
  83. //
  84. // Check for NULL pointer.
  85. //
  86. if (player == NULL)
  87. {
  88. return FALSE;
  89. }
  90. player->fLoop = fLoop;
  91. if (iNum == player->iMusic)
  92. {
  93. return TRUE;
  94. }
  95. MP3_Close(player);
  96. if (iNum > 0)
  97. {
  98. if ((player->pMP3 = mad_openFile(va("%s/mp3/%.2d.mp3", PAL_PREFIX, iNum), SOUND_GetAudioSpec(), gpGlobals->iResampleQuality)) == NULL)
  99. {
  100. player->pMP3 = mad_openFile(va("%s/MP3/%.2d.MP3", PAL_PREFIX, iNum), SOUND_GetAudioSpec(), gpGlobals->iResampleQuality);
  101. }
  102. if (player->pMP3)
  103. {
  104. player->iMusic = iNum;
  105. mad_start(player->pMP3);
  106. return TRUE;
  107. }
  108. else
  109. {
  110. return FALSE;
  111. }
  112. }
  113. else
  114. {
  115. return TRUE;
  116. }
  117. }
  118. LPMUSICPLAYER
  119. MP3_Init(
  120. LPCSTR szFileName
  121. )
  122. {
  123. LPMP3PLAYER player;
  124. if (player = (LPMP3PLAYER)malloc(sizeof(MP3PLAYER)))
  125. {
  126. player->FillBuffer = MP3_FillBuffer;
  127. player->Play = MP3_Play;
  128. player->Shutdown = MP3_Shutdown;
  129. player->pMP3 = NULL;
  130. player->iMusic = -1;
  131. player->fLoop = FALSE;
  132. return (LPMUSICPLAYER)player;
  133. }
  134. else
  135. {
  136. return NULL;
  137. }
  138. }
  139. #endif