-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2__configuration.ino
232 lines (187 loc) · 5.51 KB
/
2__configuration.ino
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
#include <Controlino.h>
#include <Midier.h>
namespace arpeggino
{
namespace state
{
midier::Layers<8> layers; // the number of layers chosen will affect the global variable size
midier::Sequencer sequencer(layers);
} // state
namespace io
{
// here we declare all I/O controls with their corresponding pin numbers
controlino::Potentiometer BPM(A0, /* min = */ 20, /* max = */ 230); // we limit the value of BPM to [20,230]
controlino::Key Note(10);
controlino::Key Mode(11);
controlino::Key Octave(12);
controlino::Key Perm(A5);
controlino::Key Steps(A4);
controlino::Key Rhythm(A3);
} // io
namespace configurer
{
// a configurer is a method that is responsible for updating a single
// configuration parameter according to changes of an I/O control
using Configurer = void(*)();
void BPM()
{
if (io::BPM.check() == controlino::Potentiometer::Event::Changed)
{
state::sequencer.bpm = io::BPM.read();
}
}
void Note()
{
if (io::Note.check() != controlino::Key::Event::Down)
{
return; // nothing to do
}
// the key was just pressed
auto & config = state::sequencer.config; // a shortcut
if (config.accidental() == midier::Accidental::Flat)
{
config.accidental(midier::Accidental::Natural);
}
else if (config.accidental() == midier::Accidental::Natural)
{
config.accidental(midier::Accidental::Sharp);
}
else if (config.accidental() == midier::Accidental::Sharp)
{
config.accidental(midier::Accidental::Flat);
if (config.note() == midier::Note::C) { config.note(midier::Note::D); }
else if (config.note() == midier::Note::D) { config.note(midier::Note::E); }
else if (config.note() == midier::Note::E) { config.note(midier::Note::F); }
else if (config.note() == midier::Note::F) { config.note(midier::Note::G); }
else if (config.note() == midier::Note::G) { config.note(midier::Note::A); }
else if (config.note() == midier::Note::A) { config.note(midier::Note::B); }
else if (config.note() == midier::Note::B) { config.note(midier::Note::C); }
}
}
void Mode()
{
if (io::Mode.check() == controlino::Key::Event::Down)
{
const auto current = state::sequencer.config.mode();
const auto next = (midier::Mode)(((unsigned)current + 1) % (unsigned)midier::Mode::Count);
state::sequencer.config.mode(next);
}
}
void Octave()
{
if (io::Octave.check() == controlino::Key::Event::Down)
{
const auto current = state::sequencer.config.octave();
const auto next = (current % 7) + 1;
state::sequencer.config.octave(next);
}
}
void Perm()
{
if (io::Perm.check() == controlino::Key::Event::Down)
{
const auto current = state::sequencer.config.perm();
const auto next = (current + 1) % midier::style::count(state::sequencer.config.steps());
state::sequencer.config.perm(next);
}
}
void Steps()
{
if (io::Steps.check() == controlino::Key::Event::Down)
{
auto & config = state::sequencer.config; // a shortcut
if (config.looped() == false) // we set to loop if currently not looping
{
config.looped(true);
}
else
{
unsigned steps = config.steps() + 1;
if (steps > 6)
{
steps = 3;
}
config.steps(steps);
config.perm(0); // reset the permutation
config.looped(false); // set as non looping
}
}
}
void Rhythm()
{
if (io::Rhythm.check() == controlino::Key::Event::Down)
{
const auto current = state::sequencer.config.rhythm();
const auto next = (midier::Rhythm)(((unsigned)current + 1) % (unsigned)midier::Rhythm::Count);
state::sequencer.config.rhythm(next);
}
}
Configurer All[] =
{
BPM,
Note,
Mode,
Octave,
Perm,
Steps,
Rhythm,
};
} // configurer
namespace handle
{
void configurers()
{
// configurers will update the configuration on I/O events
for (const auto & configurer : configurer::All)
{
configurer();
}
}
void keys()
{
// we extend `controlino::Key` so we could hold a Midier handle with every key
struct Key : controlino::Key
{
Key(char pin) : controlino::Key(pin)
{}
midier::Sequencer::Handle h;
};
static Key __keys[] = { 2, 3, 4, 5, 6, 7, 8, 9 }; // initialize with pin numbers
for (auto i = 0; i < sizeof(__keys) / sizeof(Key); ++i)
{
auto & key = __keys[i];
const auto event = key.check();
if (event == Key::Event::None)
{
continue; // nothing has changed
}
if (event == Key::Event::Down) // a key was pressed
{
key.h = state::sequencer.start(i + 1); // start playing an arpeggio of the respective scale degree
}
else if (event == Key::Event::Up) // a key was released
{
state::sequencer.stop(key.h); // stop playing the arpeggio
}
}
}
void click()
{
// actually click Midier for it to play the MIDI notes
state::sequencer.click(midier::Sequencer::Run::Async);
}
} // handle
extern "C" void setup()
{
// initialize the Arduino "Serial" module and set the baud rate
// to the same value you are using in your software.
// if connected physically using a MIDI 5-DIN connection, use 31250.
Serial.begin(9600);
}
extern "C" void loop()
{
handle::configurers();
handle::keys();
handle::click();
}
} // arpeggino