-
Notifications
You must be signed in to change notification settings - Fork 0
/
GHC_piano.pde
executable file
·320 lines (287 loc) · 5.35 KB
/
GHC_piano.pde
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
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Direct Digital Synthesis (DDS) Sine Generator
* Timer2 generates the 31250 KHz Clock Interrupt
*
* KHM 2009 / Martin Nawrath, Academy of Media Arts Cologne
*/
#include <Tone.h>
#include "avr/pgmspace.h"
#define PARAM_SLOW_ATTACK 3
#define PARAM_SLOW_DECAY 4
#define PARAM_MINOR 5
#define PARAM_MAJOR 1
#define PARAM_HARMONIC 2
#define PARAM_CHROMATIC 3
// 256 element flash memory array of one sinewave cycle
PROGMEM prog_uchar chromatic[] = {
NOTE_C3,
NOTE_CS3,
NOTE_D3,
NOTE_DS3,
NOTE_E3,
NOTE_F3,
NOTE_FS3,
NOTE_G3,
NOTE_GS3,
NOTE_A3,
NOTE_AS3,
NOTE_B3,
NOTE_C4
};
PROGMEM prog_uchar minormajor[] = {
NOTE_A3,
NOTE_B3,
NOTE_C3,
NOTE_D3,
NOTE_E3,
NOTE_F3,
NOTE_G3,
NOTE_A3,
NOTE_B3,
NOTE_C4,
NOTE_D3,
NOTE_E3,
NOTE_F3,
NOTE_G3
};
Tone notePlayer[2];
int ntes[12];
byte notes[12];
byte previous[12];
byte recording;
byte r;
int now;
int rec[100];
byte k;
// Table of input pin assignments:
// The first ten are digital Inputs, skipping over 11
// The last two, note[10-11] are analog inputs
int note[] = { 0,1,2,3,4,5,7,8,9,10,11,12 };
// Status of the two tone generators
byte busy[] = { 0,0 };
void setup()
{
byte i;
Serial.begin(115200); // connect to the serial port
Serial.println("GHC");
for(i=6;i<12;i++)
{
pinMode(note[i], INPUT);
digitalWrite(note[i], HIGH);
}
for(i=0;i<2;i++)
{
busy[i] = 255;
}
delay(300);
int prog = getValue();
printParameters(prog);
prog_uchar *noteptr;
if ( (prog&3 == PARAM_MINOR)
|| (prog&3 == PARAM_HARMONIC)) noteptr = minormajor;
else if (prog&3 == PARAM_MAJOR) noteptr = minormajor + 3;
else if (prog&3 == PARAM_CHROMATIC) noteptr = chromatic;
for(i=0;i<12;i++)
{
ntes[i] = pgm_read_byte_near(noteptr + i);
}
if (prog&3 == PARAM_HARMONIC) ntes[6] = NOTE_GS3; // Sharp seventh
for(i=6;i<12;i++)
{
pinMode(note[i], INPUT);
digitalWrite(note[i], HIGH);
}
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
notePlayer[0].begin(2);
notePlayer[1].begin(3);
}
unsigned int getValue()
{
int value = 0;
byte i;
for (i=0; i<6; i++)
{
if (analogRead(note[i]) > 300)
{
value = value | (1<<i);
notes[i] = 1;
}
else
{
notes[i] = 0;
}
}
for (i=6; i<12; i++)
{
if (digitalRead(note[i]))
{
value = value | (1<<i);
notes[i] = 1;
}
else
{
notes[i] = 0;
}
}
return value;
}
/*
* With the GHC_Piano device positioned over the
* programming pattern, start the Serial monitor
* this will restart the Arduino and therefore
* execute the setup code which reads and then
* interprets the black/white pattern it sees
* as a set of programming parameters for scale,
* waveform, attack, decay, tremolo, vibrato.
*/
void printParameters(int p)
{
if ((p&3)==PARAM_MAJOR) Serial.print("Major key ");
if ((p&3)==PARAM_MINOR) Serial.print("minor key ");
if ((p&3)==PARAM_HARMONIC) Serial.print("Harmonic minor ");
if ((p>>2)&1) Serial.print("slow attack (NYI)");
else Serial.print("fast attack (NYI)");
if (((p>>3)&1)==PARAM_SLOW_DECAY) Serial.print("slow decay (NYI)");
else Serial.print("fast decay (NYI)");
if ((p>>5)&1) Serial.print("vibrato (NYI)");
if ((p>>5)&1) Serial.print("tremolo (NYI)");
Serial.println("");
}
void playBack()
{
}
void record()
{
}
void displayNotes()
{
byte i;
for(i=0; i<12; i++)
{
if (notes[11-i]) Serial.print(" 1");
else Serial.print(" 0");
}
Serial.println("");
}
void silence()
{
for(byte j=0;j<2;j++)
{
notePlayer[j].stop();
}
}
int noteChange()
{
byte change;
byte i,j;
getValue();
change = 0;
for(i=0;i<12;i++)
{
if (notes[i] != previous[i])
{
change = 1;
if (notes[i] == 0)
{
for(j=0;j<2;j++)
{
if (busy[j] == i)
{
notePlayer[j].stop();
busy[j] = 255;
if (recording)
{
rec[r++] = j || (now<<8);
}
break;
}
else
{
Serial.println((int)busy[i]);
}
}
}
else // Note just starting
{
for(j=0;j<2;j++)
{
if (busy[j] == 255)
{
notePlayer[j].play(ntes[i]);
busy[j] = i;
Serial.print("playing ");
Serial.println((int)i);
break;
}
}
}
previous[i] = notes[i];
}
}
return change;
}
void loop()
{
byte change;
byte i,j;
while(1)
{
getValue();
/*
* If more than seven notes are playing, we will
* be silent, assuming the device has been lifted
* from the reflective surface. This does not
* apply to the programming word (which may have
* more than seven 1 bits) which is read during
* the setup phase.
*/
j = 0;
for(i=0;i<12;i++)
{
if (notes[i]) j++;
}
if (j>7)
{
silence();
continue;
}
change = 0;
for(i=0;i<12;i++)
{
if (notes[i] != previous[i])
{
change = 1;
if (notes[i] == 0)
{
for(j=0;j<6;j++)
{
if (busy[j] == i)
{
notePlayer[j].stop();
busy[j] = 255;
break;
}
}
}
else // Note just starting
{
for(j=0;j<6;j++)
{
if (busy[j] == 255)
{
notePlayer[j].play(ntes[i]);
busy[j] = i;
break;
}
}
}
previous[i] = notes[i];
}
}
if (change)
{
displayNotes();
}
}
}