effects_internal.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 15 Jul 2007 15:43:02 +0000
changeset 359 83eaf245b7e7
parent 339 30996c5fd448
child 386 695494546b3c
permissions -rw-r--r--
On shutdown, deallocate global memory we might have allocated for effects.

Partially fixes Bugzilla #385.
slouken@113
     1
/*
slouken@138
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@241
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@113
     4
slouken@113
     5
    This library is free software; you can redistribute it and/or
slouken@113
     6
    modify it under the terms of the GNU Library General Public
slouken@113
     7
    License as published by the Free Software Foundation; either
slouken@113
     8
    version 2 of the License, or (at your option) any later version.
slouken@113
     9
slouken@113
    10
    This library is distributed in the hope that it will be useful,
slouken@113
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@113
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@113
    13
    Library General Public License for more details.
slouken@113
    14
slouken@113
    15
    You should have received a copy of the GNU Library General Public
slouken@113
    16
    License along with this library; if not, write to the Free
slouken@113
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@113
    18
icculus@339
    19
    This file by Ryan C. Gordon (icculus@icculus.org)
slouken@113
    20
slouken@113
    21
    These are some helper functions for the internal mixer special effects.
slouken@113
    22
*/
slouken@113
    23
slouken@140
    24
/* $Id$ */
slouken@113
    25
slouken@113
    26
slouken@113
    27
     /* ------ These are used internally only. Don't touch. ------ */
slouken@113
    28
slouken@113
    29
slouken@113
    30
slouken@113
    31
#include <stdio.h>
slouken@113
    32
#include <stdlib.h>
slouken@113
    33
#include "SDL_mixer.h"
slouken@113
    34
icculus@359
    35
#define __MIX_INTERNAL_EFFECT__
icculus@359
    36
#include "effects_internal.h"
icculus@359
    37
slouken@113
    38
/* Should we favor speed over memory usage and/or quality of output? */
slouken@113
    39
int _Mix_effects_max_speed = 0;
slouken@113
    40
slouken@113
    41
slouken@113
    42
void _Mix_InitEffects(void)
slouken@113
    43
{
slouken@113
    44
    _Mix_effects_max_speed = (getenv(MIX_EFFECTSMAXSPEED) != NULL);
slouken@113
    45
}
slouken@113
    46
icculus@359
    47
void _Mix_DeinitEffects(void)
icculus@359
    48
{
icculus@359
    49
    _Eff_PositionDeinit();
icculus@359
    50
}
icculus@359
    51
slouken@113
    52
slouken@113
    53
void *_Eff_volume_table = NULL;
slouken@113
    54
slouken@113
    55
slouken@113
    56
/* Build the volume table for Uint8-format samples.
slouken@113
    57
 *
slouken@113
    58
 * Each column of the table is a possible sample, while each row of the
slouken@113
    59
 *  table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
slouken@113
    60
 *  volume. So _Eff_volume_table[128][mysample] would be the value of
slouken@113
    61
 *  mysample, at half volume.
slouken@113
    62
 */
slouken@113
    63
void *_Eff_build_volume_table_u8(void)
slouken@113
    64
{
slouken@113
    65
    int volume;
slouken@113
    66
    int sample;
slouken@113
    67
    Uint8 *rc;
slouken@113
    68
slouken@113
    69
    if (!_Mix_effects_max_speed) {
slouken@113
    70
        return(NULL);
slouken@113
    71
    }
slouken@113
    72
slouken@113
    73
    if (!_Eff_volume_table) {
slouken@113
    74
        rc = malloc(256 * 256);
slouken@113
    75
        if (rc) {
slouken@113
    76
            _Eff_volume_table = (void *) rc;
slouken@113
    77
            for (volume = 0; volume < 256; volume++) {
icculus@201
    78
                for (sample = -128; sample < 128; sample ++) {
icculus@201
    79
                    *rc = (Uint8)(((float) sample) * ((float) volume / 255.0)) 
icculus@201
    80
                        + 128;
slouken@113
    81
                    rc++;
slouken@113
    82
                }
slouken@113
    83
            }
slouken@113
    84
        }
slouken@113
    85
    }
slouken@113
    86
slouken@113
    87
    return(_Eff_volume_table);
slouken@113
    88
}
slouken@113
    89
slouken@113
    90
slouken@113
    91
/* Build the volume table for Sint8-format samples.
slouken@113
    92
 *
slouken@113
    93
 * Each column of the table is a possible sample, while each row of the
slouken@113
    94
 *  table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
icculus@201
    95
 *  volume. So _Eff_volume_table[128][mysample+128] would be the value of
slouken@113
    96
 *  mysample, at half volume.
slouken@113
    97
 */
slouken@113
    98
void *_Eff_build_volume_table_s8(void)
slouken@113
    99
{
slouken@113
   100
    int volume;
slouken@113
   101
    int sample;
slouken@113
   102
    Sint8 *rc;
slouken@113
   103
slouken@113
   104
    if (!_Eff_volume_table) {
slouken@113
   105
        rc = malloc(256 * 256);
slouken@113
   106
        if (rc) {
slouken@113
   107
            _Eff_volume_table = (void *) rc;
slouken@113
   108
            for (volume = 0; volume < 256; volume++) {
icculus@201
   109
                for (sample = -128; sample < 128; sample ++) {
slouken@113
   110
                    *rc = (Sint8)(((float) sample) * ((float) volume / 255.0));
slouken@113
   111
                    rc++;
slouken@113
   112
                }
slouken@113
   113
            }
slouken@113
   114
        }
slouken@113
   115
    }
slouken@113
   116
slouken@113
   117
    return(_Eff_volume_table);
slouken@113
   118
}
slouken@113
   119
slouken@113
   120
slouken@113
   121
/* end of effects.c ... */
slouken@113
   122