-
Notifications
You must be signed in to change notification settings - Fork 0
/
midiPlayer.c
349 lines (295 loc) · 9.46 KB
/
midiPlayer.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "midiPlayer.h"
int numVoices;
float sawPhase;
bool isDone;
PaError err;
int main(int argc, char **argv) {
numVoices = 0;
//input validation
if (argc != 2) {
printf("Please include only the filename as the parameter\n");
return 0;
}
eventNodeMain = malloc(sizeof(midi_event_node_t));
eventNodeMain = processMidi(argv[1]);
printf("First event vel: %d\n", eventNodeMain->event.data[1]);
printf("First event note: %d\n", eventNodeMain->event.data[0]);
PaStream *stream;
Pa_Sleep(1*1000);
sawPhase = 0;
printf("-------------- Initializing Audio --------------\n");
//initialize portAudio
err = Pa_Initialize();
if (err != paNoError) { print_error(); }
//start default stream
err = Pa_OpenDefaultStream( &stream,
0, //input channels
2, //output channels
paFloat32, //32 bit float output
SAMPLE_RATE,
256, //samples per block
getNextBlock, //callback function
&sawPhase ); //pointer passed to callback
if (err != paNoError) { print_error(); }
err = Pa_StartStream(stream); //begin streaming
if (err != paNoError) { print_error(); }
printf("-------------- Done Initializing Audio --------------\n");
//wait for a bit
Pa_Sleep(3*1000);
isDone = false;
struct sigaction sa;
memset (&sa, 0, sizeof(sa));
sa.sa_handler = alrmHandler;
sa.sa_flags = 0;
if (sigaction(SIGALRM, &sa, NULL) != 0) {
printf("Error setting up alarm\n");
}
struct itimerval timerMain;
//printf("playing note: %d\n", eventNodeMain->event.data[0]);
//printf("next note: %d\n", curEventNode->next->event.data[0]);
//eventNodeMain = eventNodeMain->next;
timerMain.it_value.tv_sec = 1;
timerMain.it_value.tv_usec = 0;
timerMain.it_interval.tv_sec = 3;
timerMain.it_interval.tv_usec = 0;
if (setitimer(ITIMER_REAL, &timerMain, NULL) < 0) {
printf("Error sending alarm\n");
exit(-1);
}
while (!isDone) {
}
//close stream
err = Pa_StopStream(stream);
if (err != paNoError) { print_error(); }
err = Pa_CloseStream(stream);
if (err != paNoError) { print_error(); }
//midi_close(
//check for input to end program
//char input[1];
//fgets(input,sizeof(input),stdin);
//while (input[0] != '\n') {
// fgets(input,sizeof(input),stdin);
//}
printf("Ending program\n");
Pa_Terminate();
return 0;
}
midi_event_node_t * processMidi(char *filename) {
//make midi object
midi_t *midi;
int status;
midi_track_t *track;
midi_event_node_t *firstEventNode = NULL;
//open midi file
status = midi_open(filename, &midi);
//check status
if (status) {
printf("Failed to open midi file\n");
exit(-1);
}
//printf("header dd: %d\n", midi->hdr.dd);
secs_per_tick = SECS_PER_BEAT / 256;
//loop through tracks and gather events
for (int i = 0; i < midi->hdr.tracks; i++) {
int curTime = 0;
//retrieve midi track
track = midi_get_track(midi, i);
//check for track existence
if (track == NULL) {
printf("Failed to find track %d\n", i);
exit(-1);
}
//parse events
midi_iter_track(track);
midi_event_t *curEvent;
midi_event_node_t *curEventNode;
//firstEventNode = track->cur;
curEventNode = track->cur;
midi_event_node_t *tempEventNode;
while (midi_track_has_next(track)) {
//assign first event pointer for use by main program
//if (firstEventNode == NULL) {
// firstEventNode = track->cur;
// curEventNode = firstEventNode;
//}
tempEventNode = track->cur;
curEvent = midi_track_next(track);
if (curEvent->type == MIDI_TYPE_EVENT && curEvent->data[0] != 70 && curEvent->data[0] != 9) {
if (curEvent->cmd == MIDI_EVENT_NOTE_ON ||
curEvent->cmd == MIDI_EVENT_NOTE_OFF) {
//note on or off event detected
if (firstEventNode == NULL) {
firstEventNode = tempEventNode;
curEventNode = firstEventNode;
//printf("First event set: %d, %d\n",
} else {
curEventNode->next = track->cur;
curEventNode = track->cur;
}
//printf("Track data 0: %d, 1: %d\n", curEvent->data[0], curEvent->data[1]);
}
}
}
//free track allocated in midi_get_track
//midi_free_track(track);
}
//for debugging purposes, print out all events
midi_event_node_t *curEventNode = firstEventNode;
while (curEventNode->next != NULL) {
printf("Data: %d, %d\n", curEventNode->event.data[0], curEventNode->event.data[1]);
curEventNode = curEventNode->next;
}
return firstEventNode;
}
static int getNextBlock( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData ) {
float *out = (float*) outputBuffer;
(void) inputBuffer; //even if not using input have this to prevent warning
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) inputBuffer;
int i;
//voice *curVoice = firstVoice;
//loop to write samples
for (i = 0; i < framesPerBuffer; i++) {
voice *curVoice = firstVoice;
*out = 0;
*(out + 1) = 0;
while (curVoice != NULL) {
//updatePhase(curVoice->oscillator);
float sampleVal = (float)sin(curVoice->oscillator->frequency * 2.0 * M_PI * (float)i / SAMPLE_RATE);
//printf("sample val: %d\n", sampleVal);
//printf("phase: %f\n", curVoice->oscillator->phase);
//*out += (float)sin(curVoice->oscillator->phase);
//*(out + 1) += (float)sin(curVoice->oscillator->phase);
//*out += sampleVal * .7f;
//*(out + 1) += sampleVal * .7f;
//float testVal = (float)sin(440 * 2 * M_PI * i / SAMPLE_RATE);
//*out += testVal;
//*(out + 1) += testVal;
curVoice = curVoice->next;
}
float testVal = (float)sin(800 * 2 * M_PI * i / SAMPLE_RATE);
*out += testVal;
*(out + 1) += testVal;
//printf("out 1: %f, out 2: %f\n", *out, *(out+1));
out += 2;
//*out++ = sawPhase;
//*out++ = sawPhase;
//sawPhase += .01f;
//if (sawPhase >= 1.0f) {
// sawPhase -= 2.0f;
//}
}
//printf("Voices: %d\n", numVoices);
return 0;
}
void updatePhase(osc *oscillator) {
double cyclesPerSample = oscillator->frequency / SAMPLE_RATE;
oscillator->phaseDelta = cyclesPerSample * 2 * M_PI;
oscillator->phase += oscillator->phaseDelta;
}
void alrmHandler(int blah) {
//printf("Event received: %d, %d\n", eventNodeMain->event.data[0], eventNodeMain->event.data[1]);
//get note number of current event
//check if note on event was detected
if (eventNodeMain->event.data[1] != 0 &&
eventNodeMain->event.cmd != MIDI_EVENT_NOTE_OFF) {
printf("Playing note: %d\n", eventNodeMain->event.data[0]);
//add voice
numVoices++;
voice *newVoice = malloc(sizeof(voice));
newVoice->midiNum = eventNodeMain->event.data[0];
newVoice->oscillator = malloc(sizeof(osc));
newVoice->oscillator->frequency = mtof(newVoice->midiNum);
newVoice->oscillator->phase = 0;
newVoice->next = NULL;
if (firstVoice == NULL) {
firstVoice = newVoice;
printf("setting first voice\n");
} else {
voice *curVoice = firstVoice;
while (curVoice->next != NULL) {
curVoice = curVoice->next;
}
curVoice->next = newVoice;
}
//print out all notes for debugging
voice *curVoice = firstVoice;
while (curVoice->next != NULL) {
printf(" %d ", curVoice->midiNum);
curVoice = curVoice->next;
}
printf("%d.\n", curVoice->midiNum);
//otherwise a note off event must have been detected, so remove note
} else {
printf("removing note: %d\n", eventNodeMain->event.data[0]);
numVoices--;
//remove voice
bool wasFound = false;
voice *curVoice = firstVoice;
if (curVoice == NULL) {
printf("whoops!\n");
}
//if (curVoice->midiNum == eventNodeMain->event.data[0]) {
// firstVoice->next = NULL;
// firstVoice = NULL;
// printf("Note %d removed from first\n", curVoice->midiNum);
//}
while (curVoice->next != NULL) {
//printf("Cycling Midi Num: %d\n", curVoice->midiNum);
if (curVoice->next->midiNum == eventNodeMain->event.data[0]) {
//free(curVoice->next);
//TODO: free this properly
voice *tempVoice = curVoice->next->next;
curVoice->next = tempVoice;
wasFound = true;
break;
}
curVoice = curVoice->next;
}
if (!wasFound) {
if (firstVoice->next != NULL) {
firstVoice = firstVoice->next;
} else {
firstVoice = NULL;
}
//free(firstVoice);
printf("first voice removed\n");
}
}
if (eventNodeMain->next == NULL) {
isDone = true;
return;
} else {
eventNodeMain = eventNodeMain->next;
}
//get delta time into seconds for timer
float dtSecsWhole = eventNodeMain->next->event.td * secs_per_tick;
int dtSecs = (int)dtSecsWhole;
float dtUSecs = (dtSecsWhole - dtSecs) * 1000000.0f;
struct itimerval timer;
if (dtSecsWhole == 0.0f) {
//set a very small amount of time between consecutive hits
dtSecs = 0;
dtUSecs = 10.0f;
}
timer.it_value.tv_sec = dtSecs;
timer.it_value.tv_usec = dtUSecs;
if (setitimer(ITIMER_REAL, &timer, NULL) < 0) {
printf("Error setting alarm in handler\n");
exit(-1);
}
//printf("timer set\n");
}
void print_error(void) {
Pa_Terminate();
printf("An error occured with portaudio\n");
printf("Error : %s\n", Pa_GetErrorText(err));
}
float mtof(int midiNote) {
return mtofarray[midiNote];
}