1 /*
2 Copyright (c) 2009-2010 Tero Lindeman (kometbomb)
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
12
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "cydfx.h"
27 #include "cyd.h"
28
29 #ifdef STEREOOUTPUT
cydfx_output(CydFx * fx,Sint32 fx_l,Sint32 fx_r,Sint32 * left,Sint32 * right)30 void cydfx_output(CydFx *fx, Sint32 fx_l, Sint32 fx_r, Sint32 *left, Sint32 *right)
31 {
32 *left = fx_l;
33 *right = fx_r;
34 #else
35 Sint32 cydfx_output(CydFx *fx, Sint32 fx_input)
36 {
37 Sint32 v = fx_input;
38 #endif
39
40 #ifndef CYD_DISABLE_FX
41
42 if (fx->flags & CYDFX_ENABLE_CHORUS)
43 {
44 #ifdef STEREOOUTPUT
45 cydchr_output(&fx->chr, fx_l, fx_r, left, right);
46 #else
47 // it's a stereo effect
48 #endif
49 }
50
51 if (fx->flags & CYDFX_ENABLE_REVERB)
52 {
53 #ifdef STEREOOUTPUT
54 cydrvb_cycle(&fx->rvb, fx_l, fx_l);
55 cydrvb_output(&fx->rvb, &fx_l, &fx_r);
56 *left += fx_l;
57 *right += fx_r;
58 #else
59 cydrvb_cycle(&fx->rvb, fx_input);
60 v = cydrvb_output(&fx->rvb);
61 #endif
62 }
63
64 if (fx->flags & CYDFX_ENABLE_CRUSH)
65 {
66 #ifdef STEREOOUTPUT
67 cydcrush_output(&fx->crush, *left, *right, left, right);
68 #else
69 v = cydcrush_output(&fx->crush, v);
70 #endif
71 }
72
73 #endif // CYD_DISABLE_FX
74
75 #ifndef STEREOOUTPUT
76 return v;
77 #endif
78 }
79
80
81 void cydfx_init(CydFx *fx, int rate)
82 {
83 #ifndef CYD_DISABLE_FX
84
85 cydrvb_init(&fx->rvb, rate);
86 #ifdef STEREOOUTPUT
87 cydchr_init(&fx->chr, rate);
88 #endif
89 cydcrush_init(&fx->crush, rate);
90
91 #endif // CYD_DISABLE_FX
92 }
93
94
95 void cydfx_deinit(CydFx *fx)
96 {
97 #ifndef CYD_DISABLE_FX
98
99 cydrvb_deinit(&fx->rvb);
100 #ifdef STEREOOUTPUT
101 cydchr_deinit(&fx->chr);
102 #endif
103 cydcrush_deinit(&fx->crush);
104
105 #endif // CYD_DISABLE_FX
106 }
107
108
109 void cydfx_set(CydFx *fx, const CydFxSerialized *ser)
110 {
111 #ifndef CYD_DISABLE_FX
112 fx->flags = ser->flags;
113
114 for (int i = 0 ; i < CYDRVB_TAPS ; ++i)
115 {
116 if (ser->rvb.tap[i].flags & 1)
117 cydrvb_set_tap(&fx->rvb, i, ser->rvb.tap[i].delay, ser->rvb.tap[i].gain, ser->rvb.tap[i].panning);
118 else
119 cydrvb_set_tap(&fx->rvb, i, 0, CYDRVB_LOW_LIMIT, 0);
120 }
121
122 cydchr_set(&fx->chr, ser->chr.rate, ser->chr.min_delay, ser->chr.max_delay, ser->chr.sep);
123 cydcrush_set(&fx->crush, ser->crushex.downsample, ser->crush.bit_drop, fx->flags & CYDFX_ENABLE_CRUSH_DITHER, ser->crushex.gain);
124
125 #endif // CYD_DISABLE_FX
126 }
127