music_mad.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 23:28:23 -0500
changeset 528 bc0dfe8ae026
parent 521 565549e046b0
child 542 3de4970b36d4
permissions -rw-r--r--
Backed out changeset 1eddea51f162
slouken@357
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@518
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@357
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@357
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@357
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@357
    20
*/
slouken@357
    21
slouken@357
    22
#ifdef MP3_MAD_MUSIC
slouken@357
    23
slouken@357
    24
#include <string.h>
slouken@357
    25
slouken@357
    26
#include "music_mad.h"
slouken@357
    27
slouken@357
    28
mad_data *
slouken@521
    29
mad_openFile(const char *filename, SDL_AudioSpec *mixer)
slouken@521
    30
{
slouken@357
    31
  SDL_RWops *rw;
slouken@380
    32
  mad_data *mp3_mad;
slouken@357
    33
slouken@357
    34
  rw = SDL_RWFromFile(filename, "rb");
slouken@357
    35
  if (rw == NULL) {
slouken@357
    36
	return NULL;
slouken@357
    37
  }
slouken@357
    38
slouken@521
    39
  return mad_openFileRW(rw, mixer, 1);
slouken@357
    40
}
slouken@357
    41
slouken@357
    42
mad_data *
slouken@521
    43
mad_openFileRW(SDL_RWops *rw, SDL_AudioSpec *mixer, int freerw)
slouken@521
    44
{
slouken@357
    45
  mad_data *mp3_mad;
slouken@357
    46
slouken@357
    47
  mp3_mad = (mad_data *)malloc(sizeof(mad_data));
slouken@380
    48
  if (mp3_mad) {
slouken@380
    49
	mp3_mad->rw = rw;
slouken@521
    50
	mp3_mad->freerw = freerw;
slouken@380
    51
	mad_stream_init(&mp3_mad->stream);
slouken@380
    52
	mad_frame_init(&mp3_mad->frame);
slouken@380
    53
	mad_synth_init(&mp3_mad->synth);
slouken@380
    54
	mp3_mad->frames_read = 0;
slouken@380
    55
	mad_timer_reset(&mp3_mad->next_frame_start);
slouken@380
    56
	mp3_mad->volume = MIX_MAX_VOLUME;
slouken@380
    57
	mp3_mad->status = 0;
slouken@380
    58
	mp3_mad->output_begin = 0;
slouken@380
    59
	mp3_mad->output_end = 0;
slouken@380
    60
	mp3_mad->mixer = *mixer;
slouken@380
    61
  }
slouken@357
    62
  return mp3_mad;
slouken@357
    63
}
slouken@357
    64
slouken@357
    65
void
slouken@521
    66
mad_closeFile(mad_data *mp3_mad)
slouken@521
    67
{
slouken@357
    68
  mad_stream_finish(&mp3_mad->stream);
slouken@357
    69
  mad_frame_finish(&mp3_mad->frame);
slouken@357
    70
  mad_synth_finish(&mp3_mad->synth);
slouken@357
    71
slouken@380
    72
  if (mp3_mad->freerw) {
slouken@521
    73
	SDL_RWclose(mp3_mad->rw);
slouken@380
    74
  }
slouken@357
    75
  free(mp3_mad);
slouken@357
    76
}
slouken@357
    77
slouken@357
    78
/* Starts the playback. */
slouken@357
    79
void
slouken@357
    80
mad_start(mad_data *mp3_mad) {
slouken@357
    81
  mp3_mad->status |= MS_playing;
slouken@357
    82
}
slouken@357
    83
slouken@357
    84
/* Stops the playback. */
slouken@357
    85
void 
slouken@357
    86
mad_stop(mad_data *mp3_mad) {
slouken@357
    87
  mp3_mad->status &= ~MS_playing;
slouken@357
    88
}
slouken@357
    89
slouken@357
    90
/* Returns true if the playing is engaged, false otherwise. */
slouken@357
    91
int
slouken@357
    92
mad_isPlaying(mad_data *mp3_mad) {
slouken@357
    93
  return ((mp3_mad->status & MS_playing) != 0);
slouken@357
    94
}
slouken@357
    95
slouken@357
    96
/* Reads the next frame from the file.  Returns true on success or
slouken@357
    97
   false on failure. */
slouken@357
    98
static int
slouken@357
    99
