linux_alsa_helloworld

simple audio playback example using ALSA
git clone https://0xdd.org/code/linux_alsa_helloworld.git
Log | Files | Refs | README | LICENSE

main.c (2160B)


      1 /*
      2 2018 David DiPaola
      3 licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/)
      4 */
      5 
      6 #include <stdio.h>
      7 
      8 #include <alsa/asoundlib.h>
      9 
     10 int
     11 main() {
     12 	int exitcode = 0;
     13 	int status;
     14 
     15 	printf("getting default sound playback stream..." "\n");
     16 	snd_pcm_t * pcm = NULL;
     17 	status = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK, 0);
     18 	if (status < 0) {
     19 		printf("ERROR: %s" "\n", snd_strerror(status));
     20 		exitcode = 1;
     21 		goto cleanup;
     22 	}
     23 
     24 	printf("setting parameters..." "\n");
     25 	const unsigned int channels   =     1;
     26 	const unsigned int rate       = 44100;
     27 	const          int resample   =     1;
     28 	const unsigned int latency_us =   100;
     29 	status = snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, channels, rate, resample, latency_us);
     30 	if (status < 0) {
     31 		printf("ERROR: %s" "\n", snd_strerror(status));
     32 		exitcode = 1;
     33 		goto cleanup;
     34 	}
     35 
     36 	printf("getting number of frames in stream..." "\n");
     37 	snd_pcm_sframes_t snd_frames = snd_pcm_avail(pcm);
     38 	if (snd_frames < 0) {
     39 		printf("ERROR: %s" "\n", snd_strerror(status));
     40 		exitcode = 1;
     41 		goto cleanup;
     42 	}
     43 	printf("\t" "number of frames: %li" "\n", snd_frames);
     44 
     45 	printf("reading frames from file..." "\n");
     46 	FILE * file = fopen("sample.raw", "r+");
     47 	if (!file) {
     48 		fprintf(stderr, "ERROR: fopen()");
     49 		exitcode = 1;
     50 		goto cleanup;
     51 	}
     52 	fseek(file, 0, SEEK_END);
     53 	long file_size = ftell(file);
     54 	fseek(file, 0, SEEK_SET);
     55 	short * buffer = malloc(file_size);
     56 	size_t buffer_length = file_size / sizeof(*buffer);
     57 	fread(buffer, file_size, 1, file);
     58 	fclose(file);
     59 	file = NULL;
     60 
     61 	printf("writing %zi frames to stream..." "\n", buffer_length);
     62 	size_t i = 0;
     63 	while (i < buffer_length) {
     64 		snd_pcm_sframes_t written = snd_pcm_writei(pcm, &(buffer[i]), (buffer_length - i));
     65 		if (written < 0) {
     66 			while (snd_pcm_recover(pcm, written, 0) != 0) {
     67 				fprintf(stderr, "\t" "INFO: recovery failed" "\n");
     68 			}
     69 			continue;
     70 		}
     71 
     72 		i += written;
     73 	}
     74 
     75 	cleanup:
     76 	printf("closing sound playback stream..." "\n");
     77 	status = snd_pcm_close(pcm);
     78 	if (status < 0) {
     79 		printf("ERROR: %s" "\n", snd_strerror(status));
     80 		return 1;
     81 	}
     82 	pcm = NULL;
     83 
     84 	return exitcode;
     85 }
     86