LiVES  3.2.0
Macros | Functions | Variables
audio.c File Reference
#include "main.h"
#include "audio.h"
#include "events.h"
#include "callbacks.h"
#include "effects.h"
#include "resample.h"

Go to the source code of this file.

Macros

#define CLIP_DECAY   ((double)16535. / (double)16536.)
 

Functions

LIVES_GLOBAL_INLINE char * get_achannel_name (int totchans, int idx)
 
LIVES_GLOBAL_INLINE char * get_audio_file_name (int fnum, boolean opening)
 
LIVES_GLOBAL_INLINE char * lives_get_audio_file_name (int fnum)
 
LIVES_GLOBAL_INLINE const char * audio_player_get_display_name (const char *aplayer)
 
void audio_free_fnames (void)
 
void append_to_audio_bufferf (float *src, uint64_t nsamples, int channum)
 
void append_to_audio_buffer16 (void *src, uint64_t nsamples, int channum)
 
void init_audio_frame_buffers (short aplayer)
 
void free_audio_frame_buffer (lives_audio_buf_t *abuf)
 
float audiofile_get_maxvol (int fnum, double start, double end, float thresh)
 
boolean normalise_audio (int fnum, double start, double end, float thresh)
 
float get_float_audio_val_at_time (int fnum, int afd, double secs, int chnum, int chans)
 
LIVES_GLOBAL_INLINE void sample_silence_dS (float *dst, uint64_t nsamples)
 
void sample_silence_stream (int nchans, int64_t nframes)
 
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)
 
void sample_move_d16_d16 (int16_t *dst, int16_t *src, uint64_t nsamples, size_t tbytes, double scale, int nDstChannels, int nSrcChannels, int swap_endian, int swap_sign)
 convert from any number of source channels to any number of destination channels - both interleaved More...
 
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)
 convert from any number of source channels to any number of destination channels - 8 bit output More...
 
float sample_move_d16_float (float *dst, short *src, uint64_t nsamples, uint64_t src_skip, int is_unsigned, boolean rev_endian, float vol)
 
void sample_move_float_float (float *dst, float *src, uint64_t nsamples, double scale, int dst_skip)
 
int64_t sample_move_float_int (void *holding_buff, float **float_buffer, int nsamps, double scale, int chans, int asamps, int usigned, boolean rev_endian, boolean interleaved, float vol)
 convert float samples back to int interleaved is for the float buffer; output int is always interleaved scale is out_sample_rate / in_sample_rate (so 2.0 would play twice as fast, etc.) nsamps is number of out samples, asamps is out sample bit size (8 or 16) output is in holding_buff which can be cast to uint8_t *, int16_t *, or uint16_t * returns number of frames out More...
 
int64_t sample_move_abuf_float (float **obuf, int nchans, int nsamps, int out_arate, float vol)
 copy audio data from cache into audio sound buffer More...
 
int64_t sample_move_abuf_int16 (short *obuf, int nchans, int nsamps, int out_arate)
 copy audio data from cache into audio sound buffer More...
 
boolean float_deinterleave (float *fbuffer, int nsamps, int nchans)
 
boolean float_interleave (float *fbuffer, int nsamps, int nchans)
 
boolean pad_with_silence (int out_fd, void *buff, off64_t oins_size, int64_t ins_size, int asamps, int aunsigned, boolean big_endian)
 
LIVES_LOCAL_INLINE void audio_process_events_to (weed_timecode_t tc)
 
int64_t render_audio_segment (int nfiles, int *from_files, int to_file, double *avels, double *fromtime, weed_timecode_t tc_start, weed_timecode_t tc_end, double *chvol, double opvol_start, double opvol_end, lives_audio_buf_t *obuf)
 render a chunk of audio, apply effects and mixing it More...
 
void aud_fade (int fileno, double startt, double endt, double startv, double endv)
 fade in/fade out More...
 
void preview_audio (void)
 
void preview_aud_vol (void)
 
boolean adjust_clip_volume (int fileno, float newvol, boolean make_backup)
 
LIVES_LOCAL_INLINE lives_audio_track_state_taframe_to_atstate (weed_plant_t *event)
 
LIVES_GLOBAL_INLINE lives_audio_track_state_taudio_frame_to_atstate (weed_event_t *event, int *ntracks)
 
