effects_internal.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 11 Sep 2001 19:14:36 +0000
changeset 113 c0c3018bd787
child 138 4d0dc6b4985d
permissions -rw-r--r--
Ryan C. Gordon - Tue Sep 11 12:05:54 PDT 2001
* Reworked playwave.c to make it more useful as a mixer testbed
* Added a realtime sound effect API to SDL_mixer.h
* Added the following standard sound effects:
panning, distance attenuation, basic positional audio, stereo reversal
* Added API for mixer versioning: Mix_Linked_Version() and MIX_VERSION()
slouken@113
     1
/*
slouken@113
     2
    MIXERLIB:  An audio mixer library based on the SDL library
slouken@113
     3
    Copyright (C) 1997-1999  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@113
    24
slouken@113
    25
slouken@113
    26
     /* ------ These are used internally only. Don't touch. ------ */
slouken@113
    27
slouken@113
    28
slouken@113
    29
slouken@113
    30
#include <stdio.h>
slouken@113
    31
#include <stdlib.h>
slouken@113
    32
#include "SDL_mixer.h"
slouken@113
    33
slouken@113
    34
/* Should we favor speed over memory usage and/or quality of output? */
slouken@113
    35
int _Mix_effects_max_speed = 0;
slouken@113
    36
slouken@113
    37
slouken@113
    38
void _Mix_InitEffects(void)
slouken@113
    39
{
slouken@113
    40
    _Mix_effects_max_speed = (getenv(MIX_EFFECTSMAXSPEED) != NULL);
slouken@113
    41
}
slouken@113
    42
slouken@113
    43
slouken@113
    44
void *_Eff_volume_table = NULL;
slouken@113
    45
slouken@113
    46
slouken@113
    47
/* Build the volume table for Uint8-format samples.
slouken@113
    48
 *
slouken@113
    49
 * Each column of the table is a possible sample, while each row of the
slouken@113
    50
 *  table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
slouken@113
    51
 *  volume. So _Eff_volume_table[128][mysample] would be the value of
slouken@113
    52
 *  mysample, at half volume.
slouken@113
    53
 */
slouken@113
    54
void *_Eff_build_volume_table_u8(void)
slouken@113
    55
{
slouken@113
    56
    int volume;
slouken@113
    57
    int sample;
slouken@113
    58
    Uint8 *rc;
slouken@113
    59
slouken@113
    60
    if (!_Mix_effects_max_speed) {
slouken@113
    61
        return(NULL);
slouken@113
    62
    }
slouken@113
    63
slouken@113
    64
    if (!_Eff_volume_table) {
slouken@113
    65
        rc = malloc(256 * 256);
slouken@113
    66
        if (rc) {
slouken@113
    67
            _Eff_volume_table = (void *) rc;
slouken@113
    68
            for (volume = 0; volume < 256; volume++) {
slouken@113
    69
                for (sample = 0; sample < 256; sample ++) {
slouken@113
    70
                    *rc = (Uint8)(((float) sample) * ((float) volume / 255.0));
slouken@113
    71
                    rc++;
slouken@113
    72
                }
slouken@113
    73
            }
slouken@113
    74
        }
slouken@113
    75
    }
slouken@113
    76
slouken@113
    77
    return(_Eff_volume_table);
slouken@113
    78
}
slouken@113
    79
slouken@113
    80
slouken@113
    81
/* Build the volume table for Sint8-format samples.
slouken@113
    82
 *
slouken@113
    83
 * Each column of the table is a possible sample, while each row of the
slouken@113
    84
 *  table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
slouken@113
    85
 *  volume. So _Eff_volume_table[128][mysample] would be the value of
slouken@113
    86
 *  mysample, at half volume.
slouken@113
    87
 */
slouken@113
    88
void *_Eff_build_volume_table_s8(void)
slouken@113
    89
{
slouken@113
    90
    int volume;
slouken@113
    91
    int sample;
slouken@113
    92
    Sint8 *rc;
slouken@113
    93
slouken@113
    94
    if (!_Eff_volume_table) {
slouken@113
    95
        rc = malloc(256 * 256);
slouken@113
    96
        if (rc) {
slouken@113
    97
            _Eff_volume_table = (void *) rc;
slouken@113
    98
            for (volume = 0; volume < 256; volume++) {
slouken@113
    99
                for (sample = 0; sample < 256; sample ++) {
slouken@113
   100
                    *rc = (Sint8)(((float) sample) * ((float) volume / 255.0));
slouken@113
   101
                    rc++;
slouken@113
   102
                }
slouken@113
   103
            }
slouken@113
   104
        }
slouken@113
   105
    }
slouken@113
   106
slouken@113
   107
    return(_Eff_volume_table);
slouken@113
   108
}
slouken@113
   109
slouken@113
   110
slouken@113
   111
/* end of effects.c ... */
slouken@113
   112