• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  This extra small demo sends a random samples to your speakers.
3  */
4 
5 #include "../include/asoundlib.h"
6 
7 static char *device = "default";			/* playback device */
8 unsigned char buffer[16*1024];				/* some random data */
9 
main(void)10 int main(void)
11 {
12 	int err;
13 	unsigned int i;
14 	snd_pcm_t *handle;
15 	snd_pcm_sframes_t frames;
16 
17 	for (i = 0; i < sizeof(buffer); i++)
18 		buffer[i] = random() & 0xff;
19 
20 	if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
21 		printf("Playback open error: %s\n", snd_strerror(err));
22 		exit(EXIT_FAILURE);
23 	}
24 	if ((err = snd_pcm_set_params(handle,
25 				      SND_PCM_FORMAT_U8,
26 				      SND_PCM_ACCESS_RW_INTERLEAVED,
27 				      1,
28 				      48000,
29 				      1,
30 				      500000)) < 0) {	/* 0.5sec */
31 		printf("Playback open error: %s\n", snd_strerror(err));
32 		exit(EXIT_FAILURE);
33 	}
34 
35         for (i = 0; i < 16; i++) {
36 		frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
37 		if (frames < 0)
38 			frames = snd_pcm_recover(handle, frames, 0);
39 		if (frames < 0) {
40 			printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
41 			break;
42 		}
43 		if (frames > 0 && frames < (long)sizeof(buffer))
44 			printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
45 	}
46 
47 	/* pass the remaining samples, otherwise they're dropped in close */
48 	err = snd_pcm_drain(handle);
49 	if (err < 0)
50 		printf("snd_pcm_drain failed: %s\n", snd_strerror(err));
51 	snd_pcm_close(handle);
52 	return 0;
53 }
54