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 "cydchr.h"
28 #include <math.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 
33 #ifdef CYD_DISABLE_CHORUS_INTERPOLATION
34 # define CHORUS_ACCURACY 1
35 #else
36 # define CHORUS_ACCURACY 256
37 #endif
38 
cydchr_output(CydChorus * chr,Sint32 in_l,Sint32 in_r,Sint32 * out_l,Sint32 * out_r)39 void cydchr_output(CydChorus *chr, Sint32 in_l, Sint32 in_r, Sint32 *out_l, Sint32 *out_r)
40 {
41 	++chr->pos_buf;
42 	if (chr->pos_buf >= chr->buf_size)
43 		chr->pos_buf = 0;
44 
45 	chr->buffer[chr->pos_buf] = in_r;
46 	chr->buffer[chr->pos_buf + chr->buf_size] = in_r;
47 
48 	int acc_l = 0, acc_r = 0;
49 
50 	for (int o = 0 ; o < CYD_CHORUS_OVERSAMPLE ; ++o)
51 	{
52 
53 		++chr->pos_l;
54 		if (chr->pos_l >= chr->lut_size)
55 			chr->pos_l = 0;
56 
57 		++chr->pos_r;
58 		if (chr->pos_r >= chr->lut_size)
59 			chr->pos_r = 0;
60 
61 #ifdef CYD_DISABLE_CHORUS_INTERPOLATION
62 		int a = chr->buffer[(chr->pos_buf - chr->lut[chr->pos_l] + chr->buf_size)];
63 
64 		if (chr->lut_size)
65 			acc_l += a;
66 		else
67 			acc_l += in_l;
68 
69 		a = chr->buffer[(chr->pos_buf - chr->lut[chr->pos_r] + chr->buf_size)];
70 		acc_r += a;
71 #else
72 		int a = chr->buffer[(chr->pos_buf - chr->lut[chr->pos_l] / CHORUS_ACCURACY + chr->buf_size)];
73 		int b = chr->buffer[(chr->pos_buf - chr->lut[chr->pos_l] / CHORUS_ACCURACY - 1 + chr->buf_size)];
74 		int s = chr->lut[chr->pos_l] % CHORUS_ACCURACY;
75 
76 		if (chr->lut_size)
77 			acc_l += a + (b - a) * s / CHORUS_ACCURACY;
78 		else
79 			acc_l += in_l;
80 
81 		a = chr->buffer[(chr->pos_buf - chr->lut[chr->pos_r] / CHORUS_ACCURACY + chr->buf_size)];
82 		b = chr->buffer[(chr->pos_buf - chr->lut[chr->pos_r] / CHORUS_ACCURACY - 1 + chr->buf_size)];
83 		s = chr->lut[chr->pos_r] % CHORUS_ACCURACY;
84 		acc_r += a + (b - a) * s / CHORUS_ACCURACY;
85 #endif
86 	}
87 
88 	*out_l = acc_l / CYD_CHORUS_OVERSAMPLE;
89 	*out_r = acc_r / CYD_CHORUS_OVERSAMPLE;
90 }
91 
92 
cydchr_set(CydChorus * chr,int rate,int min_delay,int max_delay,int stereo_separation)93 void cydchr_set(CydChorus *chr, int rate, int min_delay, int max_delay, int stereo_separation)
94 {
95 #ifdef STEREOOUTPUT
96 	if (rate)
97 	{
98 		int old = chr->lut_size;
99 		chr->lut_size = CYD_CHORUS_OVERSAMPLE * chr->sample_rate * 4 * 10 / (10 + (rate - 1));
100 
101 		chr->pos_l = 0;
102 		chr->pos_r = (stereo_separation * chr->lut_size / 2 / 64) % chr->lut_size;
103 
104 		if (old == chr->lut_size && min_delay == chr->min_delay && chr->max_delay == max_delay) return;
105 
106 		chr->min_delay = min_delay;
107 		chr->max_delay = max_delay;
108 
109 		for (int i = 0 ; i < chr->lut_size ; ++i)
110 			chr->lut[i] = (int)(((sin((double)i / chr->lut_size * M_PI * 2) * 0.5 + 0.5) * (max_delay - min_delay) + min_delay) * CHORUS_ACCURACY * chr->sample_rate / 10000) % (chr->buf_size * CHORUS_ACCURACY);
111 	}
112 	else
113 	{
114 		chr->pos_l = 0;
115 		chr->pos_r = 0;
116 		chr->lut_size = 0;
117 		chr->lut[0] = chr->sample_rate * min_delay / 10000;
118 	}
119 #endif
120 }
121 
122 
cydchr_init(CydChorus * chr,int sample_rate)123 void cydchr_init(CydChorus *chr, int sample_rate)
124 {
125 	memset(chr, 0, sizeof(*chr));
126 	chr->sample_rate = sample_rate;
127 	chr->buf_size = sample_rate * CYDCHR_SIZE / 1000;
128 	chr->buffer = calloc(chr->buf_size, sizeof(chr->buffer[0]) * 2);
129 	chr->lut = calloc(sample_rate * 4 * CYD_CHORUS_OVERSAMPLE, sizeof(chr->buffer[0]));
130 	chr->lut_size = 0;
131 }
132 
133 
cydchr_deinit(CydChorus * chr)134 void cydchr_deinit(CydChorus *chr)
135 {
136 	free(chr->buffer);
137 	free(chr->lut);
138 }
139 
140