commit 8a7c022626e1301f9d7420d964075fceadd0a638
parent c76c91fa614d2aedef9301f7207bc41e3632821c
Author: Henry Wilson <henry@henryandlizzy.uk>
Date: Thu, 21 Apr 2022 23:49:48 +0100
Add ALSA example
Diffstat:
2 files changed, 35 insertions(+), 0 deletions(-)
diff --git a/makefile b/makefile
@@ -22,5 +22,6 @@ gl-asteroids: -lglfw -lGL -lm
io_uring: -luring
pulse-async-client: -lpulse
pulse-simple-client: -lpulse-simple -lm
+alsa-simple: -lasound
.PHONY: clean all all-c all-cpp
diff --git a/src/alsa-simple.c b/src/alsa-simple.c
@@ -0,0 +1,34 @@
+#include <alsa/asoundlib.h>
+#include <stdio.h>
+
+void maybe_die(int err, char const* name)
+{
+ if (err >= 0)
+ return;
+
+ printf("Playback open error: %s\n", snd_strerror(err));
+ exit(1);
+}
+
+int main()
+{
+ unsigned char buf[1024];
+ snd_pcm_t* handle;
+
+ for (unsigned i = 0; i < sizeof buf; ++ i)
+ buf[i] = i;
+
+ int err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
+ maybe_die(err, "snd_pcm_open");
+ err = snd_pcm_set_params(handle, SND_PCM_FORMAT_U8, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 48000, 1, 500000);
+ maybe_die(err, "snd_pcm_set_params");
+ for (;;)
+ {
+ snd_pcm_sframes_t frames = snd_pcm_writei(handle, buf, sizeof buf);
+ if (frames < 0)
+ {
+ frames = snd_pcm_recover(handle, frames, 0);
+ maybe_die(err, "snd_pcm_recover");
+ }
+ }
+}