1 /*
2   SDL_mixer:  An audio mixer library based on the SDL library
3   Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 
21   This file by Ryan C. Gordon (icculus@icculus.org)
22 
23   These are some helper functions for the internal mixer special effects.
24 */
25 
26 /* $Id$ */
27 
28 
29      /* ------ These are used internally only. Don't touch. ------ */
30 
31 
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include "SDL_mixer.h"
36 
37 #define __MIX_INTERNAL_EFFECT__
38 #include "effects_internal.h"
39 
40 /* Should we favor speed over memory usage and/or quality of output? */
41 int _Mix_effects_max_speed = 0;
42 
43 
_Mix_InitEffects(void)44 void _Mix_InitEffects(void)
45 {
46     _Mix_effects_max_speed = (SDL_getenv(MIX_EFFECTSMAXSPEED) != NULL);
47 }
48 
_Mix_DeinitEffects(void)49 void _Mix_DeinitEffects(void)
50 {
51     _Eff_PositionDeinit();
52 }
53 
54 
55 void *_Eff_volume_table = NULL;
56 
57 
58 /* Build the volume table for Uint8-format samples.
59  *
60  * Each column of the table is a possible sample, while each row of the
61  *  table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
62  *  volume. So _Eff_volume_table[128][mysample] would be the value of
63  *  mysample, at half volume.
64  */
_Eff_build_volume_table_u8(void)65 void *_Eff_build_volume_table_u8(void)
66 {
67     int volume;
68     int sample;
69     Uint8 *rc;
70 
71     if (!_Mix_effects_max_speed) {
72         return(NULL);
73     }
74 
75     if (!_Eff_volume_table) {
76         rc = SDL_malloc(256 * 256);
77         if (rc) {
78             _Eff_volume_table = (void *) rc;
79             for (volume = 0; volume < 256; volume++) {
80                 for (sample = -128; sample < 128; sample ++) {
81                     *rc = (Uint8)(((float) sample) * ((float) volume / 255.0))
82                         + 128;
83                     rc++;
84                 }
85             }
86         }
87     }
88 
89     return(_Eff_volume_table);
90 }
91 
92 
93 /* Build the volume table for Sint8-format samples.
94  *
95  * Each column of the table is a possible sample, while each row of the
96  *  table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
97  *  volume. So _Eff_volume_table[128][mysample+128] would be the value of
98  *  mysample, at half volume.
99  */
_Eff_build_volume_table_s8(void)100 void *_Eff_build_volume_table_s8(void)
101 {
102     int volume;
103     int sample;
104     Sint8 *rc;
105 
106     if (!_Eff_volume_table) {
107         rc = SDL_malloc(256 * 256);
108         if (rc) {
109             _Eff_volume_table = (void *) rc;
110             for (volume = 0; volume < 256; volume++) {
111                 for (sample = -128; sample < 128; sample ++) {
112                     *rc = (Sint8)(((float) sample) * ((float) volume / 255.0));
113                     rc++;
114                 }
115             }
116         }
117     }
118 
119     return(_Eff_volume_table);
120 }
121 
122 
123 /* end of effects.c ... */
124 
125 /* vi: set ts=4 sw=4 expandtab: */
126