Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
306 lines (239 loc) · 8.15 KB
/
Copy pathmain.cpp
File metadata and controls
306 lines (239 loc) · 8.15 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "RtAudio.h"
#include "lo/lo.h"
#include "RtMidi.h"
#include "ConfigFile.h"
#include "Looper.h"
#include "MidiBind.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
// This program is a multi-channel looper with sophisticated MIDI
// capabilities. It's designed particularly for guitar, with thought
// towards other non-traditional MIDI devices, where bending, volume
// swells, or other capabilities not typically needed for keyboard
// based performances may arise.
ConfigFile config("settings.txt");
RtAudio audio;
std::vector<RtMidiIn*> midiIn;
std::vector<RtMidiOut*> midiOut;
MidiLooper looper;
std::vector<MidiBind> midiBinds;
double lastPlayTimestamp;
std::string loop_position_led_prefix;
int loop_position_led_count;
lo_address osc_target_address;
void readMiscellaneousSettings()
{
loop_position_led_prefix = config.read<std::string>("loop_position_led_prefix", "/2/led");
loop_position_led_count = config.read<int>("loop_position_led_count", 16);
}
// liblo functions
void osc_error(int num, const char *m, const char *path);
int osc_generic_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data);
int foo_handler(const char *path, const char *types, lo_arg **argv, int argc,
void *data, void *user_data);
int quit_handler(const char *path, const char *types, lo_arg **argv, int argc,
void *data, void *user_data);
void read_stdin(void);
void listDevices(RtAudio& audio, RtMidiIn& midiIn, RtMidiOut& midiOut)
{
unsigned int devices = audio.getDeviceCount();
std::cout << "Device count: " << devices << std::endl;
// Scan through devices for various capabilities
RtAudio::DeviceInfo info;
for ( unsigned int i=0; i<devices; i++ ) {
info = audio.getDeviceInfo( i );
if ( info.probed == true ) {
std::cout << "Audio device #" << i << ": " << info.name << " (maximum output channels = " << info.outputChannels << ")\n";
}
}
for ( unsigned int i=0; i<midiIn.getPortCount(); ++i )
std::cout << "Midi In #" << i << ": " << midiIn.getPortName(i) << std::endl;
for ( unsigned int i=0; i<midiOut.getPortCount(); ++i )
std::cout << "Midi Out #" << i << ": " << midiOut.getPortName(i) << std::endl;
}
int audioCallback( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *data )
{
class SineTable
{
public:
std::vector<float> table;
SineTable(float freq, int sampleRate) {
// calculate wavelength in samples
float wavelength = sampleRate/freq;
table.resize(static_cast<int>(wavelength));
for (int i=0; i<table.size(); ++i)
{
table[i] = std::sin(i*2*M_PI/wavelength);
}
}
};
static SineTable sineTable(440, 48000);
static int sineTableIndex(0);
// Since the number of input and output channels is equal, we can do
// a simple buffer copy operation here.
if ( status )
std::cerr << "Stream over/underflow detected." << std::endl;
unsigned long *bytes = (unsigned long *) data;
//memcpy( outputBuffer, inputBuffer, *bytes );
for (int i=0; i<nBufferFrames; ++i)
{
((float*)outputBuffer)[i] = 0.0*sineTable.table[sineTableIndex];
sineTableIndex = (1+sineTableIndex)%sineTable.table.size();
}
// give looper a chance to output it's stored events
looper.advancePlayback(streamTime - lastPlayTimestamp);
lastPlayTimestamp = streamTime;
// output time indication to OSC
static int lastLedTime = 0;
int ledTime = static_cast<int>(1 + loop_position_led_count * (looper.getScrubPositionInSeconds() / looper.getLengthInSeconds()));
if (ledTime != lastLedTime)
{
std::ostringstream lastLedPath;
lastLedPath << loop_position_led_prefix << lastLedTime;
lo_send(osc_target_address, lastLedPath.str().c_str(), "i", 0);
std::ostringstream ledPath;
ledPath << loop_position_led_prefix << ledTime;
lo_send(osc_target_address, ledPath.str().c_str(), "i", 1);
lastLedTime = ledTime;
}
//std::cout << "OSC TIME " << ledTime << std::endl;
return 0;
}
void midiInputCallback(double deltatime, std::vector<unsigned char> *message, void *userData)
{
int device = *((int*)(userData));
double timestamp = audio.getStreamTime();
unsigned int nBytes = message->size();
// output the MIDI message
std::cout << std::setprecision(10) << std::setw(8)
<< "INP" << device << '\t'
<< timestamp << '\t'
<< std::setw(8) << deltatime << device << std::hex;
for (unsigned int i=0; i<nBytes; ++i)
{
std::cout << '\t' << (int)(message->at(i));
}
std::cout << std::dec << '\n';
// check bindings
for (int i=0; i<midiBinds.size(); ++i)
{
// TODO: pass proper device ID
midiBinds[i].processMessage(timestamp, device, message, userData);
}
// pass to phrase tracks
looper.consumeMidiMessage(timestamp, message, userData);
// pass to output
midiOut[0]->sendMessage(message);
}
/* catch any incoming messages and display them. returning 1 means that the
* message has not been fully handled and the server should try other methods */
int osc_generic_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data)
{
int i;
std::cout << "path: " << path << '\n';
for (i=0; i<argc; i++) {
std::cout << "arg " << i << ' ' << types[i];
lo_arg_pp((lo_type)types[i], argv[i]);
std::cout << '\n';
}
std::cout << std::endl;
return 1;
}
void osc_error(int num, const char *msg, const char *path)
{
std::cout << "OSC error #" << num << " at path " << path << ": "
<< msg << std::endl;
}
void bind_toggleRecording(MidiBind* bind, BindState state)
{
std::cout << "TOGGLE RECORDING LOOP" << std::endl;
looper.toggleRecording();
}
void loadBindSettings()
{
midiBinds.push_back(MidiBind("func_toggle_recording", &bind_toggleRecording));
}
int main(int argc, char** argv)
{
midiIn.push_back(new RtMidiIn("Guityup remote"));
midiIn.push_back(new RtMidiIn("Guityup instrument"));
midiOut.push_back(new RtMidiOut("Guityup"));
listDevices(audio,*midiIn[0],*midiOut[0]);
RtAudio::StreamParameters inputParams, outputParams;
inputParams.deviceId = config.read<int>("audio_device"); //std::atoi(argv[1]);
inputParams.nChannels = 2;
outputParams = inputParams;
RtAudio::StreamOptions options;
options.flags = RTAUDIO_NONINTERLEAVED | RTAUDIO_MINIMIZE_LATENCY;
options.streamName = "Guityup";
unsigned int bufferBytes, bufferFrames = 128;
try
{
audio.openStream( &outputParams, &inputParams, RTAUDIO_FLOAT32, 48000, &bufferFrames, &audioCallback, (void *)&bufferBytes, &options);
}
catch ( RtError& e )
{
e.printMessage();
exit( 0 );
}
bufferBytes = bufferFrames * 2 * 4;
// MIDI initialization
int midi_input_device = config.read<int>("midi_in", -1);
if (midi_input_device == -1)
midiIn[0]->openVirtualPort("Guityup");
else
midiIn[0]->openPort(midi_input_device, "Guityup");
midiIn[0]->setCallback(&midiInputCallback, &midi_input_device);
midiIn[0]->ignoreTypes(false,false,false);
int midi_input_instrument_device = config.read<int>("midi_instrument_in",-1);
if (midi_input_instrument_device == -1)
midiIn[1]->openVirtualPort("Guityup");
else
midiIn[1]->openPort(midi_input_instrument_device, "Guityup");
midiIn[1]->setCallback(&midiInputCallback, &midi_input_instrument_device);
midiIn[1]->ignoreTypes(false,false,false);
int midi_output_device = config.read<int>("midi_out", -1);
if (midi_output_device == -1)
midiOut[0]->openVirtualPort("Guityup");
else
midiOut[0]->openPort(midi_output_device, "Guityup");
// OSC initialization
int lo_fd;
fd_set rfds;
struct timeval tv;
int retval;
lo_server osc_server = lo_server_new("7770", osc_error);
lo_server_add_method(osc_server, NULL, NULL, osc_generic_handler, NULL);
osc_target_address = lo_address_new("25.177.252.126", "3333");
// restore bonds
loadBindSettings();
readMiscellaneousSettings();
try
{
lastPlayTimestamp = 0;
audio.startStream();
// TODO: replace this with select or put it in its
// own thread
while (true)
{
lo_server_recv_noblock(osc_server, 0);
}
char input;
std::cout << "\nRunning ... press <enter> to quit.\n";
std::cin.get(input);
}
catch ( RtError& e ) {
e.printMessage();
}
if ( audio.isStreamOpen() )
audio.closeStream();
}