lives_audio_track_state_tget_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 More...
 
void fill_abuffer_from (lives_audio_buf_t *abuf, weed_plant_t *event_list, weed_plant_t *st_event, boolean exact)
 
void init_jack_audio_buffers (int achans, int arate, boolean exact)
 
void init_pulse_audio_buffers (int achans, int arate, boolean exact)
 
void free_jack_audio_buffers (void)
 
void free_pulse_audio_buffers (void)
 
LIVES_GLOBAL_INLINE void avsync_force (void)
 
boolean resync_audio (double frameno)
 resync audio playback to the current video frame More...
 
void wake_audio_thread (void)
 
lives_audio_buf_taudio_cache_init (void)
 
void audio_cache_end (void)
 
LIVES_GLOBAL_INLINE lives_audio_buf_taudio_cache_get_buffer (void)
 
boolean get_audio_from_plugin (float **fbuffer, int nchans, int arate, int nsamps, boolean is_audio_thread)
 
void reinit_audio_gen (void)
 
boolean apply_rte_audio_init (void)
 
void apply_rte_audio_end (boolean del)
 
boolean apply_rte_audio (int64_t nframes)
 
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 More...
 
boolean start_audio_stream (void)
 
void stop_audio_stream (void)
 
void clear_audio_stream (void)
 
LIVES_GLOBAL_INLINE void audio_stream (void *buff, size_t nbytes, int fd)
 
LIVES_GLOBAL_INLINE lives_cancel_t handle_audio_timeout (void)
 

Variables

off64_t audio_pos
 
weed_timecode_t aud_tc
 
lives_pgid_t astream_pgid = 0
 

Macro Definition Documentation

◆ CLIP_DECAY

#define CLIP_DECAY   ((double)16535. / (double)16536.)

Definition at line 776 of file audio.c.

Function Documentation

◆ adjust_clip_volume()

boolean adjust_clip_volume ( int  fileno,
float  newvol,
boolean  make_backup 
)

Definition at line 2001 of file audio.c.

◆ aframe_to_atstate()

LIVES_LOCAL_INLINE lives_audio_track_state_t* aframe_to_atstate ( weed_plant_t *  event)

Definition at line 2400 of file audio.c.

Referenced by fill_abuffer_from(), and get_audio_and_effects_state_at().

◆ append_to_audio_buffer16()

void append_to_audio_buffer16 ( void *  src,
uint64_t  nsamples,
int  channum 
)

Definition at line 118 of file audio.c.

◆ append_to_audio_bufferf()

void append_to_audio_bufferf ( float *  src,
uint64_t  nsamples,
int  channum 
)

Definition at line 82 of file audio.c.

◆ apply_rte_audio()

boolean apply_rte_audio ( int64_t  nframes)

Definition at line 3514 of file audio.c.

◆ apply_rte_audio_end()

void apply_rte_audio_end ( boolean  del)

Definition at line 3507 of file audio.c.

◆ apply_rte_audio_init()

boolean apply_rte_audio_init ( void  )

Definition at line 3474 of file audio.c.

◆ aud_fade()

void aud_fade ( int  fileno,
double  startt,
double  endt,
double  startv,
double  endv 
)

fade in/fade out

Definition at line 1813 of file audio.c.

Referenced by adjust_clip_volume(), and on_fade_audio_activate().

◆ audio_cache_end()

void audio_cache_end ( void  )

< tell cache thread to exit when possible

Definition at line 3276 of file audio.c.

◆ audio_cache_get_buffer()

LIVES_GLOBAL_INLINE lives_audio_buf_t* audio_cache_get_buffer ( void  )

Definition at line 3324 of file audio.c.

◆ audio_cache_init()

lives_audio_buf_t* audio_cache_init ( void  )

Definition at line 3238 of file audio.c.

◆ audio_frame_to_atstate()

LIVES_GLOBAL_INLINE lives_audio_track_state_t* audio_frame_to_atstate ( weed_event_t event,
int *  ntracks 
)

Definition at line 2405 of file audio.c.

Referenced by pre_analyse().

◆ audio_free_fnames()

void audio_free_fnames ( void  )

Definition at line 71 of file audio.c.

