music_mad.c 13 KB

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