examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

alsa-simple.c (754B)


      1 #include <alsa/asoundlib.h>
      2 #include <stdio.h>
      3 
      4 void maybe_die(int err, char const* name)
      5 {
      6 	if (err >= 0)
      7 		return;
      8 
      9 	printf("%s: %s\n", name, snd_strerror(err));
     10         exit(1);
     11 }
     12 
     13 int main()
     14 {
     15 	unsigned char buf[1024];
     16 	snd_pcm_t* handle;
     17 
     18 	for (unsigned i = 0; i < sizeof buf; ++ i)
     19 		buf[i] = i;
     20 
     21 	int err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
     22 	maybe_die(err, "snd_pcm_open");
     23 	err = snd_pcm_set_params(handle, SND_PCM_FORMAT_U8, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 48000, 1, 500000);
     24 	maybe_die(err, "snd_pcm_set_params");
     25 	for (;;)
     26 	{
     27 		snd_pcm_sframes_t frames = snd_pcm_writei(handle, buf, sizeof buf);
     28 		if (frames < 0)
     29 		{
     30 			frames = snd_pcm_recover(handle, frames, 0);
     31 			maybe_die(err, "snd_pcm_recover");
     32 		}
     33 	}
     34 }