Utility function for playing audio on MacOS.
More...
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <pthread.h>
#include "audio_loop.h"
#include <unistd.h>
#include "AudioToolbox/AudioToolbox.h"
Utility function for playing audio on MacOS.
- Author
- Edward A. Lee
- Copyright
- (c) 2020-2023, The University of California at Berkeley and UT Dallas. License in BSD 2-clause
See audio_loop.h for instructions.
◆ add_to_sound()
void add_to_sound |
( |
int | index_offset, |
|
|
double | value ) |
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.
- Parameters
-
index | Where in the buffer to add the amplitude. |
value | The amplitude to add to whatever amplitude is already there. |
◆ callback()
void callback |
( |
void * | ignored, |
|
|
AudioQueueRef | queue, |
|
|
AudioQueueBufferRef | buf_ref ) |
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.
◆ lf_play_audio_waveform()
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
-
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. |
◆ 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_time | The logical time that aligns with the first audio buffer. |
◆ lf_stop_audio_loop()
void lf_stop_audio_loop |
( |
| ) |
|
Stop the audio loop thread.
◆ run_audio_loop()
void * run_audio_loop |
( |
void * | ignored | ) |
|
Run the audio loop indefinitely.
◆ lf_audio_cond
pthread_cond_t lf_audio_cond = PTHREAD_COND_INITIALIZER |
◆ lf_audio_mutex
pthread_mutex_t lf_audio_mutex = PTHREAD_MUTEX_INITIALIZER |
◆ loop_thread_id
◆ loop_thread_started
bool loop_thread_started = false |
◆ next_buffer
int16_t* next_buffer = NULL |
◆ next_buffer_start_time
◆ note_counter
◆ notes