reactor-c
C Runtime for Lingua Franca
Loading...
Searching...
No Matches
audio_loop.h File Reference

Utility function for playing audio on Linux or MacOS. More...

#include "wave_file_reader.h"
#include "tag.h"

Go to the source code of this file.

Macros

#define SAMPLE_RATE   44100
 
#define AUDIO_BUFFER_SIZE   4410
 
#define BUFFER_DURATION_NS   100000000LL
 
#define NUM_CHANNELS   1
 
#define MAX_AMPLITUDE   32765
 
#define NUM_NOTES   8
 

Functions

void lf_start_audio_loop (instant_t start_time)
 
void lf_stop_audio_loop ()
 
int lf_play_audio_waveform (lf_waveform_t *waveform, float emphasis, instant_t start_time)
 

Detailed Description

Utility function for playing audio on Linux or MacOS.

Author
Edward A. Lee
Soroush Bateni

Audio functions for Linux or MacOS. To start an audio loop, call lf_start_audio_loop(), passing it the logical time at which you would like the loop to start. To play a waveform, call lf_play_audio_waveform(). A waveform may be synthesized or read from a .wav file using read_wave_file() (see wave_file_reader.h).

To use this, include the following in your target properties:

 files: [
     "/lib/c/reactor-c/util/audio_loop_mac.c",
     "/lib/c/reactor-c/util/audio_loop.h",
     "/lib/c/reactor-c/util/audio_loop_linux.c",
 ],
 cmake-include: [
     "/lib/c/reactor-c/util/audio_loop.cmake"
 ]

In addition, you need this in your Lingua Franca file:

preamble {=
    #include "audio_loop.h"
=}

Macro Definition Documentation

◆ AUDIO_BUFFER_SIZE

#define AUDIO_BUFFER_SIZE   4410

◆ BUFFER_DURATION_NS

#define BUFFER_DURATION_NS   100000000LL

◆ MAX_AMPLITUDE

#define MAX_AMPLITUDE   32765

◆ NUM_CHANNELS

#define NUM_CHANNELS   1

◆ NUM_NOTES

#define NUM_NOTES   8

◆ SAMPLE_RATE

#define SAMPLE_RATE   44100

Function Documentation

◆ lf_play_audio_waveform()

int lf_play_audio_waveform ( lf_waveform_t * waveform,
float emphasis,
instant_t start_time )

Play the specified waveform with the specified emphasis at the specified time. If the waveform is null, play a simple tick (an impulse). If the waveform has length zero or volume 0, play nothing.

If the time is too far in the future (beyond the window of the current audio write buffer), then block until the audio output catches up. If the audio playback has already passed the specified point, then play the waveform as soon as possible and return 1. Otherwise, return 0.

Parameters
waveformThe waveform to play or NULL to just play a tick.
emphasisThe emphasis (0.0 for silence, 1.0 for waveform volume).
start_timeThe time to start playing the waveform.

◆ lf_start_audio_loop()

void lf_start_audio_loop ( instant_t start_time)

Start an audio loop thread that becomes ready to receive audio amplitude samples via add_to_sound(). If there is already an audio loop running, then do nothing.

Parameters
start_timeThe logical time that aligns with the first audio buffer.

◆ lf_stop_audio_loop()

void lf_stop_audio_loop ( )

Stop the audio loop thread.