-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpiano.c
More file actions
335 lines (299 loc) · 13.1 KB
/
Copy pathpiano.c
File metadata and controls
335 lines (299 loc) · 13.1 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <limits.h>
#include <unistd.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <portaudio.h>
inline int min(int a, int b) {
return a < b ? a : b;
}
// All of my samples are 44.1kHz 2Channel 16bit audio files
#define SAMPLE_RATE 44100
#define CHANNELS 2
#define BIT_SIZE 16
#define SAMPLE_MAX SHRT_MAX
typedef short SampleType;
typedef struct {
int sample_count;
SampleType (*data)[CHANNELS];
} SoundData;
// Notes are indexed from 0 starting at C0 ang incrementing by one semitone
typedef int Note;
const char* note_to_string[] = {
"C%i", "C#%i", "D%i", "D#%i", "E%i", "F%i", "F#%i", "G%i", "G#%i", "A%i", "A#%i", "B%i"
};
void noteToString(Note note, char* str) {
int octave = note / 12;
int off = note % 12;
sprintf(str, note_to_string[off], octave);
}
SoundData loadWavFileForNote(Note note) {
SoundData sound_data = { 0, NULL };
char name_buffer[1024];
snprintf(name_buffer, 1024, "samples/%i.wav", note);
FILE* file = fopen(name_buffer, "r");
if(file != NULL) {
uint8_t size_buffer[4];
fseek(file, 40, SEEK_SET);
fread(size_buffer, 1, 4, file);
int size = 0;
for(int i = 0; i < 4; i++) {
size |= (int)(size_buffer[i]) << (i*8);
}
sound_data.sample_count = size / (BIT_SIZE / 8) / CHANNELS;
sound_data.data = (SampleType(*)[CHANNELS])malloc(sizeof(SampleType) * CHANNELS * sound_data.sample_count);
for (int s = 0; s < sound_data.sample_count; s++) {
for(int c = 0; c < CHANNELS; c++) {
uint8_t value_buffer[(BIT_SIZE / 8)];
fread(value_buffer, 1, (BIT_SIZE / 8), file);
SampleType value = 0;
for(int i = 0; i < (BIT_SIZE / 8); i++) {
value |= (int)(value_buffer[i]) << (i*8);
}
sound_data.data[s][c] = value;
}
}
fclose(file);
} else {
sound_data.data = (SampleType(*)[CHANNELS])malloc(sizeof(SampleType) * CHANNELS);
sound_data.data[0][0] = 0;
sound_data.data[0][1] = 0;
sound_data.sample_count = 1;
}
return sound_data;
}
typedef struct {
bool pressed;
bool faiding;
int sample_position;
int fading_count;
} PlayingState;
typedef struct {
SoundData sound_data;
PlayingState playing_state;
} PianoString;
#define BASE_NOTE 9
#define NUMBER_OF_NOTES 84
PianoString piano[NUMBER_OF_NOTES];
void initPiano() {
/* char str_buffer[10]; */
for(int i = 0; i < NUMBER_OF_NOTES; i++) {
/* noteToString(i + BASE_NOTE, str_buffer); */
/* fprintf(stderr, "loading sample for %s...\n", str_buffer); */
piano[i].sound_data = loadWavFileForNote(BASE_NOTE + i);
}
}
void deinitPiano() {
for(int i = 0; i < NUMBER_OF_NOTES; i++) {
free(piano[i].sound_data.data);
}
}
void pressPianoKey(Note note) {
if(note >= BASE_NOTE && note < NUMBER_OF_NOTES + BASE_NOTE) {
piano[note - BASE_NOTE].playing_state.sample_position = 0;
piano[note - BASE_NOTE].playing_state.pressed = true;
}
}
void depressPianoKey(Note note) {
if(note >= BASE_NOTE && note < NUMBER_OF_NOTES + BASE_NOTE && piano[note - BASE_NOTE].playing_state.pressed) {
piano[note - BASE_NOTE].playing_state.fading_count = 0;
piano[note - BASE_NOTE].playing_state.faiding = true;
piano[note - BASE_NOTE].playing_state.pressed = false;
}
}
void printPianoState() {
fprintf(stdout, "\e[K");
char str_buffer[10];
for(int ps = 0; ps < NUMBER_OF_NOTES; ps++) {
if(piano[ps].playing_state.pressed) {
noteToString(ps + BASE_NOTE, str_buffer);
fprintf(stdout, "%s ", str_buffer);
}
}
fprintf(stdout, "\n\e[A");
}
void printPianoStateSheet() {
fprintf(stdout, "\e[J");
char str_buffer[10];
fprintf(stdout, piano[79 - BASE_NOTE].playing_state.pressed ? " -O- \n" : piano[80 - BASE_NOTE].playing_state.pressed ? "# -O- \n" : " --- \n");
fprintf(stdout, piano[77 - BASE_NOTE].playing_state.pressed ? " O \n" : piano[78 - BASE_NOTE].playing_state.pressed ? "# O \n" : " \n");
fprintf(stdout, piano[76 - BASE_NOTE].playing_state.pressed ? " -O- \n" : " --- \n");
fprintf(stdout, piano[74 - BASE_NOTE].playing_state.pressed ? " O \n" : piano[75 - BASE_NOTE].playing_state.pressed ? "# O \n" : " \n");
fprintf(stdout, piano[72 - BASE_NOTE].playing_state.pressed ? " -O- \n" : piano[73 - BASE_NOTE].playing_state.pressed ? "# -O- \n" : " --- \n");
fprintf(stdout, piano[71 - BASE_NOTE].playing_state.pressed ? " O \n" : " \n");
fprintf(stdout, piano[69 - BASE_NOTE].playing_state.pressed ? " -O- \n" : piano[70 - BASE_NOTE].playing_state.pressed ? "# -O- \n" : " --- \n");
fprintf(stdout, piano[67 - BASE_NOTE].playing_state.pressed ? " O \n" : piano[68 - BASE_NOTE].playing_state.pressed ? "# O \n" : " \n");
fprintf(stdout, piano[65 - BASE_NOTE].playing_state.pressed ? "---O---\n" : piano[66 - BASE_NOTE].playing_state.pressed ? "#--O---\n" : "-------\n");
fprintf(stdout, piano[64 - BASE_NOTE].playing_state.pressed ? " O \n" : " \n");
fprintf(stdout, piano[62 - BASE_NOTE].playing_state.pressed ? "---O---\n" : piano[63 - BASE_NOTE].playing_state.pressed ? "#--O---\n" : "-------\n");
fprintf(stdout, piano[60 - BASE_NOTE].playing_state.pressed ? " O \n" : piano[61 - BASE_NOTE].playing_state.pressed ? "# O \n" : " \n");
fprintf(stdout, piano[59 - BASE_NOTE].playing_state.pressed ? "---O---\n" : "-------\n");
fprintf(stdout, piano[57 - BASE_NOTE].playing_state.pressed ? " O \n" : piano[58 - BASE_NOTE].playing_state.pressed ? "# O \n" : " \n");
fprintf(stdout, piano[55 - BASE_NOTE].playing_state.pressed ? "---O---\n" : piano[56 - BASE_NOTE].playing_state.pressed ? "#--O---\n" : "-------\n");
fprintf(stdout, piano[53 - BASE_NOTE].playing_state.pressed ? " O \n" : piano[54 - BASE_NOTE].playing_state.pressed ? "# O \n" : " \n");
fprintf(stdout, piano[52 - BASE_NOTE].playing_state.pressed ? "---O---\n" : "-------\n");
fprintf(stdout, piano[50 - BASE_NOTE].playing_state.pressed ? " O \n" : piano[51 - BASE_NOTE].playing_state.pressed ? "# O \n" : " \n");
fprintf(stdout, piano[48 - BASE_NOTE].playing_state.pressed ? " -O- \n" : piano[49 - BASE_NOTE].playing_state.pressed ? "# -O- \n" : " --- \n");
fprintf(stdout, piano[47 - BASE_NOTE].playing_state.pressed ? " O \n" : " \n");
fprintf(stdout, piano[45 - BASE_NOTE].playing_state.pressed ? " -O- \n" : piano[46 - BASE_NOTE].playing_state.pressed ? "# -O- \n" : " --- \n");
fprintf(stdout, piano[43 - BASE_NOTE].playing_state.pressed ? " O \n" : piano[44 - BASE_NOTE].playing_state.pressed ? "# O \n" : " \n");
fprintf(stdout, piano[41 - BASE_NOTE].playing_state.pressed ? " -O- \n" : piano[42 - BASE_NOTE].playing_state.pressed ? "# -O- \n" : " --- \n");
fprintf(stdout, piano[40 - BASE_NOTE].playing_state.pressed ? " O \n" : " \n");
fprintf(stdout, piano[38 - BASE_NOTE].playing_state.pressed ? " -O- \n" : piano[39 - BASE_NOTE].playing_state.pressed ? "# -O- \n" : " --- \n");
fprintf(stdout, "\n\e[26A");
}
PaStream* audio_stream;
int audioCallback(const void* input, void* output, uint64_t frame_count, const PaStreamCallbackTimeInfo* time_info, PaStreamCallbackFlags status_flage, void* user_data) {
float (*out)[2] = (float(*)[2])output;
for(int s = 0; s < frame_count; s++) {
out[s][0] = 0;
out[s][1] = 0;
}
for(int ps = 0; ps < NUMBER_OF_NOTES; ps++) {
if(piano[ps].playing_state.pressed) {
int current_position = piano[ps].playing_state.sample_position;
int frames = min(frame_count, piano[ps].sound_data.sample_count - current_position);
for(int s = 0; s < frames; s++) {
out[s][0] += ((float)(piano[ps].sound_data.data[current_position + s][0]) / (float)SAMPLE_MAX / 10.);
out[s][1] += ((float)(piano[ps].sound_data.data[current_position + s][1]) / (float)SAMPLE_MAX / 10.);
}
if(frames != frame_count) {
piano[ps].playing_state.pressed = false;
} else {
piano[ps].playing_state.sample_position += frame_count;
}
} else if(piano[ps].playing_state.faiding) {
int current_position = piano[ps].playing_state.sample_position;
int fading_count = piano[ps].playing_state.fading_count;
int frames = min(frame_count, piano[ps].sound_data.sample_count - current_position);
for(int s = 0; s < frames; s++) {
fading_count++;
out[s][0] += ((float)(piano[ps].sound_data.data[current_position + s][0]) / (float)SAMPLE_MAX / 10. / (1 + (float)fading_count / 3000.));
out[s][1] += ((float)(piano[ps].sound_data.data[current_position + s][1]) / (float)SAMPLE_MAX / 10. / (1 + (float)fading_count / 3000.));
}
if(frames != frame_count || fading_count > 44100) {
piano[ps].playing_state.faiding = false;
} else {
piano[ps].playing_state.fading_count = fading_count;
piano[ps].playing_state.sample_position += frame_count;
}
}
}
return 0;
}
void initPianoAudio() {
freopen("/dev/null","w",stderr);
Pa_Initialize();
freopen("/dev/tty","w",stderr);
Pa_OpenDefaultStream(&audio_stream, 0, CHANNELS, paFloat32, SAMPLE_RATE, paFramesPerBufferUnspecified, audioCallback, NULL);
Pa_StartStream(audio_stream);
}
void deinitPianoAudio() {
Pa_StopStream(audio_stream);
Pa_Terminate();
}
int keycode_to_note[256];
const int keys[] = {
67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 95, 96,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 51,
94, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
};
void initKeys(Display* display) {
Window root_window = DefaultRootWindow(display);
for (int i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
keycode_to_note[keys[i]] = i;
}
keycode_to_note[XKeysymToKeycode(display, XK_Return)] = keycode_to_note[51];
XGrabKeyboard(display, root_window, false, GrabModeAsync, GrabModeAsync, CurrentTime);
XAutoRepeatOff(display);
XSelectInput(display, root_window, KeyPressMask | KeyReleaseMask);
}
void deinitKeys(Display* display) {
Window root_window = DefaultRootWindow(display);
XUngrabKeyboard(display, root_window);
XAutoRepeatOn(display);
}
#define DEFAULT_SHIFT 12
#define UP_SHIFT 12
#define DOWN_SHIFT 12
void reactToKeyEvents(Display* display, int output) {
bool should_exit = false;
int shift = DEFAULT_SHIFT;
if(output == 1) {
printPianoState();
} else if(output == 2) {
printPianoStateSheet();
}
while (!should_exit)
{
XEvent event;
XNextEvent(display, &event);
switch (event.type)
{
case KeyPress:
if(event.xkey.keycode == XKeysymToKeycode(display, XK_Escape)) {
should_exit = true;
} else if(event.xkey.keycode == XKeysymToKeycode(display, XK_Shift_L)) {
shift += UP_SHIFT;
} else if(event.xkey.keycode == XKeysymToKeycode(display, XK_Control_L)) {
shift -= DOWN_SHIFT;
} else {
pressPianoKey(keycode_to_note[event.xkey.keycode] + BASE_NOTE + shift);
if(output == 1) {
printPianoState();
} else if(output == 2) {
printPianoStateSheet();
}
}
break;
case KeyRelease:
if(event.xkey.keycode == XKeysymToKeycode(display, XK_Escape)) {
should_exit = true;
} else if(event.xkey.keycode == XKeysymToKeycode(display, XK_Shift_L)) {
shift -= UP_SHIFT;
} else if(event.xkey.keycode == XKeysymToKeycode(display, XK_Control_L)) {
shift += DOWN_SHIFT;
} else {
depressPianoKey(keycode_to_note[event.xkey.keycode] + BASE_NOTE + shift);
if(output == 1) {
printPianoState();
} else if(output == 2) {
printPianoStateSheet();
}
}
break;
}
}
}
int main(int argc, char** argv) {
Display* display = XOpenDisplay(NULL);
int output = 0;
for(int i = 1; i < argc; i++) {
if(strcmp(argv[i], "-onotes") == 0) {
output = 1;
}
if(strcmp(argv[i], "-osheet") == 0) {
output = 2;
}
}
initPiano();
initPianoAudio();
initKeys(display);
fprintf(stderr, "Ready to play...\n");
reactToKeyEvents(display, output);
fprintf(stdout, "\e[J");
deinitKeys(display);
deinitPianoAudio();
deinitPiano();
XCloseDisplay(display);
return EXIT_SUCCESS;
}