-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy paththe_synth.h
More file actions
309 lines (263 loc) · 9.45 KB
/
the_synth.h
File metadata and controls
309 lines (263 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//**************************************************************************
//* Simple AVR wavetable synthesizer V1.0 *
//* *
//* Implements 4 voices using selectable waveform and envelope tables *
//* Uses 8-bit PWM @ 62.5 kHz for audio output *
//* *
//* (C) DZL 2008 *
//**************************************************************************
#include <math.h>
#include "avr/interrupt.h"
#include "avr/pgmspace.h"
#include "sin256.h"
#include "ramp256.h"
#include "saw256.h"
#include "square256.h"
#include "noise256.h"
#include "tria256.h"
#include "env0.h"
#include "env1.h"
#include "env2.h"
#include "env3.h"
#define SET(x,y) (x |=(1<<y)) //-Bit set/clear macros
#define CLR(x,y) (x &= (~(1<<y))) // |
#define CHK(x,y) (x & (1<<y)) // |
#define TOG(x,y) (x^=(1<<y)) //-+
//*********************************************************************
// Audio interrupt
//*********************************************************************
volatile unsigned int PCW[4]={0,0,0,0}; //-Wave phase accumolators
volatile unsigned int FTW[4]={1000,200,300,400}; //-Wave frequency tuning words
volatile unsigned char AMP[4]={255,255,255,255}; //-Wave amplitudes [0-255]
volatile unsigned int PITCH[4]={500,500,500,500}; //-Voice pitch
volatile int MOD[4]={20,0,64,127}; //-Voice envelope modulation [0-1023 512=no mod. <512 pitch down >512 pitch up]
volatile unsigned int wavs[4]; //-Wave table selector [address of wave in memory]
volatile unsigned int envs[4]; //-Envelopte selector [address of envelope in memory]
volatile unsigned int EPCW[4]={0x8000,0x8000,0x8000,0x8000}; //-Envelope phase accumolator
volatile unsigned int EFTW[4]={10,10,10,10}; //-Envelope speed tuning word
volatile unsigned int tim=0; //-Sample counter eg. for sequencer
volatile unsigned int tim2=0; //-Sample counter eg. for sequencer
volatile unsigned char divider=4; //-Sample rate decimator for envelope
volatile unsigned char tick=0;
volatile unsigned char envg=0;
unsigned char synthTick(void)
{
if(tick)
{
tick=0;
return 1;
}
return 0;
}
#define FS 16000.0 //-Sample rate
SIGNAL(TIMER1_COMPA_vect)
{
OCR1A+=2000000/FS; //-Auto sample rate
if(divider)
{
divider--;
}
else
{
//-------------------------------
// Volume envelope generator
//-------------------------------
divider=4;
if(!(EPCW[0]&0x8000))
{
AMP[0]=pgm_read_byte(envs[0]+ ((EPCW[0]+=EFTW[0])>>7) );
if(EPCW[0]&0x8000)
AMP[0]=0;
}
else
AMP[0]=0;
if(!(EPCW[1]&0x8000))
{
AMP[1]=pgm_read_byte(envs[1]+ ((EPCW[1]+=EFTW[1])>>7) );
if(EPCW[1]&0x8000)
AMP[1]=0;
}
else
AMP[1]=0;
if(!(EPCW[2]&0x8000))
{
AMP[2]=pgm_read_byte(envs[2]+ ((EPCW[2]+=EFTW[2])>>7) );
if(EPCW[2]&0x8000)
AMP[2]=0;
}
else
AMP[2]=0;
if(!(EPCW[3]&0x8000))
{
AMP[3]=pgm_read_byte(envs[3]+ ((EPCW[3]+=EFTW[3])>>7) );
if(EPCW[3]&0x8000)
AMP[3]=0;
}
else
AMP[3]=0;
}
//-------------------------------
// Synthesizer/audio mixer
//-------------------------------
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) //Arduino MEGA(2560) boards
OCR2A=127+
#else //Arduino ATmega168/328 based boards
OCR0A=127+
#endif
((
(((signed char)pgm_read_byte(wavs[0]+((PCW[0]+=FTW[0])>>8))*AMP[0])>>8)+
(((signed char)pgm_read_byte(wavs[1]+((PCW[1]+=FTW[1])>>8))*AMP[1])>>8)+
(((signed char)pgm_read_byte(wavs[2]+((PCW[2]+=FTW[2])>>8))*AMP[2])>>8)+
(((signed char)pgm_read_byte(wavs[3]+((PCW[3]+=FTW[3])>>8))*AMP[3])>>8)
)>>2);
tim++;
tim2++;
//************************************************
// Modulation engine
//************************************************
if(tim>FS/20)
{
switch(envg)
{
case 0:
{
FTW[0]=PITCH[0]+(PITCH[0]*(EPCW[0]/(32767.5*128.0 ))*((int)MOD[0]-512));
envg++;
};break;
case 1:
{
FTW[1]=PITCH[1]+(PITCH[1]*(EPCW[1]/(32767.5*128.0 ))*((int)MOD[1]-512));
envg++;
};break;
case 2:
{
FTW[2]=PITCH[2]+(PITCH[2]*(EPCW[2]/(32767.5*128.0 ))*((int)MOD[2]-512));
envg++;
};break;
case 3:
{
FTW[3]=PITCH[3]+(PITCH[3]*(EPCW[3]/(32767.5*128.0 ))*((int)MOD[3]-512));
envg++;
};break;
case 4:
{
tim=0;
envg=0;
tick=1;
};break;
}
}
}
//*********************************************************************
// Setup all voice parameters
//*********************************************************************
void setup_voice(unsigned char voice,unsigned int waveform, float pitch, unsigned int envelope, float length, unsigned int mod)
{
wavs[voice]=waveform;//[address in program memory]
envs[voice]=envelope;//[address in program memory]
EFTW[voice]=(1.0/length)/(FS/(32767.5*10.0));//[s];
PITCH[voice]=pitch/(FS/65535.0); //[Hz]
MOD[voice]=mod;//0-1023 512=no mod
}
//*********************************************************************
// Setup wave
//*********************************************************************
void setup_wave(unsigned char voice,unsigned char wave)
{
switch(wave/16)
{
case 1: wavs[voice]=(unsigned int)SinTable;break;
case 2: wavs[voice]=(unsigned int)TriangleTable;break;
case 3: wavs[voice]=(unsigned int)SquareTable;break;
case 4: wavs[voice]=(unsigned int)SawTable;break;
case 5: wavs[voice]=(unsigned int)RampTable;break;
case 6: wavs[voice]=(unsigned int)NoiseTable;break;
default: wavs[voice]=(unsigned int)SinTable;break;
}
}
//*********************************************************************
// Setup Envelope
//*********************************************************************
void setup_env(unsigned char voice,unsigned char env)
{
switch(env/16)
{
case 1: envs[voice]=(unsigned int)Env0;break;
case 2: envs[voice]=(unsigned int)Env1;break;
case 3: envs[voice]=(unsigned int)Env2;break;
case 4: envs[voice]=(unsigned int)Env3;break;
default: envs[voice]=(unsigned int)Env0;break;
}
}
unsigned int EFTWS[128];
//*********************************************************************
// Setup Length
//*********************************************************************
void setup_length(unsigned char voice,unsigned char length)
{
EFTW[voice]=EFTWS[length];
/* EFTW[voice]=(1.0/exp(.057762265 * (length - 69.)))/(FS/(32767.5*10.0));//[s];
TOG(PORTB,5);
*/
}
//*********************************************************************
// Setup mod
//*********************************************************************
void setup_mod(unsigned char voice,unsigned char mod)
{
MOD[voice]=mod*8;//0-1023 512=no mod
//TOG(PORTB,4);
}
unsigned int PITCHS[128];
unsigned int FTWS[128];
//*********************************************************************
// Midi trigger
//*********************************************************************
void mtrigger(unsigned char voice,unsigned char note)
{
// PITCH[voice]=(440. * exp(.057762265 * (note - 69.)))/(FS/65535.0); //[MIDI note]
PITCH[voice]=PITCHS[note];
EPCW[voice]=0;
FTW[voice]=PITCH[voice]+(PITCH[voice]*(EPCW[voice]/(32767.5*128.0 ))*((int)MOD[voice]-512));
}
//*********************************************************************
// Simple trigger
//*********************************************************************
void trigger(unsigned char voice)
{
EPCW[voice]=0;
}
//*********************************************************************
// Init synth
//*********************************************************************
void initSynth()
{
for(unsigned char i=0;i<128;i++)
{
EFTWS[i]=(1.0/exp(.057762265 * (i - 69.)))/(FS/(32767.5*10.0));//[s];
PITCHS[i]=(440. * exp(.057762265 * (i - 69.)))/(FS/65535.0);
// FTWS[voice]=PITCH[voice]+(PITCH[voice]*(EPCW[voice]/(32767.5*128.0 ))*((int)MOD[voice]-512));
}
TCCR1B=0x02; //-Start audio interrupt
SET(TIMSK1,OCIE1A); // |
sei(); //-+
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) //Arduino MEGA(2560) boards
TCCR2A=0x83; //-8 bit audio PWM
TCCR2B=0x01; // |
OCR2A=127; //-+
SET(DDRB,4); //-PWM pin
#else //Arduino ATmega328/168 based boards
TCCR0A=0x83; //-8 bit audio PWM
TCCR0B=0x01; // |
OCR0A=127; //-+
SET(DDRD,6); //-PWM pin
#endif
/*
UCSR0A=0x02; //-Set up serial port 31250
UCSR0B=0x18; // |
UCSR0C=0x06; // |
UBRR0=63;
SET(DDRB,5);
SET(DDRB,4);
*/ //-+
}