-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththrportaudio.cpp
More file actions
740 lines (702 loc) · 22.5 KB
/
thrportaudio.cpp
File metadata and controls
740 lines (702 loc) · 22.5 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/*
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 <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <iterator>
#include <list>
#include <pthread.h>
#include <SDL2/SDL.h>
#include "portaudio.h"
#include "pa_ringbuffer.h"
#include "audioapi.h"
#include "audiodata.h"
#include "shared.h"
#include "thrportaudio.h"
#include "audiofifo.h"
//#define USE_BLOCKINGIO
#define SHOW_INTERFACES
#ifdef _DEBUG
#define SHOW_TIME_LOCKS
#endif
using namespace std;
// Everything linked to audio io
int g_thrportaretval;
int audioCallback(const void *input, void *output, unsigned long frameCount, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
CaudioFifo *paudiofifo;
unsigned int i;
int ret;
float buffers;
//float dump[FRAMES_PER_BUFFER];
//#define TESTOUTPUT
#ifdef TESTOUTPUT
static int phase = 0;
unsigned long e;
float *pout = (float*)output;
for (e = 0; e < frameCount; e++)
pout[e] = 0.1 * sin((float)(phase + e) * 50. * 2. * 3.14);
phase += e;
ret = paContinue;
return ret;
#endif
paudiofifo = (CaudioFifo*)userData;
if ((frameCount % FRAMES_PER_BUFFER) != 0 || frameCount == 0)
{
printf("Framecount critical error in portaudio callback!\n");
ret = paComplete;
paudiofifo->set_quit(true);
}
else
{
ret = paContinue;
buffers = frameCount / FRAMES_PER_BUFFER;
for (i = 0; i < buffers; i++)
{
paudiofifo->push_input_samples((float*)input + i * FRAMES_PER_BUFFER, FRAMES_PER_BUFFER);
paudiofifo->pop_output_samples((float*)output + i * FRAMES_PER_BUFFER, FRAMES_PER_BUFFER);
}
// Dump if it is late FIXME
/*
while (available > 1)
{
available = paudiofifo->pop_output_samples(dump, FRAMES_PER_BUFFER);
}
*/
}
return ret;
}
int get_next_chunk(float *pb, t_audio_strip *ps, int offset, int chunksize)
{
int e;
int res;
int samplenum = ps->samplenum;
for (e = 0; e < offset + chunksize; e++)
{
if (e < offset || (ps->current_sample + e - offset) >= samplenum)
pb[e] = 0; // Fill with 0, portaudio will say it's an underflow... FIXME???
else
pb[e] = ps->samples[ps->current_sample + e - offset];
}
for (; e < FRAMES_PER_BUFFER; e++)
{
pb[e] = 0; // Fill with 0, portaudio will say it's an underflow... FIXME???
}
ps->current_sample += chunksize;
if (ps->current_sample >= samplenum)
return 0;
res = samplenum - ps->current_sample;
return res;
}
void flush_filtered_bands(t_shared* pshared_data)
{
std::list<t_audio_strip>::iterator siter;
FLOCK;
if (pshared_data->filtered_sound_list.size())
{
siter = pshared_data->filtered_sound_list.begin();
while (siter != pshared_data->filtered_sound_list.end())
{
delete[] (*siter).samples;
siter++;
}
pshared_data->filtered_sound_list.clear();
}
FUNLOCK;
}
void play_filtered_bands(PaStream* stream, t_shared* pshared_data, float *mix, int samplerate, CaudioFifo *paudiofifo)
{
std::list<t_audio_strip> *stlist;
float soundstrip[FRAMES_PER_BUFFER];
int numsamples = 0;
int i, offset, needed_samples;
float listsize;
t_audio_strip strip;
std::list<t_audio_strip>::iterator siter;
double current_timecode;
int t1, t2, t3, t4;
t1 = SDL_GetTicks();
FLOCK;
stlist = &pshared_data->filtered_sound_list;
t3 = SDL_GetTicks();
if (stlist->size() > 0)
{
for (i = 0; i < FRAMES_PER_BUFFER; i++)
mix[i] = 0;
current_timecode = 0.001 * SDL_GetTicks();
listsize = 0;
siter = stlist->begin();
while (siter != stlist->end())
{
strip = *siter;
if (strip.playtimecode <= current_timecode)
{
needed_samples = (int)((current_timecode - strip.playtimecode) * samplerate);
if (needed_samples >= FRAMES_PER_BUFFER)
{
needed_samples = FRAMES_PER_BUFFER;
offset = 0;
}
else
offset = FRAMES_PER_BUFFER - needed_samples;
numsamples = get_next_chunk(soundstrip, &(*siter), offset, needed_samples);
if (numsamples > 0)
{
//printf("numsamples is %d should be %d\n", numsamples, FRAMES_PER_BUFFER);
for (i = 0; i < FRAMES_PER_BUFFER; i++)
mix[i] += soundstrip[i];
listsize++;
}
if (numsamples < FRAMES_PER_BUFFER)
{
//printf("deleting filter object\n");
delete[] (*siter).samples;
siter = stlist->erase(siter);
}
else
siter++;
}
else
siter++;
}
}
else
{
for (i = 0; i < FRAMES_PER_BUFFER; i++)
mix[i] = 0;
}
FUNLOCK;
//#define TESTOUTPUT
#ifdef TESTOUTPUT
for (i = 0; i < FRAMES_PER_BUFFER; i++)
mix[i] += 0.001 * sin((float)i * 100. * 2. * 3.14);
#endif
t4 = SDL_GetTicks();
// Output the mixed sound chunks
#ifdef USE_BLOCKINGIO
int err = Pa_WriteStream(stream, mix, FRAMES_PER_BUFFER);
if (err)
{
printf("Pa stream wr error: \"%s\"\n", Pa_GetErrorText(err));
}
#else
paudiofifo->push_output_samples(mix, FRAMES_PER_BUFFER);
#endif
t2 = SDL_GetTicks();
if (t2 - t1 > 8) printf("Playfiltered bands toook %d and %d in locked and %d outputing the stream\n", t2 - t1, t3 - t1, t2 - t4);
}
// Using the api and device names because some apis like pulse crash
// and disapear sometimes so indexes are not reliable.
bool save_audio_parameters(t_channel_select_strings *pchs, PaHostApiIndex api_index, PaDeviceIndex input_device, PaDeviceIndex output_device)
{
const PaDeviceInfo *deviceInfo;
long deviceID;
bool binputfound, boutputfound;
const PaHostApiInfo* pinfo = Pa_GetHostApiInfo(api_index);
pchs->api_name = string(pinfo->name);
binputfound = boutputfound = false;
for (int d = 0; d < pinfo->deviceCount; d++)
{
deviceID = Pa_HostApiDeviceIndexToDeviceIndex(api_index, d);
deviceInfo = Pa_GetDeviceInfo(deviceID);
if (deviceID == input_device)
{
pchs->in_device_name = string(deviceInfo->name);
binputfound = true;
}
if (deviceID == output_device)
{
pchs->out_device_name = string(deviceInfo->name);
boutputfound = true;
}
}
return binputfound && boutputfound;
}
bool get_audio_parameters(t_channel_select_strings *pchs, PaHostApiIndex *papi_index, PaDeviceIndex *pinput_device, PaDeviceIndex *poutput_device)
{
int apiinterfaces = Pa_GetHostApiCount();
const PaDeviceInfo *deviceInfo;
long deviceID;
int i, d;
bool binputfound, boutputfound;
#ifdef SHOW_INTERFACES
printf("get audio parameters from:\n%s\n%s\n%s\n", pchs->api_name.c_str(), pchs->in_device_name.c_str(), pchs->out_device_name.c_str());
#endif
binputfound = boutputfound = false;
for (i = 0; i < apiinterfaces; i++)
{
const PaHostApiInfo* pinfo = Pa_GetHostApiInfo(i);
if (pinfo->deviceCount > 0)
{
if (pchs->api_name == string(pinfo->name))
{
*papi_index = i;
for (d = 0; d < pinfo->deviceCount; d++)
{
deviceID = Pa_HostApiDeviceIndexToDeviceIndex(i, d);
deviceInfo = Pa_GetDeviceInfo(deviceID);
if (pchs->in_device_name == string(deviceInfo->name) &&
deviceInfo->maxInputChannels > 0)
{
*pinput_device = deviceID;
binputfound = true;
}
if (pchs->out_device_name == string(deviceInfo->name) &&
deviceInfo->maxOutputChannels > 0)
{
*poutput_device = deviceID;
boutputfound = true;
}
}
}
}
}
return binputfound && boutputfound;
}
void open_stream(t_shared* pshared_data, PaStream** pstream, CaudioFifo *paudiofifo)
{
PaStreamParameters inputParameters;
PaStreamParameters outputParameters;
PaError err = paNoError;
PaHostApiIndex api_index;
if (!get_audio_parameters(&pshared_data->chs, &api_index, &inputParameters.device, &outputParameters.device))
{
inputParameters.device = Pa_GetDefaultInputDevice(); // default input device, in absolute padevice index
if (inputParameters.device == paNoDevice)
{
fprintf(stderr, "Error: No default input device.\n");
g_thrportaretval = PO_ERR_NOINPUT;
exit(1);
}
outputParameters.device = Pa_GetDefaultOutputDevice(); // default output device
if (outputParameters.device == paNoDevice)
{
fprintf(stderr,"Error: No default output device.\n");
g_thrportaretval = PO_ERR_NOOUTPUT;
exit(1);
}
// Store if in the form (API, device)
if (!save_audio_parameters(&pshared_data->chs, Pa_GetDefaultHostApi(), inputParameters.device, outputParameters.device))
{
printf("Warning: saving the audio config failed.\n");
}
}
#ifdef SHOW_INTERFACES
printf("openingconv in api=\"%s\"\n in =\"%s\"\n out=\"%s\"\n", pshared_data->chs.api_name.c_str(), pshared_data->chs.in_device_name.c_str(), pshared_data->chs.out_device_name.c_str());
printf(" input device=%d output device=%d\n", inputParameters.device, outputParameters.device);
#endif
//-------------------------------------------------------------------------
//
// Open the stream
//
//-------------------------------------------------------------------------
inputParameters.channelCount = 1; // mono input
inputParameters.sampleFormat = paFloat32;
inputParameters.suggestedLatency = Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL; // Host api whatever, not used
// Out
outputParameters.channelCount = 1;
outputParameters.sampleFormat = paFloat32;
outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowInputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL; // Host api whatever, not used
// Start recording
err = Pa_OpenStream(pstream,
&inputParameters,
&outputParameters,
pshared_data->samplerate,
FRAMES_PER_BUFFER,
paNoFlag,
#ifdef USE_BLOCKINGIO
NULL,
#else
audioCallback,
#endif
paudiofifo);
if (err != paNoError)
{
printf("Error opening the api=\"%s\"\n input =\"%s\"\n output=\"%s\"\n", pshared_data->chs.api_name.c_str(), pshared_data->chs.in_device_name.c_str(), pshared_data->chs.out_device_name.c_str());
printf("Error, failed to open the stream: \"%s\"\n", Pa_GetErrorText(err));
g_thrportaretval = PO_ERR_OPENS;
//LOCK;
//pshared_data->bquit = true;
//UNLOCK;
exit(EXIT_FAILURE);
//pthread_exit(&g_thrportaretval);
}
err = Pa_StartStream(*pstream);
if (err != paNoError)
{
printf("Error: Failed to start the stream: \"%s\"\n", Pa_GetErrorText(err));
g_thrportaretval = PO_ERR_OPENS;
exit(1);
//pthread_exit(&g_thrportaretval);
}
}
void close_stream(PaStream** pstream)
{
PaError errcode;
errcode = Pa_StopStream(*pstream);
if (errcode != paNoError)
{
printf("Error: stoping the audio stream.\n");
}
usleep(40000);
errcode = Pa_CloseStream(*pstream);
if (errcode != paNoError)
{
printf("Error: closing the audio stream.\n");
}
}
void process_audio_commands_in_locked_area(t_shared* pshared_data, PaStream** pstream, CaudioFifo *paudiofifo, double *pSDL_start_timecode, double *pSDL_start_sample_time)
{
std::list<t_audio_track_cmd>::iterator iter;
double endtimecode;
iter = pshared_data->audio_cmds.begin();
while (iter != pshared_data->audio_cmds.end())
{
if ((*iter).direction == ad_app2threadaudio)
{
switch ((*iter).command)
{
case ac_reset:
{
// Free the previously recorded data
delete pshared_data->pad;
pshared_data->pad = new Caudiodata();
// Reset timecodes in the shared memory
endtimecode = 0.;
{
pshared_data->timecode = endtimecode;
pshared_data->lastimgtimecode = endtimecode;
pshared_data->trackend = endtimecode;
}
pshared_data->play_State = state_stop;
}
break;
case ac_record:
{
*pSDL_start_timecode = SDL_GetTicks() / 1000.;
*pSDL_start_sample_time = (double)pshared_data->pad->get_samplenum() / (double)pshared_data->samplerate;
pshared_data->play_State = state_record;
}
break;
case ac_stop:
{
pshared_data->play_State = state_stop;
}
break;
case ac_change_io:
{
printf("Closing stream\n");
close_stream(pstream);
usleep(4000);
open_stream(pshared_data, pstream, paudiofifo);
*pSDL_start_timecode = SDL_GetTicks() / 1000.;
*pSDL_start_sample_time = (double)pshared_data->pad->get_samplenum() / (double)pshared_data->samplerate;
printf("Stream opened\n");
}
break;
case ac_flush_strips:
{
flush_filtered_bands(pshared_data);
}
break;
default:
break;
}
iter = pshared_data->audio_cmds.erase(iter);
}
else
iter++;
}
}
void send_event_to_app(int type)
{
SDL_Event event;
SDL_zero(event);
event.type = type;
event.user.code = 0;
event.user.data1 = 0;
if (SDL_PushEvent(&event) == 0)
printf("SDL error, pushing the user event failed in \"%s\".\n", __FILE__);
}
void probe_audio_APIs_in_locked_area(t_shared* pshared_data)
{
int apiinterfaces = Pa_GetHostApiCount();
t_portaudio_api api;
t_pa_device dev;
pshared_data->pa_api_list.clear();
for (int i = 0; i < apiinterfaces; i++)
{
const PaHostApiInfo* pinfo = Pa_GetHostApiInfo(i);
#ifdef SHOW_INTERFACES
printf("%d interface name is=%s device count is %d type is=%d\n", i, pinfo->name, pinfo->deviceCount, pinfo->type);
#endif
if(pinfo->deviceCount > 0)
{
api.name = string(pinfo->name);
api.device_list.clear();
for (int d = 0; d < pinfo->deviceCount; d++)
{
const PaDeviceInfo *deviceInfo;
long deviceID = Pa_HostApiDeviceIndexToDeviceIndex(i, d);
deviceInfo = Pa_GetDeviceInfo(deviceID);
#ifdef SHOW_INTERFACES
printf(" %d device info: name=%s\n", d, deviceInfo->name);
printf(" padevice: %d\n", Pa_HostApiDeviceIndexToDeviceIndex(i, d));
printf(" maxInputChannels=%d\n maxOutputChannels=%d\n\n", deviceInfo->maxInputChannels, deviceInfo->maxOutputChannels);
#endif
dev.name = string(deviceInfo->name);
dev.inputs = deviceInfo->maxInputChannels;
dev.outputs = deviceInfo->maxOutputChannels;
dev.device_api_code = d;
//dev.device_code = deviceID;
api.device_list.push_back(dev);
}
pshared_data->pa_api_list.push_back(api);
}
}
}
// 2 solutions to avoid threads messing with the audio input without mutexes (not allowed in callbacks):
// a) use the lockfree PaUtilRingBuffer in the callback
// b) use the blocking Pa_ReadStream inside the thread with callback function pointer set to NULL
/*static*/ void* thr_portaudio(void *p_data)
{
t_shared* pshared_data = (t_shared*)p_data;
bool bquit;
PaStream* stream;
PaError err = paNoError;
unsigned long samplecount;
double endtimecode;
double timecode;
double practice_start_tick;
float sampleblock[FRAMES_PER_BUFFER];
float mix[FRAMES_PER_BUFFER];
t_audio_track_cmd acmd;
bool bsuccess;
bool boutput;
int samplerate;
CaudioFifo *paudiofifo;
//PaTime paudio_start_timecode;
double SDL_start_timecode;
double last;
double SDL_start_sample_time;
bool bOpenClEnabled;
#ifdef SHOW_TIME_LOCKS
int t1, t2;
#endif
g_thrportaretval = 0;
bOpenClEnabled = false;
while (!bOpenClEnabled) // Because it's "je t'aime moi non plus" between the sound chip and the graphics card drivers.
{
LOCK;
bOpenClEnabled = pshared_data->bOpenClEnabled;
UNLOCK;
usleep(10000);
}
// Atomic init
LOCK;
printf("Initialising the portaudio thread\n");
// Init portaudio
err = Pa_Initialize();
if (err != paNoError)
{
g_thrportaretval = PO_ERR_INIT;
pthread_exit(&g_thrportaretval);
}
UNLOCK;
//
samplecount = 0;
LOCK;
probe_audio_APIs_in_locked_area(pshared_data);
samplerate = pshared_data->samplerate;
paudiofifo = new CaudioFifo(samplerate, FRAMES_PER_BUFFER);
open_stream(pshared_data, &stream, paudiofifo);
practice_start_tick = 0;
//paudio_start_timecode = Pa_GetStreamTime(&stream);
//SDL_start_timecode = SDL_GetTicks() / 1000.;
SDL_start_timecode = -1;
SDL_start_sample_time = 0;
bquit = pshared_data->bquit;
UNLOCK;
while (!bquit)
{
// Get entering audio data
boutput = true;
#ifdef USE_BLOCKINGIO
err = Pa_ReadStream(stream, sampleblock, FRAMES_PER_BUFFER);
// Checkoverflow is disabled in portaudio, do not exit the thread on error
bsuccess = true;
if (err)
{
bsuccess = false;
g_thrportaretval = PO_ERR_OVERFLOW;
printf("Pa stream rd error: \"%s\"\n", Pa_GetErrorText(err));
}
#else
bsuccess = paudiofifo->pop_input_samples(sampleblock, FRAMES_PER_BUFFER);
if (paudiofifo->get_quit())
{
printf("Error in the audio calback.\n");
exit(EXIT_FAILURE);
}
boutput = bsuccess;
#endif
#ifdef SHOW_TIME_LOCKS
t1 = SDL_GetTicks();
#endif
//printf("recorded samples = %ld\n", samplecount);
// Fill the shared buffer
LOCK;
// Check for audio command messages
if (pshared_data->audio_cmds.size() > 0)
{
process_audio_commands_in_locked_area(pshared_data, &stream, paudiofifo, &SDL_start_timecode, &SDL_start_sample_time);
}
switch (pshared_data->play_State)
{
case state_record:
{
if (bsuccess)
{
// Add the new samples to the track
pshared_data->pad->add_data(sampleblock, FRAMES_PER_BUFFER);
// Set the timecode on the last sample
samplecount = pshared_data->pad->get_samplenum();
endtimecode = (double)samplecount / (double)pshared_data->samplerate;
pshared_data->trackend = endtimecode;
}
//---------------------------------------------------------------------------------------
// Place the last sample timecode on a theoritical smooth sample rate placement.
// Do not care if it passes tracklen.
//---------------------------------------------------------------------------------------
if (SDL_start_timecode == -1)
{
SDL_start_timecode = SDL_GetTicks() / 1000.;
}
last = SDL_GetTicks() / 1000.;
pshared_data->timecode = SDL_start_sample_time + (last - SDL_start_timecode);
/*
if (pshared_data->timecode > endtimecode)
{
printf("overflowed by %f samples\n", (pshared_data->timecode - (endtimecode - tperframe)) * (double)pshared_data->samplerate);
pshared_data->timecode = endtimecode - tperframe / 3.;
}
*/
if (pshared_data->timecode < 0)
pshared_data->timecode = 0;
}
break;
case state_wait_practiceloop:
{
timecode = SDL_GetTicks() / 1000.;
//printf("comparinf %f to %f\n", timecode, pshared_data->practice_countdown_tick);
if (fabs(timecode - pshared_data->practice_countdown_tick) > 3.)
{
// Set this value so it starts playing now
pshared_data->practice_countdown_tick = timecode - ((pshared_data->practice_loop_duration / pshared_data->practicespeed) + PRACTICE_BETWEEN_REPLAY_TIME);
pshared_data->play_State = state_practiceloop;
}
}
break;
case state_practiceloop:
{
timecode = SDL_GetTicks() / 1000.;
if (fabs(timecode - pshared_data->practice_countdown_tick) > (pshared_data->practice_loop_duration / pshared_data->practicespeed) + PRACTICE_BETWEEN_REPLAY_TIME)
{
pshared_data->practice_countdown_tick = timecode;
// Send a practice loop command to the main app
acmd.v = 0;
acmd.command = ac_practice_loop;
acmd.direction = ad_thread2app;
pshared_data->audio_cmds.push_back(acmd);
// Force it's treatment with an SDL user event
send_event_to_app(pshared_data->audio_cmd_sdlevent);
}
}
break;
case state_wait_practice:
{
timecode = SDL_GetTicks() / 1000.;
//printf("comparinf %f to %f\n", timecode, pshared_data->practice_countdown_tick);
if (fabs(timecode - pshared_data->practice_countdown_tick) > 3.)
{
pshared_data->practice_start = pshared_data->timecode;
practice_start_tick = timecode;
pshared_data->play_State = state_practice;
}
}
break;
case state_practice:
{
timecode = SDL_GetTicks() / 1000.;
pshared_data->timecode = pshared_data->practice_start + (timecode - practice_start_tick) * pshared_data->practicespeed;
if (pshared_data->timecode > pshared_data->trackend + pshared_data->viewtime / 2)
{
pshared_data->timecode = pshared_data->practice_start;
pshared_data->play_State = state_stop;
// Send a stop command to the main app
acmd.v = 0;
acmd.command = ac_stop;
acmd.direction = ad_thread2app;
pshared_data->audio_cmds.push_back(acmd);
// Force it's treatment with an SDL user event
send_event_to_app(pshared_data->audio_cmd_sdlevent);
}
//samplecount = 0;
}
break;
case state_stop:
{
samplecount = 0;
}
break;
default:
break;
}
bquit = pshared_data->bquit;
samplerate = pshared_data->samplerate;
UNLOCK;
#ifdef SHOW_TIME_LOCKS
t2 = SDL_GetTicks();
if (t2 - t1 > 20) printf("Warning: audio thread locked for more than %dms\n", t2 - t1);
#endif
// Filter and play selected sounds or just write null sound
if (boutput)
{
play_filtered_bands(stream, pshared_data, mix, samplerate, paudiofifo);
}
#ifdef SHOW_TIME_LOCKS
t1 = SDL_GetTicks();
if (t1 - t2 > 20) printf("Warning: audio thread locked for more than %dms at play filtered.\n", t1 - t2 );
#endif
#ifndef USE_BLOCKINGIO
double framespersecond = (double)samplerate / (double)FRAMES_PER_BUFFER;
double pausetime = 1. / (2. * framespersecond);
pausetime = pausetime * 1000000;
usleep((int)pausetime);
//usleep(1000);
#endif
}
// Free everything
close_stream(&stream);
Pa_Terminate();
#ifdef _DEBUG
printf("Audio thread closing\n");
#endif
return &g_thrportaretval;
}