music_mad.c 13 KB

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