Skip to content

Commit bde05f4

Browse files
author
Francois Best
committed
Added simple synth example with Mono Last operation.
1 parent d13353a commit bde05f4

File tree

4 files changed

+527
-0
lines changed

4 files changed

+527
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <MIDI.h>
2+
#include "noteList.h"
3+
#include "pitches.h"
4+
5+
#ifdef ARDUINO_SAM_DUE // Due has no tone function (yet), overriden to prevent build errors.
6+
#define tone(...)
7+
#define noTone(...)
8+
#endif
9+
10+
// This example shows how to make a simple synth out of an Arduino, using the
11+
// tone() function. It also outputs a gate signal for controlling external
12+
// analog synth components (like envelopes).
13+
14+
static const unsigned sGatePin = 13;
15+
static const unsigned sAudioOutPin = 10;
16+
static const unsigned sMaxNumNotes = 16;
17+
MidiNoteList<sMaxNumNotes> midiNotes;
18+
19+
// -----------------------------------------------------------------------------
20+
21+
void handleGateChanged(bool inGateActive)
22+
{
23+
digitalWrite(sGatePin, inGateActive ? HIGH : LOW);
24+
}
25+
26+
void pulseGate()
27+
{
28+
handleGateChanged(false);
29+
delay(1);
30+
handleGateChanged(true);
31+
}
32+
33+
// -----------------------------------------------------------------------------
34+
35+
void handleNotesChanged()
36+
{
37+
if (midiNotes.empty())
38+
{
39+
handleGateChanged(false);
40+
noTone(sAudioOutPin);
41+
}
42+
else
43+
{
44+
byte currentNote = 0;
45+
if (midiNotes.getTail(currentNote))
46+
{
47+
tone(sAudioOutPin, sNotePitches[currentNote]);
48+
pulseGate(); // Retrigger envelopes. Remove for legato effect.
49+
}
50+
}
51+
}
52+
53+
// -----------------------------------------------------------------------------
54+
55+
void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
56+
{
57+
midiNotes.add(MidiNote(inNote, inVelocity));
58+
handleNotesChanged();
59+
}
60+
61+
void handleNoteOff(byte inChannel, byte inNote, byte inVelocity)
62+
{
63+
midiNotes.remove(inNote);
64+
handleNotesChanged();
65+
}
66+
67+
// -----------------------------------------------------------------------------
68+
69+
void setup()
70+
{
71+
pinMode(sGatePin, OUTPUT);
72+
pinMode(sAudioOutPin, OUTPUT);
73+
MIDI.setHandleNoteOn(handleNoteOn);
74+
MIDI.setHandleNoteOff(handleNoteOff);
75+
MIDI.begin();
76+
}
77+
78+
void loop()
79+
{
80+
MIDI.read();
81+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*!
2+
* \file synth-core_NoteList.h
3+
* \author Francois Best
4+
* \date 24/05/2013
5+
* \license GPL v3.0 - Copyright Forty Seven Effects 2013
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#include "noteList.h"

0 commit comments

Comments
 (0)