-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathappfiltercmds.cpp
More file actions
366 lines (341 loc) · 9.99 KB
/
appfiltercmds.cpp
File metadata and controls
366 lines (341 loc) · 9.99 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
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 "tcp_message_receiver.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();
}
}
// Deletes any filtered sound band except note selections if specified
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();
}
}
// If anythin is playing, send a command to clear it to the sound thread
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;
bempty = pshared_data->filtered_sound_list.size() == 0;
FUNLOCK;
while (!bempty)
{
FLOCK;
bempty = pshared_data->filtered_sound_list.size() == 0;
FUNLOCK;
usleep(5000);
}
}
// 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;
}
void Cappdata::create_filter_play_message_from_track(int xstart, int xstop, t_coord pos, t_coord dim)
{
t_shared *pshared_data = (t_shared*)&m_shared_data;
float fbase;
float fmax;
double t1, t2;
double duration;
int xp;
t_audioOutCmd cmd;
std::list<t_audioOutCmd> *plist = &pshared_data->filter_cmd_list;
flush_filtered_strips();
delete_filter_commands(&m_note_selection.cmdlist, true); // keep the note selction bands
LOCK;
// Start and stop time
duration = pshared_data->trackend;
UNLOCK;
xp = xstart < xstop? xstart : xstop;
t1 = (double)(xp - pos.x) * duration / (double)dim.x;
xp = xstart > xstop? xstart : xstop;
t2 = (double)(xp - pos.x) * duration / (double)dim.x;
// All the frequency range
fbase = MINNOTEF;
fmax = 44000;
t2 = t2 - t1 > MAX_INTRACK_PLAY_TIME? t1 + MAX_INTRACK_PLAY_TIME : t2;
cmd.start = t1;
cmd.stop = t2;
cmd.playdelay = 0.;
cmd.playstart = -1.;
cmd.bsubstract = false;
cmd.bloop_data = false;
cmd.notestate = state_wait_filtering;
cmd.bands = 1;
cmd.pnote = NULL;
if (fabs(cmd.stop - cmd.start) > 0.005)
{
cmd.fhicut[0] = fmax;
cmd.flocut[0] = fbase;
if (fabs(cmd.fhicut[0] - cmd.flocut[0]) > 1.) // Hz
{
SLOCK;
plist->push_front(cmd);
SUNLOCK;
}
}
wakeup_bandpass_thread();
}