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 "cyddefs.h"
27 #include "cydrvb.h"
28 #include "macros.h"
29 #include <math.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
cydrvb_init(CydReverb * rvb,int rate)33 void cydrvb_init(CydReverb *rvb, int rate)
34 {
35 	memset(rvb, 0, sizeof(*rvb));
36 
37 	int bufsize = CYDRVB_SIZE * rate / 1000;
38 
39 	rvb->size = bufsize;
40 	rvb->rate = rate;
41 #ifdef STEREOOUTPUT
42 	rvb->buffer = calloc(sizeof(*rvb->buffer) * 2, bufsize);
43 #else
44 	rvb->buffer = calloc(sizeof(*rvb->buffer), bufsize);
45 #endif
46 
47 	for (int i = 0 ; i < CYDRVB_TAPS ; ++i)
48 		cydrvb_set_tap(rvb, i, i * 100 + 50, (i + 1) * -30, CYD_PAN_CENTER);
49 }
50 
51 
cydrvb_deinit(CydReverb * rvb)52 void cydrvb_deinit(CydReverb *rvb)
53 {
54 	free(rvb->buffer);
55 	rvb->buffer = NULL;
56 }
57 
58 
59 #ifdef STEREOOUTPUT
cydrvb_cycle(CydReverb * rvb,Sint32 left,Sint32 right)60 void cydrvb_cycle(CydReverb *rvb, Sint32 left, Sint32 right)
61 {
62 	for (int i = 0 ; i < CYDRVB_TAPS ; ++i)
63 	{
64 		++rvb->tap[i].position;
65 		if (rvb->tap[i].position >= rvb->size)
66 			rvb->tap[i].position = 0;
67 	}
68 
69 	++rvb->position;
70 	if (rvb->position >= rvb->size)
71 		rvb->position = 0;
72 
73 	rvb->buffer[rvb->position * 2] = left;
74 	rvb->buffer[rvb->position * 2 + 1] = right;
75 }
76 
77 #else
78 
cydrvb_cycle(CydReverb * rvb,Sint32 input)79 void cydrvb_cycle(CydReverb *rvb, Sint32 input)
80 {
81 	for (int i = 0 ; i < CYDRVB_TAPS ; ++i)
82 	{
83 		++rvb->tap[i].position;
84 		if (rvb->tap[i].position >= rvb->size)
85 			rvb->tap[i].position = 0;
86 	}
87 
88 	++rvb->position;
89 	if (rvb->position >= rvb->size)
90 		rvb->position = 0;
91 
92 	rvb->buffer[rvb->position] = input;
93 }
94 #endif
95 
96 #ifdef STEREOOUTPUT
cydrvb_output(CydReverb * rvb,Sint32 * left,Sint32 * right)97 void cydrvb_output(CydReverb *rvb, Sint32 *left, Sint32 *right)
98 {
99 	*left = 0;
100 	*right = 0;
101 
102 	for (int i = 0 ; i < CYDRVB_TAPS ; ++i)
103 	{
104 		if (rvb->tap[i].gain_l != 0 || rvb->tap[i].gain_r != 0)
105 		{
106 			*left += rvb->tap[i].gain_l * (Sint32)rvb->buffer[rvb->tap[i].position * 2] / CYDRVB_0dB;
107 			*right += rvb->tap[i].gain_r * (Sint32)rvb->buffer[rvb->tap[i].position * 2 + 1] / CYDRVB_0dB;
108 		}
109 	}
110 }
111 
112 #else
113 
cydrvb_output(CydReverb * rvb)114 Sint32 cydrvb_output(CydReverb *rvb)
115 {
116 	Sint32 o = 0;
117 
118 	for (int i = 0 ; i < CYDRVB_TAPS ; ++i)
119 	{
120 		if (rvb->tap[i].gain != 0)
121 		{
122 			o += rvb->tap[i].gain * rvb->buffer[rvb->tap[i].position] / CYDRVB_0dB;
123 		}
124 	}
125 
126 	return o;
127 }
128 #endif
129 
130 
cydrvb_set_tap(CydReverb * rvb,int idx,int delay_ms,int gain_db,int panning)131 void cydrvb_set_tap(CydReverb *rvb, int idx, int delay_ms, int gain_db, int panning)
132 {
133 	rvb->tap[idx].delay = delay_ms;
134 	rvb->tap[idx].position = (rvb->position - (delay_ms * rvb->rate / 1000) + rvb->size) % rvb->size;
135 
136 	if (gain_db <= CYDRVB_LOW_LIMIT)
137 	{
138 #ifdef STEREOOUTPUT
139 
140 		rvb->tap[idx].gain_l = 0;
141 		rvb->tap[idx].gain_r = 0;
142 #else
143                 rvb->tap[idx].gain = 0;
144 #endif
145 	}
146 	else
147 	{
148 #ifdef STEREOOUTPUT
149 		panning = my_min(CYD_PAN_RIGHT, my_max(CYD_PAN_LEFT, panning));
150 		float a = M_PI / 2 * (float)(panning - CYD_PAN_LEFT) / (CYD_PAN_RIGHT - CYD_PAN_LEFT);
151 		int gain = pow(10.0, (double)gain_db * 0.01) * CYDRVB_0dB;
152 		rvb->tap[idx].gain_l = gain * cos(a);
153 		rvb->tap[idx].gain_r = gain * sin(a);
154 #else
155 		int gain = pow(10.0, (double)gain_db * 0.01) * CYDRVB_0dB;
156 		rvb->tap[idx].gain = gain;
157 #endif
158 	}
159 }
160