music_mad.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. */
  18. #ifdef MP3_MAD_MUSIC
  19. #include <string.h>
  20. #include "music_mad.h"
  21. mad_data *
  22. mad_openFileRW(SDL_RWops *src, SDL_AudioSpec *mixer, int freesrc)
  23. {
  24. mad_data *mp3_mad;
  25. mp3_mad = (mad_data *)SDL_malloc(sizeof(mad_data));
  26. if (mp3_mad) {
  27. mp3_mad->src = src;
  28. mp3_mad->freesrc = freesrc;
  29. mad_stream_init(&mp3_mad->stream);
  30. mad_frame_init(&mp3_mad->frame);
  31. mad_synth_init(&mp3_mad->synth);
  32. mp3_mad->frames_read = 0;
  33. mad_timer_reset(&mp3_mad->next_frame_start);
  34. mp3_mad->volume = MIX_MAX_VOLUME;
  35. mp3_mad->status = 0;
  36. mp3_mad->output_begin = 0;
  37. mp3_mad->output_end = 0;
  38. mp3_mad->mixer = *mixer;
  39. }
  40. return mp3_mad;
  41. }
  42. void
  43. mad_closeFile(mad_data *mp3_mad)
  44. {
  45. mad_stream_finish(&mp3_mad->stream);
  46. mad_frame_finish(&mp3_mad->frame);
  47. mad_synth_finish(&mp3_mad->synth);
  48. if (mp3_mad->freesrc) {
  49. SDL_RWclose(mp3_mad->src);
  50. }
  51. SDL_free(mp3_mad);
  52. }
  53. /* Starts the playback. */
  54. void
  55. mad_start(mad_data *mp3_mad) {
  56. mp3_mad->status |= MS_playing;
  57. }
  58. /* Stops the playback. */
  59. void
  60. mad_stop(mad_data *mp3_mad) {
  61. mp3_mad->status &= ~MS_playing;
  62. }
  63. /* Returns true if the playing is engaged, false otherwise. */
  64. int
  65. mad_isPlaying(mad_data *mp3_mad) {
  66. return ((mp3_mad->status & MS_playing) != 0);
  67. }
  68. /* Reads the next frame from the file. Returns true on success or
  69. false on failure. */
  70. static int
  71. read_next_frame(mad_data *mp3_mad) {
  72. if (mp3_mad->stream.buffer == NULL ||
  73. mp3_mad->stream.error == MAD_ERROR_BUFLEN) {
  74. size_t read_size;
  75. size_t remaining;
  76. unsigned char *read_start;
  77. /* There might be some bytes in the buffer left over from last
  78. time. If so, move them down and read more bytes following
  79. them. */
  80. if (mp3_mad->stream.next_frame != NULL) {
  81. remaining = mp3_mad->stream.bufend - mp3_mad->stream.next_frame;
  82. memmove(mp3_mad->input_buffer, mp3_mad->stream.next_frame, remaining);
  83. read_start = mp3_mad->input_buffer + remaining;
  84. read_size = MAD_INPUT_BUFFER_SIZE - remaining;
  85. } else {
  86. read_size = MAD_INPUT_BUFFER_SIZE;
  87. read_start = mp3_mad->input_buffer;
  88. remaining = 0;
  89. }
  90. /* Now read additional bytes from the input file. */
  91. read_size = SDL_RWread(mp3_mad->src, read_start, 1, read_size);
  92. if (read_size <= 0) {
  93. if ((mp3_mad->status & (MS_input_eof | MS_input_error)) == 0) {
  94. if (read_size == 0) {
  95. mp3_mad->status |= MS_input_eof;
  96. } else {
  97. mp3_mad->status |= MS_input_error;
  98. }
  99. /* At the end of the file, we must stuff MAD_BUFFER_GUARD
  100. number of 0 bytes. */
  101. SDL_memset(read_start + read_size, 0, MAD_BUFFER_GUARD);
  102. read_size += MAD_BUFFER_GUARD;
  103. }
  104. }
  105. /* Now feed those bytes into the libmad stream. */
  106. mad_stream_buffer(&mp3_mad->stream, mp3_mad->input_buffer,
  107. read_size + remaining);
  108. mp3_mad->stream.error = MAD_ERROR_NONE;
  109. }
  110. /* Now ask libmad to extract a frame from the data we just put in
  111. its buffer. */
  112. if (mad_frame_decode(&mp3_mad->frame, &mp3_mad->stream)) {
  113. if (MAD_RECOVERABLE(mp3_mad->stream.error)) {
  114. return 0;
  115. } else if (mp3_mad->stream.error == MAD_ERROR_BUFLEN) {
  116. return 0;
  117. } else {
  118. mp3_mad->status |= MS_decode_error;
  119. return 0;
  120. }
  121. }
  122. mp3_mad->frames_read++;
  123. mad_timer_add(&mp3_mad->next_frame_start, mp3_mad->frame.header.duration);
  124. return 1;
  125. }
  126. /* Scale a MAD sample to 16 bits for output. */
  127. static signed int
  128. scale(mad_fixed_t sample) {
  129. /* round */
  130. sample += (1L << (MAD_F_FRACBITS - 16));
  131. /* clip */
  132. if (sample >= MAD_F_ONE)
  133. sample = MAD_F_ONE - 1;
  134. else if (sample < -MAD_F_ONE)
  135. sample = -MAD_F_ONE;
  136. /* quantize */
  137. return sample >> (MAD_F_FRACBITS + 1 - 16);
  138. }
  139. /* Once the frame has been read, copies its samples into the output
  140. buffer. */
  141. static void
  142. decode_frame(mad_data *mp3_mad) {
  143. struct mad_pcm *pcm;
  144. unsigned int nchannels, nsamples;
  145. mad_fixed_t const *left_ch, *right_ch;
  146. unsigned char *out;
  147. int ret;
  148. mad_synth_frame(&mp3_mad->synth, &mp3_mad->frame);
  149. pcm = &mp3_mad->synth.pcm;
  150. out = mp3_mad->output_buffer + mp3_mad->output_end;
  151. if ((mp3_mad->status & MS_cvt_decoded) == 0) {
  152. mp3_mad->status |= MS_cvt_decoded;
  153. /* The first frame determines some key properties of the stream.
  154. In particular, it tells us enough to set up the convert
  155. structure now. */
  156. SDL_BuildAudioCVT(&mp3_mad->cvt, AUDIO_S16, pcm->channels, mp3_mad->frame.header.samplerate, mp3_mad->mixer.format, mp3_mad->mixer.channels, mp3_mad->mixer.freq);
  157. }
  158. /* pcm->samplerate contains the sampling frequency */
  159. nchannels = pcm->channels;
  160. nsamples = pcm->length;
  161. left_ch = pcm->samples[0];
  162. right_ch = pcm->samples[1];
  163. while (nsamples--) {
  164. signed int sample;
  165. /* output sample(s) in 16-bit signed little-endian PCM */
  166. sample = scale(*left_ch++);
  167. *out++ = ((sample >> 0) & 0xff);
  168. *out++ = ((sample >> 8) & 0xff);
  169. if (nchannels == 2) {
  170. sample = scale(*right_ch++);
  171. *out++ = ((sample >> 0) & 0xff);
  172. *out++ = ((sample >> 8) & 0xff);
  173. }
  174. }
  175. mp3_mad->output_end = out - mp3_mad->output_buffer;
  176. /*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/
  177. }
  178. int
  179. mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len) {
  180. int bytes_remaining;
  181. int num_bytes;
  182. Uint8 *out;
  183. if ((mp3_mad->status & MS_playing) == 0) {
  184. /* We're not supposed to be playing, so send silence instead. */
  185. SDL_memset(stream, 0, len);
  186. return 0;
  187. }
  188. out = stream;
  189. bytes_remaining = len;
  190. while (bytes_remaining > 0) {
  191. if (mp3_mad->output_end == mp3_mad->output_begin) {
  192. /* We need to get a new frame. */
  193. mp3_mad->output_begin = 0;
  194. mp3_mad->output_end = 0;
  195. if (!read_next_frame(mp3_mad)) {
  196. if ((mp3_mad->status & MS_error_flags) != 0) {
  197. /* Couldn't read a frame; either an error condition or
  198. end-of-file. Stop. */
  199. SDL_memset(out, 0, bytes_remaining);
  200. mp3_mad->status &= ~MS_playing;
  201. return bytes_remaining;
  202. }
  203. } else {
  204. decode_frame(mp3_mad);
  205. /* Now convert the frame data to the appropriate format for
  206. output. */
  207. mp3_mad->cvt.buf = mp3_mad->output_buffer;
  208. mp3_mad->cvt.len = mp3_mad->output_end;
  209. mp3_mad->output_end = (int)(mp3_mad->output_end * mp3_mad->cvt.len_ratio);
  210. /*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/
  211. SDL_ConvertAudio(&mp3_mad->cvt);
  212. }
  213. }
  214. num_bytes = mp3_mad->output_end - mp3_mad->output_begin;
  215. if (bytes_remaining < num_bytes) {
  216. num_bytes = bytes_remaining;
  217. }
  218. if (mp3_mad->volume == MIX_MAX_VOLUME) {
  219. SDL_memcpy(out, mp3_mad->output_buffer + mp3_mad->output_begin, num_bytes);
  220. } else {
  221. SDL_MixAudio(out, mp3_mad->output_buffer + mp3_mad->output_begin,
  222. num_bytes, mp3_mad->volume);
  223. }
  224. out += num_bytes;
  225. mp3_mad->output_begin += num_bytes;
  226. bytes_remaining -= num_bytes;
  227. }
  228. return 0;
  229. }
  230. void
  231. mad_seek(mad_data *mp3_mad, double position) {
  232. mad_timer_t target;
  233. int int_part;
  234. int_part = (int)position;
  235. mad_timer_set(&target, int_part,
  236. (int)((position - int_part) * 1000000), 1000000);
  237. if (mad_timer_compare(mp3_mad->next_frame_start, target) > 0) {
  238. /* In order to seek backwards in a VBR file, we have to rewind and
  239. start again from the beginning. This isn't necessary if the
  240. file happens to be CBR, of course; in that case we could seek
  241. directly to the frame we want. But I leave that little
  242. optimization for the future developer who discovers she really
  243. needs it. */
  244. mp3_mad->frames_read = 0;
  245. mad_timer_reset(&mp3_mad->next_frame_start);
  246. mp3_mad->status &= ~MS_error_flags;
  247. mp3_mad->output_begin = 0;
  248. mp3_mad->output_end = 0;
  249. SDL_RWseek(mp3_mad->src, 0, RW_SEEK_SET);
  250. }
  251. /* Now we have to skip frames until we come to the right one.
  252. Again, only truly necessary if the file is VBR. */
  253. while (mad_timer_compare(mp3_mad->next_frame_start, target) < 0) {
  254. if (!read_next_frame(mp3_mad)) {
  255. if ((mp3_mad->status & MS_error_flags) != 0) {
  256. /* Couldn't read a frame; either an error condition or
  257. end-of-file. Stop. */
  258. mp3_mad->status &= ~MS_playing;
  259. return;
  260. }
  261. }
  262. }
  263. /* Here we are, at the beginning of the frame that contains the
  264. target time. Ehh, I say that's close enough. If we wanted to,
  265. we could get more precise by decoding the frame now and counting
  266. the appropriate number of samples out of it. */
  267. }
  268. void
  269. mad_setVolume(mad_data *mp3_mad, int volume) {
  270. mp3_mad->volume = volume;
  271. }
  272. #endif /* MP3_MAD_MUSIC */