comparison loadtools/buzplaypwt.c @ 893:85091e14be9c

fc-buzplay: PWT refactoring, first step
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 03 Apr 2022 08:00:50 +0000
parents 502aec4c1e8e
children
comparison
equal deleted inserted replaced
892:110749b39ea2 893:85091e14be9c
64 {"e8", 0}, /* 5274 Hz */ 64 {"e8", 0}, /* 5274 Hz */
65 /* table search terminator */ 65 /* table search terminator */
66 {0, -1} 66 {0, -1}
67 }; 67 };
68 68
69 cmd_playt(argc, argv) 69 buzplay_pwt_file(filename, global_vol)
70 char **argv; 70 char *filename;
71 unsigned global_vol;
71 { 72 {
72 FILE *f; 73 FILE *f;
73 char linebuf[256], *cp, *fields[3]; 74 char linebuf[256], *cp, *fields[3];
74 int lineno, nfields; 75 int lineno, nfields;
75 char *targv[5], argbuf1[16], argbuf2[16], argbuf3[16]; 76 char *targv[5], argbuf1[16], argbuf2[16], argbuf3[16];
76 struct pwt_note *tp; 77 struct pwt_note *tp;
77 u_long global_vol, note_vol, duration, total_ms; 78 unsigned note_vol, duration;
79 u_long total_ms;
78 int rc, timeout; 80 int rc, timeout;
79 81
80 f = fopen(argv[1], "r"); 82 f = fopen(filename, "r");
81 if (!f) { 83 if (!f) {
82 perror(argv[1]); 84 perror(filename);
83 return(-1); 85 return(-1);
84 } 86 }
85 if (argv[2]) {
86 global_vol = strtoul(argv[2], 0, 0);
87 if (global_vol < 1 || global_vol > 64) {
88 fprintf(stderr,
89 "error: invalid global volume argument\n");
90 fclose(f);
91 return(-1);
92 }
93 } else
94 global_vol = 64;
95 printf("Uploading the melody to the target\n"); 87 printf("Uploading the melody to the target\n");
96 targv[0] = "IT"; 88 targv[0] = "IT";
97 targv[1] = 0; 89 targv[1] = 0;
98 tpinterf_make_cmd(targv); 90 tpinterf_make_cmd(targv);
99 if (tpinterf_send_cmd() < 0) { 91 if (tpinterf_send_cmd() < 0) {
108 total_ms = 0; 100 total_ms = 0;
109 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) { 101 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
110 cp = index(linebuf, '\n'); 102 cp = index(linebuf, '\n');
111 if (!cp) { 103 if (!cp) {
112 fprintf(stderr, "%s line %d: missing newline\n", 104 fprintf(stderr, "%s line %d: missing newline\n",
113 argv[1], lineno); 105 filename, lineno);
114 fclose(f); 106 fclose(f);
115 return(-1); 107 return(-1);
116 } 108 }
117 cp = linebuf; 109 cp = linebuf;
118 nfields = 0; 110 nfields = 0;
121 cp++; 113 cp++;
122 if (*cp == '\0' || *cp == '#') 114 if (*cp == '\0' || *cp == '#')
123 break; 115 break;
124 if (nfields >= 3) { 116 if (nfields >= 3) {
125 fprintf(stderr, "%s line %d: too many fields\n", 117 fprintf(stderr, "%s line %d: too many fields\n",
126 argv[1], lineno); 118 filename, lineno);
127 fclose(f); 119 fclose(f);
128 return(-1); 120 return(-1);
129 } 121 }
130 fields[nfields++] = cp; 122 fields[nfields++] = cp;
131 while (*cp && !isspace(*cp)) 123 while (*cp && !isspace(*cp))
140 if (!strcmp(tp->name, fields[0])) 132 if (!strcmp(tp->name, fields[0]))
141 break; 133 break;
142 if (tp->code < 0) { 134 if (tp->code < 0) {
143 fprintf(stderr, 135 fprintf(stderr,
144 "%s line %d: invalid note name\n", 136 "%s line %d: invalid note name\n",
145 argv[1], lineno); 137 filename, lineno);
146 fclose(f); 138 fclose(f);
147 return(-1); 139 return(-1);
148 } 140 }
149 note_vol = strtoul(fields[1], 0, 0); 141 note_vol = strtoul(fields[1], 0, 0);
150 if (note_vol < 1 || note_vol > 64) { 142 if (note_vol < 1 || note_vol > 64) {
151 fprintf(stderr, 143 fprintf(stderr,
152 "%s line %d: invalid note volume\n", 144 "%s line %d: invalid note volume\n",
153 argv[1], lineno); 145 filename, lineno);
154 fclose(f); 146 fclose(f);
155 return(-1); 147 return(-1);
156 } 148 }
157 duration = strtoul(fields[2], 0, 0); 149 duration = strtoul(fields[2], 0, 0);
158 if (duration < 1 || duration > 0xFFFF) { 150 if (duration < 1 || duration > 0xFFFF) {
159 fprintf(stderr, 151 fprintf(stderr,
160 "%s line %d: the duration number is out of range\n", 152 "%s line %d: the duration number is out of range\n",
161 argv[1], lineno); 153 filename, lineno);
162 fclose(f); 154 fclose(f);
163 return(-1); 155 return(-1);
164 } 156 }
165 sprintf(argbuf1, "%u", (unsigned) tp->code); 157 sprintf(argbuf1, "%u", (unsigned) tp->code);
166 sprintf(argbuf2, "%u", 158 sprintf(argbuf2, "%u",
174 } else if (nfields == 2 && !strcmp(fields[0], "rest")) { 166 } else if (nfields == 2 && !strcmp(fields[0], "rest")) {
175 duration = strtoul(fields[1], 0, 0); 167 duration = strtoul(fields[1], 0, 0);
176 if (duration < 1 || duration > 0xFFFF) { 168 if (duration < 1 || duration > 0xFFFF) {
177 fprintf(stderr, 169 fprintf(stderr,
178 "%s line %d: the duration number is out of range\n", 170 "%s line %d: the duration number is out of range\n",
179 argv[1], lineno); 171 filename, lineno);
180 fclose(f); 172 fclose(f);
181 return(-1); 173 return(-1);
182 } 174 }
183 sprintf(argbuf1, "%u", duration); 175 sprintf(argbuf1, "%u", duration);
184 targv[0] = "EP"; 176 targv[0] = "EP";
185 targv[1] = argbuf1; 177 targv[1] = argbuf1;
186 targv[2] = 0; 178 targv[2] = 0;
187 } else { 179 } else {
188 fprintf(stderr, "%s line %d: invalid syntax\n", 180 fprintf(stderr, "%s line %d: invalid syntax\n",
189 argv[1], lineno); 181 filename, lineno);
190 fclose(f); 182 fclose(f);
191 return(-1); 183 return(-1);
192 } 184 }
193 /* send it to the target */ 185 /* send it to the target */
194 tpinterf_make_cmd(targv); 186 tpinterf_make_cmd(targv);
204 /* account for the duration */ 196 /* account for the duration */
205 total_ms += duration * 5; 197 total_ms += duration * 5;
206 } 198 }
207 fclose(f); 199 fclose(f);
208 if (!total_ms) { 200 if (!total_ms) {
209 fprintf(stderr, "%s is empty!\n", argv[1]); 201 fprintf(stderr, "%s is empty!\n", filename);
210 return(-1); 202 return(-1);
211 } 203 }
212 printf("Requesting play of the uploaded melody on the target\n"); 204 printf("Requesting play of the uploaded melody on the target\n");
213 targv[0] = "P"; 205 targv[0] = "P";
214 targv[1] = 0; 206 targv[1] = 0;
216 if (tpinterf_send_cmd() < 0) 208 if (tpinterf_send_cmd() < 0)
217 return(-1); 209 return(-1);
218 timeout = total_ms / 1000 + 2; 210 timeout = total_ms / 1000 + 2;
219 return tpinterf_pass_output(timeout); 211 return tpinterf_pass_output(timeout);
220 } 212 }
213
214 cmd_play_pwt(argc, argv)
215 char **argv;
216 {
217 unsigned global_vol;
218
219 if (argv[2]) {
220 global_vol = strtoul(argv[2], 0, 0);
221 if (global_vol < 1 || global_vol > 64) {
222 fprintf(stderr,
223 "error: invalid global volume argument\n");
224 return(-1);
225 }
226 } else
227 global_vol = 64;
228 return buzplay_pwt_file(argv[1], global_vol);
229 }