LiVES 3.2.0
audio.h
Go to the documentation of this file.
1// audio.h
2// LiVES (lives-exe)
3// (c) G. Finch (salsaman+lives@gmail.com) 2005 - 2020
4// Released under the GPL 3 or later
5// see file ../COPYING for licensing details
6
7#ifndef HAS_LIVES_AUDIO_H
8#define HAS_LIVES_AUDIO_H
9
10#define SAMPLE_MAX_16BIT_P 32767.4999999f
11#define SAMPLE_MAX_16BIT_N 32768.0f
12#define SAMPLE_MAX_16BITI 32768
13
15#define SWAP_U_TO_S 1
16#define SWAP_S_TO_U 2
17
19#define SWAP_X_TO_L 1
20#define SWAP_L_TO_X 2
21
23# define DEFAULT_AUDIO_RATE 44100
24# define DEFAULT_AUDIO_CHANS 2
25# define DEFAULT_AUDIO_SAMPS 16
26# define DEFAULT_AUDIO_SIGNED8 (AFORM_UNSIGNED)
27# define DEFAULT_AUDIO_SIGNED16 (!AFORM_UNSIGNED)
28
30#define MAX_ACHANS 2
31
32// keep first N audio_in filesysten handles open - multitrack only
33#define NSTOREDFDS 64
34
36#define MAX_AUDIO_MEM 32 * 1024 * 1024
37
39#define RENDER_BLOCK_SIZE 1024
40
42#define SILENCE_BLOCK_SIZE BUFFER_FILL_BYTES_LARGE
43
47#define XSAMPLES 393216
48
49#define AUD_WRITE_CHECK 0xFFFFFFFFF4000000
50
51#define WEED_LEAF_HOST_KEEP_ADATA "keep_adata"
52
55
56#define ASERVER_CMD_PROCESSED 0
57#define ASERVER_CMD_FILE_OPEN 1
58#define ASERVER_CMD_FILE_CLOSE 2
59#define ASERVER_CMD_FILE_SEEK 3
60
61/* message passing structure */
62typedef struct _aserver_message_t {
63 volatile int command;
65 volatile char *data;
66 volatile struct _aserver_message_t *next;
68
69typedef enum {
75
76typedef struct {
77 lives_operation_t operation; // read, write, or convert [readonly by server]
78 volatile boolean is_ready; // [readwrite all]
79 boolean eof;
80 int fileno; // [readonly by server]
81
82 // readonly by server:
83
84 // use one or other
85 off_t seek;
87
88 int arate;
89
90 ssize_t bytesize; // file in/out length in bytes [write by server in case of eof]
91
92 boolean in_interleaf;
94
97 int in_asamps; // set to -val for float
98 int out_asamps; // set to -val for float
102
103 size_t samp_space;
104
105 boolean sequential;
106
107 // in or out buffers
108 uint8_t **buffer8;
109 union {
110 short **buffer16;
111 uint8_t **buffer16_8;
112 };
113 int32_t **buffer24;
114 int32_t **buffer32;
115 float **bufferf;
116
117 // input values
118 boolean s8_signed;
119 boolean s16_signed;
120 boolean s24_signed;
121 boolean s32_signed;
122
123 // ring buffer
124 volatile size_t samples_filled;
126
127 // private fields (used by server)
128 uint8_t *_filebuffer;
129 ssize_t _cbytesize;
131 int _fd;
133 int _cseek;
139
140 volatile boolean die;
142
144
145typedef enum lives_audio_loop {
150
151float get_float_audio_val_at_time(int fnum, int afd, double secs, int chnum, int chans) GNU_HOT;
152float audiofile_get_maxvol(int fnum, double start, double end, float thresh);
153
154boolean normalise_audio(int fnum, double start, double end, float thresh);
155
156void sample_silence_dS(float *dst, uint64_t nsamples);
157
158void sample_silence_stream(int nchans, int64_t nframes);
159
160boolean pad_with_silence(int out_fd, void *buff, off64_t oins_size, int64_t ins_size, int asamps, int aunsigned,
161 boolean big_endian);
162
163void sample_move_d8_d16(short *dst, uint8_t *src,
164 uint64_t nsamples, size_t tbytes, double scale, int nDstChannels, int nSrcChannels, int swap_sign) GNU_HOT;
165
166void sample_move_d16_d16(short *dst, short *src,
167 uint64_t nsamples, size_t tbytes, double scale, int nDstChannels, int nSrcChannels, int swap_endian,
168 int swap_sign) GNU_HOT;
169
170void sample_move_d16_d8(uint8_t *dst, short *src,
171 uint64_t nsamples, size_t tbytes, double scale, int nDstChannels, int nSrcChannels, int swap_sign) GNU_HOT;
172
173float sample_move_d16_float(float *dst, short *src, uint64_t nsamples, uint64_t src_skip, int is_unsigned, boolean rev_endian,
174 float vol) GNU_HOT;
175
176int64_t sample_move_float_int(void *holding_buff, float **float_buffer, int nsamps, double scale, int chans, int asamps,
177 int usigned,
178 boolean swap_endian, boolean float_interleaved, float vol) GNU_HOT;
179
180int64_t sample_move_abuf_float(float **obuf, int nchans, int nsamps, int out_arate, float vol) GNU_HOT;
181
182int64_t sample_move_abuf_int16(short *obuf, int nchans, int nsamps, int out_arate) GNU_HOT;
183
184void sample_move_float_float(float *dst, float *src, uint64_t nsamples, double scale, int dst_skip) GNU_HOT;
185
186boolean float_deinterleave(float *fbuffer, int nsamps, int nchans) GNU_HOT;
187boolean float_interleave(float *fbuffer, int nsamps, int nchans) GNU_HOT;
188
189int64_t render_audio_segment(int nfiles, int *from_files, int to_file, double *avels, double *fromtime, ticks_t tc_start,
190 ticks_t tc_end, double *chvol, double opvol_start, double opvol_end, lives_audio_buf_t *obuf);
191
192void aud_fade(int fileno, double startt, double endt, double startv, double endv);
193boolean adjust_clip_volume(int fileno, float newvol, boolean make_backup);
194
195typedef enum {
203
204#ifdef ENABLE_JACK
205void jack_rec_audio_to_clip(int fileno, int oldfileno,
206 lives_rec_audio_type_t rec_type);
207void jack_rec_audio_end(boolean close_dev, boolean close_fd);
208#endif
209
210#ifdef HAVE_PULSE_AUDIO
211void pulse_rec_audio_to_clip(int fileno, int oldfileno,
212 lives_rec_audio_type_t rec_type);
213void pulse_rec_audio_end(boolean close_dev, boolean close_fd);
214#endif
215
216void fill_abuffer_from(lives_audio_buf_t *abuf, weed_plant_t *event_list, weed_plant_t *st_event, boolean exact);
217void wake_audio_thread(void);
218
219boolean resync_audio(double frameno);
220void avsync_force(void);
221
222lives_audio_track_state_t *get_audio_and_effects_state_at(weed_plant_t *event_list, weed_plant_t *st_event,
223 weed_timecode_t fill_tc, int what_to_get, boolean exact);
224
225boolean get_audio_from_plugin(float **fbuffer, int nchans, int arate, int nsamps, boolean is_audio_thread);
226void reinit_audio_gen(void);
227
228void init_jack_audio_buffers(int achans, int arate, boolean exact);
229void free_jack_audio_buffers(void);
230
231void init_pulse_audio_buffers(int achans, int arate, boolean exact);
232void free_pulse_audio_buffers(void);
233
234void audio_free_fnames(void);
235
236#define is_realtime_aplayer(ptype) ((ptype == AUD_PLAYER_JACK || ptype == AUD_PLAYER_PULSE || ptype == AUD_PLAYER_NONE))
237
238void preview_aud_vol(void);
239
241void audio_cache_end(void);
243
244boolean apply_rte_audio_init(void);
245void apply_rte_audio_end(boolean del);
246boolean apply_rte_audio(int64_t nframes);
247
248void init_audio_frame_buffers(short aplayer);
250void append_to_audio_bufferf(float *src, uint64_t nsamples, int channum);
251void append_to_audio_buffer16(void *src, uint64_t nsamples, int channum);
252boolean push_audio_to_channel(weed_plant_t *filter, weed_plant_t *achan, lives_audio_buf_t *abuf);
253
254boolean start_audio_stream(void);
255void stop_audio_stream(void);
256void clear_audio_stream(void);
257void audio_stream(void *buff, size_t nbytes, int fd);
258
259char *lives_get_audio_file_name(int fnum);
260char *get_audio_file_name(int fnum, boolean opening);
261
262char *get_achannel_name(int totchans, int idx) WARN_UNUSED;
263const char *audio_player_get_display_name(const char *aplayer);
264
266
267lives_audio_track_state_t *audio_frame_to_atstate(weed_plant_t *event, int *ntracks);
268
269#define lives_vol_from_linear(vol) ((float)squared(squared((vol))))
270#define lives_vol_to_linear(vol) (sqrtf(sqrtf((vol))))
271
272#endif
void sample_move_d16_d8(uint8_t *dst, short *src, uint64_t nsamples, size_t tbytes, double scale, int nDstChannels, int nSrcChannels, int swap_sign) GNU_HOT
convert from any number of source channels to any number of destination channels - 8 bit output
Definition: audio.c:617
int64_t sample_move_abuf_float(float **obuf, int nchans, int nsamps, int out_arate, float vol) GNU_HOT
copy audio data from cache into audio sound buffer
Definition: audio.c:888
int64_t sample_move_float_int(void *holding_buff, float **float_buffer, int nsamps, double scale, int chans, int asamps, int usigned, boolean swap_endian, boolean float_interleaved, float vol) GNU_HOT
returns frames output
Definition: audio.c:793
void audio_free_fnames(void)
Definition: audio.c:71
float get_float_audio_val_at_time(int fnum, int afd, double secs, int chnum, int chans) GNU_HOT
Definition: audio.c:374
void append_to_audio_bufferf(float *src, uint64_t nsamples, int channum)
Definition: audio.c:82
boolean apply_rte_audio(int64_t nframes)
Definition: audio.c:3514
void init_audio_frame_buffers(short aplayer)
Definition: audio.c:155
boolean get_audio_from_plugin(float **fbuffer, int nchans, int arate, int nsamps, boolean is_audio_thread)
Definition: audio.c:3332
char * get_audio_file_name(int fnum, boolean opening)
Definition: audio.c:38
void append_to_audio_buffer16(void *src, uint64_t nsamples, int channum)
Definition: audio.c:118
void wake_audio_thread(void)
Definition: audio.c:3230
float sample_move_d16_float(float *dst, short *src, uint64_t nsamples, uint64_t src_skip, int is_unsigned, boolean rev_endian, float vol) GNU_HOT
Definition: audio.c:677
void audio_cache_end(void)
Definition: audio.c:3276
char * lives_get_audio_file_name(int fnum)
Definition: audio.c:55
char * get_achannel_name(int totchans, int idx) WARN_UNUSED
Definition: audio.c:28
boolean resync_audio(double frameno)
resync audio playback to the current video frame
Definition: audio.c:2822
float audiofile_get_maxvol(int fnum, double start, double end, float thresh)
Definition: audio.c:248
void avsync_force(void)
Definition: audio.c:2785
boolean push_audio_to_channel(weed_plant_t *filter, weed_plant_t *achan, lives_audio_buf_t *abuf)
fill the audio channel(s) for effects with mixed audio / video
Definition: audio.c:3688
void sample_move_d16_d16(short *dst, short *src, uint64_t nsamples, size_t tbytes, double scale, int nDstChannels, int nSrcChannels, int swap_endian, int swap_sign) GNU_HOT
boolean float_deinterleave(float *fbuffer, int nsamps, int nchans) GNU_HOT
Definition: audio.c:1107
void fill_abuffer_from(lives_audio_buf_t *abuf, weed_plant_t *event_list, weed_plant_t *st_event, boolean exact)
Definition: audio.c:2567
void preview_aud_vol(void)
Definition: audio.c:1991
lives_cancel_t handle_audio_timeout(void)
Definition: audio.c:3971
boolean start_audio_stream(void)
Definition: audio.c:3827
void init_jack_audio_buffers(int achans, int arate, boolean exact)
Definition: audio.c:2708
int64_t render_audio_segment(int nfiles, int *from_files, int to_file, double *avels, double *fromtime, ticks_t tc_start, ticks_t tc_end, double *chvol, double opvol_start, double opvol_end, lives_audio_buf_t *obuf)
void aud_fade(int fileno, double startt, double endt, double startv, double endv)
fade in/fade out
Definition: audio.c:1813
const char * audio_player_get_display_name(const char *aplayer)
Definition: audio.c:65
int64_t sample_move_abuf_int16(short *obuf, int nchans, int nsamps, int out_arate) GNU_HOT
copy audio data from cache into audio sound buffer
Definition: audio.c:993
void reinit_audio_gen(void)
Definition: audio.c:3449
void sample_silence_stream(int nchans, int64_t nframes)
Definition: audio.c:421
void stop_audio_stream(void)
Definition: audio.c:3906
void init_pulse_audio_buffers(int achans, int arate, boolean exact)
Definition: audio.c:2729
lives_audio_buf_t * audio_cache_get_buffer(void)
Definition: audio.c:3324
boolean float_interleave(float *fbuffer, int nsamps, int nchans) GNU_HOT
Definition: audio.c:1125
void free_audio_frame_buffer(lives_audio_buf_t *abuf)
Definition: audio.c:207
lives_audio_buf_t * audio_cache_init(void)
Definition: audio.c:3238
void sample_move_float_float(float *dst, float *src, uint64_t nsamples, double scale, int dst_skip) GNU_HOT
Definition: audio.c:745
void free_pulse_audio_buffers(void)
Definition: audio.c:2768
boolean adjust_clip_volume(int fileno, float newvol, boolean make_backup)
Definition: audio.c:2001
void clear_audio_stream(void)
Definition: audio.c:3949
lives_operation_t
Definition: audio.h:69
@ LIVES_READ_OPERATION
Definition: audio.h:71
@ LIVES_WRITE_OPERATION
Definition: audio.h:72
@ LIVES_NOP_OPERATION
Definition: audio.h:70
@ LIVES_CONVERT_OPERATION
Definition: audio.h:73
void sample_silence_dS(float *dst, uint64_t nsamples)
Definition: audio.c:415
void free_jack_audio_buffers(void)
Definition: audio.c:2748
boolean normalise_audio(int fnum, double start, double end, float thresh)
Definition: audio.c:280
boolean pad_with_silence(int out_fd, void *buff, off64_t oins_size, int64_t ins_size, int asamps, int aunsigned, boolean big_endian)
Definition: audio.c:1159
lives_rec_audio_type_t
Definition: audio.h:195
@ RECA_EXISTING
Definition: audio.h:199
@ RECA_WINDOW_GRAB
Definition: audio.h:197
@ RECA_GENERATED
Definition: audio.h:201
@ RECA_EXTERNAL
Definition: audio.h:200
@ RECA_NEW_CLIP
Definition: audio.h:198
@ RECA_MONITOR
Definition: audio.h:196
lives_audio_track_state_t * audio_frame_to_atstate(weed_plant_t *event, int *ntracks)
Definition: audio.c:2405
void sample_move_d8_d16(short *dst, uint8_t *src, uint64_t nsamples, size_t tbytes, double scale, int nDstChannels, int nSrcChannels, int swap_sign) GNU_HOT
Definition: audio.c:459
void apply_rte_audio_end(boolean del)
Definition: audio.c:3507
void audio_stream(void *buff, size_t nbytes, int fd)
Definition: audio.c:3964
lives_audio_loop_t
Definition: audio.h:145
@ AUDIO_LOOP_FORWARD
Definition: audio.h:147
@ AUDIO_LOOP_NONE
Definition: audio.h:146
@ AUDIO_LOOP_PINGPONG
Definition: audio.h:148
boolean apply_rte_audio_init(void)
Definition: audio.c:3474
lives_audio_track_state_t * get_audio_and_effects_state_at(weed_plant_t *event_list, weed_plant_t *st_event, weed_timecode_t fill_tc, int what_to_get, boolean exact)
get audio (and optionally video) state at timecode tc OR before event st_event
Definition: audio.c:2419
#define GNU_HOT
Definition: main.h:88
#define WARN_UNUSED
Definition: main.h:77
int64_t ticks_t
Definition: main.h:97
lives_cancel_t
cancel reason
Definition: main.h:699
ticks_t tc
Definition: audio.h:64
volatile struct _aserver_message_t * next
Definition: audio.h:66
volatile int command
Definition: audio.h:63
volatile char * data
Definition: audio.h:65
double _shrink_factor
resampling ratio
Definition: audio.h:138
int32_t ** buffer32
sample data in 32 bit format (or NULL)
Definition: audio.h:114
int _fd
file descriptor
Definition: audio.h:131
ssize_t bytesize
Definition: audio.h:90
int _casamps
current out_asamps
Definition: audio.h:137
short ** buffer16
sample data in 16 bit format (or NULL)
Definition: audio.h:110
lives_operation_t operation
Definition: audio.h:77
float ** bufferf
sample data in float format (or NULL)
Definition: audio.h:115
boolean eof
did we read EOF ? [readonly by client]
Definition: audio.h:79
size_t _csamp_space
current sample buffer size in single channel samples
Definition: audio.h:130
uint8_t ** buffer16_8
sample data in 8 bit format (or NULL)
Definition: audio.h:111
int _cout_interleaf
Definition: audio.h:136
volatile boolean is_ready
Definition: audio.h:78
ssize_t _cbytesize
current _filebuffer bytesize; if this changes we need to realloc _filebuffer
Definition: audio.h:129
int in_achans
channels for _filebuffer side
Definition: audio.h:95
boolean out_interleaf
Definition: audio.h:93
int _cin_interleaf
Definition: audio.h:135
int32_t ** buffer24
sample data in 24 bit format (or NULL)
Definition: audio.h:113
volatile size_t samples_filled
number of samples filled (readonly client)
Definition: audio.h:124
int out_asamps
Definition: audio.h:98
boolean in_interleaf
Definition: audio.h:92
int out_achans
channels for buffer* side
Definition: audio.h:96
volatile boolean die
set to TRUE to shut down thread
Definition: audio.h:140
boolean s16_signed
Definition: audio.h:119
int _cachans
current output channels
Definition: audio.h:134
boolean sequential
hint that we will read sequentially starting from seek
Definition: audio.h:105
size_t start_sample
used for reading (readonly server)
Definition: audio.h:125
size_t samp_space
buffer space in samples (* by sizeof(type) to get bytesize) [if interleaf, also * by chans]
Definition: audio.h:103
int _cseek
current seek pos
Definition: audio.h:133
boolean s8_signed
Definition: audio.h:118
uint8_t * _filebuffer
raw data to/from file - can be cast to int16_t
Definition: audio.h:128
boolean s24_signed
Definition: audio.h:120
ticks_t start_tc
Definition: audio.h:86
off_t seek
Definition: audio.h:85
uint8_t ** buffer8
sample data in 8 bit format (or NULL)
Definition: audio.h:108
boolean s32_signed
Definition: audio.h:121
double shrink_factor
resampling ratio
Definition: audio.h:101
int _cfileno
current fileno
Definition: audio.h:132