Referenced by on_fade_audio_activate(), render_events(), and render_to_clip().

◆ audio_player_get_display_name()

LIVES_GLOBAL_INLINE const char* audio_player_get_display_name ( const char *  aplayer)

Definition at line 65 of file audio.c.

Referenced by handle_audio_timeout().

◆ audio_process_events_to()

LIVES_LOCAL_INLINE void audio_process_events_to ( weed_timecode_t  tc)

Definition at line 1241 of file audio.c.

◆ audio_stream()

LIVES_GLOBAL_INLINE void audio_stream ( void *  buff,
size_t  nbytes,
int  fd 
)

Definition at line 3964 of file audio.c.

◆ audiofile_get_maxvol()

float audiofile_get_maxvol ( int  fnum,
double  start,
double  end,
float  thresh 
)

Definition at line 248 of file audio.c.

◆ avsync_force()

LIVES_GLOBAL_INLINE void avsync_force ( void  )

force realignment of video and audio at current file->frameno / player->seek_pos

Definition at line 2785 of file audio.c.

Referenced by switch_audio_clip().

◆ clear_audio_stream()

void clear_audio_stream ( void  )

Definition at line 3949 of file audio.c.

◆ 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 at line 2567 of file audio.c.

◆ float_deinterleave()

boolean float_deinterleave ( float *  fbuffer,
int  nsamps,
int  nchans 
)

Definition at line 1107 of file audio.c.

◆ float_interleave()

boolean float_interleave ( float *  fbuffer,
int  nsamps,
int  nchans 
)

Definition at line 1125 of file audio.c.

◆ free_audio_frame_buffer()

void free_audio_frame_buffer ( lives_audio_buf_t abuf)

Definition at line 207 of file audio.c.

Referenced by append_to_audio_buffer16(), and append_to_audio_bufferf().

◆ free_jack_audio_buffers()

void free_jack_audio_buffers ( void  )

Definition at line 2748 of file audio.c.

◆ free_pulse_audio_buffers()

void free_pulse_audio_buffers ( void  )

Definition at line 2768 of file audio.c.

◆ get_achannel_name()

LIVES_GLOBAL_INLINE char* get_achannel_name ( int  totchans,
int  idx 
)

Definition at line 28 of file audio.c.

Referenced by add_audio_track(), and show_playbar_labels().

◆ 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

if st_event is not NULL, we get the state just prior to it (state being effects and filter maps, audio tracks / positions)

if st_event is NULL, this is a continuation, and we get the audio state only at timecode tc similar to quantise_events(), except we don't produce output frames

TODO: if exact && non-stateless, silently process audio / video until st_event

update audio state

Definition at line 2419 of file audio.c.

Referenced by audio_process_events_to(), and fill_abuffer_from().

◆ get_audio_file_name()

LIVES_GLOBAL_INLINE char* get_audio_file_name ( int  fnum,
boolean  opening 
)

Definition at line 38 of file audio.c.

Referenced by lives_get_audio_file_name().

◆ get_audio_from_plugin()

boolean get_audio_from_plugin ( float **  fbuffer,
int  nchans,
int  arate,
int  nsamps,
boolean  is_audio_thread 
)

Definition at line 3332 of file audio.c.

◆ get_float_audio_val_at_time()

float get_float_audio_val_at_time ( int  fnum,
int  afd,
double  secs,
int  chnum,
int  chans 
)

Definition at line 374 of file audio.c.

◆ handle_audio_timeout()

LIVES_GLOBAL_INLINE lives_cancel_t handle_audio_timeout ( void  )

◆ init_audio_frame_buffers()

void init_audio_frame_buffers ( short  aplayer)

Definition at line 155 of file audio.c.

◆ init_jack_audio_buffers()

void init_jack_audio_buffers ( int  achans,
int  arate,
boolean  exact 
)

Definition at line 2708 of file audio.c.

◆ init_pulse_audio_buffers()

void init_pulse_audio_buffers ( int  achans,
int  arate,
boolean  exact 
)

Definition at line 2729 of file audio.c.

◆ lives_get_audio_file_name()

LIVES_GLOBAL_INLINE char* lives_get_audio_file_name ( int  fnum)

◆ normalise_audio()

