-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
298 lines (265 loc) · 7.45 KB
/
test.c
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
#include <fluidsynth.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <math.h>
#define abs(x) ((x) < 0 ? -(x) : (x))
#define set1bit(x, i) ((x) |= (1<<(i)))
struct fx_data_t {
fluid_synth_t *synth;
float val;
};
int instrument = 0;
fluid_settings_t* settings = NULL;
fluid_synth_t* synth = NULL;
fluid_sfont_t* sfont = NULL;
fluid_sequencer_t* seq;
short synth_destination, client_destination;
fluid_audio_driver_t* adriver = NULL;
fluid_midi_driver_t* mdriver = NULL;
fluid_player_t* dplayer = NULL;
const int CMajor[7] = {60, 62, 64, 65, 67, 69, 71};
const char const *NOTE_NAME[12] = {
"C", "C#/Db", "D", "D#/Eb",
"E", "F", "F#/Gb", "G", "G#/Ab",
"A", "A#/Bb", "B"
};
void showInst(int preNum) {
fluid_preset_t* ps = sfont->get_preset(sfont, 0, preNum);
printf("current instrument: No.%d: %s\n", preNum,
ps->get_name(ps));
}
int distortion = 0;
int fx_func(void *data, int len, int nin, float **in, int nout, float **out) {
struct fx_data_t *fx_data = (struct fx_data_t*) data;
if (fluid_synth_process(fx_data->synth, len, nin, in, nout, out) != 0) {
return -1;
}
if (distortion) {
for (int i = 0; i < nout; ++i) {
float *out_i = out[i];
for (int k = 0; k < len; ++k) {
float x = out_i[k] * 20;
if (x > 0) out_i[k] = 1 - exp(-x);
else out_i[k] = -1 + exp(x);
out_i[k] /= 4;
/*
if (abs(x) < 1e-8) continue;
float y = x * x / abs(x);
out_i[k] = x / abs(x) * (1 - exp(-y));
*/
// printf("%.2f, %.2f ", x, out_i[k]);
}
}
}
return 0;
}
struct cell {
int on;
int chan;
int key;
int vel;
unsigned int time;
} sequence[200];
int recording = 0, total = 0;
void recordingControl(fluid_midi_event_t *evt) {
sequence[total].on = (fluid_midi_event_get_type(evt) == 144 ? 1 : 0);
sequence[total].chan = fluid_midi_event_get_channel(evt);
sequence[total].key = fluid_midi_event_get_key(evt);
sequence[total].vel = fluid_midi_event_get_velocity(evt);
sequence[total].time = fluid_sequencer_get_tick(seq);
++total;
}
#define CHORD_NUM 4
const int CHORD[CHORD_NUM] = { 1168, 144, 136, 72 };
const char const *CHORD_NAME[CHORD_NUM] = {
"dominant seventh",
"major",
"minor",
"diminished",
};
const int SCALE[CHORD_NUM][7] = {
{2, 4, 5, 7, 9, 11},
{2, 3, 5, 7, 8, 10},
};
int pitchs[130] = {0}, pcnt = 0;
void detectChord() {
if (pcnt < 3) return;
int oct[12] = {0}; // notes in one octave
int flag[12] = {0}; // notes appearance flag
int cnt = 0;
for (int i = 0; i < 130; ++i) {
if (pitchs[i] && !flag[i % 12]) {
oct[cnt++] = i;
flag[i % 12] = 1;
}
}
for (int i = 1; i < cnt; ++i) { // shift notes to one octave
while (oct[i] - 12 >= oct[0]) oct[i] -= 12;
}
int itv = 0; // intervals
for (int i = 1; i < cnt; ++i) {
set1bit(itv, oct[i] - oct[0]);
}
for (int c = 0; c < CHORD_NUM; ++c) {
int chord = CHORD[c];
if (itv == chord) {
printf("%s %s\n", NOTE_NAME[oct[0] % 12], CHORD_NAME[c]);
return;
}
else if ((itv & chord) == chord) {
printf("partial %s %s\n", NOTE_NAME[oct[0] % 12], CHORD_NAME[c]);
return;
}
}
}
int midiControl (void* data, fluid_midi_event_t* event) {
fluid_synth_handle_midi_event(data, event);
int type = fluid_midi_event_get_type(event), pitch = fluid_midi_event_get_pitch(event);
if (type == 144 && pitchs[pitch] == 0) {
pitchs[pitch] = 1;
++pcnt;
detectChord();
if (recording) recordingControl(event);
}
else if (type == 128 && pitchs[pitch] == 1) {
pitchs[pitch] = 0;
--pcnt;
if (recording) recordingControl(event);
}
else if (type == 192) {
instrument = fluid_midi_event_get_control(event);
showInst(instrument);
}
// printf("%d %d %d\n", pcnt, type, pitch);
// printf("%u\n", fluid_sequencer_get_tick(seq));
return 0;
}
int looping = 0;
void drumControl() {
if (!digitalRead(7)) {
if (looping) {
// printf("before: %d\n", fluid_player_get_status(dplayer));
fluid_player_stop(dplayer);
// printf("after: %d\n", fluid_player_get_status(dplayer));
looping = 0;
}
else {
// printf("before: %d\n", fluid_player_get_status(dplayer));
fluid_player_play(dplayer);
// printf("after: %d\n", fluid_player_get_status(dplayer));
looping = 1;
}
delay(100);
}
}
void schedule_note(int chan, short key, short vel, unsigned int time, int on) {
fluid_event_t *ev = new_fluid_event();
fluid_event_set_source(ev, -1);
fluid_event_set_dest(ev, synth_destination);
if (on) fluid_event_noteon(ev, chan, key, vel);
else fluid_event_noteoff(ev, chan, key);
fluid_sequencer_send_at(seq, ev, time, 0);
delete_fluid_event(ev);
}
void seq_callback(unsigned int time, fluid_event_t *evt, fluid_sequencer_t *seq, void *data) {
for (int i = 0; i < total; ++i) {
schedule_note(sequence[i].chan, sequence[i].key, sequence[i].vel, sequence[i].time, sequence[i].on);
}
}
int main(int argc, char** argv)
{
struct fx_data_t fx_data;
int sfont_id;
int ret;
/* initialize wiringPi and pins */
wiringPiSetup();
for (int i = 0; i <= 7; ++i) {
pinMode(i, INPUT);
pullUpDnControl(i, PUD_UP);
}
for (int i = 21; i <= 25; ++i) {
pinMode(i, INPUT);
pullUpDnControl(i, PUD_UP);
}
/* Create the settings. */
settings = new_fluid_settings();
/* Change the settings if necessary*/
ret = fluid_settings_setstr(settings, "audio.driver", "alsa");
if (ret == FLUID_FAILED) {
fprintf(stderr, "error on setting audio driver\n");
goto cleanUp;
}
ret = fluid_settings_setnum(settings, "synth.gain", 2.0);
if (ret == FLUID_FAILED) {
fprintf(stderr, "error on setting gain\n");
goto cleanUp;
}
ret = fluid_settings_setstr(settings, "midi.driver", "alsa_raw");
if (ret == FLUID_FAILED) {
fprintf(stderr, "error on setting midi driver\n");
goto cleanUp;
}
ret = fluid_settings_setstr(settings, "midi.alsa.device", "hw:1,0,0");
if (ret == FLUID_FAILED) {
fprintf(stderr, "error on setting midi device\n");
goto cleanUp;
}
/* Create the synthesizer. */
synth = new_fluid_synth(settings);
fx_data.synth = synth;
mdriver = new_fluid_midi_driver(settings, midiControl, synth);
/* Create the audio driver. */
adriver = new_fluid_audio_driver2(settings, fx_func, (void *) &fx_data);
seq = new_fluid_sequencer();
synth_destination = fluid_sequencer_register_fluidsynth(seq, synth);
client_destination = fluid_sequencer_register_client(seq, "test", seq_callback, NULL);
/* Load a SoundFont and reset presets */
sfont_id = fluid_synth_sfload(synth, "./samples/touhou.sf2", 1);
if (sfont_id == FLUID_FAILED) {
fprintf(stderr, "error on opening soundfont\n");
goto cleanUp;
}
sfont = fluid_synth_get_sfont_by_id(synth, sfont_id);
dplayer = new_fluid_player(synth);
fluid_player_add(dplayer, "./drumloops/Downtempo.mid");
fluid_player_set_loop(dplayer, -1);
showInst(instrument);
for (;;) {
drumControl();
if (!digitalRead(25)) distortion = 1;
else distortion = 0;
if (!digitalRead(0)) { // recording
if (recording == 0) {
total = 0;
recording = 1;
printf("recording on\n");
}
}
else {
if (recording == 1) {
for (int i = 1; i < total; ++i) {
sequence[i].time -= sequence[0].time;
}
sequence[0].time = 0;
recording = 0;
printf("recording off\n");
}
}
if (!digitalRead(3)) { // recording playback
fluid_event_t *ev = new_fluid_event();
fluid_event_set_source(ev, -1);
fluid_event_set_dest(ev, client_destination);
fluid_event_timer(ev, NULL);
fluid_sequencer_send_now(seq, ev);
delete_fluid_event(ev);
delay(100);
// printf("p?\n");
}
delay(100);
}
cleanUp:
delete_fluid_audio_driver(adriver);
delete_fluid_synth(synth);
delete_fluid_settings(settings);
return 0;
}