read_next_frame(mad_data *mp3_mad) {
slouken@357
   100
  if (mp3_mad->stream.buffer == NULL || 
slouken@357
   101
	  mp3_mad->stream.error == MAD_ERROR_BUFLEN) {
slouken@357
   102
	size_t read_size;
slouken@357
   103
	size_t remaining;
slouken@357
   104
	unsigned char *read_start;
slouken@357
   105
	
slouken@357
   106
	/* There might be some bytes in the buffer left over from last
slouken@357
   107
	   time.  If so, move them down and read more bytes following
slouken@357
   108
	   them. */
slouken@357
   109
	if (mp3_mad->stream.next_frame != NULL) {
slouken@357
   110
	  remaining = mp3_mad->stream.bufend - mp3_mad->stream.next_frame;
slouken@357
   111
	  memmove(mp3_mad->input_buffer, mp3_mad->stream.next_frame, remaining);
slouken@357
   112
	  read_start = mp3_mad->input_buffer + remaining;
slouken@357
   113
	  read_size = MAD_INPUT_BUFFER_SIZE - remaining;
slouken@357
   114
	  
slouken@357
   115
	} else {
slouken@357
   116
	  read_size = MAD_INPUT_BUFFER_SIZE;
slouken@357
   117
	  read_start = mp3_mad->input_buffer;
slouken@357
   118
	  remaining = 0;
slouken@357
   119
	}
slouken@357
   120
slouken@357
   121
	/* Now read additional bytes from the input file. */
slouken@357
   122
	read_size = SDL_RWread(mp3_mad->rw, read_start, 1, read_size);
slouken@357
   123
	
slouken@357
   124
	if (read_size <= 0) {
slouken@357
   125
	  if ((mp3_mad->status & (MS_input_eof | MS_input_error)) == 0) {
slouken@357
   126
		if (read_size == 0) {
slouken@357
   127
		  mp3_mad->status |= MS_input_eof;
slouken@357
   128
		} else {
slouken@357
   129
		  mp3_mad->status |= MS_input_error;
slouken@357
   130
		}
slouken@357
   131
		
slouken@357
   132
		/* At the end of the file, we must stuff MAD_BUFFER_GUARD
slouken@357
   133
		   number of 0 bytes. */
slouken@357
   134
		memset(read_start + read_size, 0, MAD_BUFFER_GUARD);
slouken@357
   135
		read_size += MAD_BUFFER_GUARD;
slouken@357
   136
	  }
slouken@357
   137
	}
slouken@357
   138
	
slouken@357
   139
	/* Now feed those bytes into the libmad stream. */
slouken@357
   140
	mad_stream_buffer(&mp3_mad->stream, mp3_mad->input_buffer,
slouken@357
   141
					  read_size + remaining);
slouken@357
   142
	mp3_mad->stream.error = MAD_ERROR_NONE;
slouken@357
   143
  }
slouken@357
   144
  
slouken@357
   145
  /* Now ask libmad to extract a frame from the data we just put in
slouken@357
   146
	 its buffer. */
slouken@357
   147
  if (mad_frame_decode(&mp3_mad->frame, &mp3_mad->stream)) {
slouken@357
   148
	if (MAD_RECOVERABLE(mp3_mad->stream.error)) {
slouken@357
   149
	  return 0;
slouken@357
   150
	  
slouken@357
   151
	} else if (mp3_mad->stream.error == MAD_ERROR_BUFLEN) {
slouken@357
   152
	  return 0;
slouken@357
   153
	  
slouken@357
   154
	} else {
slouken@357
   155
	  mp3_mad->status |= MS_decode_error;
slouken@357
   156
	  return 0;
slouken@357
   157
	}
slouken@357
   158
  }
slouken@357
   159
  
slouken@357
   160
  mp3_mad->frames_read++;
slouken@357
   161
  mad_timer_add(&mp3_mad->next_frame_start, mp3_mad->frame.header.duration);
slouken@357
   162
slouken@357
   163
  return 1;
slouken@357
   164
}
slouken@357
   165
slouken@357
   166
/* Scale a MAD sample to 16 bits for output. */
slouken@357
   167
static signed int
slouken@357
   168
scale(mad_fixed_t sample) {
slouken@357
   169
  /* round */
slouken@357
   170
  sample += (1L << (MAD_F_FRACBITS - 16));
slouken@357
   171
slouken@357
   172
  /* clip */
slouken@357
   173
  if (sample >= MAD_F_ONE)
slouken@357
   174
    sample = MAD_F_ONE - 1;
slouken@357
   175
  else if (sample < -MAD_F_ONE)
slouken@357
   176
    sample = -MAD_F_ONE;
slouken@357
   177
slouken@357
   178
  /* quantize */
slouken@357
   179
  return sample >> (MAD_F_FRACBITS + 1 - 16);
slouken@357
   180
}
slouken@357
   181
