Mercurial > hg > beaglert
comparison projects/basic_midi/render.cpp @ 191:b3a306da03e0
Implemented Midi output
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Wed, 03 Feb 2016 01:18:30 +0000 |
parents | d7148d21aaa5 |
children | 265a527f8be8 |
comparison
equal
deleted
inserted
replaced
189:7144c5594d16 | 191:b3a306da03e0 |
---|---|
22 // Return true on success; returning false halts the program. | 22 // Return true on success; returning false halts the program. |
23 Midi midi; | 23 Midi midi; |
24 bool setup(BeagleRTContext *context, void *userData) | 24 bool setup(BeagleRTContext *context, void *userData) |
25 { | 25 { |
26 midi.readFrom(0); | 26 midi.readFrom(0); |
27 midi.writeTo(0); | |
27 if(context->analogFrames == 0) { | 28 if(context->analogFrames == 0) { |
28 rt_printf("Error: this example needs the matrix enabled\n"); | 29 rt_printf("Error: this example needs the matrix enabled\n"); |
29 return false; | 30 return false; |
30 } | 31 } |
31 return true; | 32 return true; |
34 // render() is called regularly at the highest priority by the audio engine. | 35 // render() is called regularly at the highest priority by the audio engine. |
35 // Input and output are given from the audio hardware and the other | 36 // Input and output are given from the audio hardware and the other |
36 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | 37 // ADCs and DACs (if available). If only audio is available, numMatrixFrames |
37 // will be 0. | 38 // will be 0. |
38 | 39 |
39 static midi_byte_t noteOnStatus =0x90; //on channel 1 | 40 static midi_byte_t noteOnStatus = 0x90; //on channel 1 |
40 | 41 |
41 enum {kVelocity, kNoteOn, kNoteNumber}; | 42 enum {kVelocity, kNoteOn, kNoteNumber}; |
42 void render(BeagleRTContext *context, void *userData) | 43 void render(BeagleRTContext *context, void *userData) |
43 { | 44 { |
44 static float f0; | 45 static float f0; |
48 static int velocity = 0; | 49 static int velocity = 0; |
49 static int noteNumber = 0; | 50 static int noteNumber = 0; |
50 static int waitingFor = kNoteOn; | 51 static int waitingFor = kNoteOn; |
51 static int playingNote = -1; | 52 static int playingNote = -1; |
52 while ((message = midi.getInput()) >= 0){ | 53 while ((message = midi.getInput()) >= 0){ |
54 rt_printf("%d\n", message); | |
53 switch(waitingFor){ | 55 switch(waitingFor){ |
54 case kNoteOn: | 56 case kNoteOn: |
55 if(message == noteOnStatus){ | 57 if(message == noteOnStatus){ |
56 waitingFor = kNoteNumber; | 58 waitingFor = kNoteNumber; |
57 } | 59 } |
84 rt_printf("NoteOn: %d, NoteNumber: %d, velocity: %d\n", noteOn, noteNumber, velocity); | 86 rt_printf("NoteOn: %d, NoteNumber: %d, velocity: %d\n", noteOn, noteNumber, velocity); |
85 } | 87 } |
86 break; | 88 break; |
87 } | 89 } |
88 } | 90 } |
89 for(unsigned int n = 0; n < context->audioFrames; n++){ | 91 |
92 for(unsigned int n = 0; n < context->analogFrames; n++){ | |
93 static int count = 0; | |
94 static bool state = 0; | |
95 analogWriteFrameOnce(context, n, 1, state); | |
96 if(count % 40000 == 0){ | |
97 state = !state; | |
98 midi_byte_t bytes[6] = {176, 30, state*127, 176, 67, 30}; // toggle the OWL led and ask for the led status | |
99 midi.writeOutput(bytes, 6); | |
100 } | |
101 | |
90 if(noteOn == 1){ | 102 if(noteOn == 1){ |
91 static float phase = 0; | 103 static float phase = 0; |
92 phase += phaseIncrement; | 104 phase += phaseIncrement; |
93 if(phase > 2 * M_PI) | 105 if(phase > 2 * M_PI) |
94 phase -= 2 * M_PI; | 106 phase -= 2 * M_PI; |
97 audioWriteFrame(context, n, 1, value); | 109 audioWriteFrame(context, n, 1, value); |
98 } else { | 110 } else { |
99 audioWriteFrame(context, n, 0, 0); | 111 audioWriteFrame(context, n, 0, 0); |
100 audioWriteFrame(context, n, 1, 0); | 112 audioWriteFrame(context, n, 1, 0); |
101 } | 113 } |
114 count++; | |
102 } | 115 } |
103 } | 116 } |
104 | 117 |
105 // cleanup() is called once at the end, after the audio has stopped. | 118 // cleanup() is called once at the end, after the audio has stopped. |
106 // Release any resources that were allocated in setup(). | 119 // Release any resources that were allocated in setup(). |