Mercurial > pmdwin
comparison pmd_play.c @ 4:c8875256b767
Use stdio for console output, update usage, and use sensible exit values.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 21 May 2013 12:10:27 +0200 |
parents | c55ea9478c80 |
children | c4218fbe158f |
comparison
equal
deleted
inserted
replaced
3:8ad174416431 | 4:c8875256b767 |
---|---|
1 #include <stdio.h> | |
1 #include <stdint.h> | 2 #include <stdint.h> |
2 #include <string.h> | 3 #include <string.h> |
3 #include <fcntl.h> | 4 #include <fcntl.h> |
4 #include <unistd.h> | 5 #include <unistd.h> |
5 #include <sys/stat.h> | 6 #include <sys/stat.h> |
112 result = music_load3(musbuf, size); | 113 result = music_load3(musbuf, size); |
113 return result; | 114 return result; |
114 } | 115 } |
115 #endif | 116 #endif |
116 | 117 |
117 #if defined(WIN32) || defined(WIN64) | 118 static void usage(char *argv0) { |
118 #define WriteToConsole(str, len) WriteFile(errh, (str), (len), &done, NULL) | |
119 #else | |
120 #define WriteToConsole(str, len) write(2, (str), (len)) | |
121 #endif | |
122 | |
123 static void usage(void) { | |
124 #if defined(WIN32) || defined(WIN64) | |
125 DWORD done; | |
126 HANDLE errh = GetStdHandle(STD_ERROR_HANDLE); | |
127 #endif | |
128 const char *usage_str = | 119 const char *usage_str = |
129 "pmdwin -f <output file> -l <loop count> -m <device mask> -c <channel mask> -t <time> [PMD File]\n\ | 120 "%s [-f <output file>] [-l <loop count>] [-m <device mask>] [-c <channel mask>] [-t <time>] [-r <samplerate>] <PMD File>\n\ |
130 -f - Write output to wavfile\n\ | 121 -f - Write output to wavfile\n\ |
131 -l - Loop n times (default is once - use 0 for infinite loop)\n\ | 122 -l - Loop n times (default is once - use 0 for infinite loop)\n\ |
132 -m - Device mask: 1 - OPNA, 2 - PSG, 4 - Rhythm (or them together)\n\ | 123 -m - Device mask: 1 - OPNA, 2 - PSG, 4 - Rhythm (or them together)\n\ |
133 -c - Channel mask: 1,2,4,8,16,32 are OPNA channels 1-6, 64,128,256 are PSG channels 1-3. Once again, or them together\n\ | 124 -c - Channel mask: 1,2,4,8,16,32 are OPNA channels 1-6, 64,128,256 are PSG channels 1-3. Once again, or them together\n\ |
134 -t - Play song for n seconds\n\n"; | 125 -t - Play song for n seconds\n\ |
135 WriteToConsole(usage_str, strlen(usage_str)); | 126 -r - Set the output sample rate\n\n"; |
127 fprintf(stderr, usage_str, argv0); | |
136 } | 128 } |
137 | 129 |
138 int main(int argc, char **argv) { | 130 int main(int argc, char **argv) { |
139 uint32_t i, frameno, samplerate_out = SOUND_44K, infloop = 0; | 131 uint32_t i, frameno, samplerate_out = SOUND_44K, infloop = 0; |
140 uint32_t pmd_length = 0, pmd_loopcount = 2; | 132 uint32_t pmd_length = 0, pmd_loopcount = 2; |
142 uint8_t tofile = 0; | 134 uint8_t tofile = 0; |
143 int16_t pcmbuf[8192]; | 135 int16_t pcmbuf[8192]; |
144 char pmd_title[1024]; | 136 char pmd_title[1024]; |
145 char pmd_compo[1024]; | 137 char pmd_compo[1024]; |
146 #if defined(WIN32) || defined(WIN64) | 138 #if defined(WIN32) || defined(WIN64) |
147 DWORD done; | |
148 HANDLE errh = GetStdHandle(STD_ERROR_HANDLE); | |
149 HWAVEOUT hWaveOut; | 139 HWAVEOUT hWaveOut; |
150 #endif | 140 #endif |
151 char buf[1024]; | 141 char buf[1024]; |
152 pmdwininit(); | 142 pmdwininit(); |
153 | 143 |
172 break; | 162 break; |
173 case 'r': | 163 case 'r': |
174 samplerate_out = atoui((uint8_t*)optarg); | 164 samplerate_out = atoui((uint8_t*)optarg); |
175 break; | 165 break; |
176 default: | 166 default: |
177 usage(); | 167 usage(argv[0]); |
178 break; | 168 return 1; |
179 } | 169 } |
180 } | 170 } |
181 | 171 |
182 if(!tofile) { | 172 if(!tofile) { |
183 #if defined(WIN32) || defined(WIN64) | 173 #if defined(WIN32) || defined(WIN64) |
187 if((out_fd = alsa_audio_open(&samplerate_out, 1, NULL, 0)) < 0) { | 177 if((out_fd = alsa_audio_open(&samplerate_out, 1, NULL, 0)) < 0) { |
188 #else | 178 #else |
189 if((out_fd = oss_audio_open(samplerate_out, 1)) < 0) { | 179 if((out_fd = oss_audio_open(samplerate_out, 1)) < 0) { |
190 #endif | 180 #endif |
191 #endif | 181 #endif |
192 WriteToConsole("Cannot open sound device, exiting.\n", 35); | 182 fprintf(stderr, "Cannot open sound device, exiting.\n"); |
193 return -1; | 183 return 3; |
194 } | 184 } |
195 } else { | 185 } else { |
196 write_wav_header(out_fd, samplerate_out); | 186 write_wav_header(out_fd, samplerate_out); |
197 } | 187 } |
198 music_load(argv[optind]); | 188 if (!argv[optind] || optind + 1 != argc) { |
199 memcpy(pmd_title, "Title = ", 8); | 189 usage(argv[0]); |
200 getmemo3(pmd_title+8, NULL, 0, 1); | 190 return 1; |
201 i = strlen(pmd_title); pmd_title[i++] = '\n'; | 191 } |
192 if (music_load(argv[optind]) == ERR_OPEN_MUSIC_FILE) { | |
193 fprintf(stderr, "Cannot open music file ā%sā, exiting.\n", argv[optind]); | |
194 return 2; | |
195 } | |
196 getmemo3(pmd_title, NULL, 0, 1); | |
202 #if defined(WIN32) || defined(WIN64) | 197 #if defined(WIN32) || defined(WIN64) |
203 SetConsoleOutputCP(65001); // UTF-8 | 198 SetConsoleOutputCP(65001); // UTF-8 |
204 #endif | 199 #endif |
205 WriteToConsole(pmd_title, i); | 200 printf("Title = %s\n", pmd_title); |
206 memcpy(pmd_compo, "Composer = ", 11); | 201 getmemo3(pmd_compo, NULL, 0, 2); |
207 getmemo3(pmd_compo+11, NULL, 0, 2); | 202 printf("Composer = %s\n", pmd_compo); |
208 i = strlen(pmd_compo); pmd_compo[i++] = '\n'; | |
209 WriteToConsole(pmd_compo, i); | |
210 setpcmrate(samplerate_out); | 203 setpcmrate(samplerate_out); |
211 music_start(); | 204 music_start(); |
212 i = 0; | 205 i = 0; |
213 if(pmd_length != 0) { | 206 if(pmd_length != 0) { |
214 frameno = lrintf(((float)pmd_length*(float)samplerate_out)/(4096.0f*1000.0f)); | 207 frameno = lrintf(((float)pmd_length*(float)samplerate_out)/(4096.0f*1000.0f)); |
215 } else { | 208 } else { |
216 frameno = 0; | 209 frameno = 0; |
217 } | 210 } |
218 buf[0] = '\r'; | |
219 while(getloopcount() < pmd_loopcount || infloop || i < frameno) { | 211 while(getloopcount() < pmd_loopcount || infloop || i < frameno) { |
220 int ret = getstatus(buf+1, 1022); | 212 getstatus(buf, 1023); |
221 WriteToConsole(buf, ret+1); | 213 printf("\r%s", buf); |
214 fflush(stdout); | |
222 getpcmdata(pcmbuf, 4096); | 215 getpcmdata(pcmbuf, 4096); |
223 #if defined(WIN32) || defined(WIN64) | 216 #if defined(WIN32) || defined(WIN64) |
224 if(!tofile) { | 217 if(!tofile) { |
225 writeAudio(hWaveOut, pcmbuf, 8192); | 218 writeAudio(hWaveOut, pcmbuf, 8192); |
226 } else | 219 } else |
234 } else | 227 } else |
235 #endif | 228 #endif |
236 write(out_fd, pcmbuf, 8192); | 229 write(out_fd, pcmbuf, 8192); |
237 i++; | 230 i++; |
238 } | 231 } |
239 WriteToConsole("\n", 1); | 232 fputc('\n', stdout); |
240 #if defined(WIN32) || defined(WIN64) | 233 #if defined(WIN32) || defined(WIN64) |
241 if(!tofile) { | 234 if(!tofile) { |
242 wave_out_close(hWaveOut); | 235 wave_out_close(hWaveOut); |
243 } else | 236 } else |
244 #endif | 237 #endif |