slouken@357
   182
/* Once the frame has been read, copies its samples into the output
slouken@357
   183
   buffer. */
slouken@357
   184
static void
slouken@357
   185
decode_frame(mad_data *mp3_mad) {
slouken@357
   186
  struct mad_pcm *pcm;
slouken@357
   187
  unsigned int nchannels, nsamples;
slouken@357
   188
  mad_fixed_t const *left_ch, *right_ch;
slouken@357
   189
  unsigned char *out;
slouken@357
   190
  int ret;
slouken@357
   191
slouken@357
   192
  mad_synth_frame(&mp3_mad->synth, &mp3_mad->frame);
slouken@357
   193
  pcm = &mp3_mad->synth.pcm;
slouken@357
   194
  out = mp3_mad->output_buffer + mp3_mad->output_end;
slouken@357
   195
slouken@357
   196
  if ((mp3_mad->status & MS_cvt_decoded) == 0) {
slouken@357
   197
	mp3_mad->status |= MS_cvt_decoded;
slouken@357
   198
slouken@357
   199
	/* The first frame determines some key properties of the stream.
slouken@357
   200
	   In particular, it tells us enough to set up the convert
slouken@357
   201
	   structure now. */
slouken@357
   202
	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);
slouken@357
   203
  }
slouken@357
   204
slouken@357
   205
  /* pcm->samplerate contains the sampling frequency */
slouken@357
   206
slouken@357
   207
  nchannels = pcm->channels;
slouken@357
   208
  nsamples  = pcm->length;
slouken@357
   209
  left_ch   = pcm->samples[0];
slouken@357
   210
  right_ch  = pcm->samples[1];
slouken@357
   211
slouken@357
   212
  while (nsamples--) {
slouken@357
   213
    signed int sample;
slouken@357
   214
slouken@357
   215
    /* output sample(s) in 16-bit signed little-endian PCM */
slouken@357
   216
slouken@357
   217
    sample = scale(*left_ch++);
slouken@357
   218
    *out++ = ((sample >> 0) & 0xff);
slouken@357
   219
    *out++ = ((sample >> 8) & 0xff);
slouken@357
   220
slouken@357
   221
    if (nchannels == 2) {
slouken@357
   222
      sample = scale(*right_ch++);
slouken@357
   223
      *out++ = ((sample >> 0) & 0xff);
slouken@357
   224
      *out++ = ((sample >> 8) & 0xff);
slouken@357
   225
    }
slouken@357
   226
  }
slouken@357
   227
slouken@357
   228
  mp3_mad->output_end = out - mp3_mad->output_buffer;
slouken@357
   229
  /*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/
slouken@357
   230
}
slouken@357
   231
slouken@407
   232
int
slouken@357
   233
mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len) {
slouken@357
   234
  int bytes_remaining;
slouken@357
   235
  int num_bytes;
slouken@357
   236
  Uint8 *out;
slouken@357
   237
slouken@357
   238
  if ((mp3_mad->status & MS_playing) == 0) {
slouken@357
   239
	/* We're not supposed to be playing, so send silence instead. */
slouken@357
   240
	memset(stream, 0, len);
slouken@357
   241
	return;
slouken@357
   242
  }
slouken@357
   243
slouken@357
   244
  out = stream;
slouken@357
   245
  bytes_remaining = len;
