From 5317e1ce90f781458e3e75585a6f46ba2ea788f2 Mon Sep 17 00:00:00 2001 From: Taylor Holberton Date: Sun, 4 Dec 2016 13:17:53 -0800 Subject: Added examples for basic IO Added an example of pcm_writei usage and an example of pcm_readi usage. Both examples use a common file called 'audio.raw'. --- examples/Makefile | 12 ++++++ examples/pcm-readi.c | 83 +++++++++++++++++++++++++++++++++++++++++ examples/pcm-writei.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 examples/Makefile create mode 100644 examples/pcm-readi.c create mode 100644 examples/pcm-writei.c (limited to 'examples') diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..235f348 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,12 @@ +CC = gcc +CFLAGS = -Wall -Wextra -Werror -Wfatal-errors + +VPATH = ../src + +.PHONY: all +all: pcm-readi pcm-writei + +pcm-readi: pcm-readi.c -ltinyalsa + +pcm-writei: pcm-writei.c -ltinyalsa + diff --git a/examples/pcm-readi.c b/examples/pcm-readi.c new file mode 100644 index 0000000..5b85ca6 --- /dev/null +++ b/examples/pcm-readi.c @@ -0,0 +1,83 @@ +#include +#include + +#include + +static size_t read_frames(void **frames) +{ + unsigned int card = 0; + unsigned int device = 0; + int flags = PCM_IN; + + const struct pcm_config config = { + .channels = 2, + .rate = 48000, + .format = PCM_FORMAT_S32_LE, + .period_size = 1024, + .period_count = 2, + .start_threshold = 1024, + .silence_threshold = 1024 * 2, + .stop_threshold = 1024 * 2 + }; + + struct pcm *pcm = pcm_open(card, device, flags, &config); + if (pcm == NULL) { + fprintf(stderr, "failed to allocate memory for PCM\n"); + return EXIT_FAILURE; + } else if (!pcm_is_ready(pcm)){ + pcm_close(pcm); + fprintf(stderr, "failed to open PCM\n"); + return EXIT_FAILURE; + } + + unsigned int frame_size = pcm_frames_to_bytes(pcm, 1); + unsigned int frames_per_sec = pcm_get_rate(pcm); + + *frames = malloc(frame_size * frames_per_sec); + if (*frames == NULL) { + fprintf(stderr, "failed to allocate frames\n"); + pcm_close(pcm); + return EXIT_FAILURE; + } + + int read_count = pcm_readi(pcm, *frames, frames_per_sec); + + size_t byte_count = pcm_frames_to_bytes(pcm, read_count); + + pcm_close(pcm); + + return byte_count; +} + +static int write_file(const void *frames, size_t size) +{ + FILE *output_file = fopen("audio.raw", "wb"); + if (output_file == NULL) { + perror("failed to open 'audio.raw' for writing"); + return EXIT_FAILURE; + } + fwrite(frames, 1, size, output_file); + fclose(output_file); + return 0; +} + +int main(void) +{ + void *frames; + size_t size; + + size = read_frames(&frames); + if (size == 0) { + return EXIT_FAILURE; + } + + if (write_file(frames, size) < 0) { + free(frames); + return EXIT_FAILURE; + } + + free(frames); + + return EXIT_SUCCESS; +} + diff --git a/examples/pcm-writei.c b/examples/pcm-writei.c new file mode 100644 index 0000000..66280eb --- /dev/null +++ b/examples/pcm-writei.c @@ -0,0 +1,100 @@ +#include +#include + +#include + +static long int file_size(FILE * file) +{ + if (fseek(file, 0, SEEK_END) < 0) { + return -1; + } + long int file_size = ftell(file); + if (fseek(file, 0, SEEK_SET) < 0) { + return -1; + } + return file_size; +} + +static size_t read_file(void ** frames){ + + FILE * input_file = fopen("audio.raw", "rb"); + if (input_file == NULL) { + perror("failed to open 'audio.raw' for writing"); + return -1; + } + + long int size = file_size(input_file); + if (size < 0) { + perror("failed to get file size of 'audio.raw'"); + fclose(input_file); + return -1; + } + + *frames = malloc(size); + if (*frames == NULL) { + fprintf(stderr, "failed to allocate frames\n"); + fclose(input_file); + return -1; + } + + size = fread(*frames, 1, size, input_file); + + fclose(input_file); + + return size; +} + +static int write_frames(const void * frames, size_t byte_count){ + + unsigned int card = 0; + unsigned int device = 0; + int flags = PCM_OUT; + + const struct pcm_config config = { + .channels = 2, + .rate = 48000, + .format = PCM_FORMAT_S32_LE, + .period_size = 1024, + .period_count = 2, + .start_threshold = 1024, + .silence_threshold = 1024 * 2, + .stop_threshold = 1024 * 2 + }; + + struct pcm * pcm = pcm_open(card, device, flags, &config); + if (pcm == NULL) { + fprintf(stderr, "failed to allocate memory for PCM\n"); + return -1; + } else if (!pcm_is_ready(pcm)){ + pcm_close(pcm); + fprintf(stderr, "failed to open PCM\n"); + return -1; + } + + unsigned int frame_count = pcm_bytes_to_frames(pcm, byte_count); + + pcm_writei(pcm, frames, frame_count); + + pcm_close(pcm); + + return 0; +} + +int main(void) +{ + void *frames; + size_t size; + + size = read_file(&frames); + if (size == 0) { + return EXIT_FAILURE; + } + + if (write_frames(frames, size) < 0) { + return EXIT_FAILURE; + } + + free(frames); + return EXIT_SUCCESS; +} + -- cgit v1.2.3