comparison pcm/pcm_tx.c @ 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
comparison
equal deleted inserted replaced
9:e3d16d490ce2 10:5cf7818a7d08
1 /* 1 /*
2 * In this module we implement PCM Tx toward the TRAU. 2 * In this module we implement PCM Tx toward the TRAU.
3 */ 3 */
4 4
5 #include <sys/types.h>
6 #include <sys/file.h>
7 #include <sys/stat.h>
5 #include <ctype.h> 8 #include <ctype.h>
6 #include <stdint.h> 9 #include <stdint.h>
7 #include <stdbool.h> 10 #include <stdbool.h>
8 #include <stdio.h> 11 #include <stdio.h>
9 #include <stdlib.h> 12 #include <stdlib.h>
37 memcpy(outbuf, play_buffer + play_buf_ptr * 160, 160); 40 memcpy(outbuf, play_buffer + play_buf_ptr * 160, 160);
38 play_buf_ptr++; 41 play_buf_ptr++;
39 if (play_buf_ptr < play_buf_nframes) 42 if (play_buf_ptr < play_buf_nframes)
40 return; 43 return;
41 free(play_buffer); 44 free(play_buffer);
42 play_buffer = 0; 45 play_buffer = NULL;
43 printf("file play finished\n"); 46 printf("file play finished\n");
44 } 47 }
45 48
46 void transmit_pcm_20ms(void) 49 void transmit_pcm_20ms(void)
47 { 50 {
84 87
85 void cmd_dmw_off(int argc, char **argv) 88 void cmd_dmw_off(int argc, char **argv)
86 { 89 {
87 dmw_active = false; 90 dmw_active = false;
88 } 91 }
92
93 void cmd_play_file(int argc, char **argv)
94 {
95 int fd;
96 struct stat st;
97
98 if (argc != 2) {
99 printf("error: play command needs 1 argument\n");
100 return;
101 }
102 if (play_buffer) {
103 printf("error: file play already in progress\n");
104 return;
105 }
106 fd = open(argv[1], O_RDONLY);
107 if (fd < 0) {
108 perror(argv[1]);
109 return;
110 }
111 fstat(fd, &st);
112 if (!S_ISREG(st.st_mode)) {
113 close(fd);
114 fprintf(stderr, "error: %s is not a regular file\n", argv[1]);
115 return;
116 }
117 if (!st.st_size) {
118 close(fd);
119 fprintf(stderr, "error: %s is an empty file\n", argv[1]);
120 return;
121 }
122 if (st.st_size % 160) {
123 close(fd);
124 fprintf(stderr,
125 "error: size of %s is not a multiple of 160 bytes\n",
126 argv[1]);
127 return;
128 }
129 play_buffer = malloc(st.st_size);
130 if (!play_buffer) {
131 close(fd);
132 fprintf(stderr, "unable to malloc buffer for %s\n", argv[1]);
133 return;
134 }
135 read(fd, play_buffer, st.st_size);
136 close(fd);
137 play_buf_nframes = st.st_size / 160;
138 play_buf_ptr = 0;
139 }