effects_internal.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 27 Dec 2002 15:13:37 +0000
changeset 201 e55fb6b45559
parent 140 efa15d2a5403
child 241 503416fca921
permissions -rw-r--r--
(From Steven Fuller. --ryan.)

"Hi,

Here's a patch that fixes problems (attenuation on unsigned samples
would cause the samples to fall towards 0 when it should instead fall
towards 128/32768) when doing panning on 8-bit sounds and 16-bit unsigned
samples."
slouken@113
     1
/*
slouken@138
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@138
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  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
slouken@113
    19
    This file by Ryan C. Gordon (icculus@linuxgames.com)
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
slouken@113
    35
/* Should we favor speed over memory usage and/or quality of output? */
slouken@113
    36
int _Mix_effects_max_speed = 0;
slouken@113
    37
slouken@113
    38
slouken@113
    39
void _Mix_InitEffects(void)
slouken@113
    40
{
slouken@113
    41
    _Mix_effects_max_speed = (getenv(MIX_EFFECTSMAXSPEED) != NULL);
slouken@113
    42
}
slouken@113
    43
slouken@113
    44
slouken@113
    45
void *_Eff_volume_table = NULL;
slouken@113
    46
slouken@113
    47
slouken@113
    48
/* Build the volume table for Uint8-format samples.
slouken@113
    49
 *
slouken@113
    50
 * Each column of the table is a possible sample, while each row of the
slouken@113
    51
 *  table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
slouken@113
    52
 *  volume. So _Eff_volume_table[128][mysample] would be the value of
slouken@113
    53
 *  mysample, at half volume.
slouken@113
    54
 */
slouken@113
    55
void *_Eff_build_volume_table_u8(void)
slouken@113
    56
{
slouken@113
    57
    int volume;
slouken@113
    58
    int sample;
slouken@113
    59
    Uint8 *rc;
slouken@113
    60
slouken@113
    61
    if (!_Mix_effects_max_speed) {
slouken@113
    62
        return(NULL);
slouken@113
    63
    }
slouken@113
    64
slouken@113
    65
    if (!_Eff_volume_table) {
slouken@113
    66
        rc = malloc(256 * 256);
slouken@113
    67
        if (rc) {
slouken@113
    68
            _Eff_volume_table = (void *) rc;
slouken@113
    69
            for (volume = 0; volume < 256; volume++) {
icculus@201
    70
                for (sample = -128; sample < 128; sample ++) {
icculus@201
    71
                    *rc = (Uint8)(((float) sample) * ((float) volume / 255.0)) 
icculus@201
    72
                        + 128;
slouken@113
    73
                    rc++;
slouken@113
    74
                }
slouken@113
    75
            }
slouken@113
    76
        }
slouken@113
    77
    }
slouken@113
    78
slouken@113
    79
    return(_Eff_volume_table);
slouken@113
    80
}
slouken@113
    81
slouken@113
    82
slouken@113
    83
/* Build the volume table for Sint8-format samples.
slouken@113
    84
 *
slouken@113
    85
 * Each column of the table is a possible sample, while each row of the
slouken@113
    86
 *  table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
icculus@201
    87
 *  volume. So _Eff_volume_table[128][mysample+128] would be the value of
slouken@113
    88
 *  mysample, at half volume.
slouken@113
    89
 */
slouken@113
    90
void *_Eff_build_volume_table_s8(void)
slouken@113
    91
{
slouken@113
    92
    int volume;
slouken@113
    93
    int sample;
slouken@113
    94
    Sint8 *rc;
slouken@113
    95
slouken@113
    96
    if (!_Eff_volume_table) {
slouken@113
    97
        rc = malloc(256 * 256);
slouken@113
    98
        if (rc) {
slouken@113
    99
            _Eff_volume_table = (void *) rc;
slouken@113
   100
            for (volume = 0; volume < 256; volume++) {
icculus@201
   101
                for (sample = -128; sample < 128; sample ++) {
slouken@113
   102
                    *rc = (Sint8)(((float) sample) * ((float) volume / 255.0));
slouken@113
   103
                    rc++;
slouken@113
   104
                }
slouken@113
   105
            }
slouken@113
   106
        }
slouken@113
   107
    }
slouken@113
   108
slouken@113
   109
    return(_Eff_volume_table);
slouken@113
   110
}
slouken@113
   111
slouken@113
   112
slouken@113
   113
/* end of effects.c ... */
slouken@113
   114