music_mad.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. SDL_mixer: An audio mixer library based on the SDL library
  3. Copyright (C) 1997-2004 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Sam Lantinga
  16. slouken@libsdl.org
  17. */
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include "music_mad.h"
  21. mad_data *
  22. mad_openFile(const char *filename, SDL_AudioSpec *mixer) {
  23. SDL_RWops *rw;
  24. rw = SDL_RWFromFile(filename, "rb");
  25. if (rw == NULL) {
  26. return NULL;
  27. }
  28. return mad_openFileRW(rw, mixer);
  29. }
  30. mad_data *
  31. mad_openFileRW(SDL_RWops *rw, SDL_AudioSpec *mixer) {
  32. mad_data *mp3_mad;
  33. mp3_mad = (mad_data *)malloc(sizeof(mad_data));
  34. mp3_mad->rw = rw;
  35. mad_stream_init(&mp3_mad->stream);
  36. mad_frame_init(&mp3_mad->frame);
  37. mad_synth_init(&mp3_mad->synth);
  38. mp3_mad->frames_read = 0;
  39. mad_timer_reset(&mp3_mad->next_frame_start);
  40. mp3_mad->volume = 128;
  41. mp3_mad->status = 0;
  42. mp3_mad->output_begin = 0;
  43. mp3_mad->output_end = 0;
  44. mp3_mad->mixer = *mixer;
  45. return mp3_mad;
  46. }
  47. void
  48. mad_closeFile(mad_data *mp3_mad) {
  49. SDL_FreeRW(mp3_mad->rw);
  50. mad_stream_finish(&mp3_mad->stream);
  51. mad_frame_finish(&mp3_mad->frame);
  52. mad_synth_finish(&mp3_mad->synth);
  53. free(mp3_mad);
  54. }
  55. /* Starts the playback. */
  56. void
  57. mad_start(mad_data *mp3_mad) {
  58. mp3_mad->status |= MS_playing;
  59. }
  60. /* Stops the playback. */
  61. void
  62. mad_stop(mad_data *mp3_mad) {
  63. mp3_mad->status &= ~MS_playing;
  64. }
  65. /* Returns true if the playing is engaged, false otherwise. */
  66. int
  67. mad_isPlaying(mad_data *mp3_mad) {
  68. return ((mp3_mad->status & MS_playing) != 0);
  69. }
  70. /* Reads the next frame from the file. Returns true on success or
  71. false on failure. */
  72. static int
  73. read_next_frame(mad_data *mp3_mad) {
  74. if (mp3_mad->stream.buffer == NULL ||
  75. mp3_mad->stream.error == MAD_ERROR_BUFLEN) {
  76. size_t read_size;
  77. size_t remaining;
  78. unsigned char *read_start;
  79. /* There might be some bytes in the buffer left over from last
  80. time. If so, move them down and read more bytes following
  81. them. */
  82. if (mp3_mad->stream.next_frame != NULL) {
  83. remaining = mp3_mad->stream.bufend - mp3_mad->stream.next_frame;
  84. memmove(mp3_mad->input_buffer, mp3_mad->stream.next_frame, remaining);
  85. read_start = mp3_mad->input_buffer + remaining;
  86. read_size = MAD_INPUT_BUFFER_SIZE - remaining;
  87. } else {
  88. read_size = MAD_INPUT_BUFFER_SIZE;
  89. read_start = mp3_mad->input_buffer;
  90. remaining = 0;
  91. }
  92. /* Now read additional bytes from the input file. */
  93. read_size = SDL_RWread(mp3_mad->rw, read_start, 1, read_size);
  94. if (read_size <= 0) {
  95. if ((mp3_mad->status & (MS_input_eof | MS_input_error)) == 0) {
  96. if (read_size == 0) {
  97. mp3_mad->status |= MS_input_eof;
  98. } else {
  99. mp3_mad->status |= MS_input_error;
  100. }
  101. /* At the end of the file, we must stuff MAD_BUFFER_GUARD
  102. number of 0 bytes. */
  103. memset(read_start + read_size, 0, MAD_BUFFER_GUARD);
  104. read_size += MAD_BUFFER_GUARD;
  105. }
  106. }
  107. /* Now feed those bytes into the libmad stream. */
  108. mad_stream_buffer(&mp3_mad->stream, mp3_mad->input_buffer,
  109. read_size + remaining);
  110. mp3_mad->stream.error = MAD_ERROR_NONE;
  111. }
  112. /* Now ask libmad to extract a frame from the data we just put in
  113. its buffer. */
  114. if (mad_frame_decode(&mp3_mad->frame, &mp3_mad->stream)) {
  115. if (MAD_RECOVERABLE(mp3_mad->stream.error)) {
  116. return 0;
  117. } else if (mp3_mad->stream.error == MAD_ERROR_BUFLEN) {
  118. return 0;
  119. } else {
  120. mp3_mad->status |= MS_decode_error;
  121. return 0;
  122. }
  123. }
  124. mp3_mad->frames_read++;
  125. mad_timer_add(&mp3_mad->next_frame_start, mp3_mad->frame.header.duration);
  126. return 1;
  127. }
  128. /* Scale a MAD sample to 16 bits for output. */
  129. static signed int
  130. scale(mad_fixed_t sample) {
  131. /* round */
  132. sample += (1L << (MAD_F_FRACBITS - 16));
  133. /* clip */
  134. if (sample >= MAD_F_ONE)
  135. sample = MAD_F_ONE - 1;
  136. else if (sample < -MAD_F_ONE)
  137. sample = -MAD_F_ONE;
  138. /* quantize */
  139. return sample >> (MAD_F_FRACBITS + 1 - 16);
  140. }
  141. /* Once the frame has been read, copies its samples into the output
  142. buffer. */
  143. static void
  144. decode_frame(mad_data *mp3_mad) {
  145. struct mad_pcm *pcm;
  146. unsigned int nchannels, nsamples;
  147. mad_fixed_t const *left_ch, *right_ch;
  148. unsigned char *out;
  149. mad_synth_frame(&mp3_mad->synth, &mp3_mad->frame);
  150. pcm = &mp3_mad->synth.pcm;
  151. out = mp3_mad->output_buffer + mp3_mad->output_end;
  152. if ((mp3_mad->status & MS_cvt_decoded) == 0) {
  153. mp3_mad->status |= MS_cvt_decoded;
  154. /* The first frame determines some key properties of the stream.
  155. In particular, it tells us enough to set up the convert
  156. structure now. */
  157. SDL_BuildAudioCVT(&mp3_mad->cvt, AUDIO_S16, (Uint8)pcm->channels, mp3_mad->frame.header.samplerate, mp3_mad->mixer.format, mp3_mad->mixer.channels, mp3_mad->mixer.freq);
  158. }
  159. /* pcm->samplerate contains the sampling frequency */
  160. nchannels = pcm->channels;
  161. nsamples = pcm->length;
  162. left_ch = pcm->samples[0];
  163. right_ch = pcm->samples[1];
  164. while (nsamples--) {
  165. signed int sample;
  166. /* output sample(s) in 16-bit signed little-endian PCM */
  167. sample = scale(*left_ch++);
  168. *out++ = ((sample >> 0) & 0xff);
  169. *out++ = ((sample >> 8) & 0xff);
  170. if (nchannels == 2) {
  171. sample = scale(*right_ch++);
  172. *out++ = ((sample >> 0) & 0xff);
  173. *out++ = ((sample >> 8) & 0xff);
  174. }
  175. }
  176. mp3_mad->output_end = out - mp3_mad->output_buffer;
  177. /*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/
  178. }
  179. void
  180. mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len) {
  181. int bytes_remaining;
  182. int num_bytes;
  183. Uint8 *out;
  184. if ((mp3_mad->status & MS_playing) == 0) {
  185. /* We're not supposed to be playing, so send silence instead. */
  186. memset(stream, 0, len);
  187. return;
  188. }
  189. out = stream;
  190. bytes_remaining = len;
  191. while (bytes_remaining > 0) {
  192. if (mp3_mad->output_end == mp3_mad->output_begin) {
  193. /* We need to get a new frame. */
  194. mp3_mad->output_begin = 0;
  195. mp3_mad->output_end = 0;
  196. if (!read_next_frame(mp3_mad)) {
  197. if ((mp3_mad->status & MS_error_flags) != 0) {
  198. /* Couldn't read a frame; either an error condition or
  199. end-of-file. Stop. */
  200. memset(out, 0, bytes_remaining);
  201. mp3_mad->status &= ~MS_playing;
  202. return;
  203. }
  204. } else {
  205. decode_frame(mp3_mad);
  206. /* Now convert the frame data to the appropriate format for
  207. output. */
  208. mp3_mad->cvt.buf = mp3_mad->output_buffer;
  209. mp3_mad->cvt.len = mp3_mad->output_end;
  210. mp3_mad->output_end = (int)(mp3_mad->output_end * mp3_mad->cvt.len_ratio);
  211. /*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/
  212. SDL_ConvertAudio(&mp3_mad->cvt);
  213. }
  214. }
  215. num_bytes = mp3_mad->output_end - mp3_mad->output_begin;
  216. if (bytes_remaining < num_bytes) {
  217. num_bytes = bytes_remaining;
  218. }
  219. if (mp3_mad->volume == 128) {
  220. memcpy(out, mp3_mad->output_buffer + mp3_mad->output_begin, num_bytes);
  221. } else {
  222. SDL_MixAudio(out, mp3_mad->output_buffer + mp3_mad->output_begin,
  223. num_bytes, mp3_mad->volume);
  224. }
  225. out += num_bytes;
  226. mp3_mad->output_begin += num_bytes;
  227. bytes_remaining -= num_bytes;
  228. }
  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->rw, 0, 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. }