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 14 ///sign swapping 15 #define SWAP_U_TO_S 1 ///< unsigned to signed 16 #define SWAP_S_TO_U 2 ///< signed to unsigned 17 18 ///endian swapping 19 #define SWAP_X_TO_L 1 ///< other to local 20 #define SWAP_L_TO_X 2 ///< local to other 21 22 /// defaults for when not specifed 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 29 /// max number of player channels 30 #define MAX_ACHANS 2 31 32 // keep first N audio_in filesysten handles open - multitrack only 33 #define NSTOREDFDS 64 34 35 /// TODO ** - make configurable - audio buffer size for rendering 36 #define MAX_AUDIO_MEM 32 * 1024 * 1024 37 38 /// chunk size for interpolate/effect cycle 39 #define RENDER_BLOCK_SIZE 1024 40 41 /// size of silent block in bytes 42 #define SILENCE_BLOCK_SIZE BUFFER_FILL_BYTES_LARGE 43 44 /// buffer size (output samples) for (semi) realtime audio (bytes == XSAMPLES * achans * samp. size) 45 /// this is shared across prefs->num_rtaudiobufs buffers (default 4) 46 /// used when we have an event_list (i.e multitrack or previewing a recording in CE) 47 #define XSAMPLES 393216 48 49 #define AUD_WRITE_CHECK 0xFFFFFFFFF4000000 ///< after recording this many bytes we check disk space (default 128MB) 50 51 #define WEED_LEAF_HOST_KEEP_ADATA "keep_adata" /// set to WEED_TRUE in layer if doing zero-copy audio porcessing 52 53 ///////////////////////////////////// 54 /// asynch msging 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 */ 62 typedef struct _aserver_message_t { 63 volatile int command; 64 ticks_t tc; 65 volatile char *data; 66 volatile struct _aserver_message_t *next; 67 } aserver_message_t; 68 69 typedef enum { 70 LIVES_NOP_OPERATION = 0, 71 LIVES_READ_OPERATION, 72 LIVES_WRITE_OPERATION, 73 LIVES_CONVERT_OPERATION 74 } lives_operation_t; 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; ///< did we read EOF ? [readonly by client] 80 int fileno; // [readonly by server] 81 82 // readonly by server: 83 84 // use one or other 85 off_t seek; 86 ticks_t start_tc; 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; ///< channels for _filebuffer side 96 int out_achans; ///< channels for buffer* side 97 int in_asamps; // set to -val for float 98 int out_asamps; // set to -val for float 99 int swap_sign; 100 int swap_endian; 101 double shrink_factor; ///< resampling ratio 102 103 size_t samp_space; ///< buffer space in samples (* by sizeof(type) to get bytesize) [if interleaf, also * by chans] 104 105 boolean sequential; ///< hint that we will read sequentially starting from seek 106 107 // in or out buffers 108 uint8_t **buffer8; ///< sample data in 8 bit format (or NULL) 109 union { 110 short **buffer16; ///< sample data in 16 bit format (or NULL) 111 uint8_t **buffer16_8; ///< sample data in 8 bit format (or NULL) 112 }; 113 int32_t **buffer24; ///< sample data in 24 bit format (or NULL) 114 int32_t **buffer32; ///< sample data in 32 bit format (or NULL) 115 float **bufferf; ///< sample data in float format (or NULL) 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; ///< number of samples filled (readonly client) 125 size_t start_sample; ///< used for reading (readonly server) 126 127 // private fields (used by server) 128 uint8_t *_filebuffer; ///< raw data to/from file - can be cast to int16_t 129 ssize_t _cbytesize; ///< current _filebuffer bytesize; if this changes we need to realloc _filebuffer 130 size_t _csamp_space; ///< current sample buffer size in single channel samples 131 int _fd; ///< file descriptor 132 int _cfileno; ///< current fileno 133 int _cseek; ///< current seek pos 134 int _cachans; ///< current output channels 135 int _cin_interleaf; 136 int _cout_interleaf; 137 int _casamps; ///< current out_asamps 138 double _shrink_factor; ///< resampling ratio 139 140 volatile boolean die; ///< set to TRUE to shut down thread 141 } lives_audio_buf_t; 142 143 ////////////////////////////////////////// 144 145 typedef enum lives_audio_loop { 146 AUDIO_LOOP_NONE, 147 AUDIO_LOOP_FORWARD, 148 AUDIO_LOOP_PINGPONG 149 } lives_audio_loop_t; 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; ///< returns frames output 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); ///< fade in/fade out 193 boolean adjust_clip_volume(int fileno, float newvol, boolean make_backup); 194 195 typedef enum { 196 RECA_MONITOR = 0, 197 RECA_WINDOW_GRAB, 198 RECA_NEW_CLIP, 199 RECA_EXISTING, 200 RECA_EXTERNAL, 201 RECA_GENERATED 202 } lives_rec_audio_type_t; 203 204 #ifdef ENABLE_JACK 205 void jack_rec_audio_to_clip(int fileno, int oldfileno, 206 lives_rec_audio_type_t rec_type); ///< record from external source to clip 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); ///< record from external source to clip 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 240 lives_audio_buf_t *audio_cache_init(void); 241 void audio_cache_end(void); 242 lives_audio_buf_t *audio_cache_get_buffer(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); 249 void free_audio_frame_buffer(lives_audio_buf_t *abuf); 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 265 lives_cancel_t handle_audio_timeout(void); 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 273