music_mad.c 9.0 KB

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