1 // jack.c
2 // LiVES (lives-exe)
3 // (c) G. Finch 2005 - 2019
4 // Released under the GPL 3 or later
5 // see file ../COPYING for licensing details
6 
7 #include "main.h"
8 #include <jack/jslist.h>
9 #include <jack/control.h>
10 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
11 
12 #ifdef ENABLE_JACK
13 #include "callbacks.h"
14 #include "effects.h"
15 #include "effects-weed.h"
16 
17 #define afile mainw->files[jackd->playing_file]
18 
19 static jack_client_t *jack_transport_client;
20 static lives_audio_buf_t *cache_buffer = NULL;
21 
22 static unsigned char *zero_buff = NULL;
23 static size_t zero_buff_count = 0;
24 
25 static boolean seek_err;
26 
27 #ifdef USE_JACKCTL
28 static jackctl_server_t *jackserver = NULL;
29 #endif
30 
31 #define JACK_READ_BYTES 262144  ///< 256 * 1024
32 
33 static uint8_t jrbuf[JACK_READ_BYTES * 2];
34 
35 static size_t jrb = 0;
36 
37 static off_t fwd_seek_pos = 0;
38 
39 static size_t audio_read_inner(jack_driver_t *jackd, float **in_buffer, int fileno,
40                                int nframes, double out_scale, boolean rev_endian, boolean out_unsigned, size_t rbytes);
41 
42 
43 #ifdef ENABLE_JACK_TRANSPORT
jack_playall(livespointer data)44 static boolean jack_playall(livespointer data) {
45   on_playall_activate(NULL, NULL);
46   return FALSE;
47 }
48 #endif
49 
50 // round int a up to next multiple of int b, unless a is already a multiple of b
align_ceilng(int64_t val,int mod)51 LIVES_LOCAL_INLINE int64_t align_ceilng(int64_t val, int mod) {
52   return (int64_t)((double)(val + mod - 1.) / (double)mod) * (int64_t)mod;
53 }
54 
check_zero_buff(size_t check_size)55 static boolean check_zero_buff(size_t check_size) {
56   if (check_size > zero_buff_count) {
57     zero_buff = (unsigned char *)lives_realloc(zero_buff, check_size);
58     if (zero_buff) {
59       lives_memset(zero_buff + zero_buff_count, 0, check_size - zero_buff_count);
60       zero_buff_count = check_size;
61       return TRUE;
62     }
63     zero_buff_count = 0;
64     return FALSE;
65   }
66   return TRUE;
67 
68 }
69 
70 
lives_jack_init(void)71 boolean lives_jack_init(void) {
72   char *jt_client = lives_strdup_printf("LiVES-%d", capable->mainpid);
73   jack_options_t options = JackServerName;
74   jack_status_t status;
75 #ifdef USE_JACKCTL
76   jackctl_driver_t *driver = NULL;
77 #endif
78 
79   const char *server_name = JACK_DEFAULT_SERVER_NAME;
80 
81   jack_transport_client = NULL;
82 
83 #ifdef USE_JACKCTL
84   if ((prefs->jack_opts & JACK_OPTS_START_TSERVER) || (prefs->jack_opts & JACK_OPTS_START_ASERVER)) {
85     const JSList *drivers;
86 #ifdef JACK_SYNC_MODE
87     const JSList *params;
88 #endif
89     // start the server
90     jackserver = jackctl_server_create(NULL, NULL);
91     if (!jackserver) {
92       LIVES_ERROR("Could not create jackd server");
93       return FALSE;
94     }
95 
96 #ifdef JACK_SYNC_MODE
97     // list server parameters
98     params = jackctl_server_get_parameters(jackserver);
99     while (params) {
100       jackctl_parameter_t *parameter = (jackctl_parameter_t *)params->data;
101       if (!strcmp(jackctl_parameter_get_name(parameter), "sync")) {
102         union jackctl_parameter_value value;
103         value.b = TRUE;
104         jackctl_parameter_set_value(parameter, &value);
105         break;
106       }
107       params = jack_slist_next(params);
108     }
109 #endif
110 
111     drivers = jackctl_server_get_drivers_list(jackserver);
112     while (drivers) {
113       driver = (jackctl_driver_t *)drivers->data;
114       if (!strcmp(jackctl_driver_get_name(driver), JACK_DRIVER_NAME)) {
115         break;
116       }
117       drivers = jack_slist_next(drivers);
118     }
119     if (!driver) {
120       LIVES_ERROR("Could not find a suitable driver for jack");
121       return FALSE;
122     }
123 
124 #ifdef JACK_V2
125     if (!jackctl_server_open(jackserver, driver)) {
126       LIVES_ERROR("Could not create the driver for jack");
127       return FALSE;
128     }
129 
130     if (!jackctl_server_start(jackserver)) {
131       LIVES_ERROR("Could not start the jack server");
132       return FALSE;
133     }
134 #else
135     if (!jackctl_server_start(jackserver, driver)) {
136       LIVES_ERROR("Could not create / start the driver for jack");
137       return FALSE;
138     }
139 #endif
140   }
141 
142   mainw->jack_inited = TRUE;
143 
144   options = (jack_options_t)((int)options | (int)JackNoStartServer);
145 
146 #else
147 
148   if ((prefs->jack_opts & JACK_OPTS_START_TSERVER) || (prefs->jack_opts & JACK_OPTS_START_ASERVER)) {
149     // TODO: check if still needed
150     lives_unsetenv("JACK_NO_START_SERVER");
151     lives_setenv("JACK_START_SERVER", "1");
152 
153     if (!lives_file_test(prefs->jack_aserver, LIVES_FILE_TEST_EXISTS)) {
154       char *com;
155       char jackd_loc[PATH_MAX];
156       get_location(EXEC_JACKD, jackd_loc, PATH_MAX);
157       if (strlen(jackd_loc)) {
158         com = lives_strdup_printf("echo \"%s -d %s\">\"%s\"", jackd_loc, JACK_DRIVER_NAME, prefs->jack_aserver);
159         lives_system(com, FALSE);
160         lives_free(com);
161         lives_chmod(prefs->jack_aserver, "o+x");
162       }
163     }
164   } else {
165     lives_unsetenv("JACK_START_SERVER");
166     lives_setenv("JACK_NO_START_SERVER", "1");
167     options = (jack_options_t)((int)options | (int)JackNoStartServer);
168   }
169 
170 #endif
171 
172   // startup the transport client now; we will open another later for audio
173   jack_transport_client = jack_client_open(jt_client, options, &status, server_name);
174   lives_free(jt_client);
175 
176   if (!jack_transport_client) return FALSE;
177 
178 #ifdef ENABLE_JACK_TRANSPORT
179   jack_activate(jack_transport_client);
180   jack_set_sync_timeout(jack_transport_client, 5000000); // seems to not work
181   jack_set_sync_callback(jack_transport_client, lives_start_ready_callback, NULL);
182   mainw->jack_trans_poll = TRUE;
183 #else
184   jack_client_close(jack_transport_client);
185   jack_transport_client = NULL;
186 #endif
187 
188   if (status & JackServerStarted) {
189     d_print(_("JACK server started\n"));
190   }
191 
192   return TRUE;
193 }
194 
195 /////////////////////////////////////////////////////////////////
196 // transport handling
197 
198 
jack_transport_get_current_ticks(void)199 ticks_t jack_transport_get_current_ticks(void) {
200 #ifdef ENABLE_JACK_TRANSPORT
201   double val;
202   jack_nframes_t srate;
203   jack_position_t pos;
204 
205   jack_transport_query(jack_transport_client, &pos);
206 
207   srate = jack_get_sample_rate(jack_transport_client);
208   val = (double)pos.frame / (double)srate;
209 
210   if (val > 0.) return val * TICKS_PER_SECOND_DBL;
211 #endif
212   return -1;
213 }
214 
215 
216 #ifdef ENABLE_JACK_TRANSPORT
jack_transport_check_state(void)217 static void jack_transport_check_state(void) {
218   jack_position_t pos;
219   jack_transport_state_t jacktstate;
220 
221   // go away until the app has started up properly
222   if (mainw->go_away) return;
223 
224   if (!(prefs->jack_opts & JACK_OPTS_TRANSPORT_CLIENT)) return;
225 
226   if (!jack_transport_client) return;
227 
228   jacktstate = jack_transport_query(jack_transport_client, &pos);
229 
230   if (mainw->jack_can_start && (jacktstate == JackTransportRolling || jacktstate == JackTransportStarting) &&
231       !LIVES_IS_PLAYING && mainw->current_file > 0 && !mainw->is_processing) {
232     mainw->jack_can_start = FALSE;
233     mainw->jack_can_stop = TRUE;
234     lives_timer_add_simple(0, jack_playall, NULL);
235     return;
236   }
237 
238   if (jacktstate == JackTransportStopped) {
239     if (LIVES_IS_PLAYING && mainw->jack_can_stop) {
240       on_stop_activate(NULL, NULL);
241     }
242     mainw->jack_can_start = TRUE;
243   }
244 }
245 #endif
246 
247 
lives_jack_poll(void)248 boolean lives_jack_poll(void) {
249   // data is always NULL
250   // must return TRUE
251 #ifdef ENABLE_JACK_TRANSPORT
252   jack_transport_check_state();
253 #endif
254   return TRUE;
255 }
256 
257 
lives_jack_end(void)258 void lives_jack_end(void) {
259 #ifdef ENABLE_JACK_TRANSPORT
260   jack_client_t *client = jack_transport_client;
261 #endif
262   jack_transport_client = NULL; // stop polling transport
263 #ifdef ENABLE_JACK_TRANSPORT
264   if (client) {
265     jack_deactivate(client);
266     jack_client_close(client);
267   }
268 #endif
269 #ifdef USE_JACKCTL
270   if (jackserver) jackctl_server_destroy(jackserver);
271   jackserver = NULL;
272 #endif
273 }
274 
275 
jack_pb_start(double pbtime)276 void jack_pb_start(double pbtime) {
277   // call this ASAP, then in load_frame_image; we will wait for sync from other clients (and ourself !)
278 #ifdef ENABLE_JACK_TRANSPORT
279   if (prefs->jack_opts & JACK_OPTS_TRANSPORT_MASTER) {
280     if (pbtime >= 0. && !mainw->jack_can_stop && (prefs->jack_opts & JACK_OPTS_TIMEBASE_LSTART))
281       jack_transport_locate(jack_transport_client, pbtime * jack_get_sample_rate(jack_transport_client));
282     jack_transport_start(jack_transport_client);
283   }
284 #endif
285 }
286 
287 
jack_pb_stop(void)288 void jack_pb_stop(void) {
289   // call this after pb stops
290 #ifdef ENABLE_JACK_TRANSPORT
291   if (prefs->jack_opts & JACK_OPTS_TRANSPORT_MASTER) jack_transport_stop(jack_transport_client);
292 #endif
293 }
294 
295 ////////////////////////////////////////////
296 // audio
297 
298 static jack_driver_t outdev[JACK_MAX_OUTDEVICES];
299 static jack_driver_t indev[JACK_MAX_INDEVICES];
300 
301 
302 /* not used yet */
303 /*
304   static float set_pulse(float *buf, size_t bufsz, int step) {
305   float *ptr=buf;
306   float *end=buf+bufsz;
307 
308   float tot;
309   int count=0;
310 
311   while (ptr<end) {
312     tot+=*ptr;
313     count++;
314     ptr+=step;
315   }
316   if (count>0) return tot/(float)count;
317   return 0.;
318   }*/
319 
320 
jack_get_rec_avals(jack_driver_t * jackd)321 void jack_get_rec_avals(jack_driver_t *jackd) {
322   mainw->rec_aclip = jackd->playing_file;
323   if (mainw->rec_aclip != -1) {
324     mainw->rec_aseek = fabs((double)fwd_seek_pos / (double)(afile->achans * afile->asampsize / 8) / (double)afile->arps)
325                        + (double)(mainw->startticks - mainw->currticks) / TICKS_PER_SECOND_DBL;
326     mainw->rec_avel = fabs((double)jackd->sample_in_rate / (double)afile->arps) * (double)afile->adirection;
327   }
328 }
329 
330 
jack_set_rec_avals(jack_driver_t * jackd)331 static void jack_set_rec_avals(jack_driver_t *jackd) {
332   // record direction change (internal)
333   mainw->rec_aclip = jackd->playing_file;
334   if (mainw->rec_aclip != -1) {
335     jack_get_rec_avals(jackd);
336   }
337 }
338 
339 
jack_get_buffsize(jack_driver_t * jackd)340 size_t jack_get_buffsize(jack_driver_t *jackd) {
341   if (cache_buffer) return cache_buffer->bytesize;
342   return 0;
343 }
344 
345 
push_cache_buffer(lives_audio_buf_t * cache_buffer,jack_driver_t * jackd,size_t in_bytes,size_t nframes,double shrink_factor)346 static void push_cache_buffer(lives_audio_buf_t *cache_buffer, jack_driver_t *jackd,
347                               size_t in_bytes, size_t nframes, double shrink_factor) {
348   // push a cache_buffer for another thread to fill
349   int qnt;
350   if (!cache_buffer) return;
351 
352   qnt = afile->achans * (afile->asampsize >> 3);
353   jackd->seek_pos = align_ceilng(jackd->seek_pos, qnt);
354 
355   if (mainw->ascrap_file > -1 && jackd->playing_file == mainw->ascrap_file) cache_buffer->sequential = TRUE;
356   else cache_buffer->sequential = FALSE;
357 
358   cache_buffer->fileno = jackd->playing_file;
359 
360   cache_buffer->seek = jackd->seek_pos;
361   cache_buffer->bytesize = in_bytes;
362 
363   cache_buffer->in_achans = jackd->num_input_channels;
364   cache_buffer->out_achans = jackd->num_output_channels;
365 
366   cache_buffer->in_asamps = afile->asampsize;
367   cache_buffer->out_asamps = -32;  ///< 32 bit float
368 
369   cache_buffer->shrink_factor = shrink_factor;
370 
371   cache_buffer->swap_sign = jackd->usigned;
372   cache_buffer->swap_endian = jackd->reverse_endian ? SWAP_X_TO_L : 0;
373 
374   cache_buffer->samp_space = nframes;
375 
376   cache_buffer->in_interleaf = TRUE;
377   cache_buffer->out_interleaf = FALSE;
378 
379   cache_buffer->operation = LIVES_READ_OPERATION;
380 
381   wake_audio_thread();
382 }
383 
384 
pop_cache_buffer(void)385 LIVES_INLINE lives_audio_buf_t *pop_cache_buffer(void) {
386   // get next available cache_buffer
387   return audio_cache_get_buffer();
388 }
389 
390 
output_silence(size_t offset,nframes_t nframes,jack_driver_t * jackd,float ** out_buffer)391 static void output_silence(size_t offset, nframes_t nframes, jack_driver_t *jackd, float **out_buffer) {
392   // write nframes silence to all output streams
393   for (int i = 0; i < jackd->num_output_channels; i++) {
394     if (!jackd->is_silent) {
395       sample_silence_dS(out_buffer[i] + offset, nframes);
396     }
397     if (mainw->audio_frame_buffer && prefs->audio_src != AUDIO_SRC_EXT) {
398       // audio to be sent to video generator plugins
399       append_to_audio_bufferf(out_buffer[i] + offset, nframes, i);
400       if (i == jackd->num_output_channels - 1) mainw->audio_frame_buffer->samples_filled += nframes;
401     }
402   }
403   if (mainw->ext_audio && mainw->vpp && mainw->vpp->render_audio_frame_float) {
404     // audio to be sent to video playback plugin
405     sample_silence_stream(jackd->num_output_channels, nframes);
406   }
407   if (jackd->astream_fd != -1) {
408     // external streaming
409     size_t rbytes = nframes * jackd->num_output_channels * 2;
410     check_zero_buff(rbytes);
411     audio_stream(zero_buff, rbytes, jackd->astream_fd);
412   }
413   if (!jackd->is_paused) jackd->frames_written += nframes;
414   jackd->real_seek_pos = jackd->seek_pos;
415   if (IS_VALID_CLIP(jackd->playing_file) && jackd->seek_pos < afile->afilesize)
416     afile->aseek_pos = jackd->seek_pos;
417 }
418 
419 
audio_process(nframes_t nframes,void * arg)420 static int audio_process(nframes_t nframes, void *arg) {
421   // JACK calls this periodically to get the next audio buffer
422   float *out_buffer[JACK_MAX_OUTPUT_PORTS];
423   jack_driver_t *jackd = (jack_driver_t *)arg;
424   jack_position_t pos;
425   aserver_message_t *msg;
426   int64_t xseek;
427   int new_file;
428   boolean got_cmd = FALSE;
429   boolean from_memory = FALSE;
430   boolean wait_cache_buffer = FALSE;
431   boolean pl_error = FALSE; ///< flag tells if we had an error during plugin processing
432   size_t nbytes, rbytes;
433 
434   int i;
435 #define DEBUG_JACK
436 #ifdef DEBUG_AJACK
437   lives_printerr("nframes %ld, sizeof(float) == %d\n", (int64_t)nframes, sizeof(float));
438 #endif
439 
440   if (!mainw->is_ready || !jackd || (!LIVES_IS_PLAYING && jackd->is_silent && !jackd->msgq)) return 0;
441 
442   /* process one message */
443   while ((msg = (aserver_message_t *)jackd->msgq) != NULL) {
444     got_cmd = TRUE;
445     switch (msg->command) {
446     case ASERVER_CMD_FILE_OPEN:
447       new_file = atoi((char *)msg->data);
448       if (jackd->playing_file != new_file) {
449         jackd->playing_file = new_file;
450       }
451       jackd->seek_pos = jackd->real_seek_pos = 0;
452       break;
453     case ASERVER_CMD_FILE_CLOSE:
454       jackd->playing_file = -1;
455       jackd->in_use = FALSE;
456       jackd->seek_pos = jackd->real_seek_pos = fwd_seek_pos = 0;
457       break;
458     case ASERVER_CMD_FILE_SEEK:
459       if (jackd->playing_file < 0) break;
460       xseek = atol((char *)msg->data);
461       xseek = ALIGN_CEIL64(xseek, afile->achans * (afile->asampsize >> 3));
462       if (xseek < 0) xseek = 0;
463       jackd->seek_pos = jackd->real_seek_pos = afile->aseek_pos = xseek;
464       push_cache_buffer(cache_buffer, jackd, 0, 0, 1.);
465       jackd->in_use = TRUE;
466       break;
467     default:
468       jackd->msgq = NULL;
469       msg->data = NULL;
470     }
471 
472     if (msg->next != msg) lives_freep((void **) & (msg->data));
473     msg->command = ASERVER_CMD_PROCESSED;
474     jackd->msgq = msg->next;
475     if (jackd->msgq && jackd->msgq->next == jackd->msgq) jackd->msgq->next = NULL;
476   }
477 
478   /* retrieve the buffers for the output ports */
479   for (i = 0; i < jackd->num_output_channels; i++)
480     out_buffer[i] = (float *)jack_port_get_buffer(jackd->output_port[i], nframes);
481 
482   if (got_cmd) {
483     output_silence(0, nframes, jackd, out_buffer);
484     return 0;
485   }
486 
487   fwd_seek_pos = jackd->real_seek_pos = jackd->seek_pos;
488 
489   if (nframes == 0) {
490     return 0;
491   }
492 
493   if ((mainw->agen_key == 0 || mainw->agen_needs_reinit || mainw->multitrack || mainw->preview)
494       && jackd->in_use && jackd->playing_file > -1) {
495     //if ((mainw->agen_key == 0 || mainw->agen_needs_reinit || mainw->multitrack) && jackd->in_use) {
496     // if a plugin is generating audio we do not use cache_buffers, otherwise:
497     if (jackd->read_abuf == -1) {
498       // assign local copy from cache_buffers
499       if (!LIVES_IS_PLAYING || (cache_buffer = pop_cache_buffer()) == NULL) {
500         // audio buffer is not ready yet
501         if (!jackd->is_silent) {
502           output_silence(0, nframes, jackd, out_buffer);
503           jackd->is_silent = TRUE;
504         }
505         return 0;
506       }
507       if (cache_buffer->fileno == -1) jackd->playing_file = -1;
508     }
509     if (cache_buffer && cache_buffer->in_achans > 0 && !cache_buffer->is_ready) wait_cache_buffer = TRUE;
510   }
511 
512   jackd->state = jack_transport_query(jackd->client, &pos);
513 
514 #ifdef DEBUG_AJACK
515   lives_printerr("STATE is %d %d\n", jackd->state, jackd->play_when_stopped);
516 #endif
517 
518   /* handle playing state */
519   if (jackd->state == JackTransportRolling || jackd->play_when_stopped) {
520     uint64_t jackFramesAvailable = nframes; /* frames we have left to write to jack */
521     uint64_t inputFramesAvailable;          /* frames we have available this loop */
522     uint64_t numFramesToWrite;              /* num frames we are writing this loop */
523     int64_t in_frames = 0;
524     uint64_t in_bytes = 0, xin_bytes = 0;
525     float shrink_factor = 1.f;
526     double vol;
527     lives_clip_t *xfile = afile;
528     int qnt = 1;
529     if (IS_VALID_CLIP(jackd->playing_file)) qnt = afile->achans * (afile->asampsize >> 3);
530 
531 #ifdef DEBUG_AJACK
532     lives_printerr("playing... jackFramesAvailable = %ld\n", jackFramesAvailable);
533 #endif
534 
535     jackd->num_calls++;
536 
537     if (!jackd->in_use || ((jackd->playing_file < 0 || jackd->seek_pos < 0.) && jackd->read_abuf < 0
538                            && ((mainw->agen_key == 0 && !mainw->agen_needs_reinit)
539                                || mainw->multitrack || mainw->preview))
540         || jackd->is_paused) {
541       /* output silence if nothing is being outputted */
542       if (!jackd->is_silent) {
543         output_silence(0, nframes, jackd, out_buffer);
544         jackd->is_silent = TRUE;
545       }
546       return 0;
547     }
548 
549     jackd->is_silent = FALSE;
550 
551     if (!mainw->audio_seek_ready) {
552       if (!mainw->video_seek_ready) {
553         if (!jackd->is_silent) {
554           output_silence(0, nframes, jackd, out_buffer);
555           jackd->is_silent = TRUE;
556         }
557         fwd_seek_pos = jackd->real_seek_pos = jackd->seek_pos;
558         return 0;
559       }
560 
561       // preload the buffer for first read
562       in_bytes = ABS((in_frames = ((double)jackd->sample_in_rate / (double)jackd->sample_out_rate *
563                                    (double)jackFramesAvailable + ((double)fastrand() / (double)LIVES_MAXUINT64))))
564                  * jackd->num_input_channels * jackd->bytes_per_channel;
565 
566       if (cache_buffer) push_cache_buffer(cache_buffer, jackd, in_bytes, nframes, shrink_factor);
567       mainw->startticks = mainw->currticks = lives_get_current_playback_ticks(mainw->origsecs, mainw->orignsecs, NULL);
568       mainw->fps_mini_ticks = mainw->currticks;
569       mainw->fps_mini_measure = 0;
570 
571       pthread_mutex_lock(&mainw->avseek_mutex);
572       mainw->audio_seek_ready = TRUE;
573       pthread_cond_signal(&mainw->avseek_cond);
574       pthread_mutex_unlock(&mainw->avseek_mutex);
575     }
576 
577     if (LIVES_LIKELY(jackFramesAvailable > 0)) {
578       /* (bytes of data) / (2 bytes(16 bits) * X input channels) == frames */
579       if (LIVES_IS_PLAYING && jackd->read_abuf > -1) {
580         // playing back from memory buffers instead of from file
581         // this is used in multitrack
582         from_memory = TRUE;
583 
584         numFramesToWrite = jackFramesAvailable;
585         jackd->frames_written += numFramesToWrite;
586         jackFramesAvailable = 0;
587       } else {
588         boolean eof = FALSE;
589         int playfile = mainw->playing_file;
590         jackd->seek_end = 0;
591         if (mainw->agen_key == 0 && !mainw->agen_needs_reinit && IS_VALID_CLIP(jackd->playing_file)) {
592           if (mainw->playing_sel) {
593             jackd->seek_end = (int64_t)((double)(afile->end - 1.) / afile->fps * afile->arps) * afile->achans
594                               * (afile->asampsize / 8);
595             if (jackd->seek_end > afile->afilesize) jackd->seek_end = afile->afilesize;
596           } else {
597             if (!mainw->loop_video) jackd->seek_end = (int64_t)((double)(mainw->play_end - 1.) / afile->fps * afile->arps)
598                   * afile->achans * (afile->asampsize / 8);
599             else jackd->seek_end = afile->afilesize;
600           }
601           if (jackd->seek_end > afile->afilesize) jackd->seek_end = afile->afilesize;
602         }
603         if (jackd->seek_end == 0 || ((jackd->playing_file == mainw->ascrap_file && !mainw->preview) && IS_VALID_CLIP(playfile)
604                                      && mainw->files[playfile]->achans > 0)) jackd->seek_end = INT64_MAX;
605         in_bytes = ABS((in_frames = ((double)jackd->sample_in_rate / (double)jackd->sample_out_rate *
606                                      (double)jackFramesAvailable + ((double)fastrand() / (double)LIVES_MAXUINT64))))
607                    * jackd->num_input_channels * jackd->bytes_per_channel;
608 
609         // update looping mode
610         if ((mainw->loop_cont || mainw->whentostop != STOP_ON_AUD_END) && !mainw->preview) {
611           if (mainw->ping_pong && (prefs->audio_opts & AUDIO_OPTS_FOLLOW_FPS)
612               && ((prefs->audio_opts & AUDIO_OPTS_FOLLOW_CLIPS) || mainw->current_file == jackd->playing_file)
613               && (!mainw->event_list || mainw->record || mainw->record_paused)
614               && mainw->agen_key == 0 && !mainw->agen_needs_reinit)
615             jackd->loop = AUDIO_LOOP_PINGPONG;
616           else jackd->loop = AUDIO_LOOP_FORWARD;
617         } else {
618           jackd->loop = AUDIO_LOOP_NONE;
619         }
620 
621         if (cache_buffer) eof = cache_buffer->eof;
622 
623         if ((shrink_factor = (float)in_frames / (float)jackFramesAvailable) >= 0.f) {
624           jackd->seek_pos += in_bytes;
625           if (jackd->playing_file != mainw->ascrap_file) {
626             if (eof || (jackd->seek_pos >= jackd->seek_end && !afile->opening)) {
627               if (jackd->loop == AUDIO_LOOP_NONE) {
628                 if (*jackd->whentostop == STOP_ON_AUD_END) {
629                   *jackd->cancelled = CANCEL_AUD_END;
630                   jackd->in_use = FALSE;
631                 }
632                 in_bytes = 0;
633               } else {
634                 if (jackd->loop == AUDIO_LOOP_PINGPONG && ((jackd->playing_file != mainw->playing_file)
635                     || clip_can_reverse(mainw->playing_file))) {
636                   jackd->sample_in_rate = -jackd->sample_in_rate;
637                   afile->adirection = -afile->adirection;
638                   jackd->seek_pos -= (jackd->seek_pos - jackd->seek_end);
639                 } else {
640                   if (mainw->playing_sel) {
641                     fwd_seek_pos = jackd->seek_pos = jackd->real_seek_pos
642                                                      = (int64_t)((double)(afile->start - 1.) / afile->fps * afile->arps)
643                                                        * afile->achans * (afile->asampsize / 8);
644                   } else fwd_seek_pos = jackd->seek_pos = jackd->real_seek_pos = 0;
645                   if (mainw->record && !mainw->record_paused) jack_set_rec_avals(jackd);
646 		// *INDENT-OFF*
647                 }}}}
648 	  // *INDENT-ON*
649         } else {
650           // reverse playback
651           off_t seek_start = (mainw->playing_sel ?
652                               (int64_t)((double)(afile->start - 1.) / afile->fps * afile->arps)
653                               * afile->achans * (afile->asampsize / 8) : 0);
654           seek_start = ALIGN_CEIL64(seek_start - qnt, qnt);
655 
656           if ((jackd->seek_pos -= in_bytes) < seek_start) {
657             // reached beginning backwards
658             if (jackd->playing_file != mainw->ascrap_file) {
659               if (jackd->loop == AUDIO_LOOP_NONE) {
660                 if (*jackd->whentostop == STOP_ON_AUD_END) {
661                   *jackd->cancelled = CANCEL_AUD_END;
662                 }
663                 jackd->in_use = FALSE;
664               } else {
665                 if (jackd->loop == AUDIO_LOOP_PINGPONG && ((jackd->playing_file != mainw->playing_file)
666                     || clip_can_reverse(mainw->playing_file))) {
667                   jackd->sample_in_rate = -jackd->sample_in_rate;
668                   afile->adirection = -afile->adirection;
669                   shrink_factor = -shrink_factor;
670                   jackd->seek_pos = seek_start;
671                 } else {
672                   jackd->seek_pos += jackd->seek_end;
673                   if (jackd->seek_pos > jackd->seek_end - in_bytes) jackd->seek_pos = jackd->seek_end - in_bytes;
674                 }
675               }
676               fwd_seek_pos = jackd->real_seek_pos = jackd->seek_pos;
677               if (mainw->record && !mainw->record_paused) jack_set_rec_avals(jackd);
678             }
679           }
680         }
681 
682         if (jackd->mute || !cache_buffer ||
683             (in_bytes == 0 &&
684              ((mainw->agen_key == 0 && !mainw->agen_needs_reinit) || mainw->multitrack || mainw->preview))) {
685           if (!mainw->multitrack && cache_buffer && !wait_cache_buffer
686               && ((mainw->agen_key == 0 && !mainw->agen_needs_reinit)
687                   || mainw->preview)) {
688             push_cache_buffer(cache_buffer, jackd, in_bytes, nframes, shrink_factor);
689           }
690           output_silence(0, nframes, jackd, out_buffer);
691           return 0;
692         } else {
693           xin_bytes = 0;
694         }
695         if (mainw->agen_key != 0 && !mainw->multitrack && !mainw->preview) {
696           // how much audio do we want to pull from any generator ?
697           in_bytes = jackFramesAvailable * jackd->num_output_channels * 4;
698           xin_bytes = in_bytes;
699         }
700 
701         if (!jackd->in_use || in_bytes == 0) {
702           // reached end of audio with no looping
703           output_silence(0, nframes, jackd, out_buffer);
704 
705           jackd->is_silent = TRUE;
706 
707           if (jackd->seek_pos < 0. && jackd->playing_file > -1 && xfile) {
708             jackd->seek_pos += nframes * xfile->achans * xfile->asampsize / 8;
709           }
710           return 0;
711         }
712 
713         if (mainw->multitrack || mainw->preview || (mainw->agen_key == 0 && !mainw->agen_needs_reinit))
714           inputFramesAvailable = cache_buffer->samp_space;
715         else inputFramesAvailable = jackFramesAvailable;
716 
717 #ifdef DEBUG_AJACK
718         lives_printerr("%d inputFramesAvailable == %ld, %ld %ld,jackFramesAvailable == %ld\n", inputFramesAvailable,
719                        in_frames, jackd->sample_in_rate, jackd->sample_out_rate, jackFramesAvailable);
720 #endif
721 
722         /* write as many bytes as we have space remaining, or as much as we have data to write */
723         numFramesToWrite = MIN(jackFramesAvailable, inputFramesAvailable);
724 
725 #ifdef DEBUG_AJACK
726         lives_printerr("nframes == %d, jackFramesAvailable == %ld,\n\tjackd->num_input_channels == %ld,"
727                        "jackd->num_output_channels == %ld, nf2w %ld, in_bytes %d, sf %.8f\n",
728                        nframes, jackFramesAvailable, jackd->num_input_channels, jackd->num_output_channels,
729                        numFramesToWrite, in_bytes, shrink_factor);
730 #endif
731         jackd->frames_written += numFramesToWrite;
732         jackFramesAvailable -= numFramesToWrite; /* take away what was written */
733 
734 #ifdef DEBUG_AJACK
735         lives_printerr("jackFramesAvailable == %ld\n", jackFramesAvailable);
736 #endif
737       }
738 
739       // playback from memory or file
740       vol = lives_vol_from_linear(future_prefs->volume * cfile->vol);
741 
742       if (numFramesToWrite > 0) {
743         if (!from_memory) {
744           //	if (((int)(jackd->num_calls/100.))*100==jackd->num_calls) if (mainw->soft_debug) g_print("audio pip\n");
745           if ((mainw->agen_key != 0 || mainw->agen_needs_reinit || cache_buffer->bufferf) && !mainw->preview &&
746               !jackd->mute) { // TODO - try buffer16 instead of bufferf
747             float *fbuffer = NULL;
748 
749             if (!mainw->preview && !mainw->multitrack && (mainw->agen_key != 0 || mainw->agen_needs_reinit)) {
750               // audio generated from plugin
751               if (mainw->agen_needs_reinit) pl_error = TRUE;
752               else {
753                 if (!get_audio_from_plugin(out_buffer, jackd->num_output_channels,
754                                            jackd->sample_out_rate, numFramesToWrite, TRUE)) {
755                   pl_error = TRUE;
756                 }
757               }
758 
759               // get back non-interleaved float fbuffer; rate and channels should match
760               if (pl_error) {
761                 // error in plugin, put silence
762                 output_silence(0, numFramesToWrite, jackd, out_buffer);
763               } else {
764                 for (i = 0; i < jackd->num_output_channels; i++) {
765                   // push non-interleaved audio in fbuffer to jack
766                   if (mainw->audio_frame_buffer && prefs->audio_src != AUDIO_SRC_EXT) {
767                     // we will push the pre-effected audio to any audio reactive generators
768                     append_to_audio_bufferf(out_buffer[i], numFramesToWrite, i);
769                     if (i == jackd->num_output_channels - 1) mainw->audio_frame_buffer->samples_filled += numFramesToWrite;
770                   }
771                 }
772               }
773               //}
774               if (!pl_error && has_audio_filters(AF_TYPE_ANY)) {
775                 float **xfltbuf;
776                 ticks_t tc = mainw->currticks;
777                 // apply inplace any effects with audio in_channels, result goes to jack
778                 weed_layer_t *layer = weed_layer_new(WEED_LAYER_TYPE_AUDIO);
779                 weed_layer_set_audio_data(layer, out_buffer, jackd->sample_out_rate,
780                                           jackd->num_output_channels, numFramesToWrite);
781                 weed_set_boolean_value(layer, WEED_LEAF_HOST_KEEP_ADATA, WEED_TRUE);
782                 weed_apply_audio_effects_rt(layer, tc, FALSE, TRUE);
783                 xfltbuf = weed_layer_get_audio_data(layer, NULL);
784                 for (i = 0; i < jackd->num_output_channels; i++) {
785                   if (xfltbuf[i] != out_buffer[i]) {
786                     lives_memcpy(out_buffer[i], xfltbuf[i], numFramesToWrite * sizeof(float));
787                     lives_free(xfltbuf[i]);
788                   }
789                 }
790                 lives_free(xfltbuf);
791                 weed_layer_set_audio_data(layer, NULL, 0, 0, 0);
792                 weed_layer_free(layer);
793               }
794 
795               pthread_mutex_lock(&mainw->vpp_stream_mutex);
796               if (mainw->ext_audio && mainw->vpp && mainw->vpp->render_audio_frame_float) {
797                 (*mainw->vpp->render_audio_frame_float)(out_buffer, numFramesToWrite);
798               }
799               pthread_mutex_unlock(&mainw->vpp_stream_mutex);
800 
801               if (mainw->record && mainw->ascrap_file != -1 && mainw->playing_file > 0) {
802                 // if recording we will save this audio fragment
803                 int out_unsigned = mainw->files[mainw->ascrap_file]->signed_endian & AFORM_UNSIGNED;
804                 rbytes = numFramesToWrite * mainw->files[mainw->ascrap_file]->achans *
805                          mainw->files[mainw->ascrap_file]->asampsize >> 3;
806 
807                 rbytes = audio_read_inner(jackd, out_buffer, mainw->ascrap_file, numFramesToWrite, 1.0,
808                                           !(mainw->files[mainw->ascrap_file]->signed_endian & AFORM_BIG_ENDIAN),
809                                           out_unsigned, rbytes);
810 
811                 mainw->files[mainw->ascrap_file]->aseek_pos += rbytes;
812               }
813             } else {
814               // audio from a file
815               if (wait_cache_buffer) {
816                 while (!cache_buffer->is_ready && !cache_buffer->die) {
817                   lives_usleep(prefs->sleep_time);
818                 }
819                 wait_cache_buffer = FALSE;
820               }
821 
822               pthread_mutex_lock(&mainw->cache_buffer_mutex);
823               if (!cache_buffer->die) {
824                 // push audio from cache_buffer to jack
825                 for (i = 0; i < jackd->num_output_channels; i++) {
826                   jackd->abs_maxvol_heard = sample_move_d16_float(out_buffer[i], cache_buffer->buffer16[0] + i, numFramesToWrite,
827                                             jackd->num_input_channels, afile->signed_endian & AFORM_UNSIGNED, FALSE, vol);
828 
829                   if (mainw->audio_frame_buffer && prefs->audio_src != AUDIO_SRC_EXT) {
830                     // we will push the pre-effected audio to any audio reactive generators
831                     append_to_audio_bufferf(out_buffer[i], numFramesToWrite, i);
832                     if (i == jackd->num_output_channels - 1) mainw->audio_frame_buffer->samples_filled += numFramesToWrite;
833                   }
834                 }
835                 pthread_mutex_unlock(&mainw->cache_buffer_mutex);
836 
837                 if (has_audio_filters(AF_TYPE_ANY) && jackd->playing_file != mainw->ascrap_file) {
838                   float **xfltbuf;
839                   ticks_t tc = mainw->currticks;
840                   // apply inplace any effects with audio in_channels
841                   weed_layer_t *layer = weed_layer_new(WEED_LAYER_TYPE_AUDIO);
842                   weed_set_voidptr_array(layer, WEED_LEAF_AUDIO_DATA, jackd->num_output_channels, (void **)out_buffer);
843                   weed_layer_set_audio_data(layer, out_buffer, jackd->sample_out_rate,
844                                             jackd->num_output_channels, numFramesToWrite);
845                   weed_set_boolean_value(layer, WEED_LEAF_HOST_KEEP_ADATA, WEED_TRUE);
846                   weed_apply_audio_effects_rt(layer, tc, FALSE, TRUE);
847                   xfltbuf = weed_layer_get_audio_data(layer, NULL);
848                   for (i = 0; i < jackd->num_output_channels; i++) {
849                     if (xfltbuf[i] != out_buffer[i]) {
850                       lives_memcpy(out_buffer[i], xfltbuf[i], numFramesToWrite * sizeof(float));
851                       lives_free(xfltbuf[i]);
852                     }
853                   }
854                   lives_free(xfltbuf);
855                   weed_layer_set_audio_data(layer, NULL, 0, 0, 0);
856                   weed_layer_free(layer);
857                 }
858 
859                 pthread_mutex_lock(&mainw->vpp_stream_mutex);
860                 if (mainw->ext_audio && mainw->vpp && mainw->vpp->render_audio_frame_float) {
861                   (*mainw->vpp->render_audio_frame_float)(out_buffer, numFramesToWrite);
862                 }
863                 pthread_mutex_unlock(&mainw->vpp_stream_mutex);
864               } else {
865                 // cache_buffer->die == TRUE
866                 pthread_mutex_unlock(&mainw->cache_buffer_mutex);
867                 output_silence(0, numFramesToWrite, jackd, out_buffer);
868               }
869             }
870 
871             if (jackd->astream_fd != -1) {
872               // audio streaming if enabled
873               unsigned char *xbuf;
874 
875               nbytes = numFramesToWrite * jackd->num_output_channels * 4;
876               rbytes = numFramesToWrite * jackd->num_output_channels * 2;
877 
878               if (pl_error) {
879                 // generator plugin error - output silence
880                 check_zero_buff(rbytes);
881                 audio_stream(zero_buff, rbytes, jackd->astream_fd);
882               } else {
883                 if ((mainw->agen_key == 0 && !mainw->agen_needs_reinit) && !mainw->multitrack && !mainw->preview)
884                   xbuf = (unsigned char *)cache_buffer->buffer16[0];
885                 else {
886                   // plugin is generating and we are streaming: convert fbuffer to s16
887                   float **fp = (float **)lives_malloc(jackd->num_output_channels * sizeof(float *));
888                   for (i = 0; i < jackd->num_output_channels; i++) {
889                     fp[i] = fbuffer + i;
890                   }
891                   xbuf = (unsigned char *)lives_malloc(nbytes * jackd->num_output_channels);
892                   sample_move_float_int((void *)xbuf, fp, numFramesToWrite, 1.0,
893                                         jackd->num_output_channels, 16, 0, TRUE, TRUE, 1.0);
894                 }
895 
896                 if (jackd->num_output_channels != 2) {
897                   // need to remap channels to stereo (assumed for now)
898                   size_t bysize = 4, tsize = 0;
899                   unsigned char *inbuf, *oinbuf = NULL;
900 
901                   if ((mainw->agen_key != 0 || mainw->agen_needs_reinit) && !mainw->multitrack && !mainw->preview)
902                     inbuf = (unsigned char *)cache_buffer->buffer16[0];
903                   else oinbuf = inbuf = xbuf;
904 
905                   xbuf = (unsigned char *)lives_malloc(nbytes);
906                   if (!xbuf) {
907                     // external streaming
908                     rbytes = numFramesToWrite * jackd->num_output_channels * 2;
909                     if (check_zero_buff(rbytes))
910                       audio_stream(zero_buff, rbytes, jackd->astream_fd);
911                     return 0;
912                   }
913                   if (jackd->num_output_channels == 1) bysize = 2;
914                   while (nbytes > 0) {
915                     lives_memcpy(xbuf + tsize, inbuf, bysize);
916                     tsize += bysize;
917                     nbytes -= bysize;
918                     if (bysize == 2) {
919                       // duplicate mono channel
920                       lives_memcpy(xbuf + tsize, inbuf, bysize);
921                       tsize += bysize;
922                       nbytes -= bysize;
923                       inbuf += bysize;
924                     } else {
925                       // or skip extra channels
926                       inbuf += jackd->num_output_channels * 4;
927                     }
928                   }
929                   nbytes = numFramesToWrite * jackd->num_output_channels * 4;
930                   lives_freep((void **)&oinbuf);
931                 }
932 
933                 // push to stream
934                 rbytes = numFramesToWrite * jackd->num_output_channels * 2;
935                 audio_stream(xbuf, rbytes, jackd->astream_fd);
936                 if (((mainw->agen_key != 0 || mainw->agen_needs_reinit) && !mainw->multitrack
937                      && !mainw->preview) || xbuf != (unsigned char *)cache_buffer->buffer16[0]) lives_free(xbuf);
938               }
939             } // end audio stream
940             lives_freep((void **)&fbuffer);
941           } else {
942             // no generator plugin, but audio is muted
943             output_silence(0, numFramesToWrite, jackd, out_buffer);
944           }
945         } else {
946           // cached from files - multitrack mode
947           if (jackd->read_abuf > -1 && !jackd->mute) {
948             sample_move_abuf_float(out_buffer, jackd->num_output_channels, nframes, jackd->sample_out_rate, vol);
949 
950             if (jackd->astream_fd != -1) {
951               // audio streaming if enabled
952               unsigned char *xbuf = (unsigned char *)out_buffer;
953               nbytes = numFramesToWrite * jackd->num_output_channels * 4;
954 
955               if (jackd->num_output_channels != 2) {
956                 // need to remap channels to stereo (assumed for now)
957                 size_t bysize = 4, tsize = 0;
958                 unsigned char *inbuf = (unsigned char *)out_buffer;
959                 xbuf = (unsigned char *)lives_malloc(nbytes);
960                 if (!xbuf) {
961                   output_silence(0, numFramesToWrite, jackd, out_buffer);
962                   return 0;
963                 }
964 
965                 if (jackd->num_output_channels == 1) bysize = 2;
966                 while (nbytes > 0) {
967                   lives_memcpy(xbuf + tsize, inbuf, bysize);
968                   tsize += bysize;
969                   nbytes -= bysize;
970                   if (bysize == 2) {
971                     // duplicate mono channel
972                     lives_memcpy(xbuf + tsize, inbuf, bysize);
973                     tsize += bysize;
974                     nbytes -= bysize;
975                     inbuf += bysize;
976                   } else {
977                     // or skip extra channels
978                     inbuf += jackd->num_output_channels * 4;
979                   }
980                 }
981                 nbytes = numFramesToWrite * jackd->num_output_channels * 2;
982               }
983               rbytes = numFramesToWrite * jackd->num_output_channels * 2;
984               audio_stream(xbuf, rbytes, jackd->astream_fd);
985               if (xbuf != (unsigned char *)out_buffer) lives_free(xbuf);
986             }
987           } else {
988             // muted or no audio available
989             output_silence(0, numFramesToWrite, jackd, out_buffer);
990           }
991         }
992       } else {
993         // no input frames left, pad with silence
994         output_silence(nframes - jackFramesAvailable, jackFramesAvailable, jackd, out_buffer);
995         jackFramesAvailable = 0;
996       }
997     }
998 
999     if (!from_memory) {
1000       // push the cache_buffer to be filled
1001       if (!mainw->multitrack && !wait_cache_buffer && ((mainw->agen_key == 0 && ! mainw->agen_needs_reinit)
1002           || mainw->preview)) {
1003         push_cache_buffer(cache_buffer, jackd, in_bytes, nframes, shrink_factor);
1004       }
1005       /// advance the seek pos even if we are reading from a generator
1006       /// audio gen outptut is float, so convert to playing file bytesize
1007       if (shrink_factor > 0.) jackd->seek_pos += xin_bytes / 4 * jackd->bytes_per_channel;
1008     }
1009 
1010     /*    jackd->jack_jack[0]=set_jack(out_buffer[0],jack->buffer_size,8);
1011       if (jackd->num_output_channels>1) {
1012       jackd->jack_jack[1]=set_jack(out_buffer[1],jackd->buffer_size,8);
1013       }
1014       else jackd->jack_jack[1]=jackd->jack_jack[0];
1015     */
1016 
1017     if (jackFramesAvailable > 0) {
1018 #ifdef DEBUG_AJACK
1019       ++mainw->uflow_count;
1020       lives_printerr("buffer underrun of %ld frames\n", jackFramesAvailable);
1021 #endif
1022       output_silence(nframes - jackFramesAvailable, jackFramesAvailable, jackd, out_buffer);
1023     }
1024   } else if (jackd->state == JackTransportStarting || jackd->state == JackTransportStopped ||
1025              jackd->state == JackTClosed || jackd->state == JackTReset) {
1026 #ifdef DEBUG_AJACK
1027     lives_printerr("PAUSED or STOPPED or CLOSED, outputting silence\n");
1028 #endif
1029 
1030     /* output silence if nothing is being outputted */
1031     output_silence(0, nframes, jackd, out_buffer);
1032     jackd->is_silent = TRUE;
1033 
1034     /* if we were told to reset then zero out some variables */
1035     /* and transition to STOPPED */
1036     if (jackd->state == JackTReset) {
1037       jackd->state = (jack_transport_state_t)JackTStopped; /* transition to STOPPED */
1038     }
1039   }
1040 
1041 #ifdef DEBUG_AJACK
1042   lives_printerr("done\n");
1043 #endif
1044 
1045   return 0;
1046 }
1047 
1048 
lives_start_ready_callback(jack_transport_state_t state,jack_position_t * pos,void * arg)1049 int lives_start_ready_callback(jack_transport_state_t state, jack_position_t *pos, void *arg) {
1050   // mainw->video_seek_ready is generally FALSE
1051   // if we are not playing, the transport poll should start playing which will set set
1052   // mainw->video_seek_ready to true, as soon as the video is at the right place
1053 
1054   // if we are playing, we set mainw->scratch
1055   // this will either force a resync of audio in free playback
1056   // or reset the event_list position in multitrack playback
1057 
1058   /// TODO ****::   NEEDS retesting !!!!!
1059 
1060   // go away until the app has started up properly
1061   if (mainw->go_away) {
1062     if (state == JackTransportStopped) mainw->jack_can_start = TRUE;
1063     else mainw->jack_can_start = mainw->jack_can_stop = FALSE;
1064     return TRUE;
1065   }
1066 
1067   if (!(prefs->jack_opts & JACK_OPTS_TRANSPORT_CLIENT)) return TRUE;
1068   if (!jack_transport_client) return TRUE;
1069 
1070   if (!LIVES_IS_PLAYING && state == JackTransportStopped) {
1071     if (prefs->jack_opts & JACK_OPTS_TIMEBASE_CLIENT) {
1072       double trtime = (double)jack_transport_get_current_ticks() / TICKS_PER_SECOND_DBL;
1073       if (!mainw->multitrack) {
1074 #ifndef ENABLE_GIW_3
1075         lives_ruler_set_value(LIVES_RULER(mainw->hruler), x);
1076         lives_widget_queue_draw_if_visible(mainw->hruler);
1077 #else
1078         lives_adjustment_set_value(giw_timeline_get_adjustment(GIW_TIMELINE(mainw->hruler)), trtime);
1079 #endif
1080       } else mt_tl_move(mainw->multitrack, trtime);
1081     }
1082     return TRUE;
1083   }
1084 
1085   if (state != JackTransportStarting) return TRUE;
1086 
1087   if (LIVES_IS_PLAYING && (prefs->jack_opts & JACK_OPTS_TIMEBASE_CLIENT)) {
1088     // trigger audio resync
1089     mainw->scratch = SCRATCH_JUMP;
1090   }
1091 
1092   return (mainw->video_seek_ready & mainw->audio_seek_ready);
1093 }
1094 
1095 
jack_flush_read_data(size_t rbytes,void * data)1096 size_t jack_flush_read_data(size_t rbytes, void *data) {
1097   // rbytes here is how many bytes to write
1098   size_t bytes = 0;
1099 
1100   if (!data) {
1101     // final flush at end
1102     data = jrbuf;
1103     rbytes = jrb;
1104   }
1105 
1106   jrb = 0;
1107 
1108   if (!THREADVAR(bad_aud_file)) {
1109     // use write not use lives_write - because of potential threading issues
1110     bytes = write(mainw->aud_rec_fd, data, rbytes);
1111     if (bytes > 0) {
1112       uint64_t chk = (mainw->aud_data_written & AUD_WRITE_CHECK);
1113       mainw->aud_data_written += bytes;
1114       if (mainw->ascrap_file != -1 && mainw->files[mainw->ascrap_file] &&
1115           mainw->aud_rec_fd == mainw->files[mainw->ascrap_file]->cb_src)
1116         add_to_ascrap_mb(bytes);
1117       check_for_disk_space((mainw->aud_data_written & AUD_WRITE_CHECK) != chk);
1118     }
1119     if (bytes < rbytes) THREADVAR(bad_aud_file) = filename_from_fd(NULL, mainw->aud_rec_fd);
1120   }
1121   return bytes;
1122 }
1123 
1124 
audio_read_inner(jack_driver_t * jackd,float ** in_buffer,int ofileno,int nframes,double out_scale,boolean rev_endian,boolean out_unsigned,size_t rbytes)1125 static size_t audio_read_inner(jack_driver_t *jackd, float **in_buffer, int ofileno, int nframes,
1126                                double out_scale, boolean rev_endian, boolean out_unsigned, size_t rbytes) {
1127 
1128   // read audio, buffer it and when we have enough, write it to aud_rec_fd
1129 
1130   int frames_out;
1131 
1132   size_t bytes_out;
1133 
1134   void *holding_buff;
1135 
1136   lives_clip_t *ofile = mainw->files[ofileno];
1137 
1138   frames_out = (int64_t)((double)nframes / out_scale + 1.);
1139   bytes_out = frames_out * ofile->achans * (ofile->asampsize >> 3);
1140 
1141   holding_buff = lives_malloc(bytes_out);
1142   if (!holding_buff) return 0;
1143 
1144   frames_out = sample_move_float_int(holding_buff, in_buffer, nframes, out_scale, ofile->achans,
1145                                      ofile->asampsize, out_unsigned, rev_endian, FALSE, 1.);
1146 
1147   if (mainw->rec_samples > 0) {
1148     if (frames_out > mainw->rec_samples) frames_out = mainw->rec_samples;
1149     mainw->rec_samples -= frames_out;
1150   }
1151 
1152   rbytes = frames_out * (ofile->asampsize / 8) * ofile->achans;
1153   jrb += rbytes;
1154 
1155   // write to jrbuf
1156   if (jrb < JACK_READ_BYTES && (mainw->rec_samples == -1 || frames_out < mainw->rec_samples)) {
1157     // buffer until we have enough
1158     lives_memcpy(&jrbuf[jrb - rbytes], holding_buff, rbytes);
1159     return rbytes;
1160   }
1161 
1162   // if we have enough, flush it to file
1163   if (jrb <= JACK_READ_BYTES * 2) {
1164     lives_memcpy(&jrbuf[jrb - rbytes], holding_buff, rbytes);
1165     jack_flush_read_data(jrb, jrbuf);
1166   } else {
1167     if (jrb > rbytes) jack_flush_read_data(jrb - rbytes, jrbuf);
1168     jack_flush_read_data(rbytes, holding_buff);
1169   }
1170 
1171   lives_free(holding_buff);
1172 
1173   return rbytes;
1174 }
1175 
1176 
audio_read(nframes_t nframes,void * arg)1177 static int audio_read(nframes_t nframes, void *arg) {
1178   // read nframes from jack buffer, and then write to mainw->aud_rec_fd
1179 
1180   // this is the jack callback for when we are recording audio
1181 
1182   // for AUDIO_SRC_EXT, jackd->playing_file is actually the file we write audio to
1183   // which can be either the ascrap file (for playback recording), or a normal file (for voiceovers), or -1 (just listening)
1184 
1185   // TODO - get abs_maxvol_heard
1186 
1187   jack_driver_t *jackd = (jack_driver_t *)arg;
1188   float *in_buffer[jackd->num_input_channels];
1189   float out_scale;
1190   float tval = 0;
1191   int out_unsigned = AFORM_UNSIGNED;
1192   int i;
1193 
1194   size_t rbytes = 0;
1195 
1196   if (!jackd->in_use) return 0;
1197 
1198   if (mainw->playing_file < 0 && prefs->audio_src == AUDIO_SRC_EXT) return 0;
1199 
1200   if (mainw->effects_paused) return 0; // pause during record
1201 
1202   if (mainw->rec_samples == 0) return 0; // wrote enough already, return until main thread stop
1203 
1204   for (i = 0; i < jackd->num_input_channels; i++) {
1205     in_buffer[i] = (float *) jack_port_get_buffer(jackd->input_port[i], nframes);
1206     tval += *in_buffer[i];
1207   }
1208 
1209   if (!mainw->fs && !mainw->faded && !mainw->multitrack && mainw->ext_audio_mon)
1210     lives_toggle_tool_button_set_active(LIVES_TOGGLE_TOOL_BUTTON(mainw->ext_audio_mon), tval > 0.);
1211 
1212   jackd->frames_written += nframes;
1213 
1214   if (prefs->audio_src == AUDIO_SRC_EXT && (jackd->playing_file == -1 || jackd->playing_file == mainw->ascrap_file)) {
1215     // TODO - dont apply filters when doing ext window grab, or voiceover
1216 
1217     // in this case we read external audio, but maybe not record it
1218     // we may wish to analyse the audio for example, or push it to a video generator
1219 
1220     if (has_audio_filters(AF_TYPE_A)) { // AF_TYPE_A are Analyser filters (audio in but no audio channels out)
1221       ticks_t tc = mainw->currticks;
1222       weed_layer_t *layer = weed_layer_new(WEED_LAYER_TYPE_AUDIO);
1223 
1224       if (mainw->audio_frame_buffer && prefs->audio_src == AUDIO_SRC_EXT) {
1225         // if we have audio triggered gens., push audio to it
1226         for (i = 0; i < jackd->num_input_channels; i++) {
1227           append_to_audio_bufferf(in_buffer[i], nframes, i);
1228         }
1229         mainw->audio_frame_buffer->samples_filled += nframes;
1230       }
1231       // apply any audio effects with in_channels and no out_channels
1232       weed_layer_set_audio_data(layer, in_buffer, jackd->sample_in_rate, jackd->num_output_channels, nframes);
1233       weed_apply_audio_effects_rt(layer, tc, TRUE, TRUE);
1234       weed_layer_set_audio_data(layer, NULL, 0, 0, 0);
1235       weed_layer_free(layer);
1236     }
1237   }
1238 
1239   pthread_mutex_lock(&mainw->audio_filewriteend_mutex);
1240 
1241   if (mainw->record && mainw->record_paused && jrb > 0) {
1242     jack_flush_read_data(jrb, jrbuf);
1243   }
1244 
1245   if (jackd->playing_file == -1 || (mainw->record && mainw->record_paused)) {
1246     jrb = 0;
1247     pthread_mutex_unlock(&mainw->audio_filewriteend_mutex);
1248     return 0;
1249   }
1250 
1251   if (!IS_VALID_CLIP(jackd->playing_file)) out_scale = 1.0; // just listening
1252   else {
1253     out_scale = (float)afile->arate / (float)jackd->sample_in_rate; // recording to ascrap_file
1254     out_unsigned = afile->signed_endian & AFORM_UNSIGNED;
1255   }
1256 
1257   rbytes = audio_read_inner(jackd, in_buffer, jackd->playing_file, nframes, out_scale, jackd->reverse_endian,
1258                             out_unsigned, rbytes);
1259 
1260   if (mainw->playing_file != mainw->ascrap_file && IS_VALID_CLIP(mainw->playing_file))
1261     mainw->files[mainw->playing_file]->aseek_pos += rbytes;
1262   if (mainw->ascrap_file != -1 && !mainw->record_paused) mainw->files[mainw->ascrap_file]->aseek_pos += rbytes;
1263 
1264   jackd->seek_pos += rbytes;
1265 
1266   pthread_mutex_unlock(&mainw->audio_filewriteend_mutex);
1267 
1268   if (mainw->rec_samples == 0 && mainw->cancelled == CANCEL_NONE) mainw->cancelled = CANCEL_KEEP; // we wrote the required #
1269 
1270   return 0;
1271 }
1272 
1273 
jack_get_srate(nframes_t nframes,void * arg)1274 int jack_get_srate(nframes_t nframes, void *arg) {
1275   //lives_printerr("the sample rate is now %ld/sec\n", (int64_t)nframes);
1276   // TODO: reset timebase
1277   return 0;
1278 }
1279 
1280 
jack_shutdown(void * arg)1281 void jack_shutdown(void *arg) {
1282   jack_driver_t *jackd = (jack_driver_t *)arg;
1283 
1284   jackd->client = NULL; /* reset client */
1285   jackd->jackd_died = TRUE;
1286   jackd->msgq = NULL;
1287 
1288   lives_printerr("jack shutdown, setting client to 0 and jackd_died to true\n");
1289   lives_printerr("trying to reconnect right now\n");
1290 
1291   /////////////////////
1292 
1293   jack_audio_init();
1294 
1295   // TODO: init reader as well
1296 
1297   mainw->jackd = jack_get_driver(0, TRUE);
1298   mainw->jackd->msgq = NULL;
1299 
1300   if (mainw->jackd->playing_file != -1 && afile)
1301     jack_audio_seek_bytes(mainw->jackd, mainw->jackd->seek_pos, afile); // at least re-seek to the right place
1302 }
1303 
1304 
jack_reset_driver(jack_driver_t * jackd)1305 static void jack_reset_driver(jack_driver_t *jackd) {
1306   // does nothing ?
1307   jackd->state = (jack_transport_state_t)JackTReset;
1308 }
1309 
1310 
jack_close_device(jack_driver_t * jackd)1311 void jack_close_device(jack_driver_t *jackd) {
1312   //int i;
1313 
1314   //lives_printerr("closing the jack client thread\n");
1315   if (jackd->client) {
1316     jack_deactivate(jackd->client); /* supposed to help the jack_client_close() to succeed */
1317     //lives_printerr("after jack_deactivate()\n");
1318     jack_client_close(jackd->client);
1319   }
1320 
1321   jack_reset_driver(jackd);
1322   jackd->client = NULL;
1323 
1324   jackd->is_active = FALSE;
1325 
1326   /* free up the port strings */
1327   //lives_printerr("freeing up port strings\n");
1328   // TODO: check this
1329   /* if (jackd->jack_port_name_count > 1) { */
1330   /*   for (i = 0; i < jackd->jack_port_name_count; i++) free(jackd->jack_port_name[i]); */
1331   /*   free(jackd->jack_port_name); */
1332   /* } */
1333 }
1334 
1335 
jack_error_func(const char * desc)1336 static void jack_error_func(const char *desc) {
1337   lives_printerr("Jack audio error %s\n", desc);
1338 }
1339 
1340 
1341 // create a new client but don't connect the ports yet
jack_create_client_writer(jack_driver_t * jackd)1342 boolean jack_create_client_writer(jack_driver_t *jackd) {
1343   const char *client_name = "LiVES_audio_out";
1344   const char *server_name = JACK_DEFAULT_SERVER_NAME;
1345   jack_options_t options = (jack_options_t)((int)JackServerName | (int)JackNoStartServer);
1346   jack_status_t status;
1347   boolean needs_sigs = FALSE;
1348   lives_alarm_t alarm_handle;
1349   int i;
1350 
1351   if (mainw->aplayer_broken) return FALSE;
1352 
1353   jackd->is_active = FALSE;
1354 
1355   /* set up an error handler */
1356   jack_set_error_function(jack_error_func);
1357   jackd->client = NULL;
1358 
1359   if (!mainw->signals_deferred) {
1360     // try to handle crashes in jack_client_open()
1361     set_signal_handlers((SignalHandlerPointer)defer_sigint);
1362     needs_sigs = TRUE;
1363     mainw->crash_possible = 1;
1364   }
1365 
1366   alarm_handle = lives_alarm_set(LIVES_SHORT_TIMEOUT);
1367   while (!jackd->client && lives_alarm_check(alarm_handle) > 0) {
1368     jackd->client = jack_client_open(client_name, options, &status, server_name);
1369     lives_usleep(prefs->sleep_time);
1370   }
1371   lives_alarm_clear(alarm_handle);
1372 
1373   if (needs_sigs) {
1374     set_signal_handlers((SignalHandlerPointer)catch_sigint);
1375     mainw->crash_possible = 0;
1376   }
1377 
1378   if (!jackd->client) {
1379     lives_printerr("jack_client_open() failed, status = 0x%2.0x\n", status);
1380     if (status & JackServerFailed) {
1381       d_print(_("Unable to connect to JACK server\n"));
1382     }
1383     return FALSE;
1384   }
1385 
1386   if (status & JackNameNotUnique) {
1387     client_name = jack_get_client_name(jackd->client);
1388     lives_printerr("unique name `%s' assigned\n", client_name);
1389   }
1390 
1391   jackd->sample_out_rate = jackd->sample_in_rate = jack_get_sample_rate(jackd->client);
1392 
1393   //lives_printerr (lives_strdup_printf("engine sample rate: %ld\n",jackd->sample_rate));
1394 
1395   for (i = 0; i < jackd->num_output_channels; i++) {
1396     char portname[32];
1397     lives_snprintf(portname, 32, "out_%d", i);
1398 
1399 #ifdef DEBUG_JACK_PORTS
1400     lives_printerr("output port %d is named '%s'\n", i, portname);
1401 #endif
1402 
1403     jackd->output_port[i] = jack_port_register(jackd->client, portname,
1404                             JACK_DEFAULT_AUDIO_TYPE,
1405                             JackPortIsOutput | JackPortIsTerminal,
1406                             0);
1407 
1408     if (!jackd->output_port[i]) {
1409       lives_printerr("nay more JACK output ports available\n");
1410       return FALSE;
1411     }
1412     jackd->out_chans_available++;
1413   }
1414 
1415   /* tell the JACK server to call `srate()' whenever
1416      the sample rate of the system changes. */
1417   jack_set_sample_rate_callback(jackd->client, jack_get_srate, jackd);
1418 
1419   /* tell the JACK server to call `jack_shutdown()' if
1420      it ever shuts down, either entirely, or if it
1421      just decides to stop calling us. */
1422   jack_on_shutdown(jackd->client, jack_shutdown, jackd);
1423 
1424   jack_set_process_callback((jack_client_t *)jackd->client, audio_process, jackd);
1425 
1426   return TRUE;
1427 }
1428 
1429 
jack_create_client_reader(jack_driver_t * jackd)1430 boolean jack_create_client_reader(jack_driver_t *jackd) {
1431   // open a device to read audio from jack
1432   const char *client_name = "LiVES_audio_in";
1433   const char *server_name = JACK_DEFAULT_SERVER_NAME;
1434   jack_options_t options = (jack_options_t)((int)JackServerName | (int)JackNoStartServer);
1435   jack_status_t status;
1436   int i;
1437 
1438   jackd->is_active = FALSE;
1439 
1440   /* set up an error handler */
1441   jack_set_error_function(jack_error_func);
1442   jackd->client = NULL;
1443 
1444   // create a client and attach it to the server
1445   while (!jackd->client)
1446     jackd->client = jack_client_open(client_name, options, &status, server_name);
1447 
1448   if (!jackd->client) {
1449     lives_printerr("jack_client_open() failed, status = 0x%2.0x\n", status);
1450     if (status & JackServerFailed) {
1451       d_print(_("Unable to connect to JACK server\n"));
1452     }
1453     return FALSE;
1454   }
1455 
1456   if (status & JackNameNotUnique) {
1457     client_name = jack_get_client_name(jackd->client);
1458     lives_printerr("unique name `%s' assigned\n", client_name);
1459   }
1460 
1461   jackd->sample_in_rate = jackd->sample_out_rate = jack_get_sample_rate(jackd->client);
1462 
1463   //lives_printerr (lives_strdup_printf("engine sample rate: %ld\n",jackd->sample_rate));
1464 
1465   // create ports for the client (left and right channels)
1466   for (i = 0; i < jackd->num_input_channels; i++) {
1467     char portname[32];
1468     lives_snprintf(portname, 32, "in_%d", i);
1469 
1470 #ifdef DEBUG_JACK_PORTS
1471     lives_printerr("input port %d is named '%s'\n", i, portname);
1472 #endif
1473     jackd->input_port[i] = jack_port_register(jackd->client, portname,
1474                            JACK_DEFAULT_AUDIO_TYPE,
1475                            JackPortIsInput | JackPortIsTerminal,
1476                            0);
1477     if (!jackd->input_port[i]) {
1478       lives_printerr("ne more JACK input ports available\n");
1479       return FALSE;
1480     }
1481   }
1482 
1483   /* tell the JACK server to call `srate()' whenever
1484      the sample rate of the system changes. */
1485   jack_set_sample_rate_callback(jackd->client, jack_get_srate, jackd);
1486 
1487   /* tell the JACK server to call `jack_shutdown()' if
1488      it ever shuts down, either entirely, or if it
1489      just decides to stop calling us. */
1490   jack_on_shutdown(jackd->client, jack_shutdown, jackd);
1491 
1492   jrb = 0;
1493   // set process callback and start
1494   jack_set_process_callback(jackd->client, audio_read, jackd);
1495 
1496   return 0;
1497 }
1498 
1499 
jack_write_driver_activate(jack_driver_t * jackd)1500 boolean jack_write_driver_activate(jack_driver_t *jackd) {
1501   // connect client and activate it
1502   int i;
1503   const char **ports;
1504   boolean failed = FALSE;
1505 
1506   if (jackd->is_active) return TRUE; // already running
1507 
1508   /* tell the JACK server that we are ready to roll */
1509   if (jack_activate(jackd->client)) {
1510     LIVES_ERROR("Cannot activate jack writer client");
1511     return FALSE;
1512   }
1513 
1514   // we are looking for input ports to connect to
1515   jackd->jack_port_flags |= JackPortIsInput;
1516 
1517   ports = jack_get_ports(jackd->client, NULL, NULL, jackd->jack_port_flags);
1518 
1519   if (!ports) {
1520     LIVES_ERROR("No jack input ports available !");
1521     return FALSE;
1522   }
1523 
1524   for (i = 0; ports[i]; i++) {
1525 #ifdef DEBUG_JACK_PORTS
1526     lives_printerr("ports[%d] = '%s'\n", i, ports[i]);
1527 #endif
1528   }
1529   jackd->in_chans_available = i;
1530 
1531   if (jackd->in_chans_available < jackd->num_output_channels) {
1532 #ifdef DEBUG_JACK_PORTS
1533     lives_printerr("ERR: jack_get_ports() failed to find enough ports with jack port flags of 0x%lX'\n", jackd->jack_port_flags);
1534 #endif
1535     free(ports);
1536     LIVES_ERROR("Not enough jack input ports available !");
1537     return FALSE;
1538   }
1539 
1540   /* connect the ports. Note: you can't do this before
1541      the client is activated (this may change in the future). */
1542   for (i = 0; i < jackd->num_output_channels; i++) {
1543 #ifdef DEBUG_JACK_PORTS
1544     lives_printerr("jack_connect() %s to port %d('%s')\n", jack_port_name(jackd->output_port[i]), i, ports[i]);
1545 #endif
1546     if (jack_connect(jackd->client, jack_port_name(jackd->output_port[i]), ports[i])) {
1547 #ifdef DEBUG_JACK_PORTS
1548       lives_printerr("cannot connect to output port %d('%s')\n", i, ports[i]);
1549 #endif
1550       LIVES_ERROR("Cannot connect all our jack outputs");
1551       failed = TRUE;
1552     }
1553   }
1554 
1555   free(ports);
1556 
1557   /* if something failed we need to shut the client down and return 0 */
1558   if (failed) {
1559     LIVES_ERROR("Jack writer creation failed, closing and returning error");
1560     jack_close_device(jackd);
1561     return FALSE;
1562   }
1563 
1564   // start using soundcard as timer source
1565   prefs->force_system_clock = FALSE;
1566 
1567   jackd->is_active = TRUE;
1568   jackd->jackd_died = FALSE;
1569   jackd->in_use = FALSE;
1570   jackd->is_paused = FALSE;
1571 
1572   d_print(_("Started jack audio subsystem.\n"));
1573 
1574   return TRUE;
1575 }
1576 
1577 
jack_read_driver_activate(jack_driver_t * jackd,boolean autocon)1578 boolean jack_read_driver_activate(jack_driver_t *jackd, boolean autocon) {
1579   // connect driver for reading
1580   const char **ports;
1581   boolean failed = FALSE;
1582   int i;
1583 
1584   if (!jackd->is_active) {
1585     if (jack_activate(jackd->client)) {
1586       LIVES_ERROR("Cannot activate jack reader client");
1587       return FALSE;
1588     }
1589   }
1590 
1591   if (!autocon && (prefs->jack_opts & JACK_OPTS_NO_READ_AUTOCON)) goto jackreadactive;
1592 
1593   // we are looking for output ports to connect to
1594   jackd->jack_port_flags |= JackPortIsOutput;
1595 
1596   ports = jack_get_ports(jackd->client, NULL, NULL, jackd->jack_port_flags);
1597 
1598   if (!ports) {
1599     LIVES_ERROR("No jack output ports available !");
1600     return FALSE;
1601   }
1602 
1603   for (i = 0; ports[i]; i++) {
1604 #ifdef DEBUG_JACK_PORTS
1605     lives_printerr("ports[%d] = '%s'\n", i, ports[i]);
1606 #endif
1607   }
1608   jackd->out_chans_available = i;
1609 
1610   if (jackd->out_chans_available < jackd->num_input_channels) {
1611 #ifdef DEBUG_JACK_PORTS
1612     lives_printerr("ERR: jack_get_ports() failed to find enough ports with jack port flags of 0x%lX'\n", jackd->jack_port_flags);
1613 #endif
1614     free(ports);
1615     LIVES_ERROR("Not enough jack output ports available !");
1616     return FALSE;
1617   }
1618 
1619   for (i = 0; i < jackd->num_input_channels; i++) {
1620 #ifdef DEBUG_JACK_PORTS
1621     lives_printerr("jack_connect() %s to port name %d('%s')\n", jack_port_name(jackd->input_port[i]), i, ports[i]);
1622 #endif
1623     if (jack_connect(jackd->client, ports[i], jack_port_name(jackd->input_port[i]))) {
1624 #ifdef DEBUG_JACK_PORTS
1625       lives_printerr("cannot connect to input port %d('%s')\n", i, ports[i]);
1626 #endif
1627       LIVES_ERROR("Cannot connect all our jack inputs");
1628       failed = TRUE;
1629     }
1630   }
1631 
1632   free(ports);
1633 
1634   if (failed) {
1635     lives_printerr("Failed, closing and returning error\n");
1636     jack_close_device(jackd);
1637     return FALSE;
1638   }
1639 
1640   // do we need to be connected for this ?
1641   // start using soundcard as timer source
1642   //prefs->force_system_clock = FALSE;
1643 
1644 jackreadactive:
1645 
1646   jackd->is_active = TRUE;
1647   jackd->jackd_died = FALSE;
1648   jackd->in_use = FALSE;
1649   jackd->is_paused = FALSE;
1650   jackd->nframes_start = 0;
1651   d_print(_("Started jack audio reader.\n"));
1652 
1653   // start using soundcard as timer source
1654   prefs->force_system_clock = FALSE;
1655 
1656   return TRUE;
1657 }
1658 
1659 
jack_get_driver(int dev_idx,boolean is_output)1660 jack_driver_t *jack_get_driver(int dev_idx, boolean is_output) {
1661   jack_driver_t *jackd;
1662 
1663   if (is_output) jackd = &outdev[dev_idx];
1664   else jackd = &indev[dev_idx];
1665 #ifdef TRACE_getReleaseDevice
1666   lives_printerr("dev_idx is %d\n", dev_idx);
1667 #endif
1668 
1669   return jackd;
1670 }
1671 
1672 
jack_audio_init(void)1673 int jack_audio_init(void) {
1674   // initialise variables
1675   int i, j;
1676   jack_driver_t *jackd;
1677 
1678   for (i = 0; i < JACK_MAX_OUTDEVICES; i++) {
1679     jackd = &outdev[i];
1680     //jack_reset_dev(i, TRUE);
1681     jackd->dev_idx = i;
1682     jackd->client = NULL;
1683     jackd->in_use = FALSE;
1684     for (j = 0; j < JACK_MAX_OUTPUT_PORTS; j++) jackd->volume[j] = 1.0f;
1685     jackd->state = (jack_transport_state_t)JackTClosed;
1686     jackd->sample_out_rate = jackd->sample_in_rate = 0;
1687     jackd->seek_pos = jackd->seek_end = jackd->real_seek_pos = 0;
1688     jackd->msgq = NULL;
1689     jackd->num_calls = 0;
1690     jackd->astream_fd = -1;
1691     jackd->abs_maxvol_heard = 0.;
1692     jackd->jackd_died = FALSE;
1693     jackd->num_output_channels = 2;
1694     jackd->play_when_stopped = FALSE;
1695     jackd->mute = FALSE;
1696     jackd->is_silent = FALSE;
1697     jackd->out_chans_available = 0;
1698     jackd->is_output = TRUE;
1699     jackd->read_abuf = -1;
1700     jackd->playing_file = -1;
1701     jackd->frames_written = 0;
1702   }
1703   return 0;
1704 }
1705 
1706 
jack_audio_read_init(void)1707 int jack_audio_read_init(void) {
1708   int i, j;
1709   jack_driver_t *jackd;
1710 
1711   for (i = 0; i < JACK_MAX_INDEVICES; i++) {
1712     jackd = &indev[i];
1713     //jack_reset_dev(i, FALSE);
1714     jackd->dev_idx = i;
1715     jackd->client = NULL;
1716     jackd->in_use = FALSE;
1717     for (j = 0; j < JACK_MAX_INPUT_PORTS; j++) jackd->volume[j] = 1.0f;
1718     jackd->state = (jack_transport_state_t)JackTClosed;
1719     jackd->sample_out_rate = jackd->sample_in_rate = 0;
1720     jackd->seek_pos = jackd->seek_end = jackd->real_seek_pos = 0;
1721     jackd->msgq = NULL;
1722     jackd->num_calls = 0;
1723     jackd->astream_fd = -1;
1724     jackd->abs_maxvol_heard = 0.;
1725     jackd->jackd_died = FALSE;
1726     jackd->num_input_channels = 2;
1727     jackd->play_when_stopped = FALSE;
1728     jackd->mute = FALSE;
1729     jackd->in_chans_available = 0;
1730     jackd->is_output = FALSE;
1731     jackd->playing_file = -1;
1732     jackd->frames_written = 0;
1733   }
1734   return 0;
1735 }
1736 
1737 
jack_get_msgq(jack_driver_t * jackd)1738 volatile aserver_message_t *jack_get_msgq(jack_driver_t *jackd) {
1739   if (jackd->jackd_died || mainw->aplayer_broken) return NULL;
1740   return jackd->msgq;
1741 }
1742 
1743 
jack_time_reset(jack_driver_t * jackd,int64_t offset)1744 void jack_time_reset(jack_driver_t *jackd, int64_t offset) {
1745   jackd->nframes_start = jack_frame_time(jack_transport_client) + (jack_nframes_t)((float)(offset / USEC_TO_TICKS) *
1746                          (jack_get_sample_rate(jackd->client) / 1000000.));
1747   jackd->frames_written = 0;
1748   mainw->currticks = offset;
1749   mainw->deltaticks = mainw->startticks = 0;
1750 }
1751 
1752 
lives_jack_get_time(jack_driver_t * jackd)1753 ticks_t lives_jack_get_time(jack_driver_t *jackd) {
1754   // get the time in ticks since playback started
1755   volatile aserver_message_t *msg = jackd->msgq;
1756   jack_nframes_t frames, retframes;
1757   static jack_nframes_t last_frames = 0;
1758 
1759   if (!jackd->client) return -1;
1760 
1761   if (msg && msg->command == ASERVER_CMD_FILE_SEEK) {
1762     ticks_t timeout;
1763     lives_alarm_t alarm_handle = lives_alarm_set(LIVES_DEFAULT_TIMEOUT);
1764     while ((timeout = lives_alarm_check(alarm_handle)) > 0 && jack_get_msgq(jackd)) {
1765       sched_yield(); // wait for seek
1766       lives_usleep(prefs->sleep_time);
1767     }
1768     lives_alarm_clear(alarm_handle);
1769     if (timeout == 0) return -1;
1770   }
1771 
1772   frames = jack_frame_time(jackd->client);
1773 
1774   retframes = frames;
1775   if (last_frames > 0 && frames <= last_frames) {
1776     retframes += jackd->frames_written;
1777   } else jackd->frames_written = 0;
1778   last_frames = frames;
1779 
1780   return (ticks_t)((frames - jackd->nframes_start) * (1000000. / jack_get_sample_rate(
1781                      jackd->client)) * USEC_TO_TICKS);
1782 }
1783 
1784 
lives_jack_get_pos(jack_driver_t * jackd)1785 double lives_jack_get_pos(jack_driver_t *jackd) {
1786   // get current time position (seconds) in audio file
1787   if (jackd->playing_file > -1)
1788     return fwd_seek_pos / (double)(afile->arps * afile->achans * afile->asampsize / 8);
1789   // from memory
1790   return (double)jackd->frames_written / (double)jackd->sample_out_rate;
1791 
1792 }
1793 
1794 
jack_audio_seek_frame(jack_driver_t * jackd,double frame)1795 boolean jack_audio_seek_frame(jack_driver_t *jackd, double frame) {
1796   // seek to frame "frame" in current audio file
1797   // position will be adjusted to (floor) nearest sample
1798 
1799   volatile aserver_message_t *jmsg;
1800   int64_t seekstart;
1801   ticks_t timeout;
1802   double thresh = 0., delta = 0.;
1803   lives_alarm_t alarm_handle = lives_alarm_set(LIVES_DEFAULT_TIMEOUT);
1804 
1805   if (alarm_handle == ALL_USED) return FALSE;
1806 
1807   if (frame < 1) frame = 1;
1808 
1809   do {
1810     jmsg = jack_get_msgq(jackd);
1811   } while ((timeout = lives_alarm_check(alarm_handle)) > 0 && jmsg && jmsg->command != ASERVER_CMD_FILE_SEEK);
1812   lives_alarm_clear(alarm_handle);
1813   if (timeout == 0 || jackd->playing_file == -1) {
1814     return FALSE;
1815   }
1816   if (frame > afile->frames && afile->frames != 0) frame = afile->frames;
1817   seekstart = (int64_t)((double)(frame - 1.) / afile->fps * afile->arps) * afile->achans * (afile->asampsize / 8);
1818   if (cache_buffer) {
1819     delta = (double)(seekstart - lives_buffered_offset(cache_buffer->_fd)) / (double)(afile->arps * afile->achans *
1820             (afile->asampsize / 8));
1821     thresh = 1. / (double)afile->fps;
1822   }
1823   if (delta >= thresh || delta <= -thresh)
1824     jack_audio_seek_bytes(jackd, seekstart, afile);
1825   return TRUE;
1826 }
1827 
1828 
jack_audio_seek_bytes(jack_driver_t * jackd,int64_t bytes,lives_clip_t * sfile)1829 int64_t jack_audio_seek_bytes(jack_driver_t *jackd, int64_t bytes, lives_clip_t *sfile) {
1830   // seek to position "bytes" in current audio file
1831   // position will be adjusted to (floor) nearest sample
1832 
1833   // if the position is > size of file, we will seek to the end of the file
1834 
1835   volatile aserver_message_t *jmsg;
1836   int64_t seekstart;
1837 
1838   ticks_t timeout;
1839   lives_alarm_t alarm_handle = lives_alarm_set(LIVES_DEFAULT_TIMEOUT);
1840 
1841   fwd_seek_pos = bytes;
1842 
1843   seek_err = FALSE;
1844 
1845   if (alarm_handle == -1) {
1846     LIVES_WARN("Invalid alarm handle");
1847     return 0;
1848   }
1849 
1850   if (jackd->in_use) {
1851     do {
1852       jmsg = jack_get_msgq(jackd);
1853     } while ((timeout = lives_alarm_check(alarm_handle)) > 0 && jmsg && jmsg->command != ASERVER_CMD_FILE_SEEK);
1854     lives_alarm_clear(alarm_handle);
1855     if (timeout == 0 || jackd->playing_file == -1) {
1856       if (timeout == 0) LIVES_WARN("Jack connect timed out");
1857       seek_err = TRUE;
1858       return 0;
1859     }
1860   }
1861 
1862   seekstart = ((int64_t)(bytes / sfile->achans / (sfile->asampsize / 8))) * sfile->achans * (sfile->asampsize / 8);
1863 
1864   if (seekstart < 0) seekstart = 0;
1865   if (seekstart > sfile->afilesize) seekstart = sfile->afilesize;
1866   jack_message2.command = ASERVER_CMD_FILE_SEEK;
1867   jack_message2.tc = lives_get_current_ticks();
1868   jack_message2.next = NULL;
1869   jack_message2.data = lives_strdup_printf("%"PRId64, seekstart);
1870   if (!jackd->msgq) jackd->msgq = &jack_message2;
1871   else jackd->msgq->next = &jack_message2;
1872   return seekstart;
1873 }
1874 
1875 
jack_try_reconnect(void)1876 boolean jack_try_reconnect(void) {
1877   jack_audio_init();
1878   jack_audio_read_init();
1879 
1880   // TODO: create the reader also
1881   mainw->jackd = jack_get_driver(0, TRUE);
1882   if (!jack_create_client_writer(mainw->jackd)) goto err123;
1883 
1884   d_print(_("\nConnection to jack audio was reset.\n"));
1885   return TRUE;
1886 
1887 err123:
1888   mainw->aplayer_broken = TRUE;
1889   mainw->jackd = NULL;
1890   do_jack_lost_conn_error();
1891   return FALSE;
1892 }
1893 
1894 
jack_pb_end(void)1895 void jack_pb_end(void) {
1896   cache_buffer = NULL;
1897 }
1898 
1899 
jack_aud_pb_ready(int fileno)1900 void jack_aud_pb_ready(int fileno) {
1901   // TODO - can we merge with switch_audio_clip()
1902 
1903   // prepare to play file fileno
1904   // - set loop mode
1905   // - check if we need to reconnect
1906   // - set vals
1907 
1908   // called at pb start and rec stop (after rec_ext_audio)
1909   char *tmpfilename = NULL;
1910   lives_clip_t *sfile = mainw->files[fileno];
1911   int asigned = !(sfile->signed_endian & AFORM_UNSIGNED);
1912   int aendian = !(sfile->signed_endian & AFORM_BIG_ENDIAN);
1913 
1914   if (mainw->jackd && mainw->aud_rec_fd == -1) {
1915     mainw->jackd->is_paused = FALSE;
1916     mainw->jackd->mute = mainw->mute;
1917     if ((mainw->loop_cont || mainw->whentostop != STOP_ON_AUD_END) && !mainw->preview) {
1918       if (mainw->ping_pong && prefs->audio_opts & AUDIO_OPTS_FOLLOW_FPS && !mainw->multitrack)
1919         mainw->jackd->loop = AUDIO_LOOP_PINGPONG;
1920       else mainw->jackd->loop = AUDIO_LOOP_FORWARD;
1921     } else mainw->jackd->loop = AUDIO_LOOP_NONE;
1922     if (sfile->achans > 0 && (!mainw->preview || (mainw->preview && mainw->is_processing)) &&
1923         (sfile->laudio_time > 0. || sfile->opening ||
1924          (mainw->multitrack && mainw->multitrack->is_rendering &&
1925           lives_file_test((tmpfilename = lives_get_audio_file_name(fileno)), LIVES_FILE_TEST_EXISTS)))) {
1926       ticks_t timeout;
1927       lives_alarm_t alarm_handle;
1928 
1929       lives_freep((void **)&tmpfilename);
1930       mainw->jackd->num_input_channels = sfile->achans;
1931       mainw->jackd->bytes_per_channel = sfile->asampsize / 8;
1932       mainw->jackd->sample_in_rate = sfile->arate;
1933       mainw->jackd->usigned = !asigned;
1934       mainw->jackd->seek_end = sfile->afilesize;
1935 
1936       if ((aendian && (capable->byte_order == LIVES_BIG_ENDIAN)) || (!aendian && (capable->byte_order == LIVES_LITTLE_ENDIAN)))
1937         mainw->jackd->reverse_endian = TRUE;
1938       else mainw->jackd->reverse_endian = FALSE;
1939 
1940       alarm_handle = lives_alarm_set(LIVES_DEFAULT_TIMEOUT);
1941       while ((timeout = lives_alarm_check(alarm_handle)) > 0 && jack_get_msgq(mainw->jackd)) {
1942         sched_yield(); // wait for seek
1943         lives_usleep(prefs->sleep_time);
1944       }
1945       lives_alarm_clear(alarm_handle);
1946       if (timeout == 0) jack_try_reconnect();
1947 
1948       if ((!mainw->multitrack || mainw->multitrack->is_rendering) &&
1949           (!mainw->event_list || mainw->record || (mainw->preview && mainw->is_processing))) {
1950         // tell jack server to open audio file and start playing it
1951         jack_message.command = ASERVER_CMD_FILE_OPEN;
1952         jack_message.data = lives_strdup_printf("%d", fileno);
1953 
1954         jack_message.next = NULL;
1955         mainw->jackd->msgq = &jack_message;
1956 
1957         jack_audio_seek_bytes(mainw->jackd, sfile->aseek_pos, sfile);
1958         if (seek_err) {
1959           if (jack_try_reconnect()) jack_audio_seek_bytes(mainw->jackd, sfile->aseek_pos, sfile);
1960         }
1961 
1962         //mainw->jackd->in_use = TRUE;
1963         mainw->rec_aclip = fileno;
1964         mainw->rec_avel = sfile->arate / sfile->arps;
1965         mainw->rec_aseek = (double)sfile->aseek_pos / (double)(sfile->arps * sfile->achans * (sfile->asampsize / 8));
1966       }
1967     }
1968     if ((mainw->agen_key != 0 || mainw->agen_needs_reinit)
1969         && !mainw->multitrack && !mainw->preview) mainw->jackd->in_use = TRUE; // audio generator is active
1970   }
1971 }
1972 
1973 #undef afile
1974 
1975 #endif
1976