Skip to content

Commit d8fcbe4

Browse files
authored
Merge pull request #29 from paulo-raca/buzzer-sample
Added example to use buzzer (tone/noTone) for MIDI playback
2 parents 07d2817 + 311a3c9 commit d8fcbe4

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* MIDIUSB_buzzer.ino
3+
*
4+
* Author: Paulo Costa
5+
*/
6+
7+
#include <MIDIUSB.h>
8+
#include "pitchToFrequency.h"
9+
10+
#define BUZZ_PIN 9
11+
12+
const char* pitch_name(byte pitch) {
13+
static const char* names[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
14+
return names[pitch % 12];
15+
}
16+
17+
int pitch_octave(byte pitch) {
18+
return (pitch / 12) - 1;
19+
}
20+
21+
void noteOn(byte channel, byte pitch, byte velocity) {
22+
tone(BUZZ_PIN, pitchFrequency[pitch]);
23+
24+
Serial.print("Note On: ");
25+
Serial.print(pitch_name(pitch));
26+
Serial.print(pitch_octave(pitch));
27+
Serial.print(", channel=");
28+
Serial.print(channel);
29+
Serial.print(", velocity=");
30+
Serial.println(velocity);
31+
}
32+
33+
void noteOff(byte channel, byte pitch, byte velocity) {
34+
noTone(BUZZ_PIN);
35+
36+
Serial.print("Note Off: ");
37+
Serial.print(pitch_name(pitch));
38+
Serial.print(pitch_octave(pitch));
39+
Serial.print(", channel=");
40+
Serial.print(channel);
41+
Serial.print(", velocity=");
42+
Serial.println(velocity);
43+
}
44+
45+
void controlChange(byte channel, byte control, byte value) {
46+
Serial.print("Control change: control=");
47+
Serial.print(control);
48+
Serial.print(", value=");
49+
Serial.print(value);
50+
Serial.print(", channel=");
51+
Serial.println(channel);
52+
}
53+
54+
void setup() {
55+
Serial.begin(115200);
56+
}
57+
58+
void loop() {
59+
midiEventPacket_t rx = MidiUSB.read();
60+
switch (rx.header) {
61+
case 0:
62+
break; //No pending events
63+
64+
case 0x9:
65+
noteOn(
66+
rx.byte1 & 0xF, //channel
67+
rx.byte2, //pitch
68+
rx.byte3 //velocity
69+
);
70+
break;
71+
72+
case 0x8:
73+
noteOff(
74+
rx.byte1 & 0xF, //channel
75+
rx.byte2, //pitch
76+
rx.byte3 //velocity
77+
);
78+
break;
79+
80+
case 0xB:
81+
controlChange(
82+
rx.byte1 & 0xF, //channel
83+
rx.byte2, //control
84+
rx.byte3 //value
85+
);
86+
break;
87+
88+
default:
89+
Serial.print("Unhandled MIDI message: ");
90+
Serial.print(rx.header, HEX);
91+
Serial.print("-");
92+
Serial.print(rx.byte1, HEX);
93+
Serial.print("-");
94+
Serial.print(rx.byte2, HEX);
95+
Serial.print("-");
96+
Serial.println(rx.byte3, HEX);
97+
}
98+
}
99+

src/pitchToFrequency.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
// Conversion from MIDI pitch codes to frequencies in Hz
4+
static const float pitchFrequency[] = {
5+
8.176, 8.662, 9.177, 9.723, 10.301, 10.913, 11.562, 12.250, 12.978, 13.750, 14.568, 15.434,
6+
16.352, 17.324, 18.354, 19.445, 20.602, 21.827, 23.125, 24.500, 25.957, 27.500, 29.135, 30.868,
7+
32.703, 34.648, 36.708, 38.891, 41.203, 43.654, 46.249, 48.999, 51.913, 55.000, 58.270, 61.735,
8+
65.406, 69.296, 73.416, 77.782, 82.407, 87.307, 92.499, 97.999, 103.826, 110.000, 116.541, 123.471,
9+
130.813, 138.591, 146.832, 155.563, 164.814, 174.614, 184.997, 195.998, 207.652, 220.000, 233.082, 246.942,
10+
261.626, 277.183, 293.665, 311.127, 329.628, 349.228, 369.994, 391.995, 415.305, 440.000, 466.164, 493.883,
11+
523.251, 554.365, 587.330, 622.254, 659.255, 698.456, 739.989, 783.991, 830.609, 880.000, 932.328, 987.767,
12+
1046.502, 1108.731, 1174.659, 1244.508, 1318.510, 1396.913, 1479.978, 1567.982, 1661.219, 1760.000, 1864.655, 1975.533,
13+
2093.005, 2217.461, 2349.318, 2489.016, 2637.020, 2793.826, 2959.955, 3135.963, 3322.438, 3520.000, 3729.310, 3951.066,
14+
4186.009, 4434.922, 4698.636, 4978.032, 5274.041, 5587.652, 5919.911, 6271.927, 6644.875, 7040.000, 7458.620, 7902.133,
15+
8372.018, 8869.844, 9397.273, 9956.063, 10548.082, 11175.303, 11839.822, 12543.854,
16+
};

0 commit comments

Comments
 (0)