changeset 10:5cf7818a7d08

pcm: implement play command
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 23 Jun 2024 19:45:52 +0000
parents e3d16d490ce2
children e149ca1dd14f
files pcm/globals.h pcm/pcm_tx.c pcm/user_cmd.c
diffstat 3 files changed, 54 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/pcm/globals.h	Sun Jun 23 19:34:08 2024 +0000
+++ b/pcm/globals.h	Sun Jun 23 19:45:52 2024 +0000
@@ -17,3 +17,4 @@
 void cmd_pcm_fill(int argc, char **argv);
 void cmd_dmw_on(int argc, char **argv);
 void cmd_dmw_off(int argc, char **argv);
+void cmd_play_file(int argc, char **argv);
--- a/pcm/pcm_tx.c	Sun Jun 23 19:34:08 2024 +0000
+++ b/pcm/pcm_tx.c	Sun Jun 23 19:45:52 2024 +0000
@@ -2,6 +2,9 @@
  * In this module we implement PCM Tx toward the TRAU.
  */
 
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/stat.h>
 #include <ctype.h>
 #include <stdint.h>
 #include <stdbool.h>
@@ -39,7 +42,7 @@
 	if (play_buf_ptr < play_buf_nframes)
 		return;
 	free(play_buffer);
-	play_buffer = 0;
+	play_buffer = NULL;
 	printf("file play finished\n");
 }
 
@@ -86,3 +89,51 @@
 {
 	dmw_active = false;
 }
+
+void cmd_play_file(int argc, char **argv)
+{
+	int fd;
+	struct stat st;
+
+	if (argc != 2) {
+		printf("error: play command needs 1 argument\n");
+		return;
+	}
+	if (play_buffer) {
+		printf("error: file play already in progress\n");
+		return;
+	}
+	fd = open(argv[1], O_RDONLY);
+	if (fd < 0) {
+		perror(argv[1]);
+		return;
+	}
+	fstat(fd, &st);
+	if (!S_ISREG(st.st_mode)) {
+		close(fd);
+		fprintf(stderr, "error: %s is not a regular file\n", argv[1]);
+		return;
+	}
+	if (!st.st_size) {
+		close(fd);
+		fprintf(stderr, "error: %s is an empty file\n", argv[1]);
+		return;
+	}
+	if (st.st_size % 160) {
+		close(fd);
+		fprintf(stderr,
+			"error: size of %s is not a multiple of 160 bytes\n",
+			argv[1]);
+		return;
+	}
+	play_buffer = malloc(st.st_size);
+	if (!play_buffer) {
+		close(fd);
+		fprintf(stderr, "unable to malloc buffer for %s\n", argv[1]);
+		return;
+	}
+	read(fd, play_buffer, st.st_size);
+	close(fd);
+	play_buf_nframes = st.st_size / 160;
+	play_buf_ptr = 0;
+}
--- a/pcm/user_cmd.c	Sun Jun 23 19:34:08 2024 +0000
+++ b/pcm/user_cmd.c	Sun Jun 23 19:45:52 2024 +0000
@@ -20,6 +20,7 @@
 	{"dmw-on", cmd_dmw_on},
 	{"dmw-off", cmd_dmw_off},
 	{"pcm-fill", cmd_pcm_fill},
+	{"play", cmd_play_file},
 	{"print-rx", cmd_print_rx},
 	{"record", cmd_record_start},
 	{"record-stop", cmd_record_stop},