-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteHandler.cpp
More file actions
160 lines (140 loc) · 5.82 KB
/
NoteHandler.cpp
File metadata and controls
160 lines (140 loc) · 5.82 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
/*
Copyright (C) 2016 Mikko Tamminen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
#include "NoteHandler.h"
#include <algorithm>
#include <fstream>
#include <sstream>
NoteHandler::NoteHandler() {
commands["stop"] = &NoteHandler::stop;
commands["start"] = &NoteHandler::start;
commands["seek"] = &NoteHandler::seek;
commands["set_recording"] = &NoteHandler::set_recording;
commands["set_rolling"] = &NoteHandler::set_rolling;
commands["set_waiting"] = &NoteHandler::set_waiting;
commands["set_passthrough"] = &NoteHandler::set_passthrough;
commands["clear_notes"] = &NoteHandler::clear_notes;
}
void NoteHandler::sendCommand(std::string command, double arg) {
if(commands.find(command) != commands.end())
command_queue.push_back(std::make_pair(command,arg));
}
void NoteHandler::TriggerLearn(std::string command, double arg) {
if(commands.find(command) != commands.end())
trigger_learning = std::make_pair(command, arg);
}
void NoteHandler::TriggerUnlearn(std::string command, double arg) {
if(commands.find(command) != commands.end())
trigger_unlearning = std::make_pair(command, arg);
if(trigger_learning.first == command && trigger_learning.second == arg)
trigger_learning.first = "";
}
void NoteHandler::JackEngineTickHandler(int nframes) {
//execute and discard all queued commands
for(auto cmd_pair : command_queue) {
CALL_MEMBER_FN(*this, commands[cmd_pair.first])(cmd_pair.second);
command_queue.clear();
}
if(!state.rolling) return;
//set the tick size for the post-tick operation
tick_size = nframes;
//add the proper notes from the notestore to the play queue
for(auto it = store.lower_bound (state.internal_frame); it != store.end() && it->first < state.internal_frame+nframes; it++) {
for (auto ¬e : it->second) {
play_queue[it->first].push_back(note);
}
}
}
void NoteHandler::JackEngineNoteHandler(Note *note, int offset) {
//If we are waiting for input, start rolling
if(state.wait_for_input) {
state.rolling = true;
state.wait_for_input = false;
}
//put the incoming notes into the store if recording,
//and straight to the playback queue if passthrough is enabled
double timeForNote = state.internal_frame + offset;
if (state.rolling && state.recording)
store[timeForNote].push_back(note);
else if (state.pass_through)
play_queue[timeForNote].push_back(note);
}
void NoteHandler::JackEngineTriggerHandler(Note *note, int offset) {
//remove the trigger that has been marked for unlearning, if present
std::string a = trigger_unlearning.first;
double b = trigger_unlearning.second;
triggers.erase(std::remove_if(triggers.begin(), triggers.end(),
[a,b](trigger t) { return (t.command == a && t.arg == b); }), triggers.end());
trigger_unlearning.first = "";
//add all the triggered commands to the command queue
for(trigger t : triggers) {
if(t.note.channel == note->channel &&
t.note.note == note->note &&
t.note.type == note->type)
this->sendCommand(t.command,t.arg);
}
//learn a new trigger, if there is a new trigger to learn
if(commands.find(trigger_learning.first) != commands.end()) {
trigger t = {trigger_learning.first, trigger_learning.second, Note(*note)};
triggers.push_back(t);
trigger_learning.first = "";
}
}
void NoteHandler::JackEnginePlayFunctionHandler(std::function<void(Note*,int)> play_fn) {
for(auto it = play_queue.begin(); it != play_queue.end(); it++) {
for (auto ¬e : it->second) {
play_fn(note, it->first - state.internal_frame);
}
}
play_queue.clear();
}
void NoteHandler::JackEnginePostTickHandler() {
if(state.rolling)
state.internal_frame += tick_size;
}
void NoteHandler::Save(std::string filename) {
std::ofstream file;
file.open (filename);
for (auto timestamp_notes : store)
for (Note *note : timestamp_notes.second)
file << "n " << timestamp_notes.first << " " << note->to_string() << std::endl;
for (trigger t : triggers)
file << "t " << t.note.to_string() << " " << t.command << " " << t.arg << std::endl;
file.close();
}
#include <iostream>
void NoteHandler::Open(std::string filename) {
std::string line;
std::ifstream file (filename);
if (file.is_open())
{
store.clear();
while ( getline (file,line) ) {
std::string buf;
std::stringstream ss(line);
std::vector<std::string> tokens;
while (ss >> buf)
tokens.push_back(buf);
if (tokens[0] == "n")
store[std::stod(tokens[1])].push_back(new Note(std::stoi(tokens[2]),std::stoi(tokens[3]),std::stoi(tokens[4]),std::stoi(tokens[5])));
if (tokens[0] == "t") {
Note t_note = Note(std::stoi(tokens[1]),std::stoi(tokens[2]),std::stoi(tokens[3]),std::stoi(tokens[4]));
trigger new_t = {tokens[5], std::stod(tokens[6]), t_note};
triggers.push_back(new_t);
}
}
file.close();
}
}