16 static boolean storedfdsset =
FALSE;
19 static void audio_reset_stored_fnames(
void) {
21 storedfnames[i] = NULL;
29 if (totchans == 1)
return (
_(
"Mono"));
31 if (idx == 0)
return (
_(
"Left channel"));
32 if (idx == 1)
return (
_(
"Right channel"));
57 if (
mainw->
files[fnum]->
opening && !lives_file_test(fname, LIVES_FILE_TEST_EXISTS)) {
73 if (!storedfdsset)
return;
106 for (i = abuf->
out_achans; i < channum; i++) {
140 for (i = abuf->
out_achans; i < channum; i++) {
149 g_print(
"append16 to afb\n");
159 for (i = 0; i < 2; i++) {
169 #ifdef HAVE_PULSE_AUDIO
202 g_print(
"init afb\n");
243 g_print(
"clear afb %p\n", abuf);
251 double atime = start;
259 while (atime <= end) {
260 for (c = 0; c < afile->
achans; c++) {
266 if (xf > xx) xx = xf;
267 if (thresh >= 0. && xx > thresh) {
272 atime += 1. / afile->
arps;
284 if (xx <= 0.)
return FALSE;
287 double atime = start;
288 float fact = thresh / xx, val;
291 boolean swap_endian =
FALSE;
321 while (atime <= end) {
323 if (count == afile->
arps) count = 0;
325 for (c = 0; c < afile->
achans; c++) {
331 val = xx * 127.4999 + 127.4999;
332 ucval = (uint8_t)(127.4999 * xx) + 127.4999;
337 scval = (char)(255.499 * xx - 128.);
344 usval = (val > 65535 ? 65535 : val < 0 ? 0 : val);
345 if (swap_endian)
swab2(&usval, &usval, 1);
351 if (swap_endian)
swab2(&ssval, &ssval, 1);
361 atime += 1. / afile->
arps;
383 size_t bytes = (size_t)(secs * (
double)afile->
arate) * quant;
385 if (!bytes)
return 0.;
387 apos = ((size_t)(bytes / quant) * quant);
396 else val = val8 - 127;
397 if (val > 0.) val /= 127.;
403 else val16 = (uint16_t)(val8b << 8) + val8;
405 else val = val16 - 32767;
406 if (val > 0.) val /= 32767.;
422 float **fbuff = (
float **)
lives_calloc(nchans,
sizeof(
float *));
423 boolean memok =
TRUE;
426 for (i = 0; i < nchans; i++) {
427 fbuff[i] = (
float *)
lives_calloc(nframes,
sizeof(
float));
428 if (!fbuff[i]) memok =
FALSE;
437 for (i = 0; i < nchans; i++) {
460 uint64_t nsamples,
size_t tbytes,
double scale,
461 int nDstChannels,
int nSrcChannels,
int swap_sign) {
465 static double rem = 0.f;
466 double src_offset_d = rem;
468 unsigned char *src_end;
469 off_t src_offset_i = 0;
471 int nSrcCount, nDstCount;
474 src_end = src + tbytes - nSrcChannels;
476 if (!nSrcChannels)
return;
479 src_offset_d = ((double)nsamples * (-scale) - 1.f - rem);
480 src_offset_i = (off_t)src_offset_d * nSrcChannels;
484 nSrcCount = nSrcChannels;
485 nDstCount = nDstChannels;
493 ptr = src + ccount + src_offset_i;
494 ptr = ptr > src ? (ptr < (src_end + ccount) ? ptr : (src_end + ccount)) : src;
496 if (!swap_sign) *(dst++) = *(ptr) << 8;
497 else if (swap_sign ==
SWAP_U_TO_S) *(dst++) = ((
short)(*(ptr)) - 128) << 8;
498 else *((
unsigned short *)(dst++)) = ((
short)(*(ptr)) + 128) << 8;
503 if (!nSrcCount && nDstCount) {
505 nSrcCount = nSrcChannels;
510 src_offset_i = (off_t)((src_offset_d += scale) + .4999) * nSrcChannels;
514 if (src_offset_d > src_offset_i) rem = src_offset_d - (double)src_offset_i;
516 if (src_offset_d < src_offset_i) rem = (double)src_offset_i - src_offset_d;
525 uint64_t nsamples,
size_t tbytes,
double scale,
int nDstChannels,
526 int nSrcChannels,
int swap_endian,
int swap_sign) {
528 static double rem = 0.f;
529 double src_offset_d = rem;
532 int nSrcCount, nDstCount;
533 off_t src_offset_i = 0;
536 if (!nSrcChannels)
return;
539 src_offset_d = ((double)nsamples * (-scale) - 1.f - rem);
540 src_offset_i = (off_t)src_offset_d * nSrcChannels;
544 src_end = src + tbytes / 2 - nSrcChannels;
546 if ((off_t)((fabs(scale) * (double)nsamples) + rem) * nSrcChannels * 2 > tbytes)
547 scale = scale > 0. ? ((double)(tbytes / nSrcChannels / 2) - rem) / (
double)nsamples
548 : -(((double)(tbytes / nSrcChannels / 2) - rem) / (
double)nsamples);
551 if ((nSrcCount = nSrcChannels) == (nDstCount = nDstChannels) && !swap_endian && !swap_sign) {
555 lives_memcpy((
void *)dst, (
void *)src, nsamples * 2 * nSrcChannels);
559 ptr = src + src_offset_i;
560 ptr = ptr > src ? (ptr < src_end ? ptr : src_end) : src;
571 ptr = src + ccount + src_offset_i;
572 ptr = ptr > src ? (ptr < (src_end + ccount) ? ptr : (src_end + ccount)) : src;
576 if (!swap_sign) *(dst++) = *ptr;
580 if (!swap_sign) *(dst++) = (((*ptr) & 0x00FF) << 8) + ((*ptr) >> 8);
581 else if (swap_sign ==
SWAP_S_TO_U) *((uint16_t *)dst++) = (uint16_t)(((*ptr & 0x00FF) << 8) + (*ptr >> 8)
585 if (!swap_sign) *(dst++) = (((*ptr) & 0x00FF) << 8) + ((*ptr) >> 8);
586 else if (swap_sign ==
SWAP_S_TO_U) *((uint16_t *)dst++) =
596 if (!nSrcCount && nDstCount) {
598 nSrcCount = nSrcChannels;
603 src_offset_i = (off_t)((src_offset_d += scale) + .4999) * nSrcChannels;
607 if (src_offset_d > src_offset_i) rem = src_offset_d - (double)src_offset_i;
609 if (src_offset_d < src_offset_i) rem = (double)src_offset_i - src_offset_d;
618 uint64_t nsamples,
size_t tbytes,
double scale,
int nDstChannels,
int nSrcChannels,
int swap_sign) {
621 double src_offset_d = rem;
624 off_t src_offset_i = 0;
626 int nSrcCount, nDstCount;
628 if (!nSrcChannels)
return;
631 src_offset_d = ((double)nsamples * (-scale) - 1.f - rem);
632 src_offset_i = (off_t)src_offset_d * nSrcChannels;
635 src_end = src + tbytes /
sizeof(short) - nSrcChannels;
638 nSrcCount = nSrcChannels;
639 nDstCount = nDstChannels;
648 ptr = src + ccount + src_offset_i;
649 ptr = ptr > src ? (ptr < (src_end + ccount) ? ptr : src_end + ccount) : src;
652 if (!swap_sign) *(dst++) = (*ptr >> 8);
653 else if (swap_sign ==
SWAP_S_TO_U) *(dst++) = (uint8_t)((int8_t)(*ptr >> 8) + 128);
654 else *((int8_t *)dst++) = (int8_t)((uint8_t)(*ptr >> 8) - 128);
659 if (!nSrcCount && nDstCount) {
661 nSrcCount = nSrcChannels;
666 src_offset_i = (off_t)((src_offset_d += scale) + .4999) * nSrcChannels;
670 if (src_offset_d > src_offset_i) rem = src_offset_d - (double)src_offset_i;
672 if (src_offset_d < src_offset_i) rem = (double)src_offset_i - src_offset_d;
677 float sample_move_d16_float(
float *dst,
short *src, uint64_t nsamples, uint64_t src_skip,
int is_unsigned,
boolean rev_endian,
700 if (vol == 0.) vol = 0.0000001f;
713 srcxs = ((srcx[1] & 0xFF) << 8) + (srcx[0] & 0xFF);
719 oil_scaleconv_f32_s16(&val, srcp, 1, &y, val > 0 ? &xp : &xn);
721 if ((val = (
float)((
float)(*srcp) / (*srcp > 0 ? svolp : svoln))) > 1.0f) val = 1.0f;
722 else if (val < -1.0f) val = -1.0f;
726 oil_scaleconv_f32_u16(&val, (
unsigned short *)srcp, 1, &y, &xa);
730 if ((val = (
float)((
float)(valss) / (valss > 0 ? svolp : svoln))) > 1.0f) val = 1.0f;
731 else if (val < -1.0f) val = -1.0f;
735 if (val > maxval) maxval = val;
736 else if (-val > maxval) maxval = -val;
747 static double rem = 0.f;
753 offs_d = ((double)nsamples * (-scale) - 1.f - rem);
754 offs = (off_t)offs_d;
757 if (scale == 1.f && dst_skip == 1) {
758 lives_memcpy((
void *)dst, (
void *)src, nsamples *
sizeof(
float));
762 for (i = 0; i < nsamples; i++) {
765 offs = (off_t)((offs_d += scale) + .4999);
769 if (offs_d > offs) rem = offs_d - (double)offs;
771 if (offs_d < offs) rem = (double)offs - offs_d;
776 #define CLIP_DECAY ((double)16535. / (double)16536.)
793 int64_t
sample_move_float_int(
void *holding_buff,
float **float_buffer,
int nsamps,
double scale,
int chans,
int asamps,
794 int usigned,
boolean rev_endian,
boolean interleaved,
float vol) {
795 int64_t frames_out = 0l;
797 off_t offs = 0, coffs = 0, lcoffs = -1;
799 static double coffs_d = 0.f;
802 short *hbuffs = (
short *)holding_buff;
803 unsigned short *hbuffu = (
unsigned short *)holding_buff;
804 unsigned char *hbuffc = (
unsigned char *)holding_buff;
806 unsigned short valu[chans];
807 static float clip = 1.0;
808 float ovalf[chans], valf[chans], fval;
809 float volx = vol, ovolx = -1.;
810 boolean checklim =
FALSE;
814 if (clip > 1.0) checklim =
TRUE;
816 while ((nsamps - frames_out) > 0) {
829 for (i = 0; i < chans; i++) {
830 if (coffs != lcoffs) {
831 if ((fval = fabsf((ovalf[i] = *(float_buffer[i] + (interleaved ? (coffs * chans) : coffs))))) > clip) {
839 if (volx != ovolx || coffs != lcoffs) {
840 valf[i] = ovalf[i] * volx;
841 if (valf[i] > vol) valf[i] = vol;
842 else if (valf[i] < -vol) valf[i] = -vol;
850 if (usigned) *(hbuffu + offs) = valu[i];
851 else *(hbuffs + offs) = val[i];
854 *(hbuffc + offs) = valu[i] & 0x00FF;
855 *(hbuffc + (++offs)) = (valu[i] & 0xFF00) >> 8;
857 *(hbuffc + offs) = val[i] & 0x00FF;
858 *(hbuffc + (++offs)) = (val[i] & 0xFF00) >> 8;
862 *(hbuffc + offs) = (
unsigned char)(valu[i] >> 8);
867 coffs = (off_t)((coffs_d += scale) + .4999);
869 coffs_d -= (double)coffs;
871 if (frames_out != nsamps) {
872 char *msg =
lives_strdup_printf(
"audio float -> int: buffer mismatch of %ld samples\n", frames_out - nsamps);
897 off_t offs = 0, ioffs, xchan;
899 double src_offset_d = 0.f;
900 off_t src_offset_i = 0;
914 in_arate = abuf->
arate;
916 scale = (double)in_arate / (
double)out_arate;
932 for (i = 0; i < nsamps; i++) {
940 for (j = 0; j < nchans; j++) {
948 obuf[j][offs + i] = abuf->
bufferf[xchan][curval] * vol;
953 src_offset_i = (off_t)((src_offset_d += scale) + .4999);
996 #ifdef HAVE_PULSE_AUDIO
1001 int in_arate, nsampsx;
1002 ssize_t offs = 0, ioffs, xchan;
1004 double src_offset_d = 0.f;
1005 ssize_t src_offset_i = 0;
1019 in_arate = abuf->
arate;
1021 scale = (double)in_arate / (
double)out_arate;
1023 while (nsamps > 0) {
1036 nsampsx = nsamps * nchans;
1038 for (i = 0; i < nsampsx; i += nchans) {
1047 for (j = 0; j < nchans; j++) {
1054 obuf[(offs++)] = abuf->
buffer16[0][curval + xchan];
1058 src_offset_i = (ssize_t)((src_offset_d += scale) + .4999);
1064 samples_out += samps;
1095 static size_t chunk_to_float_abuf(
lives_audio_buf_t *abuf,
float **float_buffer,
int nsamps) {
1100 for (i = 0; i < chans; i++) {
1103 return (
size_t)nsamps;
1112 if (!tmpfbuffer)
return FALSE;
1114 for (i = 0; i < nsamps; i++) {
1115 for (j = 0; j < nchans; j++) {
1116 tmpfbuffer[nsamps * j + i] = fbuffer[i * nchans + j];
1119 lives_memcpy(fbuffer, tmpfbuffer, nsamps * nchans *
sizeof(
float));
1130 if (!tmpfbuffer)
return FALSE;
1132 for (i = 0; i < nsamps; i++) {
1133 for (j = 0; j < nchans; j++) {
1134 tmpfbuffer[i * nchans + j] = fbuffer[j * nsamps + i];
1137 lives_memcpy(fbuffer, tmpfbuffer, nsamps * nchans *
sizeof(
float));
1145 static size_t chunk_to_int16_abuf(
lives_audio_buf_t *abuf,
float **float_buffer,
int nsamps) {
1153 return (
size_t)frames_out;
1159 boolean pad_with_silence(
int out_fd,
void *buff, off64_t oins_size, int64_t ins_size,
int asamps,
int aunsigned,
1160 boolean big_endian) {
1167 static uint64_t *zero_buff = NULL;
1168 static int oasamps = 0;
1169 static int ounsigned = 0;
1170 static int orevendian = 0;
1175 boolean retval =
TRUE;
1176 boolean revendian =
FALSE;
1180 if (ins_size <= oins_size) {
1181 #ifdef DEBUG_ARENDER
1182 g_print(
"sbytes is l.t zero\n");
1186 sbytes = ins_size - oins_size;
1188 #ifdef DEBUG_ARENDER
1189 g_print(
"sbytes is %ld\n", sbytes);
1196 if (!buff)
return FALSE;
1200 if (ounsigned != aunsigned || oasamps != asamps || orevendian != revendian) {
1210 uint64_t theval = (revendian ? 0x0080008000800080ul : 0x8000800080008000ul);
1211 for (i = 0; i < sblocksize; i ++) {
1212 zero_buff[i] = theval;
1217 ounsigned = aunsigned;
1219 orevendian = revendian;
1225 if (
THREADVAR(write_failed) == out_fd + 1) {
1234 }
else if (out_fd >= 0) {
1243 #ifdef DEBUG_ARENDER
1277 weed_timecode_t tc_start, weed_timecode_t tc_end,
double *chvol,
double opvol_start,
1284 weed_plant_t *shortcut = NULL;
1287 void *finish_buff = NULL;
1289 short *holding_buff;
1291 char *infilename, *outfilename;
1292 off64_t seekstart[nfiles];
1296 int in_asamps[nfiles];
1297 int in_achans[nfiles];
1298 int in_arate[nfiles];
1299 int in_arps[nfiles];
1300 int in_unsigned[nfiles];
1302 boolean in_reverse_endian[nfiles];
1303 boolean is_silent[nfiles];
1305 size_t max_aud_mem, bytes_to_read, aud_buffer;
1312 weed_timecode_t tc = tc_start;
1316 double opvol = opvol_start;
1317 double zavel, zzavel, zavel_max = 0.;
1319 boolean out_reverse_endian =
FALSE;
1320 boolean is_fade =
FALSE;
1321 boolean use_live_chvols =
FALSE;
1325 int out_arate = to_file > -1 ? outfile->
arate : obuf->
arate;
1331 int first_nonsilent = -1;
1339 int64_t frames_out = 0;
1340 int64_t ins_size = 0l, cur_size;
1341 int64_t tsamples = ((double)(tc_end - tc_start) /
TICKS_PER_SECOND_DBL * (double)out_arate + .5);
1342 int64_t blocksize, zsamples, xsamples;
1343 int64_t tot_frames = 0l;
1345 float *float_buffer[out_achans * nfiles];
1346 float *chunk_float_buffer[out_achans * nfiles];
1349 if (out_achans * nfiles * tsamples == 0)
return 0l;
1353 if (!storedfdsset) audio_reset_stored_fnames();
1357 render_block_size *= 100;
1362 #ifdef DEBUG_ARENDER
1363 g_print(
"writing to %s\n", outfilename);
1370 THREADVAR(write_failed_file) = lives_strdup(outfilename);
1378 ins_pt *= out_achans * out_arate * out_asamps;
1379 ins_size = ((int64_t)(ins_pt / out_achans / out_asamps + .5)) * out_achans * out_asamps;
1383 out_reverse_endian =
TRUE;
1384 else out_reverse_endian =
FALSE;
1386 if (ins_size > cur_size) {
1388 pad_with_silence(out_fd, NULL, cur_size, ins_size, out_asamps, out_unsigned, out_bendian);
1391 if (opvol_start == opvol_end && opvol_start == 0.) {
1404 #ifdef DEBUG_ARENDER
1405 g_print(
"here %d %ld %ld %d\n", nfiles, tc_start, tc_end, to_file);
1408 for (track = 0; track < nfiles; track++) {
1412 #ifdef DEBUG_ARENDER
1413 g_print(
" track %d %d %.4f %.4f\n", track, from_files[track], fromtime[track], avels[track]);
1416 if (avels[track] == 0.) {
1417 is_silent[track] =
TRUE;
1421 is_silent[track] =
FALSE;
1424 in_asamps[track] = infile->
asampsize / 8;
1425 in_achans[track] = infile->
achans;
1426 in_arate[track] = infile->
arate;
1427 in_arps[track] = infile->
arps;
1431 if (LIVES_UNLIKELY(in_achans[track] == 0)) is_silent[track] =
TRUE;
1435 in_reverse_endian[track] =
TRUE;
1436 else in_reverse_endian[track] =
FALSE;
1440 zavel = avels[track] * (double)in_arate[track] / (
double)out_arate * in_asamps[track] * in_achans[track];
1442 if (fabs(zavel) > zavel_max) zavel_max = fabs(zavel);
1447 if (track <
NSTOREDFDS && storedfnames[track] && !strcmp(infilename, storedfnames[track])) {
1448 in_fd[track] = storedfds[track];
1452 if (in_fd[track] < 0) {
1454 THREADVAR(read_failed_file) = lives_strdup(infilename);
1458 storedfds[track] = in_fd[track];
1459 storedfnames[track] = lives_strdup(infilename);
1464 seekstart[track] =
quant_abytes(fromtime[track], in_arps[track], in_achans[track], in_asamps[track]);
1472 for (track = 0; track < nfiles; track++) {
1473 if (!is_silent[track]) {
1474 first_nonsilent = track;
1479 if (first_nonsilent == -1) {
1483 int64_t oins_size = ins_size;
1485 ins_pt *= out_achans * out_arate * out_asamps;
1486 ins_size = ((int64_t)(ins_pt / out_achans / out_asamps) + .5) * out_achans * out_asamps;
1487 pad_with_silence(out_fd, NULL, oins_size, ins_size, out_asamps, out_unsigned, out_bendian);
1491 for (i = 0; i < out_achans; i++) {
1509 max_aud_mem = (max_aud_mem >> 7) << 7;
1510 max_aud_mem = max_aud_mem / out_achans / nfiles;
1514 bytes_to_read = tsamples * (
sizeof(float));
1517 max_segments = (int)((
double)bytes_to_read / (double)max_aud_mem + 1.);
1520 aud_buffer = bytes_to_read / max_segments;
1522 zsamples = (int)(aud_buffer /
sizeof(
float) + .5);
1524 xsamples = zsamples + (tsamples - (max_segments * zsamples));
1528 for (i = 0; i < out_achans * nfiles; i++) {
1535 #ifdef DEBUG_ARENDER
1536 g_print(
" rendering %ld samples %f\n", tsamples, opvol);
1545 while (tsamples > 0) {
1546 tsamples -= xsamples;
1548 for (track = 0; track < nfiles; track++) {
1549 if (is_silent[track]) {
1551 for (c = 0; c < out_achans; c++) {
1552 lives_memset(float_buffer[track * out_achans + c], 0, xsamples *
sizeof(
float));
1557 zavel = avels[track] * (double)in_arate[track] / (
double)out_arate;
1563 tbytes = (int)((
double)xsamples * fabs(zavel) + ((double)
fastrand() / (double)LIVES_MAXUINT64)) *
1564 in_asamps[track] * in_achans[track];
1567 for (c = 0; c < out_achans; c++) {
1568 lives_memset(float_buffer[track * out_achans + c], 0, xsamples *
sizeof(
float));
1575 if (in_fd[track] > -1) {
1589 if (bytes_read < 0) bytes_read = 0;
1591 if (in_fd[track] > -1) {
1598 / (double)(in_asamps[track] * in_achans[track] * in_arate[track]);
1602 if (
THREADVAR(read_failed) == in_fd[track] + 1) {
1607 if (bytes_read < tbytes && bytes_read >= 0) {
1613 nframes = (tbytes / (in_asamps[track]) / in_achans[track] / fabs(zavel) + .001);
1620 if (in_asamps[track] == 1) {
1625 sample_move_d8_d16(holding_buff, (uint8_t *)in_buff, nframes, tbytes, zavel, out_achans, in_achans[track], 0);
1632 in_achans[track], in_reverse_endian[track] ?
SWAP_X_TO_L : 0, 0);
1640 for (c = 0; c < out_achans; c++) {
1643 out_achans, in_unsigned[track],
FALSE, clip_vol * (use_live_chvols ? 1. : chvol[track]));
1649 blocksize = render_block_size;
1651 for (i = 0; i < xsamples; i += render_block_size) {
1652 if (i + render_block_size > xsamples) blocksize = xsamples - i;
1654 for (track = 0; track < nfiles; track++) {
1658 for (c = 0; c < out_achans; c++) {
1660 chunk_float_buffer[track * out_achans + c] = float_buffer[track * out_achans + c] + i;
1701 for (x = 0; x < nfiles; x++) {
1702 float **adata = (
float **)
lives_calloc(out_achans,
sizeof(
float *));
1704 for (y = 0; y < out_achans; y++) {
1705 adata[y] = chunk_float_buffer[x * out_achans + y];
1719 for (x = 0; x < nfiles; x++) {
1721 for (y = 0; y < out_achans; y++) {
1722 if (chunk_float_buffer[x * out_achans + y] != adata[y]) {
1739 time += (double)frames_out / (
double)out_arate;
1740 opvol = opvol_start + (opvol_end - opvol_start) * (time / (
double)((tc_end - tc_start) /
TICKS_PER_SECOND_DBL));
1747 frames_out =
sample_move_float_int((
void *)finish_buff, chunk_float_buffer, blocksize, 1., out_achans,
1748 out_asamps * 8, out_unsigned, out_reverse_endian,
FALSE, opvol);
1751 tot_frames += frames_out;
1752 #ifdef DEBUG_ARENDER
1765 out_asamps * 8, out_unsigned, out_reverse_endian,
FALSE, opvol);
1768 #ifdef DEBUG_ARENDER
1771 tot_frames += frames_out;
1775 frames_out = chunk_to_float_abuf(obuf, float_buffer, xsamples);
1777 frames_out = chunk_to_int16_abuf(obuf, float_buffer, xsamples);
1780 tot_frames += frames_out;
1783 xsamples = zsamples;
1787 for (i = 0; i < out_achans * nfiles; i++) {
1788 if (float_buffer[i])
lives_free(float_buffer[i]);
1796 for (track = 0; track < nfiles; track++) {
1797 if (!is_silent[track]) {
1803 #ifdef DEBUG_ARENDER
1813 void aud_fade(
int fileno,
double startt,
double endt,
double startv,
double endv) {
1814 double vel = 1., vol = 1.;
1841 if (
cfile->achans > 0) {
1845 (
cfile->asampsize / 8);
1846 if (apos >
cfile->aseek_pos)
cfile->aseek_pos = apos;
1857 #ifdef HAVE_PULSE_AUDIO
1887 #ifdef HAVE_PULSE_AUDIO
1915 #ifdef HAVE_PULSE_AUDIO
1945 jack_message.data = NULL;
1946 jack_message.next = NULL;
1952 #ifdef HAVE_PULSE_AUDIO
1968 pulse_message.data = NULL;
1969 pulse_message.next = NULL;
1992 float ovol =
cfile->vol;
2002 double dvol = (double)newvol;
2053 if (retval == LIVES_RESPONSE_CANCEL) {
2058 }
while (retval == LIVES_RESPONSE_RETRY);
2066 if (!jackd_read_started) {
2155 void jack_rec_audio_end(
boolean close_device,
boolean close_fd) {
2160 jack_flush_read_data(0, NULL);
2181 #ifdef HAVE_PULSE_AUDIO
2209 if (retval == LIVES_RESPONSE_CANCEL) {
2214 }
while (retval == LIVES_RESPONSE_RETRY);
2317 void pulse_rec_audio_end(
boolean close_device,
boolean close_fd) {
2357 int n = MIN(nostate, nstate);
2367 int num_aclips = 0, atrack;
2369 double *aseeks = NULL;
2377 for (i = 0; i < num_aclips; i += 2) {
2378 if (aclips[i + 1] > 0) {
2380 if (atrack + btoffs >= naudstate - 1) {
2381 atstate = resize_audstate(atstate, naudstate, atrack + btoffs + 2);
2382 naudstate = atrack + btoffs + 1;
2383 atstate[naudstate].
afile = -1;
2385 atstate[atrack + btoffs].
afile = aclips[i + 1];
2386 atstate[atrack + btoffs].
seek = aseeks[i];
2387 atstate[atrack + btoffs].
vel = aseeks[i + 1];
2394 if (ntracks) *ntracks = num_aclips;
2401 return aframe_to_atstate_inner(event, NULL);
2406 return aframe_to_atstate_inner(event, ntracks);
2420 weed_timecode_t fill_tc,
int what_to_get,
boolean exact) {
2424 weed_timecode_t last_tc = 0;
2427 int nfiles, nnfiles, etype;
2446 while ((st_event && event != st_event) || (!st_event &&
get_event_timecode(event) < fill_tc)) {
2450 case WEED_EVENT_TYPE_FILTER_MAP:
2457 case WEED_EVENT_TYPE_FILTER_INIT:
2468 case WEED_EVENT_TYPE_FILTER_DEINIT:
2475 case WEED_EVENT_TYPE_PARAM_CHANGE:
2482 char *key_string = weed_get_string_value((weed_plant_t *)init_event,
WEED_LEAF_HOST_TAG, NULL);
2483 int key = atoi(key_string);
2484 char *filter_name = weed_get_string_value((weed_plant_t *)init_event,
WEED_LEAF_FILTER, NULL);
2500 weed_leaf_dup(param, event, WEED_LEAF_VALUE);
2505 case WEED_EVENT_TYPE_FRAME:
2517 for (nfiles = 0; audstate[nfiles].afile != -1; nfiles++) {
2524 for (nnfiles = 0; atstate[nnfiles].
afile != -1; nnfiles++);
2525 if (nnfiles > nfiles) {
2526 audstate = resize_audstate(audstate, nfiles, nnfiles + 1);
2527 audstate[nnfiles].afile = -1;
2530 for (
int i = 0; i < nnfiles; i++) {
2531 if (atstate[i].afile > 0) {
2532 audstate[i].afile = atstate[i].
afile;
2533 audstate[i].seek = atstate[i].
seek;
2534 audstate[i].vel = atstate[i].
vel;
2554 for (nfiles = 0; audstate[nfiles].afile != -1; nfiles++) {
2580 static weed_timecode_t last_tc, tc;
2581 static weed_timecode_t fill_tc;
2582 static weed_plant_t *event;
2585 static int *from_files = NULL;
2586 static double *aseeks = NULL, *avels = NULL;
2614 for (i = 0; i < nfiles; i++) {
2616 avels[i] = aseeks[i] = 0.;
2623 for (i = 0; atstate[i].
afile != -1; i++) {
2624 if (atstate[i].afile > 0) {
2625 from_files[i] = atstate[i].
afile;
2626 avels[i] = atstate[i].
vel;
2627 aseeks[i] = atstate[i].
seek;
2636 for (i = 0; i < nfiles; i++) {
2641 }
else chvols[0] = 1.;
2651 render_audio_segment(nfiles, from_files, -1, avels, aseeks, last_tc, tc, chvols, 1., 1., abuf);
2664 for (nnfiles = 0; atstate[nnfiles].
afile != -1; nnfiles++);
2665 for (i = 0; i < nnfiles; i++) {
2666 if (atstate[i].afile > 0) {
2667 from_files[i] = atstate[i].
afile;
2668 avels[i] = atstate[i].
vel;
2669 aseeks[i] = atstate[i].
seek;
2678 if (last_tc < fill_tc) {
2683 render_audio_segment(nfiles, from_files, -1, avels, aseeks, last_tc, fill_tc, chvols, 1., 1., abuf);
2721 for (chan = 0; chan < achans; chan++) {
2730 #ifdef HAVE_PULSE_AUDIO
2742 achans *
sizeof(
short));
2756 for (chan = 0; chan <
mainw->
jackd->abufs[i]->out_achans; chan++) {
2769 #ifdef HAVE_PULSE_AUDIO
2786 #ifdef RESEEK_ENABLE
2832 if (
cfile->achans == 0
2834 #ifdef HAVE_PULSE_AUDIO
2848 if (!jack_audio_seek_frame(
mainw->
jackd, frameno)) {
2849 if (jack_try_reconnect()) jack_audio_seek_frame(
mainw->
jackd, frameno);
2861 #ifdef HAVE_PULSE_AUDIO
2866 if (!pulse_audio_seek_frame(
mainw->
pulsed, frameno)) {
2867 if (pulse_try_reconnect()) pulse_audio_seek_frame(
mainw->
pulsed, frameno);
2884 static pthread_t athread;
2886 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
2887 static pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
2904 static void *cache_my_audio(
void *arg) {
2909 while (!cbuffer->
die) {
2912 pthread_mutex_lock(&cond_mutex);
2913 pthread_cond_wait(&cond, &cond_mutex);
2914 pthread_mutex_unlock(&cond_mutex);
2920 if (cbuffer->
_fd != -1)
2938 #ifdef HAVE_PULSE_AUDIO
3136 if (cbuffer->
_fd == -1) {
3137 lives_printerr(
"audio cache thread: error opening %s\n", filename);
3232 pthread_mutex_lock(&cond_mutex);
3233 pthread_cond_signal(&cond);
3234 pthread_mutex_unlock(&cond_mutex);
3264 cache_buffer->
_cseek = -1;
3265 cache_buffer->
_fd = -1;
3270 pthread_create(&athread, NULL, cache_my_audio, cache_buffer);
3272 return cache_buffer;
3281 if (!cache_buffer) {
3287 pthread_join(athread, NULL);
3313 xcache_buffer = cache_buffer;
3314 cache_buffer = NULL;
3325 return cache_buffer;
3335 weed_plant_t *orig_inst = inst;
3336 weed_plant_t *filter;
3337 weed_plant_t *channel;
3338 weed_plant_t *ctmpl;
3340 weed_error_t retval;
3342 int xnchans = 0, xxnchans, xrate = 0;
3353 if (flags & WEED_FILTER_AUDIO_RATES_MAY_VARY) rvary =
TRUE;
3354 if (flags & WEED_FILTER_CHANNEL_LAYOUTS_MAY_VARY) lvary =
TRUE;
3364 if (lvary && weed_plant_has_leaf(ctmpl, WEED_LEAF_AUDIO_CHANNELS))
3365 xnchans = weed_get_int_value(ctmpl, WEED_LEAF_AUDIO_CHANNELS, NULL);
3366 else if (weed_plant_has_leaf(filter, WEED_LEAF_MAX_AUDIO_CHANNELS)) {
3367 xxnchans = weed_get_int_value(filter, WEED_LEAF_MAX_AUDIO_CHANNELS, NULL);
3368 if (xxnchans > 0 && xxnchans < nchans) xnchans = xxnchans;
3370 if (xnchans > nchans) {
3374 if (weed_get_int_value(channel, WEED_LEAF_AUDIO_CHANNELS, NULL) != nchans
3375 && (cflags & WEED_CHANNEL_REINIT_ON_LAYOUT_CHANGE))
3377 else weed_set_int_value(channel, WEED_LEAF_AUDIO_CHANNELS, nchans);
3380 if (rvary && weed_plant_has_leaf(ctmpl, WEED_LEAF_AUDIO_RATE))
3381 xrate = weed_get_int_value(ctmpl, WEED_LEAF_AUDIO_RATE, NULL);
3382 else if (weed_plant_has_leaf(filter, WEED_LEAF_AUDIO_RATE))
3383 xrate = weed_get_int_value(filter, WEED_LEAF_AUDIO_RATE, NULL);
3384 if (arate != xrate) {
3389 if (weed_get_int_value(channel, WEED_LEAF_AUDIO_RATE, NULL) != arate) {
3390 if (cflags & WEED_CHANNEL_REINIT_ON_RATE_CHANGE) {
3395 weed_set_int_value(channel, WEED_LEAF_AUDIO_RATE, arate);
3403 weed_set_int64_value(channel, WEED_LEAF_TIMECODE, tc);
3406 weed_set_double_value(inst, WEED_LEAF_FPS,
cfile->pb_fps);
3425 if (retval != WEED_SUCCESS) {
3431 if (channel && xnchans == 1 && nchans == 2) {
3433 lives_memcpy(fbuffer[1], fbuffer[0], nsamps *
sizeof(
float));
3467 static int audio_fd;
3468 static char *audio_file;
3493 if (audio_fd == -1)
return FALSE;
3496 off64_t audio_end_pos = (double)((
cfile->start - 1) *
cfile->arate *
cfile->achans *
cfile->asampsize / 8) /
cfile->fps;
3525 float **fltbuf, *fltbufni = NULL;
3526 short *shortbuf = NULL;
3527 boolean rev_endian =
FALSE;
3536 if ((abigendian &&
capable->
byte_order == LIVES_LITTLE_ENDIAN) || (!abigendian &&
3539 tbytes = nframes *
cfile->achans *
cfile->asampsize / 8;
3543 if (tbytes <= 0)
return TRUE;
3544 nframes = tbytes /
cfile->achans / (
cfile->asampsize / 8);
3550 if (!in_buff)
return FALSE;
3552 if (
cfile->asampsize == 8) {
3567 if (
THREADVAR(read_failed) == audio_fd + 1) {
3577 if (
cfile->asampsize == 8) {
3580 }
else shortbuf = (
short *)in_buff;
3582 nframes = tbytes /
cfile->achans / (
cfile->asampsize / 8);
3586 for (i = 0; i <
cfile->achans; i++) {
3588 fltbuf[i] = (
float *)
lives_calloc(nframes,
sizeof(
float));
3590 for (--i; i >= 0; i--) {
3594 if (shortbuf != (
short *)in_buff)
lives_free(shortbuf);
3628 for (i = 0; i <
cfile->achans; i++) {
3635 if (shortbuf != (
short *)in_buff)
lives_free(shortbuf);
3646 for (i = 0; i <
cfile->achans; i++) {
3653 if (audio_fd >= 0) {
3656 tbytes = onframes *
cfile->achans *
cfile->asampsize / 8;
3661 if (shortbuf != (
short *)in_buff)
lives_free(shortbuf);
3664 if (
THREADVAR(write_failed) == audio_fd + 1) {
3691 weed_plant_t *ctmpl;
3695 size_t samps, offs = 0;
3697 int trate, tchans, xnchans, flags;
3710 ctmpl = weed_get_plantptr_value(achan, WEED_LEAF_TEMPLATE, NULL);
3712 flags = weed_get_int_value(filter, WEED_LEAF_FLAGS, NULL);
3713 if (flags & WEED_FILTER_AUDIO_RATES_MAY_VARY) rvary =
TRUE;
3714 if (flags & WEED_FILTER_CHANNEL_LAYOUTS_MAY_VARY) lvary =
TRUE;
3720 if (maxlen < samps) {
3721 offs = samps - maxlen;
3728 if (rvary && weed_plant_has_leaf(ctmpl, WEED_LEAF_AUDIO_RATE))
3729 trate = weed_get_int_value(ctmpl, WEED_LEAF_AUDIO_RATE, NULL);
3730 else if (weed_plant_has_leaf(filter, WEED_LEAF_AUDIO_RATE))
3731 trate = weed_get_int_value(filter, WEED_LEAF_AUDIO_RATE, NULL);
3735 if (lvary && weed_plant_has_leaf(ctmpl, WEED_LEAF_AUDIO_CHANNELS))
3736 tchans = weed_get_int_value(ctmpl, WEED_LEAF_AUDIO_CHANNELS, NULL);
3737 else if (weed_plant_has_leaf(filter, WEED_LEAF_MAX_AUDIO_CHANNELS)) {
3738 xnchans = weed_get_int_value(filter, WEED_LEAF_MAX_AUDIO_CHANNELS, NULL);
3739 if (xnchans > 0 && xnchans < tchans) tchans = xnchans;
3793 scale = (double)trate / (
double)abuf->
arate;
3794 alen = (size_t)(fabs(((
double)alen * scale)));
3800 for (i = 0; i < tchans; i++) {
3805 if (abuf->
arate == trate) {
3811 }
else dst[i] = NULL;
3828 const char *playername =
"audiostreamer.pl";
3829 char *astream_name = NULL;
3830 char *astream_name_out = NULL;
3836 char *astreamer, *com;
3844 astream_name = lives_build_filename(
prefs->
workdir, astname, NULL);
3847 mkfifo(astream_name, S_IRUSR | S_IWUSR);
3850 astream_name_out = lives_build_filename(
prefs->
workdir, astname_out, NULL);
3856 #ifdef HAVE_PULSE_AUDIO
3881 afd =
lives_open2(astream_name, O_WRONLY | O_SYNC);
3882 if (afd != -1)
break;
3888 #ifdef HAVE_PULSE_AUDIO
3909 const char *playername =
"audiostreamer.pl";
3914 char *astream_name = lives_build_filename(
prefs->
workdir, astname, NULL);
3915 char *astream_name_out = lives_build_filename(
prefs->
workdir, astname_out, NULL);
3922 #ifdef HAVE_PULSE_AUDIO
3952 char *astream_name = lives_build_filename(
prefs->
workdir, astname, NULL);
3954 char *astream_name_out = lives_build_filename(
prefs->
workdir, astname_out, NULL);
3973 _(
"\nClick Retry to attempt to restart the audio server.\n")) :
3977 _(
"LiVES was unable to connect to %s.\nPlease check your audio settings and restart %s\n"
3978 "and LiVES if necessary.\n%s"),
3982 #ifdef HAVE_PULSE_AUDIO
3984 if (retval == LIVES_RESPONSE_RETRY) pulse_try_reconnect();
4004 void nullaudio_time_reset(int64_t offset) {
4006 mainw->nullaudio_seek_posn = 0;
4007 mainw->nullaudio_arate = DEF_AUDIO_RATE;
4008 mainw->nullaudio_playing_file = -1;
4013 void nullaudio_arate_set(
int arate) {
4014 mainw->nullaudio_arate = arate;
4018 void nullaudio_seek_set(offs64_t seek_posn) {
4020 mainw->nullaudio_seek_posn = seek_posn;
4024 void nullaudio_clip_set(
int clipno) {
4025 mainw->nullaudio_playing_file = clipno;
4029 int64_t nullaudio_update_seek_posn() {
4035 mainw->nullaudo_startticks = current_ticks;
4036 if (
mainw->nullaudio_seek_posn < 0) {
4043 mainw->nullaudio_arate = -
mainw->nullaudio->arate;
4044 mainw->nullaudio_seek_posn = 0;
4045 }
else mainw->nullaudio_seek_posn +=
mainw->nullaudio_seek_end;
4048 if (
mainw->nullaudio_seek_posn >
mainw->nullaudio_seek_end) {
4056 mainw->nullaudio_arate = -
mainw->nullaudio_arate;
4057 mainw->nullaudio_seek_posn =
mainw->nullaudio_seek_end - (
mainw->nullaudio_seek_pos -
mainw->nullaudio_seek_end);
4059 mainw->nullaudio_seek_posn -=
mainw->nullaudio_seek_end;
4061 nullaudio_set_rec_avals();
4068 void nullaudio_get_rec_avals(
void) {
4079 static void nullaudio_set_rec_avals(
boolean is_forward) {
4083 nullaudio_get_rec_avals();