reactor-c
C Runtime for Lingua Franca
|
Utility function for playing audio on Linux. More...
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <pthread.h>
#include "audio_loop.h"
#include <unistd.h>
#include <poll.h>
#include <alsa/asoundlib.h>
#include <stdbool.h>
Data Structures | |
struct | note |
Macros | |
#define | AUDIO_DEVICE "default" |
Functions | |
void | add_to_sound (int index_offset, double value) |
int | callback (snd_pcm_t *playback_handle, int16_t buf_ref[]) |
void * | run_audio_loop (void *ignored) |
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) |
Utility function for playing audio on Linux.
See audio_loop.h for instructions.
#define AUDIO_DEVICE "default" |
Add the given value to the current write buffer at the specified index. If the resulting value is larger than what can be represented in the 16-bit short, truncate it.
index | Where in the buffer to add the amplitude. |
value | The amplitude to add to whatever amplitude is already there. |
Function that is called by the audio loop to fill the audio buffer with the next batch of audio data. When this callback occurs, this grabs the mutex lock, copies the buffer that the main program has been filling into the destination buffer, clears the next buffer, and updates the start time of the next buffer.
playback_handle | Handle for the audio interface |
buffer_ref | Reference to the buffer of size AUDIO_BUFFER_SIZE to be copied to the hardware |
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.
waveform | The waveform to play or NULL to just play a tick. |
emphasis | The emphasis (0.0 for silence, 1.0 for waveform volume). |
start_time | The time to start playing the waveform. |
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.
start_time | The logical time that aligns with the first audio buffer. |
void lf_stop_audio_loop | ( | ) |
Stop the audio loop thread.
pthread_cond_t lf_audio_cond = PTHREAD_COND_INITIALIZER |
pthread_mutex_t lf_audio_mutex = PTHREAD_MUTEX_INITIALIZER |
pthread_t loop_thread_id |
int note_counter = 0 |
snd_async_handler_t* pcm_callback |
snd_pcm_t* playback_handle |