effects_internal.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 13 Jan 2012 03:15:19 -0500
changeset 561 87bdb4c81c0b
parent 518 8bc9b5fd2aae
child 601 05123263dab3
permissions -rw-r--r--
Fixed memory crash loading Ogg Vorbis files on Windows
The pointer to the audio data could come from SDL_LoadWAV_RW() or from our other loaders, and we need to use the correct free() to release the memory. So we'll just use the SDL memory functions for consistency.
This pretty much only matters on Windows where we can't guarantee a certain C runtime so we provide our own malloc() and friends.
slouken@113
     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@113
     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@113
     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@113
    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@113
    20
slouken@518
    21
  This file by Ryan C. Gordon (icculus@icculus.org)
slouken@113
    22
slouken@518
    23
  These are some helper functions for the internal mixer special effects.
slouken@113
    24
*/
slouken@113
    25
slouken@140
    26
/* $Id$ */
slouken@113
    27
slouken@113
    28
slouken@113
    29
     /* ------ These are used internally only. Don't touch. ------ */
slouken@113
    30
slouken@113
    31
slouken@113
    32
slouken@113
    33
#include <stdio.h>
slouken@113
    34
#include <stdlib.h>
slouken@113
    35
#include "SDL_mixer.h"
slouken@113
    36
icculus@359
    37
#define __MIX_INTERNAL_EFFECT__
icculus@359
    38
#include "effects_internal.h"
icculus@359
    39
slouken@113
    40
/* Should we favor speed over memory usage and/or quality of output? */
slouken@113
    41
int _Mix_effects_max_speed = 0;
slouken@113
    42
slouken@113
    43
slouken@113
    44
void _Mix_InitEffects(void)
slouken@113
    45
{
slouken@488
    46
    _Mix_effects_max_speed = (SDL_getenv(MIX_EFFECTSMAXSPEED) != NULL);
slouken@113
    47
}
slouken@113
    48
icculus@359
    49
void _Mix_DeinitEffects(void)
icculus@359
    50
{
icculus@359
    51
    _Eff_PositionDeinit();
icculus@359
    52
}
icculus@359
    53
slouken@113
    54
slouken@113
    55
void *_Eff_volume_table = NULL;
slouken@113
    56
slouken@113
    57
slouken@113
    58
/* Build the volume table for Uint8-format samples.
slouken@113
    59
 *
slouken@113
    60
 * Each column of the table is a possible sample, while each row of the
slouken@113
    61
 *  table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
slouken@113
    62
 *  volume. So _Eff_volume_table[128][mysample] would be the value of
slouken@113
    63
 *  mysample, at half volume.
slouken@113
    64
 */
slouken@113
    65
void *_Eff_build_volume_table_u8(void)
slouken@113
    66
{
slouken@113
    67
    int volume;
slouken@113
    68
    int sample;
slouken@113
    69
    Uint8 *rc;
slouken@113
    70
slouken@113
    71
    if (!_Mix_effects_max_speed) {
slouken@113
    72
        return(NULL);
slouken@113
    73
    }
slouken@113
    74
slouken@113
    75
    if (!_Eff_volume_table) {
slouken@561
    76
        rc = SDL_malloc(256 * 256);
slouken@113
    77
        if (rc) {
slouken@113
    78
            _Eff_volume_table = (void *) rc;
slouken@113
    79
            for (volume = 0; volume < 256; volume++) {
icculus@201
    80
                for (sample = -128; sample < 128; sample ++) {
icculus@201
    81
                    *rc = (Uint8)(((float) sample) * ((float) volume / 255.0)) 
icculus@201
    82
                        + 128;
slouken@113
    83
                    rc++;
slouken@113
    84
                }
slouken@113
    85
            }
slouken@113
    86
        }
slouken@113
    87
    }
slouken@113
    88
slouken@113
    89
    return(_Eff_volume_table);
slouken@113
    90
}
slouken@113
    91
slouken@113
    92
slouken@113
    93
/* Build the volume table for Sint8-format samples.
slouken@113
    94
 *
slouken@113
    95
 * Each column of the table is a possible sample, while each row of the
slouken@113
    96
 *  table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
icculus@201
    97
 *  volume. So _Eff_volume_table[128][mysample+128] would be the value of
slouken@113
    98
 *  mysample, at half volume.
slouken@113
    99
 */
slouken@113
   100
void *_Eff_build_volume_table_s8(void)
slouken@113
   101
{
slouken@113
   102
    int volume;
slouken@113
   103
    int sample;
slouken@113
   104
    Sint8 *rc;
slouken@113
   105
slouken@113
   106
    if (!_Eff_volume_table) {
slouken@561
   107
        rc = SDL_malloc(256 * 256);
slouken@113
   108
        if (rc) {
slouken@113
   109
            _Eff_volume_table = (void *) rc;
slouken@113
   110
            for (volume = 0; volume < 256; volume++) {
icculus@201
   111
                for (sample = -128; sample < 128; sample ++) {
slouken@113
   112
                    *rc = (Sint8)(((float) sample) * ((float) volume / 255.0));
slouken@113
   113
                    rc++;
slouken@113
   114
                }
slouken@113
   115
            }
slouken@113
   116
        }
slouken@113
   117
    }
slouken@113
   118
slouken@113
   119
    return(_Eff_volume_table);
slouken@113
   120
}
slouken@113
   121
slouken@113
   122
slouken@113
   123
/* end of effects.c ... */
slouken@113
   124