From 2f30c4986c5f564b206f4acee8bb9b1824b131df Mon Sep 17 00:00:00 2001 From: Michel Pollet Date: Sat, 28 Oct 2023 06:16:56 +0100 Subject: [PATCH] speaker: Allow disabling audio, not just muting Allow turning off the audio output (ie not initialize it). Also removed the debug file descriptor global Signed-off-by: Michel Pollet --- src/mii_speaker.c | 27 +++++++++++++++------------ src/mii_speaker.h | 2 ++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/mii_speaker.c b/src/mii_speaker.c index 521b899..42f1287 100644 --- a/src/mii_speaker.c +++ b/src/mii_speaker.c @@ -90,7 +90,8 @@ mii_speaker_init( s->mii = mii; s->fsize = MII_SPEAKER_FRAME_SIZE; #ifdef HAS_ALSA - _alsa_init(s); // this can/will change fsize + if (!s->off) + _alsa_init(s); // this can/will change fsize #endif s->vol_multiplier = 0.2; s->sample = 0x8000; @@ -132,7 +133,8 @@ mii_speaker_click( (s->mii->cycles - f->start) > (2 * s->fsize * s->clk_per_sample)) { // printf("Restarting playback\n"); #ifdef HAS_ALSA - snd_pcm_prepare(s->alsa_pcm); + if (s->alsa_pcm) + snd_pcm_prepare(s->alsa_pcm); #endif f->start = s->mii->cycles - (s->clk_per_sample * 8); f->fill = 0; @@ -166,7 +168,6 @@ mii_speaker_click( f->audio[sample_index] = s->sample * s->vol_multiplier; } -int g_mii_audio_record_fd = -1; // Check to see if there's a new frame to send, send it void @@ -186,15 +187,17 @@ mii_speaker_run( s->fplay = (s->fplay + 1) % MII_SPEAKER_FRAME_COUNT; f = &s->frame[s->fplaying]; if (!s->muted) { -#ifdef HAS_ALSA - int pcm; - if (g_mii_audio_record_fd != -1) - write(g_mii_audio_record_fd, f->audio, + if (s->debug_fd != -1) + write(s->debug_fd, f->audio, f->fill * sizeof(s->frame[0].audio[0])); - if ((pcm = snd_pcm_writei(s->alsa_pcm, - f->audio, f->fill)) == -EPIPE) { - printf("%s Underrun.\n", __func__); - snd_pcm_recover(s->alsa_pcm, pcm, 1); +#ifdef HAS_ALSA + if (s->alsa_pcm) { + int pcm; + if ((pcm = snd_pcm_writei(s->alsa_pcm, + f->audio, f->fill)) == -EPIPE) { + printf("%s Underrun.\n", __func__); + snd_pcm_recover(s->alsa_pcm, pcm, 1); + } } #endif } @@ -223,4 +226,4 @@ mii_speaker_volume( s->volume = volume; // printf("audio: speaker volume set to %.3f (%.4f)\n", volume, mul); -} \ No newline at end of file +} diff --git a/src/mii_speaker.h b/src/mii_speaker.h index a8b1b42..a9995cc 100644 --- a/src/mii_speaker.h +++ b/src/mii_speaker.h @@ -24,8 +24,10 @@ typedef struct mii_audio_frame_t { typedef struct mii_speaker_t { struct mii_t * mii; + int debug_fd; // if > 0, dump audio to this fd void * alsa_pcm; // alsa pcm handle bool muted; // if true, don't play anything + bool off; // if true, don't even initialize (alsa) float volume; // volume, 0.0 to 10.0 float vol_multiplier; // sample multiplier, 0.0 to 1.0 float cpu_speed; // CPU speed in MHz, to calculate clk_per_sample