mp3player.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #if PAL_HAS_MP3
  21. #include "sound.h"
  22. #include "players.h"
  23. #include "libmad/music_mad.h"
  24. typedef struct tagMP3PLAYER
  25. {
  26. MUSICPLAYER_FUNCTIONS;
  27. mad_data *pMP3;
  28. INT iMusic;
  29. BOOL fLoop;
  30. } MP3PLAYER, *LPMP3PLAYER;
  31. static VOID MP3_Close(
  32. LPMP3PLAYER player
  33. )
  34. {
  35. if (player->pMP3)
  36. {
  37. mad_stop(player->pMP3);
  38. mad_closeFile(player->pMP3);
  39. player->pMP3 = NULL;
  40. }
  41. }
  42. static VOID
  43. MP3_FillBuffer(
  44. VOID *object,
  45. LPBYTE stream,
  46. INT len
  47. )
  48. {
  49. LPMP3PLAYER player = (LPMP3PLAYER)object;
  50. if (player->pMP3) {
  51. mad_getSamples(player->pMP3, stream, len);
  52. if (!mad_isPlaying(player->pMP3) && player->fLoop)
  53. {
  54. mad_seek(player->pMP3, 0);
  55. mad_start(player->pMP3);
  56. mad_getSamples(player->pMP3, stream, len);
  57. }
  58. }
  59. }
  60. static VOID
  61. MP3_Shutdown(
  62. VOID *object
  63. )
  64. {
  65. if (object)
  66. {
  67. MP3_Close((LPMP3PLAYER)object);
  68. free(object);
  69. }
  70. }
  71. static BOOL
  72. MP3_Play(
  73. VOID *object,
  74. INT iNum,
  75. BOOL fLoop,
  76. FLOAT flFadeTime
  77. )
  78. {
  79. LPMP3PLAYER player = (LPMP3PLAYER)object;
  80. //
  81. // Check for NULL pointer.
  82. //
  83. if (player == NULL)
  84. {
  85. return FALSE;
  86. }
  87. player->fLoop = fLoop;
  88. if (iNum == player->iMusic)
  89. {
  90. return TRUE;
  91. }
  92. MP3_Close(player);
  93. if (iNum > 0)
  94. {
  95. if ((player->pMP3 = mad_openFile(va("%s/mp3/%.2d.mp3", PAL_PREFIX, iNum), SOUND_GetAudioSpec())) == NULL)
  96. {
  97. player->pMP3 = mad_openFile(va("%s/MP3/%.2d.MP3", PAL_PREFIX, iNum), SOUND_GetAudioSpec());
  98. }
  99. if (player->pMP3)
  100. {
  101. player->iMusic = iNum;
  102. mad_start(player->pMP3);
  103. return TRUE;
  104. }
  105. else
  106. {
  107. return FALSE;
  108. }
  109. }
  110. else
  111. {
  112. return TRUE;
  113. }
  114. }
  115. LPMUSICPLAYER
  116. MP3_Init(
  117. LPCSTR szFileName
  118. )
  119. {
  120. LPMP3PLAYER player;
  121. if (player = (LPMP3PLAYER)malloc(sizeof(MP3PLAYER)))
  122. {
  123. player->FillBuffer = MP3_FillBuffer;
  124. player->Play = MP3_Play;
  125. player->Shutdown = MP3_Shutdown;
  126. player->pMP3 = NULL;
  127. player->iMusic = -1;
  128. player->fLoop = FALSE;
  129. return (LPMUSICPLAYER)player;
  130. }
  131. else
  132. {
  133. return NULL;
  134. }
  135. }
  136. #endif