slouken@357
   246
  while (bytes_remaining > 0) {
slouken@357
   247
	if (mp3_mad->output_end == mp3_mad->output_begin) {
slouken@357
   248
	  /* We need to get a new frame. */
slouken@357
   249
	  mp3_mad->output_begin = 0;
slouken@357
   250
	  mp3_mad->output_end = 0;
slouken@357
   251
	  if (!read_next_frame(mp3_mad)) {
slouken@357
   252
		if ((mp3_mad->status & MS_error_flags) != 0) {
slouken@357
   253
		  /* Couldn't read a frame; either an error condition or
slouken@357
   254
			 end-of-file.  Stop. */
slouken@357
   255
		  memset(out, 0, bytes_remaining);
slouken@357
   256
		  mp3_mad->status &= ~MS_playing;
slouken@407
   257
		  return bytes_remaining;
slouken@357
   258
		}
slouken@357
   259
	  } else {
slouken@357
   260
		decode_frame(mp3_mad);
slouken@357
   261
slouken@357
   262
		/* Now convert the frame data to the appropriate format for
slouken@357
   263
		   output. */
slouken@357
   264
		mp3_mad->cvt.buf = mp3_mad->output_buffer;
slouken@357
   265
		mp3_mad->cvt.len = mp3_mad->output_end;
slouken@357
   266
		
slouken@357
   267
		mp3_mad->output_end = (int)(mp3_mad->output_end * mp3_mad->cvt.len_ratio);
slouken@357
   268
		/*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/
slouken@357
   269
		SDL_ConvertAudio(&mp3_mad->cvt);
slouken@357
   270
	  }
slouken@357
   271
	}
slouken@357
   272
slouken@357
   273
	num_bytes = mp3_mad->output_end - mp3_mad->output_begin;
slouken@357
   274
	if (bytes_remaining < num_bytes) {
slouken@357
   275
	  num_bytes = bytes_remaining;
slouken@357
   276
	}
slouken@357
   277
slouken@357
   278
	if (mp3_mad->volume == MIX_MAX_VOLUME) {
slouken@357
   279
	  memcpy(out, mp3_mad->output_buffer + mp3_mad->output_begin, num_bytes);
slouken@357
   280
	} else {
slouken@357
   281
	  SDL_MixAudio(out, mp3_mad->output_buffer + mp3_mad->output_begin,
slouken@357
   282
				   num_bytes, mp3_mad->volume);
slouken@357
   283
	}
slouken@357
   284
	out += num_bytes;
slouken@357
   285
	mp3_mad->output_begin += num_bytes;
slouken@357
   286
	bytes_remaining -= num_bytes;
slouken@357
   287
  }
slouken@407
   288
  return 0;
slouken@357
   289
}
slouken@357
   290
slouken@357
   291
void
slouken@357
   292
mad_seek(mad_data *mp3_mad, double position) {
slouken@357
   293
  mad_timer_t target;
slouken@357
   294
  int int_part;
slouken@357
   295
slouken@357
   296
  int_part = (int)position;
slouken@357
   297
  mad_timer_set(&target, int_part, 
slouken@357
   298
				(int)((position - int_part) * 1000000), 1000000);
slouken@357
   299
slouken@357
   300
  if (mad_timer_compare(mp3_mad->next_frame_start, target) > 0) {
slouken@357
   301
	/* In order to seek backwards in a VBR file, we have to rewind and
slouken@357
   302
	   start again from the beginning.  This isn't necessary if the
slouken@357
   303
	   file happens to be CBR, of course; in that case we could seek
slouken@357
   304
	   directly to the frame we want.  But I leave that little
slouken@357
   305
	   optimization for the future developer who discovers she really
slouken@357
   306
	   needs it. */
slouken@357
   307
	mp3_mad->frames_read = 0;
slouken@357
   308
	mad_timer_reset(&mp3_mad->next_frame_start);
slouken@357
   309
	mp3_mad->status &= ~MS_error_flags;
slouken@357
   310
	mp3_mad->output_begin = 0;
slouken@357
   311
	mp3_mad->output_end = 0;
slouken@357
   312
slouken@473
   313
	SDL_RWseek(mp3_mad->rw, 0, RW_SEEK_SET);
slouken@357
   314
  }
slouken@357
   315
slouken@357
   316
  /* Now we have to skip frames until we come to the right one.
slouken@357
   317
	 Again, only truly necessary if the file is VBR. */
slouken@357
   318
  while (mad_timer_compare(mp3_mad->next_frame_start, target) < 0) {
slouken@357
   319
	if (!read_next_frame(mp3_mad)) {
slouken@357
   320
	  if ((mp3_mad->status & MS_error_flags) != 0) {
slouken@357
   321
		/* Couldn't read a frame; either an error condition or
slouken@357
   322
		   end-of-file.  Stop. */
slouken@357
   323
		mp3_mad->status &= ~MS_playing;
slouken@357
   324
		return;
slouken@357
   325
	  }
slouken@357
   326
	}
slouken@357
   327
  }
slouken@357
   328
slouken@357
   329
  /* Here we are, at the beginning of the frame that contains the
slouken@357
   330
	 target time.  Ehh, I say that's close enough.  If we wanted to,
slouken@357
   331
	 we could get more precise by decoding the frame now and counting
slouken@357
   332
	 the appropriate number of samples out of it. */
slouken@357
   333
}
slouken@357
   334
slouken@357
   335
void
slouken@357
   336
mad_setVolume(mad_data *mp3_mad, int volume) {
slouken@357
   337
  mp3_mad->volume = volume;
slouken@357
   338
}
slouken@357
   339
slouken@357
   340
slouken@357
   341
#endif  /* MP3_MAD_MUSIC */