forked from vreemdelabs/Scoreview-Base
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappfiltercmds.cpp
More file actions
314 lines (290 loc) · 8.44 KB
/
appfiltercmds.cpp
File metadata and controls
314 lines (290 loc) · 8.44 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
306
307
308
309
310
311
312
313
/*
Scoreview (R)
Copyright (C) 2015 Patrick Areny
All Rights Reserved.
Scoreview 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <assert.h>
#include <iterator>
#include <list>
#include <vector>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <GL/gl.h>
#include "audiodata.h"
#include "audioapi.h"
#include "shared.h"
#include "gfxareas.h"
#include "mesh.h"
#include "gl2Dprimitives.h"
#include "score.h"
#include "f2n.h"
#include "pictures.h"
#include "keypress.h"
#include "instrumenthand.h"
#include "scoreplacement.h"
#include "scoreplacement_violin.h"
#include "scoreplacement_piano.h"
#include "scoreplacement_guitar.h"
#include "scoreedit.h"
#include "scorerenderer.h"
#include "messages.h"
#include "messages_network.h"
#include "card.h"
#include "app.h"
// Plays second to 16th harmonics of a note
void Cappdata::create_filter_play_harmonics_message(CNote *pnote, double playdelay, bool bloopdata, std::list<t_audioOutCmd*> *pcmdlist)
{
t_shared *pshared_data = (t_shared*)&m_shared_data;
std::list<t_notefreq>::iterator iter;
float basef;
t_audioOutCmd cmd;
t_audioOutCmd *pcmd;
float practicespeed;
LOCK;
practicespeed = pshared_data->practicespeed;
UNLOCK;
// Note frequency, FIXME chord note
iter = pnote->m_freq_list.begin();
while (iter != pnote->m_freq_list.end())
{
basef = (*iter).f;
// Start and stop time
cmd.start = pnote->m_time;
cmd.stop = pnote->m_time + pnote->m_duration;
cmd.playdelay = playdelay * 1 / practicespeed; // relative to the time after the filtering
cmd.playstart = -1;
cmd.bsubstract = false;
cmd.bloop_data = bloopdata;
cmd.notestate = state_wait_filtering;
cmd.pnote = pnote;
if (fabs(cmd.stop - cmd.start) > 0.005)
{
set_cmd_bands(cmd, basef, m_localfbase, m_localfmax);
//printf("pushing f=%f start=%f stop=%f listsize=%d\n", basef, cmd.start, cmd.stop, (int)pshared_data->filter_cmd_list.size());
SLOCK;
pshared_data->filter_cmd_list.push_front(cmd);
pcmd = &(*pshared_data->filter_cmd_list.begin());
SUNLOCK;
pcmdlist->push_front(pcmd);
}
iter++;
}
//wakeup_bandpass_thread();
}
bool Cappdata::check_if_notes_just_filtered(std::list<t_audioOutCmd*> *pcmdlist)
{
t_shared *pshared_data = (t_shared*)&m_shared_data;
std::list<t_audioOutCmd*>::iterator iter;
bool res;
res = pcmdlist->size() > 0;
SLOCK;
iter = pcmdlist->begin();
while (iter != pcmdlist->end())
{
if ((*iter)->notestate == state_ready_2play)
{
(*iter)->notestate = state_playing;
res = res && true;
}
else
{
res = false;
}
iter++;
}
SUNLOCK;
#ifdef _DEBUG
if (res)
printf("Notes just filtered\n");
#endif
return res;
}
// The bandpass thread will filter again theese commands
void Cappdata::reset_filter_on_loop_commands(t_note_selection *pnote_selection)
{
t_shared *pshared_data = (t_shared*)&m_shared_data;
std::list<t_audioOutCmd*> *pcmdlist;
std::list<t_audioOutCmd*>::iterator cmditer;
t_audioOutCmd* pcmd;
pcmdlist = &pnote_selection->cmdlist;
if (pcmdlist->size() > 0)
{
SLOCK;
assert(pshared_data->filter_cmd_list.size() > 0);
cmditer = pcmdlist->begin();
while (cmditer != pcmdlist->end())
{
pcmd = (*cmditer);
assert(pcmd->bloop_data);
pcmd->notestate = state_wait_filtering; // They will be filtered and played again
pcmd->playstart = -1;
if (pnote_selection->practice_speed != pshared_data->practicespeed)
{
pcmd->playdelay = pcmd->playdelay * pnote_selection->practice_speed / pshared_data->practicespeed;
}
cmditer++;
}
SUNLOCK;
if (pnote_selection->practice_speed != pshared_data->practicespeed)
{
pnote_selection->practice_speed = pshared_data->practicespeed;
}
wakeup_bandpass_thread();
}
}
void Cappdata::delete_filter_commands(std::list<t_audioOutCmd*> *pcmdlist, bool bkeep_loop_cmds)
{
t_shared *pshared_data = (t_shared*)&m_shared_data;
std::list<t_audioOutCmd>::iterator cmditer;
t_audioOutCmd *pcmd;
SLOCK;
cmditer = pshared_data->filter_cmd_list.begin();
while (cmditer != pshared_data->filter_cmd_list.end())
{
pcmd = &(*cmditer);
if (pcmd->bloop_data && bkeep_loop_cmds)
cmditer++;
else
cmditer = pshared_data->filter_cmd_list.erase(cmditer);
}
SUNLOCK;
if (!bkeep_loop_cmds)
{
pcmdlist->clear();
}
}
void Cappdata::flush_filtered_strips()
{
t_shared *pshared_data = (t_shared*)&m_shared_data;
t_audio_track_cmd acmd;
bool bempty;
FLOCK;
bempty = pshared_data->filtered_sound_list.size() == 0;
FUNLOCK;
if (bempty)
return ;
LOCK;
// Send a stop command to the main app
acmd.v = 0;
acmd.command = ac_flush_strips;
acmd.direction = ad_app2threadaudio;
pshared_data->audio_cmds.push_back(acmd);
UNLOCK;
// Wait for the flush
FLOCK;
while (pshared_data->filtered_sound_list.size() > 0)
{
FUNLOCK;
usleep(5000);
FLOCK;
}
FUNLOCK;
}
// Clears any playing note
void Cappdata::stop_note_selection(t_note_selection *psel)
{
t_shared *pshared_data = (t_shared*)&m_shared_data;
std::list<t_audioOutCmd*>::iterator cmditer;
t_audioOutCmd* pcmd;
if (psel->cmdlist.size() > 0 && psel->state != loopstate_stop)
{
delete_filter_commands(&psel->cmdlist, true);
flush_filtered_strips();
psel->play_timecode = -1;
psel->state = loopstate_stop;
SLOCK;
cmditer = psel->cmdlist.begin();
while (cmditer != psel->cmdlist.end())
{
pcmd = (*cmditer);
assert(pcmd->bloop_data);
pcmd->notestate = state_done; // Nothing to do with it
pcmd->playstart = -1;
cmditer++;
}
SUNLOCK;
}
}
void Cappdata::remove_note_selection(t_note_selection *psel)
{
if (psel->cmdlist.size() > 0)
{
delete_filter_commands(&psel->cmdlist, false);
flush_filtered_strips();
psel->play_timecode = psel->box.start_tcode = psel->box.stop_tcode = -1;
psel->state = loopstate_stop;
}
}
// Transfers a note selection in the editor to the application note selection and play the notes
void Cappdata::create_note_box(t_note_selbox *pnsb, t_note_selection *pappsel)
{
t_shared *pshared_data = (t_shared*)&m_shared_data;
std::list<CNote*>::iterator Note_iter;
CNote* pnote;
double note_delay_time;
bool bloop = true;
if (pnsb->bupdate)
{
delete_filter_commands(&pappsel->cmdlist, false);
pappsel->play_timecode = pappsel->box.start_tcode = pappsel->box.stop_tcode = -1;
flush_filtered_strips();
if (pnsb->Note_list.size() > 0)
{
pappsel->box = pnsb->nsb;
LOCK;
pappsel->practice_speed = pshared_data->practicespeed;
UNLOCK;
// Check if they need to be played
Note_iter = pnsb->Note_list.begin();
while (Note_iter != pnsb->Note_list.end())
{
pnote = *Note_iter;
note_delay_time = pnote->m_time - pnsb->nsb.start_tcode;
create_filter_play_harmonics_message(pnote, note_delay_time, bloop, &pappsel->cmdlist);
Note_iter = pnsb->Note_list.erase(Note_iter);
}
wakeup_bandpass_thread();
}
pnsb->bupdate = false;
}
}
void Cappdata::play_edition_sel_notes(t_edit_state *pedit_state)
{
if (pedit_state != NULL)
{
// The send_practice_message will be sent only when filetring is finished, cause filtering takes noticeable time
create_note_box(&pedit_state->note_sel, &m_note_selection);
}
}
bool Cappdata::queued_filtered_band()
{
t_shared *pshared_data = (t_shared*)&m_shared_data;
std::list<t_audioOutCmd>::iterator cmditer;
t_audioOutCmd* pcmd;
bool bresult;
bresult = false;
SLOCK;
if (pshared_data->filter_cmd_list.size() > 0)
{
cmditer = pshared_data->filter_cmd_list.begin();
while (cmditer != pshared_data->filter_cmd_list.end())
{
pcmd = &(*cmditer);
bresult = bresult && (pcmd->notestate <= state_playing);
cmditer++;
}
}
SUNLOCK;
return bresult;
}