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