From: Jaroslav Kysela Date: Fri, 6 Jan 2006 20:01:08 +0000 (+0000) Subject: Added pcm_min test & example - minimalistic pcm output X-Git-Tag: v1.0.11rc3~15 X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=4e1452ac07614e0536a20098700ca35de7f10f05;p=alsa-lib.git Added pcm_min test & example - minimalistic pcm output --- diff --git a/test/Makefile.am b/test/Makefile.am index 1fbde8f0..ba3082df 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,9 +1,10 @@ -check_PROGRAMS=control pcm latency seq \ +check_PROGRAMS=control pcm pcm_min latency seq \ playmidi1 timer rawmidi midiloop \ oldapi queue_timer control_LDADD=../src/libasound.la pcm_LDADD=../src/libasound.la +pcm_min_LDADD=../src/libasound.la latency_LDADD=../src/libasound.la seq_LDADD=../src/libasound.la playmidi1_LDADD=../src/libasound.la diff --git a/test/pcm_min.c b/test/pcm_min.c new file mode 100644 index 00000000..3c610189 --- /dev/null +++ b/test/pcm_min.c @@ -0,0 +1,56 @@ +/* + * This extra small demo sends a raw file to your speakers. + */ + +#include "../include/asoundlib.h" + +static char *device = "default"; /* playback device */ + +snd_output_t *output = NULL; +unsigned char buffer[16*1024]; /* some random data */ + +int main(void) +{ + int err; + unsigned int i; + snd_pcm_t *handle; + snd_pcm_sframes_t frames; + + for (i = 0; i < sizeof(buffer); i++) + buffer[i] = random() & 0xff; + + if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { + printf("Playback open error: %s\n", snd_strerror(err)); + exit(EXIT_FAILURE); + } + if ((err = snd_pcm_set_params(handle, + SND_PCM_FORMAT_U8, + SND_PCM_ACCESS_RW_INTERLEAVED, + 1, + 48000, + 1, + 500000)) < 0) { /* 0.5sec */ + printf("Playback open error: %s\n", snd_strerror(err)); + exit(EXIT_FAILURE); + } + + if ((err = snd_pcm_nonblock(handle, 0)) < 0) { + printf("Set to blocking mode failed: %s\n", snd_strerror(err)); + exit(EXIT_FAILURE); + } + + for (i = 0; i < 16; i++) { + frames = snd_pcm_writei(handle, buffer, sizeof(buffer)); + if (frames < 0) + frames = snd_pcm_recover(handle, frames, 0); + if (frames < 0) { + printf("snd_pcm_writei failed: %s\n", snd_strerror(err)); + break; + } + if (frames > 0 && frames < (long)sizeof(buffer)) + printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames); + } + + snd_pcm_close(handle); + return 0; +}