| 
									
										
										
										
											2006-01-06 20:01:08 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2006-01-10 09:58:15 +00:00
										 |  |  |  *  This extra small demo sends a random samples to your speakers. | 
					
						
							| 
									
										
										
										
											2006-01-06 20:01:08 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "../include/asoundlib.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *device = "default";			/* playback device */ | 
					
						
							|  |  |  | unsigned char buffer[16*1024];				/* some random data */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-05-03 20:07:29 +02:00
										 |  |  | 	int err; | 
					
						
							|  |  |  | 	unsigned int i; | 
					
						
							|  |  |  | 	snd_pcm_t *handle; | 
					
						
							|  |  |  | 	snd_pcm_sframes_t frames; | 
					
						
							| 
									
										
										
										
											2006-01-06 20:01:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 20:07:29 +02:00
										 |  |  | 	for (i = 0; i < sizeof(buffer); i++) | 
					
						
							|  |  |  | 		buffer[i] = random() & 0xff; | 
					
						
							| 
									
										
										
										
											2006-01-06 20:01:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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, | 
					
						
							| 
									
										
										
										
											2020-05-03 20:07:29 +02:00
										 |  |  | 				      SND_PCM_FORMAT_U8, | 
					
						
							|  |  |  | 				      SND_PCM_ACCESS_RW_INTERLEAVED, | 
					
						
							|  |  |  | 				      1, | 
					
						
							|  |  |  | 				      48000, | 
					
						
							|  |  |  | 				      1, | 
					
						
							|  |  |  | 				      500000)) < 0) {	/* 0.5sec */ | 
					
						
							| 
									
										
										
										
											2006-01-06 20:01:08 +00:00
										 |  |  | 		printf("Playback open error: %s\n", snd_strerror(err)); | 
					
						
							|  |  |  | 		exit(EXIT_FAILURE); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (i = 0; i < 16; i++) { | 
					
						
							| 
									
										
										
										
											2020-05-03 20:07:29 +02:00
										 |  |  | 		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(frames)); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (frames > 0 && frames < (long)sizeof(buffer)) | 
					
						
							|  |  |  | 			printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-01-06 20:01:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 20:07:29 +02:00
										 |  |  | 	/* pass the remaining samples, otherwise they're dropped in close */ | 
					
						
							|  |  |  | 	err = snd_pcm_drain(handle); | 
					
						
							|  |  |  | 	if (err < 0) | 
					
						
							|  |  |  | 		printf("snd_pcm_drain failed: %s\n", snd_strerror(err)); | 
					
						
							| 
									
										
										
										
											2006-01-06 20:01:08 +00:00
										 |  |  | 	snd_pcm_close(handle); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } |