-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
205 lines (172 loc) · 5.56 KB
/
main.cpp
File metadata and controls
205 lines (172 loc) · 5.56 KB
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
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cmath>
#include <iostream>
#include <thread>
#include <ableton/Link.hpp>
extern "C" {
#include <wiringPi.h>
}
using namespace std;
namespace {
const double PULSES_PER_BEAT = 4.0;
const double PULSE_LENGTH = 0.015; // seconds
const double QUANTUM = 4;
float clock_div = 1.0; // clock division for supporting volca/eurorack/etc, multiply by PULSES_PER_BEAT
// for first command line argument to set clock division
enum ClockDivModes {
Sixteenth = 0,
Eighth,
Quarter,
NUM_CLOCK_DIVS
};
int selectedClockDiv = Sixteenth;
// Using WiringPi numbering scheme
enum InPin {
PlayStop = 1,
ClockDiv = 10
};
enum OutPin {
Clock = 25,
Reset = 28,
PlayIndicator = 4
};
enum PlayState {
Stopped,
Cued,
Playing
};
struct State {
ableton::Link link;
std::atomic<bool> running;
std::atomic<bool> playPressed;
std::atomic<PlayState> playState;
std::atomic<bool> clockDivPressed;
State()
: link(120.0)
, running(true)
, playPressed(false)
, playState(Stopped)
, clockDivPressed(false)
{
link.enable(true);
}
};
void configurePins() {
wiringPiSetup();
pinMode(PlayStop, INPUT);
pullUpDnControl(PlayStop, PUD_DOWN);
pinMode(ClockDiv, INPUT);
pullUpDnControl(ClockDiv, PUD_DOWN);
pinMode(Clock, OUTPUT);
pinMode(Reset, OUTPUT);
pinMode(PlayIndicator, OUTPUT);
}
void clearLine() {
std::cout << " \r" << std::flush;
std::cout.fill(' ');
}
void printState(const std::chrono::microseconds time,
const ableton::Link::Timeline timeline)
{
const auto beats = timeline.beatAtTime(time, QUANTUM);
const auto phase = timeline.phaseAtTime(time, QUANTUM);
std::cout << "tempo: " << timeline.tempo()
<< " | " << std::fixed << "beats: " << beats
<< " | " << std::fixed << "phase: " << phase;
clearLine();
}
void outputClock(double beats, double phase, double tempo) {
const double secondsPerBeat = 60.0 / tempo;
// Fractional portion of current beat value
double intgarbage;
const auto beatFraction = std::modf(beats * PULSES_PER_BEAT * clock_div, &intgarbage);
// Fractional beat value for which clock should be high
const auto highFraction = PULSE_LENGTH / secondsPerBeat;
const bool resetHigh = (phase <= highFraction);
digitalWrite(Reset, resetHigh ? HIGH : LOW);
const bool clockHigh = (beatFraction <= highFraction);
digitalWrite(Clock, clockHigh ? HIGH : LOW);
}
void input(State& state) {
while (state.running) {
const bool clockDivPressed = digitalRead(ClockDiv) == HIGH;
const bool playPressed = digitalRead(PlayStop) == HIGH;
if (playPressed && !state.playPressed) {
switch (state.playState) {
case Stopped:
state.playState.store(Cued);
break;
case Cued:
case Playing:
state.playState.store(Stopped);
break;
}
}
if (clockDivPressed && !state.clockDivPressed) {
selectedClockDiv = (selectedClockDiv + 1) % NUM_CLOCK_DIVS;
switch (selectedClockDiv) {
case Sixteenth:
clock_div = 1.0;
break;
case Eighth:
clock_div = 0.5;
break;
case Quarter:
clock_div = 0.25;
break;
default:
clock_div = 1.0;
break;
}
}
state.playPressed.store(playPressed);
state.clockDivPressed.store(clockDivPressed);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void output(State& state) {
while (state.running) {
const auto time = state.link.clock().micros();
auto timeline = state.link.captureAppTimeline();
const double beats = timeline.beatAtTime(time, QUANTUM);
const double phase = timeline.phaseAtTime(time, QUANTUM);
const double tempo = timeline.tempo();
switch (state.playState) {
case Cued: {
// Tweak this
const bool playHigh = (long)(beats * 2) % 2 == 0;
digitalWrite(PlayIndicator, playHigh ? HIGH : LOW);
if (phase <= 0.01) {
state.playState.store(Playing);
}
break;
}
case Playing:
digitalWrite(PlayIndicator, HIGH);
outputClock(beats, phase, tempo);
break;
default:
digitalWrite(PlayIndicator, LOW);
break;
}
std::this_thread::sleep_for(std::chrono::microseconds(250));
}
}
}
int main(void) {
configurePins();
State state;
std::thread inputThread(input, std::ref(state));
std::thread outputThread(output, std::ref(state));
while (state.running) {
const auto time = state.link.clock().micros();
auto timeline = state.link.captureAppTimeline();
printState(time, timeline);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
inputThread.join();
outputThread.join();
return 0;
}