Skip to content

Instantly share code, notes, and snippets.

@dasl-
Created November 12, 2022 21:29
Show Gist options
  • Save dasl-/2d81fd265bd751b0b98e1e3598625cd9 to your computer and use it in GitHub Desktop.
Save dasl-/2d81fd265bd751b0b98e1e3598625cd9 to your computer and use it in GitHub Desktop.
diff --git a/audio_alsa.c b/audio_alsa.c
index 62db843f..33394481 100644
--- a/audio_alsa.c
+++ b/audio_alsa.c
@@ -75,7 +75,7 @@ static void *alsa_buffer_monitor_thread_code(void *arg);
static void volume(double vol);
static void do_volume(double vol);
static int prepare(void);
-static int do_play(void *buf, int samples);
+static int do_play(void *buf, int samples, int sample_type, uint32_t timestamp, uint64_t playtime);
static void parameters(audio_parameters *info);
static int mute(int do_mute); // returns true if it actually is allowed to use the mute
@@ -218,7 +218,7 @@ static int precision_delay_available() {
generate_zero_frames(silence, frames_of_silence, config.output_format,
use_dither, // i.e. with dither
dither_random_number_store);
- do_play(silence, frames_of_silence);
+ do_play(silence, frames_of_silence, play_samples_are_untimed, 0, 0);
pthread_cleanup_pop(1);
// now we can get the delay, and we'll note if it uses update timestamps
yndk_type uses_update_timestamps;
@@ -1726,7 +1726,7 @@ static int get_rate_information(uint64_t *elapsed_time, uint64_t *frames_played)
}
*/
-static int do_play(void *buf, int samples) {
+static int do_play(void *buf, int samples, int sample_type, uint32_t timestamp, uint64_t playtime) {
// assuming the alsa_mutex has been acquired
// debug(3,"audio_alsa play called.");
int oldState;
@@ -1749,7 +1749,19 @@ static int do_play(void *buf, int samples) {
snd_pcm_state_t prior_state = state; // keep this for afterwards....
// debug(3, "alsa: write %d frames.", samples);
+
+ if (sample_type == play_samples_are_timed) {
+ int64_t lead_time = playtime - get_absolute_time_in_ns();
+ debug(2, "starting alsa_pcm_write for frame: %d, delay: %ld frames, leadtime: %f milliseconds, deviation from requested playtime: %f milliseconds",
+ timestamp, my_delay, 0.000001 * lead_time, ((0.000001 * lead_time) - (1000.0 * my_delay / 44100)));
+ }
ret = alsa_pcm_write(alsa_handle, buf, samples);
+ if (sample_type == play_samples_are_timed) {
+ lead_time = playtime - get_absolute_time_in_ns();
+ debug(2, "finished alsa_pcm_write for frame: %d, delay: %ld frames, leadtime: %f milliseconds, deviation from requested playtime: %f milliseconds",
+ timestamp, my_delay, 0.000001 * lead_time, ((0.000001 * lead_time) - (1000.0 * my_delay / 44100)));
+ }
+
if (ret > 0)
frames_sent_for_playing += ret; // this is the number of frames accepted
if (ret == samples) {
@@ -1889,6 +1901,12 @@ static int play(void *buf, int samples, __attribute__((unused)) int sample_type,
__attribute__((unused)) uint32_t timestamp,
__attribute__((unused)) uint64_t playtime) {
+ // if (sample_type == play_samples_are_timed) {
+ // int64_t lead_time = playtime - get_absolute_time_in_ns();
+ // debug(2, "leadtime for frame %u is %" PRId64 " nanoseconds, i.e. %f seconds. Playtime: %" PRId64, timestamp,
+ // lead_time, 0.000000001 * lead_time, playtime);
+ // }
+
// play() will change the state of the alsa_backend_mode to abm_playing
// also, if the present alsa_backend_state is abm_disconnected, then first the
// DAC must be
@@ -1916,7 +1934,7 @@ static int play(void *buf, int samples, __attribute__((unused)) int sample_type,
// set_mute_state(); // try to action the request and return a status
// do_mute(0); // unmute for backend's reason
}
- ret = do_play(buf, samples);
+ ret = do_play(buf, samples, sample_type, timestamp, playtime);
}
debug_mutex_unlock(&alsa_mutex, 0);
@@ -2135,7 +2153,7 @@ static void *alsa_buffer_monitor_thread_code(__attribute__((unused)) void *arg)
generate_zero_frames(silence, frames_of_silence, config.output_format,
use_dither, // i.e. with dither
dither_random_number_store);
- ret = do_play(silence, frames_of_silence);
+ ret = do_play(silence, frames_of_silence, play_samples_are_untimed, 0, 0);
frame_count++;
pthread_cleanup_pop(1); // free malloced buffer
if (ret < 0) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment