load_mp3.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. SDL_mixer: An audio mixer library based on the SDL library
  3. Copyright (C) 1997-2013 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. This is the source needed to decode an MP3 into a waveform.
  18. */
  19. /* $Id$ */
  20. #if defined(MP3_MUSIC) || defined(MP3_MAD_MUSIC)
  21. #include "SDL_mixer.h"
  22. #include "load_mp3.h"
  23. #if defined(MP3_MUSIC)
  24. #include "dynamic_mp3.h"
  25. #elif defined(MP3_MAD_MUSIC)
  26. #include "music_mad.h"
  27. #endif
  28. SDL_AudioSpec *Mix_LoadMP3_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
  29. {
  30. /* note: spec is initialized to mixer spec */
  31. #if defined(MP3_MUSIC)
  32. SMPEG* mp3;
  33. SMPEG_Info info;
  34. #elif defined(MP3_MAD_MUSIC)
  35. mad_data *mp3_mad;
  36. #endif
  37. long samplesize;
  38. int read_len;
  39. const Uint32 chunk_len = 4096;
  40. int err = 0;
  41. if ((!src) || (!spec) || (!audio_buf) || (!audio_len))
  42. {
  43. return NULL;
  44. }
  45. if (!err)
  46. {
  47. *audio_len = 0;
  48. *audio_buf = (Uint8*) SDL_malloc(chunk_len);
  49. err = (*audio_buf == NULL);
  50. }
  51. if (!err)
  52. {
  53. err = ((Mix_Init(MIX_INIT_MP3) & MIX_INIT_MP3) == 0);
  54. }
  55. if (!err)
  56. {
  57. #if defined(MP3_MUSIC)
  58. mp3 = smpeg.SMPEG_new_rwops(src, &info, freesrc, 0);
  59. err = (mp3 == NULL);
  60. #elif defined(MP3_MAD_MUSIC)
  61. mp3_mad = mad_openFileRW(src, spec, freesrc);
  62. err = (mp3_mad == NULL);
  63. #endif
  64. }
  65. #if defined(MP3_MUSIC)
  66. if (!err)
  67. {
  68. err = !info.has_audio;
  69. }
  70. #endif
  71. if (!err)
  72. {
  73. #if defined(MP3_MUSIC)
  74. smpeg.SMPEG_actualSpec(mp3, spec);
  75. smpeg.SMPEG_enableaudio(mp3, 1);
  76. smpeg.SMPEG_enablevideo(mp3, 0);
  77. smpeg.SMPEG_play(mp3);
  78. /* read once for audio length */
  79. while ((read_len = smpeg.SMPEG_playAudio(mp3, *audio_buf, chunk_len)) > 0)
  80. {
  81. *audio_len += read_len;
  82. }
  83. smpeg.SMPEG_stop(mp3);
  84. #elif defined(MP3_MAD_MUSIC)
  85. mad_start(mp3_mad);
  86. /* read once for audio length */
  87. while ((read_len = mad_getSamples(mp3_mad, *audio_buf, chunk_len)) > 0)
  88. {
  89. *audio_len += read_len;
  90. }
  91. mad_stop(mp3_mad);
  92. #endif
  93. err = (read_len < 0);
  94. }
  95. if (!err)
  96. {
  97. /* reallocate, if needed */
  98. if ((*audio_len > 0) && (*audio_len != chunk_len))
  99. {
  100. *audio_buf = (Uint8*) SDL_realloc(*audio_buf, *audio_len);
  101. err = (*audio_buf == NULL);
  102. }
  103. }
  104. if (!err)
  105. {
  106. /* read again for audio buffer, if needed */
  107. if (*audio_len > chunk_len)
  108. {
  109. #if defined(MP3_MUSIC)
  110. smpeg.SMPEG_rewind(mp3);
  111. smpeg.SMPEG_play(mp3);
  112. err = (*audio_len != smpeg.SMPEG_playAudio(mp3, *audio_buf, *audio_len));
  113. smpeg.SMPEG_stop(mp3);
  114. #elif defined(MP3_MAD_MUSIC)
  115. mad_seek(mp3_mad, 0);
  116. mad_start(mp3_mad);
  117. err = (*audio_len != mad_getSamples(mp3_mad, *audio_buf, *audio_len));
  118. mad_stop(mp3_mad);
  119. #endif
  120. }
  121. }
  122. if (!err)
  123. {
  124. /* Don't return a buffer that isn't a multiple of samplesize */
  125. samplesize = ((spec->format & 0xFF)/8)*spec->channels;
  126. *audio_len &= ~(samplesize-1);
  127. }
  128. #if defined(MP3_MUSIC)
  129. if (mp3)
  130. {
  131. smpeg.SMPEG_delete(mp3); mp3 = NULL;
  132. /* Deleting the MP3 closed the source if desired */
  133. freesrc = SDL_FALSE;
  134. }
  135. #elif defined(MP3_MAD_MUSIC)
  136. if (mp3_mad)
  137. {
  138. mad_closeFile(mp3_mad); mp3_mad = NULL;
  139. /* Deleting the MP3 closed the source if desired */
  140. freesrc = SDL_FALSE;
  141. }
  142. #endif
  143. if (freesrc)
  144. {
  145. SDL_RWclose(src); src = NULL;
  146. }
  147. /* handle error */
  148. if (err)
  149. {
  150. if (*audio_buf != NULL)
  151. {
  152. SDL_free(*audio_buf); *audio_buf = NULL;
  153. }
  154. *audio_len = 0;
  155. spec = NULL;
  156. }
  157. return spec;
  158. }
  159. #endif