-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLooper.cpp
210 lines (185 loc) · 5.33 KB
/
Looper.cpp
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
/**
* @file Looper.cpp
*
* @brief Looper class
*
* This file implements the Looper class
*
* @author Bryan Cisneros
*/
#include "Looper.h"
#include "Globals.h"
#include "AudioInterface.h"
#include <stdio.h>
Looper::Looper(Button* recPlay, Button* startStop, Button* resetButton, Led* red_led, Led* green_led)
{
// Point the member variables to the buttons and LEDs
recPlayButton = recPlay;
startStopButton = startStop;
this->resetButton = resetButton;
this->red_led = red_led;
this->green_led = green_led;
// Initialize to idle state, recording mode
state = idle;
recordingMode = true;
masterTrack = NULL;
}
Looper::~Looper()
{
}
void Looper::tick()
{
// masterDone is set to true when a rollover (wrap-around) occurs. Rollover
// is set by the audio interface, so we want to clear it as soon as possible,
// but we want to make sure that masterDone is true for one (and only one)
// whole tick process.
masterDone = rollover;
if (rollover)
{
rollover = false;
}
// Run the state machine for each track controller
for (unsigned i = 0; i < trackControllers.size(); i++)
{
trackControllers[i]->tick();
}
// Run the state machine for each button
recPlayButton->tick();
startStopButton->tick();
resetButton->tick();
// Check if any of the buttons have been pressed
bool recPlayButtonPressed = recPlayButton->fell();
bool startStopButtonPressed = startStopButton->fell();
bool resetButtonPressed = resetButton->fell();
if (resetButtonPressed)
{
resetPressed();
}
if (startStopButtonPressed)
{
printf("Start/stop button pressed!\n");
// If we were in the stopped state, call the startButton function.
// If we were in any other state, call the stopButton function
switch (state)
{
case Looper::stopped:
//start playing again
startButton();
//move to normal operation
state = normalOperation;
break;
default:
stopButton();
state = stopped;
break;
}
}
//state action
switch (state)
{
case Looper::idle:
break;
case Looper::firstRecording:
break;
case Looper::normalOperation:
if (recPlayButtonPressed)
{
printf("recPlay button pressed!\n");
recordingMode = !recordingMode;
if (recordingMode)
{
// In recording mode, red should be on and green off
green_led->turnOff();
red_led->turnOn();
}
else
{
// In playing mode, red should be off and green on
red_led->turnOff();
green_led->turnOn();
}
}
break;
case Looper::stopped:
break;
default:
break;
}
//state update
switch (state)
{
case Looper::idle:
// Check each track to see if any of them have moved into the recording
// state. If they have, we have started our first recording. That track
// becomes our master track, and we move into the first recording state
for (unsigned i = 0; i < trackControllers.size(); i++)
{
if (trackControllers[i]->getState() == TrackController::recording)
{
masterTrack = trackControllers[i];
state = firstRecording;
printf("looper state: firstRecording\n");
break;
}
}
break;
case Looper::firstRecording:
if (masterTrack->getState() == TrackController::playing)
{
// If the master track has moved to the playing state, then our first
// recording is complete. move to the normal operation state
rollover = false; // make sure rollover is false before normal operation
state = normalOperation;
printf("looper state: normalOperation\n");
}
break;
case Looper::normalOperation:
break;
case Looper::stopped:
break;
default:
break;
}
}
void Looper::addTrack(TrackController* track)
{
// Just add the ttrack controller to the vector
trackControllers.push_back(track);
}
void Looper::stopButton()
{
// Call the stop button function on each of the track controllers
for (unsigned i = 0; i < trackControllers.size(); i++)
{
trackControllers[i]->stopButton();
}
}
void Looper::startButton()
{
// Reset the audio position back to 0
audio_set_track_position(0);
// Call the start button function on each of the track controllers
for (unsigned i = 0; i < trackControllers.size(); i++)
{
trackControllers[i]->startButton();
}
}
void Looper::resetPressed()
{
// Reset everything back to default!
printf("Reset button pressed!\n");
state = idle;
recordingMode = true;
masterTrack = NULL;
waitingToStart = 0;
// Call the reset button function on each of the track controllers
for (unsigned i = 0; i < trackControllers.size(); i++)
{
trackControllers[i]->resetButton();
}
// Reset the audio interface
audio_reset();
// Reset the LEDs back to just the red one on
green_led->turnOff();
red_led->turnOn();
}