boolean normalise_audio ( int  fnum,
double  start,
double  end,
float  thresh 
)

Definition at line 280 of file audio.c.

Referenced by on_normalise_audio_activate().

◆ 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 at line 1159 of file audio.c.

◆ preview_aud_vol()

void preview_aud_vol ( void  )

Definition at line 1991 of file audio.c.

◆ preview_audio()

void preview_audio ( void  )

Definition at line 1835 of file audio.c.

Referenced by preview_aud_vol().

◆ 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

push audio from abuf into an audio channel audio will be formatted to the channel requested format

when we have audio effects running, the buffer is constantly fiiled from the audio thread (we have two buffers so that we dont hang the player during read). When the first client reads, the buffers are swapped.

if player is jack, we will have non-interleaved float, if player is pulse, we will have interleaved S16 so we have to convert to float and then back again.

(this is currently only used to push audio to generators, since there are currently no filters which allow both video and audio input)

Definition at line 3688 of file audio.c.

◆ reinit_audio_gen()

void reinit_audio_gen ( void  )

Definition at line 3449 of file audio.c.

◆ render_audio_segment()

int64_t render_audio_segment ( int  nfiles,
int *  from_files,
int  to_file,
double *  avels,
double *  fromtime,
weed_timecode_t  tc_start,
weed_timecode_t  tc_end,
double *  chvol,
double  opvol_start,
double  opvol_end,
lives_audio_buf_t obuf 
)

render a chunk of audio, apply effects and mixing it

called during multitrack rendering to create the actual audio file (or in-memory buffer for preview playback in multitrack)

also used for fade-in/fade-out in the clip editor (opvol_start, opvol_end)

in multitrack, chvol is taken from the audio mixer; opvol is always 1.

what we will do here: calculate our target samples (= period * out_rate)

calculate how many in_samples for each track (= period * in_rate / ABS (vel) )

read in the relevant number of samples for each track and convert to float

write this into our float buffers (1 buffer per channel per track)

we then send small chunks at a time to any audio effects; this is to allow for parameter interpolation

the small chunks are processed and mixed, converted from float back to int, and then written to the outfile

if obuf != NULL we write to obuf instead

< only used if we are writing output to a file

this is not the velocity we will use for reading, but we need to estimate how many bytes we will read in so we can calculate how many buffers to allocate

we don't want to use more than MAX_AUDIO_MEM bytes (numbers will be much larger than examples given)

calculate tbytes for xsamples

tbytes: how many bytes we want to read in. This is xsamples * the track velocity. we add a small random factor here, so half the time we round up, half the time we round down otherwise we would be gradually losing or gaining samples

convert to float

  • first we convert to 16 bit stereo (if it was 8 bit and / or mono) and we resample input is tbytes bytes at rate * velocity, and we should get out nframes audio frames at out_arate. out_achans result is in holding_buff

if we are previewing a rendering, we would get double the volume adjustment, once from the rendering and again from the audio player, so in that case we skip the adjustment here

now we convert to holding_buff to float in float_buffer and adjust the track volume

< this is our chunk size

here we work out the "visibility" of each track at tc (i.e we only get audio from the front track + backing audio) any transitions will combine audio from 2 layers (if the pref is set) backing audio tracks are always full visible the array is used to set the values of the "is_volume_master" parameter of the effect (see the Weed Audio spec.)

first track is ascrap_file - flag that no effects should be applied to it, except for the audio mixer since effects were already applied to the saved audio

the audio is now packaged into audio layers, one for each track (file). This makes it easier to remap the audio tracks from effect to effect, as layers are interchangeable with filter channels

apply the audo effects

after processing we get the audio data back from the layers

non-inplace, audio was replaced so wee need to copy to float_buffer and free

output to file: convert back to int; use out_scale of 1., since we did our resampling in sample_move_*_d16

output to memory buffer; for jack we retain the float audio, for pulse we use int16_t

Definition at line 1276 of file audio.c.

Referenced by aud_fade(), fill_abuffer_from(), on_paste_as_new_activate(), and on_recaudclip_ok_clicked().

◆ resync_audio()

boolean resync_audio ( double  frameno)

resync audio playback to the current video frame

if we are using a realtime audio player, resync to frameno and return TRUE

otherwise return FALSE

