music_mad.h
author Sam Lantinga <slouken@libsdl.org>
Sat, 23 Aug 2014 10:57:26 -0700
changeset 696 9599bb0ff844
parent 630 39fbc5a85070
child 711 f40c5ac95b12
permissions -rw-r--r--
Fixed bug 2690 - Floating point exception in Mix_Volume()

Francisco de la Peña

This happens rarely under uncertain circunstances, as it doesn't crash always and does it randomly. Fortunately, I've got a backtrace when running a GDB session. Might be a divide by zero issue in SDL or SDL_mixer.

Program received signal SIGFPE, Arithmetic exception.
0x00000038e8c08a7d in mix_channels (udata=<optimized out>, stream=0x24e11b0 "", len=2048) at mixer.c:345
345 Mix_Volume(i, (mix_channel[i].fade_volume * ticks) / mix_channel[i].fade_length );
(gdb) bt f
#0 0x00000038e8c08a7d in mix_channels (udata=<optimized out>, stream=0x24e11b0 "", len=2048) at mixer.c:345
ticks = 0
mix_input = <optimized out>
i = 0
mixable = <optimized out>
volume = <optimized out>
sdl_ticks = 129373

Using SDL 2.0.3 and SDL_Mixer 2.0.0, PulseAudio, Fedora 20 x86_64 but looks like can be reproduced on other platforms.
slouken@357
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@601
     3
  Copyright (C) 1997-2013 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 "mad.h"
slouken@357
    25
#include "SDL_rwops.h"
slouken@357
    26
#include "SDL_audio.h"
slouken@357
    27
#include "SDL_mixer.h"
slouken@357
    28
slouken@617
    29
#define MAD_INPUT_BUFFER_SIZE   (5*8192)
slouken@617
    30
#define MAD_OUTPUT_BUFFER_SIZE  8192
slouken@357
    31
slouken@357
    32
enum {
slouken@357
    33
  MS_input_eof    = 0x0001,
slouken@357
    34
  MS_input_error  = 0x0001,
slouken@357
    35
  MS_decode_eof   = 0x0002,
slouken@357
    36
  MS_decode_error = 0x0004,
slouken@357
    37
  MS_error_flags  = 0x000f,
slouken@357
    38
slouken@357
    39
  MS_playing      = 0x0100,
slouken@357
    40
  MS_cvt_decoded  = 0x0200,
slouken@357
    41
};
slouken@357
    42
slouken@357
    43
typedef struct {
slouken@630
    44
  SDL_RWops *src;
slouken@630
    45
  int freesrc;
slouken@357
    46
  struct mad_stream stream;
slouken@357
    47
  struct mad_frame frame;
slouken@357
    48
  struct mad_synth synth;
slouken@357
    49
  int frames_read;
slouken@357
    50
  mad_timer_t next_frame_start;
slouken@357
    51
  int volume;
slouken@357
    52
  int status;
slouken@357
    53
  int output_begin, output_end;
slouken@357
    54
  SDL_AudioSpec mixer;
slouken@357
    55
  SDL_AudioCVT cvt;
slouken@357
    56
slouken@357
    57
  unsigned char input_buffer[MAD_INPUT_BUFFER_SIZE + MAD_BUFFER_GUARD];
slouken@357
    58
  unsigned char output_buffer[MAD_OUTPUT_BUFFER_SIZE];
slouken@357
    59
} mad_data;
slouken@357
    60
slouken@630
    61
mad_data *mad_openFileRW(SDL_RWops *src, SDL_AudioSpec *mixer, int freesrc);
slouken@357
    62
void mad_closeFile(mad_data *mp3_mad);
slouken@357
    63
slouken@357
    64
void mad_start(mad_data *mp3_mad);
slouken@357
    65
void mad_stop(mad_data *mp3_mad);
slouken@357
    66
int mad_isPlaying(mad_data *mp3_mad);
slouken@357
    67
slouken@407
    68
int mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len);
slouken@357
    69
void mad_seek(mad_data *mp3_mad, double position);
slouken@357
    70
void mad_setVolume(mad_data *mp3_mad, int volume);
slouken@357
    71
slouken@357
    72
#endif