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 
18 #define SWAP_X_TO_L 1
20 #define SWAP_L_TO_X 2
21 
22 # 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 
53 
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 */
62 typedef struct _aserver_message_t {
63  volatile int command;
65  volatile char *data;
66  volatile struct _aserver_message_t *next;
68 
69 typedef enum {
75 
76 typedef 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;
93  boolean out_interleaf;
94 
95  int in_achans;
96  int out_achans;
97  int in_asamps; // set to -val for float
98  int out_asamps; // set to -val for float
99  int swap_sign;
101  double shrink_factor;
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;
125  size_t start_sample;
126 
127  // private fields (used by server)
128  uint8_t *_filebuffer;
129  ssize_t _cbytesize;
130  size_t _csamp_space;
131  int _fd;
132  int _cfileno;
133  int _cseek;
134  int _cachans;
137  int _casamps;
138  double _shrink_factor;
139 
140  volatile boolean die;
142 
144 
145 typedef enum lives_audio_loop {
150 
151 float get_float_audio_val_at_time(int fnum, int afd, double secs, int chnum, int chans) GNU_HOT;
152 float audiofile_get_maxvol(int fnum, double start, double end, float thresh);
153 
154 boolean normalise_audio(int fnum, double start, double end, float thresh);
155 
156 void sample_silence_dS(float *dst, uint64_t nsamples);
157 
158 void sample_silence_stream(int nchans, int64_t nframes);
159 
160 boolean 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 
163 void 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 
166 void 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 
170 void 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 
173 float 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 
176 int64_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 
180 int64_t sample_move_abuf_float(float **obuf, int nchans, int nsamps, int out_arate, float vol) GNU_HOT;
181 
182 int64_t sample_move_abuf_int16(short *obuf, int nchans, int nsamps, int out_arate) GNU_HOT;
183 
184 void sample_move_float_float(float *dst, float *src, uint64_t nsamples, double scale, int dst_skip) GNU_HOT;
185 
186 boolean float_deinterleave(float *fbuffer, int nsamps, int nchans) GNU_HOT;
187 boolean float_interleave(float *fbuffer, int nsamps, int nchans) GNU_HOT;
188 
189 int64_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 
192 void aud_fade(int fileno, double startt, double endt, double startv, double endv);
193 boolean adjust_clip_volume(int fileno, float newvol, boolean make_backup);
194 
195 typedef enum {
203 
204 #ifdef ENABLE_JACK
205 void jack_rec_audio_to_clip(int fileno, int oldfileno,
206  lives_rec_audio_type_t rec_type);
207 void jack_rec_audio_end(boolean close_dev, boolean close_fd);
208 #endif
209 
210 #ifdef HAVE_PULSE_AUDIO
211 void pulse_rec_audio_to_clip(int fileno, int oldfileno,
212  lives_rec_audio_type_t rec_type);
213 void pulse_rec_audio_end(boolean close_dev, boolean close_fd);
214 #endif
215 
216 void fill_abuffer_from(lives_audio_buf_t *abuf, weed_plant_t *event_list, weed_plant_t *st_event, boolean exact);
217 void wake_audio_thread(void);
218 
219 boolean resync_audio(double frameno);
220 void avsync_force(void);
221 
222 lives_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 
225 boolean get_audio_from_plugin(float **fbuffer, int nchans, int arate, int nsamps, boolean is_audio_thread);
226 void reinit_audio_gen(void);
227 
228 void init_jack_audio_buffers(int achans, int arate, boolean exact);
229 void free_jack_audio_buffers(void);
230 
231 void init_pulse_audio_buffers(int achans, int arate, boolean exact);
232 void free_pulse_audio_buffers(void);
233 
234 void audio_free_fnames(void);
235 
236 #define is_realtime_aplayer(ptype) ((ptype == AUD_PLAYER_JACK || ptype == AUD_PLAYER_PULSE || ptype == AUD_PLAYER_NONE))
237 
238 void preview_aud_vol(void);
239 
241 void audio_cache_end(void);
243 
244 boolean apply_rte_audio_init(void);
245 void apply_rte_audio_end(boolean del);
246 boolean apply_rte_audio(int64_t nframes);
247 
248 void init_audio_frame_buffers(short aplayer);
250 void append_to_audio_bufferf(float *src, uint64_t nsamples, int channum);
251 void append_to_audio_buffer16(void *src, uint64_t nsamples, int channum);
252 boolean push_audio_to_channel(weed_plant_t *filter, weed_plant_t *achan, lives_audio_buf_t *abuf);
253 
254 boolean start_audio_stream(void);
255 void stop_audio_stream(void);
256 void clear_audio_stream(void);
257 void audio_stream(void *buff, size_t nbytes, int fd);
258 
259 char *lives_get_audio_file_name(int fnum);
260 char *get_audio_file_name(int fnum, boolean opening);
261 
262 char *get_achannel_name(int totchans, int idx) WARN_UNUSED;
263 const char *audio_player_get_display_name(const char *aplayer);
264 
266 
267 lives_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
free_jack_audio_buffers
void free_jack_audio_buffers(void)
Definition: audio.c:2748
pad_with_silence
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_audio_buf_t::operation
lives_operation_t operation
Definition: audio.h:77
lives_audio_buf_t::out_achans
int out_achans
channels for buffer* side
Definition: audio.h:96
lives_audio_buf_t::swap_sign
int swap_sign
Definition: audio.h:99
reinit_audio_gen
void reinit_audio_gen(void)
Definition: audio.c:3449
wake_audio_thread
void wake_audio_thread(void)
Definition: audio.c:3230
lives_audio_buf_t::eof
boolean eof
did we read EOF ? [readonly by client]
Definition: audio.h:79
sample_move_d16_d8
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
audio_frame_to_atstate
lives_audio_track_state_t * audio_frame_to_atstate(weed_plant_t *event, int *ntracks)
Definition: audio.c:2405
lives_audio_buf_t::buffer24
int32_t ** buffer24
sample data in 24 bit format (or NULL)
Definition: audio.h:113
get_audio_file_name
char * get_audio_file_name(int fnum, boolean opening)
Definition: audio.c:38
lives_audio_buf_t
Definition: audio.h:76
audio_stream
void audio_stream(void *buff, size_t nbytes, int fd)
Definition: audio.c:3964
fill_abuffer_from
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
free_pulse_audio_buffers
void free_pulse_audio_buffers(void)
Definition: audio.c:2768
lives_audio_track_state_t
Definition: main.h:646
lives_audio_buf_t::s24_signed
boolean s24_signed
Definition: audio.h:120
lives_audio_buf_t::samples_filled
volatile size_t samples_filled
number of samples filled (readonly client)
Definition: audio.h:124
LIVES_READ_OPERATION
@ LIVES_READ_OPERATION
Definition: audio.h:71
audio_cache_end
void audio_cache_end(void)
Definition: audio.c:3276
GNU_HOT
#define GNU_HOT
Definition: main.h:88
lives_audio_buf_t::_shrink_factor
double _shrink_factor
resampling ratio
Definition: audio.h:138
lives_audio_buf_t::_cin_interleaf
int _cin_interleaf
Definition: audio.h:135
lives_audio_buf_t::s16_signed
boolean s16_signed
Definition: audio.h:119
lives_audio_buf_t::start_sample
size_t start_sample
used for reading (readonly server)
Definition: audio.h:125
sample_move_float_int
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
lives_audio_buf_t::_filebuffer
uint8_t * _filebuffer
raw data to/from file - can be cast to int16_t
Definition: audio.h:128
normalise_audio
boolean normalise_audio(int fnum, double start, double end, float thresh)
Definition: audio.c:280
append_to_audio_bufferf
void append_to_audio_bufferf(float *src, uint64_t nsamples, int channum)
Definition: audio.c:82
get_float_audio_val_at_time
float get_float_audio_val_at_time(int fnum, int afd, double secs, int chnum, int chans) GNU_HOT
Definition: audio.c:374
AUDIO_LOOP_NONE
@ AUDIO_LOOP_NONE
Definition: audio.h:146
resync_audio
boolean resync_audio(double frameno)
resync audio playback to the current video frame
Definition: audio.c:2822
RECA_MONITOR
@ RECA_MONITOR
Definition: audio.h:196
lives_audio_buf_t::buffer16_8
uint8_t ** buffer16_8
sample data in 8 bit format (or NULL)
Definition: audio.h:111
ticks_t
int64_t ticks_t
Definition: main.h:97
audiofile_get_maxvol
float audiofile_get_maxvol(int fnum, double start, double end, float thresh)
Definition: audio.c:248
lives_audio_buf_t::arate
int arate
Definition: audio.h:88
lives_audio_buf_t::out_asamps
int out_asamps
Definition: audio.h:98
float_deinterleave
boolean float_deinterleave(float *fbuffer, int nsamps, int nchans) GNU_HOT
Definition: audio.c:1107
lives_audio_buf_t::seek
off_t seek
Definition: audio.h:85
lives_audio_buf_t::die
volatile boolean die
set to TRUE to shut down thread
Definition: audio.h:140
RECA_WINDOW_GRAB
@ RECA_WINDOW_GRAB
Definition: audio.h:197
sample_move_float_float
void sample_move_float_float(float *dst, float *src, uint64_t nsamples, double scale, int dst_skip) GNU_HOT
Definition: audio.c:745
handle_audio_timeout
lives_cancel_t handle_audio_timeout(void)
Definition: audio.c:3971
sample_move_d16_d16
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
LIVES_WRITE_OPERATION
@ LIVES_WRITE_OPERATION
Definition: audio.h:72
lives_operation_t
lives_operation_t
Definition: audio.h:69
audio_player_get_display_name
const char * audio_player_get_display_name(const char *aplayer)
Definition: audio.c:65
sample_silence_stream
void sample_silence_stream(int nchans, int64_t nframes)
Definition: audio.c:421
lives_audio_buf_t::in_interleaf
boolean in_interleaf
Definition: audio.h:92
lives_audio_buf_t::bytesize
ssize_t bytesize
Definition: audio.h:90
start_audio_stream
boolean start_audio_stream(void)
Definition: audio.c:3827
append_to_audio_buffer16
void append_to_audio_buffer16(void *src, uint64_t nsamples, int channum)
Definition: audio.c:118
preview_aud_vol
void preview_aud_vol(void)
Definition: audio.c:1991
LIVES_CONVERT_OPERATION
@ LIVES_CONVERT_OPERATION
Definition: audio.h:73
sample_move_abuf_float
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
aud_fade
void aud_fade(int fileno, double startt, double endt, double startv, double endv)
fade in/fade out
Definition: audio.c:1813
lives_audio_buf_t::_cout_interleaf
int _cout_interleaf
Definition: audio.h:136
free_audio_frame_buffer
void free_audio_frame_buffer(lives_audio_buf_t *abuf)
Definition: audio.c:207
lives_audio_buf_t::fileno
int fileno
Definition: audio.h:80
lives_audio_buf_t::bufferf
float ** bufferf
sample data in float format (or NULL)
Definition: audio.h:115
lives_cancel_t
lives_cancel_t
cancel reason
Definition: main.h:699
stop_audio_stream
void stop_audio_stream(void)
Definition: audio.c:3906
RECA_GENERATED
@ RECA_GENERATED
Definition: audio.h:201
lives_audio_buf_t::buffer32
int32_t ** buffer32
sample data in 32 bit format (or NULL)
Definition: audio.h:114
apply_rte_audio_end
void apply_rte_audio_end(boolean del)
Definition: audio.c:3507
lives_audio_loop_t
lives_audio_loop_t
Definition: audio.h:145
lives_audio_buf_t::_cseek
int _cseek
current seek pos
Definition: audio.h:133
audio_cache_get_buffer
lives_audio_buf_t * audio_cache_get_buffer(void)
Definition: audio.c:3324
render_audio_segment
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)
lives_audio_buf_t::samp_space
size_t samp_space
buffer space in samples (* by sizeof(type) to get bytesize) [if interleaf, also * by chans]
Definition: audio.h:103
get_audio_from_plugin
boolean get_audio_from_plugin(float **fbuffer, int nchans, int arate, int nsamps, boolean is_audio_thread)
Definition: audio.c:3332
lives_audio_buf_t::_cfileno
int _cfileno
current fileno
Definition: audio.h:132
lives_audio_buf_t::s32_signed
boolean s32_signed
Definition: audio.h:121
apply_rte_audio
boolean apply_rte_audio(int64_t nframes)
Definition: audio.c:3514
sample_move_d16_float
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
sample_silence_dS
void sample_silence_dS(float *dst, uint64_t nsamples)
Definition: audio.c:415
lives_audio_buf_t::s8_signed
boolean s8_signed
Definition: audio.h:118
lives_audio_buf_t::_csamp_space
size_t _csamp_space
current sample buffer size in single channel samples
Definition: audio.h:130
aserver_message_t::data
volatile char * data
Definition: audio.h:65
avsync_force
void avsync_force(void)
Definition: audio.c:2785
WARN_UNUSED
#define WARN_UNUSED
Definition: main.h:77
lives_audio_buf_t::shrink_factor
double shrink_factor
resampling ratio
Definition: audio.h:101
RECA_NEW_CLIP
@ RECA_NEW_CLIP
Definition: audio.h:198
init_pulse_audio_buffers
void init_pulse_audio_buffers(int achans, int arate, boolean exact)
Definition: audio.c:2729
lives_get_audio_file_name
char * lives_get_audio_file_name(int fnum)
Definition: audio.c:55
clear_audio_stream
void clear_audio_stream(void)
Definition: audio.c:3949
aserver_message_t
Definition: audio.h:62
adjust_clip_volume
boolean adjust_clip_volume(int fileno, float newvol, boolean make_backup)
Definition: audio.c:2001
init_jack_audio_buffers
void init_jack_audio_buffers(int achans, int arate, boolean exact)
Definition: audio.c:2708
AUDIO_LOOP_FORWARD
@ AUDIO_LOOP_FORWARD
Definition: audio.h:147
lives_rec_audio_type_t
lives_rec_audio_type_t
Definition: audio.h:195
lives_audio_buf_t::is_ready
volatile boolean is_ready
Definition: audio.h:78
sample_move_d8_d16
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
LIVES_NOP_OPERATION
@ LIVES_NOP_OPERATION
Definition: audio.h:70
aserver_message_t::tc
ticks_t tc
Definition: audio.h:64
lives_audio_buf_t::out_interleaf
boolean out_interleaf
Definition: audio.h:93
lives_audio_buf_t::swap_endian
int swap_endian
Definition: audio.h:100
RECA_EXTERNAL
@ RECA_EXTERNAL
Definition: audio.h:200
lives_audio_buf_t::sequential
boolean sequential
hint that we will read sequentially starting from seek
Definition: audio.h:105
lives_audio_buf_t::_fd
int _fd
file descriptor
Definition: audio.h:131
audio_free_fnames
void audio_free_fnames(void)
Definition: audio.c:71
lives_audio_buf_t::in_asamps
int in_asamps
Definition: audio.h:97
audio_cache_init
lives_audio_buf_t * audio_cache_init(void)
Definition: audio.c:3238
aserver_message_t::next
volatile struct _aserver_message_t * next
Definition: audio.h:66
push_audio_to_channel
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
lives_audio_buf_t::_cbytesize
ssize_t _cbytesize
current _filebuffer bytesize; if this changes we need to realloc _filebuffer
Definition: audio.h:129
lives_audio_buf_t::buffer8
uint8_t ** buffer8
sample data in 8 bit format (or NULL)
Definition: audio.h:108
init_audio_frame_buffers
void init_audio_frame_buffers(short aplayer)
Definition: audio.c:155
lives_audio_buf_t::_casamps
int _casamps
current out_asamps
Definition: audio.h:137
get_achannel_name
char * get_achannel_name(int totchans, int idx) WARN_UNUSED
Definition: audio.c:28
get_audio_and_effects_state_at
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
apply_rte_audio_init
boolean apply_rte_audio_init(void)
Definition: audio.c:3474
aserver_message_t::command
volatile int command
Definition: audio.h:63
AUDIO_LOOP_PINGPONG
@ AUDIO_LOOP_PINGPONG
Definition: audio.h:148
RECA_EXISTING
@ RECA_EXISTING
Definition: audio.h:199
lives_audio_buf_t::start_tc
ticks_t start_tc
Definition: audio.h:86
lives_audio_buf_t::buffer16
short ** buffer16
sample data in 16 bit format (or NULL)
Definition: audio.h:110
lives_audio_buf_t::in_achans
int in_achans
channels for _filebuffer side
Definition: audio.h:95
sample_move_abuf_int16
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
lives_audio_buf_t::_cachans
int _cachans
current output channels
Definition: audio.h:134
float_interleave
boolean float_interleave(float *fbuffer, int nsamps, int nchans) GNU_HOT
Definition: audio.c:1125