this is called internally - for example when the play position jumps, either due to external transport changes, (e.g. jack transport, osc retrigger / goto) or if we are looping a video selection, or it may be triggered from the keyboard

this is only active if "audio follows video rate/fps changes" is set and various other conditions are met.

Definition at line 2822 of file audio.c.

Referenced by aud_lock_callback(), fps_reset_callback(), and load_frame_image().

◆ sample_move_abuf_float()

int64_t sample_move_abuf_float ( float **  obuf,
int  nchans,
int  nsamps,
int  out_arate,
float  vol 
)

copy audio data from cache into audio sound buffer

  • float32 version (e.g. jack) nchans, nsamps. out_arate all refer to player values

Definition at line 888 of file audio.c.

◆ sample_move_abuf_int16()

int64_t sample_move_abuf_int16 ( short *  obuf,
int  nchans,
int  nsamps,
int  out_arate 
)

copy audio data from cache into audio sound buffer

  • int 16 version (e.g. pulseaudio) nchans, nsamps. out_arate all refer to player values

Definition at line 993 of file audio.c.

◆ sample_move_d16_d16()

void sample_move_d16_d16 ( int16_t *  dst,
int16_t *  src,
uint64_t  nsamples,
size_t  tbytes,
double  scale,
int  nDstChannels,
int  nSrcChannels,
int  swap_endian,
int  swap_sign 
)

convert from any number of source channels to any number of destination channels - both interleaved

Definition at line 524 of file audio.c.

◆ 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 
)

convert from any number of source channels to any number of destination channels - 8 bit output

Definition at line 617 of file audio.c.

◆ 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 
)

Definition at line 677 of file audio.c.

Referenced by push_audio_to_channel().

◆ 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 
)

Definition at line 459 of file audio.c.

Referenced by push_audio_to_channel().

◆ sample_move_float_float()

void sample_move_float_float ( float *  dst,
float *  src,
uint64_t  nsamples,
double  scale,
int  dst_skip 
)

Definition at line 745 of file audio.c.

Referenced by push_audio_to_channel().

◆ 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  rev_endian,
boolean  interleaved,
float  vol 
)

convert float samples back to int interleaved is for the float buffer; output int is always interleaved scale is out_sample_rate / in_sample_rate (so 2.0 would play twice as fast, etc.) nsamps is number of out samples, asamps is out sample bit size (8 or 16) output is in holding_buff which can be cast to uint8_t *, int16_t *, or uint16_t * returns number of frames out

returns frames output

clipping is applied so that -1.0 <= fval <= 1.0 the clipping value is applied linearly to vol (as a divisor), and if not reset it will decay so clip = 1.0 + (clip - 1.0) * CLIP_DECAY each sample --> clip = 1.0 + clip * CLIP_DECAY - CLIP_DECAY --> clip = (clip * CLIP_DECAY) + (1.0 - CLIP_DECAY) --> clip *= CLIP_DECAY; clip += (1.0 - CLIP_DECAY)

Definition at line 793 of file audio.c.

◆ sample_silence_dS()

LIVES_GLOBAL_INLINE void sample_silence_dS ( float *  dst,
uint64_t  nsamples 
)

Definition at line 415 of file audio.c.

◆ sample_silence_stream()

void sample_silence_stream ( int  nchans,
int64_t  nframes 
)

Definition at line 421 of file audio.c.

◆ start_audio_stream()

boolean start_audio_stream ( void  )

Definition at line 3827 of file audio.c.

Referenced by on_vppa_ok_clicked().

◆ stop_audio_stream()

void stop_audio_stream ( void  )

◆ wake_audio_thread()

void wake_audio_thread ( void  )

Definition at line 3230 of file audio.c.

Referenced by sample_move_abuf_float(), and sample_move_abuf_int16().

Variable Documentation

◆ astream_pgid

lives_pgid_t astream_pgid = 0

Definition at line 3825 of file audio.c.

Referenced by start_audio_stream(), and stop_audio_stream().

◆ aud_tc

weed_timecode_t aud_tc

Definition at line 3472 of file audio.c.

◆ audio_pos

off64_t audio_pos

Definition at line 3470 of file audio.c.

Referenced by apply_rte_audio(), and apply_rte_audio_init().