mp3play.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Copyright (c) 2011-2017, SDLPAL development team.
  5. // All rights reserved.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // SDLPAL 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. #include "audio.h"
  26. #include "players.h"
  27. #if PAL_HAS_MP3
  28. #include "libmad/music_mad.h"
  29. #include "resampler.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. player->iMusic = iNum;
  100. if (iNum > 0)
  101. {
  102. player->pMP3 = mad_openFile(UTIL_GetFullPathName(PAL_BUFFER_SIZE_ARGS(0), gConfig.pszGamePath, PAL_va(1, "mp3/%.2d.mp3", iNum)), AUDIO_GetDeviceSpec(), gConfig.iResampleQuality);
  103. if (player->pMP3)
  104. {
  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. LPAUDIOPLAYER
  119. MP3_Init(
  120. VOID
  121. )
  122. {
  123. LPMP3PLAYER player;
  124. if ((player = (LPMP3PLAYER)malloc(sizeof(MP3PLAYER))) != NULL)
  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. }
  133. return (LPAUDIOPLAYER)player;
  134. }
  135. #else
  136. LPAUDIOPLAYER
  137. MP3_Init(
  138. VOID
  139. )
  140. {
  141. return NULL;
  142. }
  143. #endif