Mercurial > hg > dmlib
annotate tools/mod2wav.c @ 1199:a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 05 Mar 2015 16:22:11 +0200 |
parents | 985225a93aeb |
children | 5b8245e5f785 |
rev | line source |
---|---|
0 | 1 /* |
2 * mod2wav - Render XM/JSSMOD module to WAV waveform file | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
4 * (C) Copyright 2007 Tecnic Software productions (TNSP) | |
5 * | |
6 * Please read file 'COPYING' for information on license and distribution. | |
7 */ | |
8 #include <stdio.h> | |
9 #include <stdlib.h> | |
10 #include <errno.h> | |
11 #include "jss.h" | |
12 #include "jssmod.h" | |
13 #include "jssmix.h" | |
14 #include "jssplr.h" | |
15 #include "dmlib.h" | |
16 #include "dmargs.h" | |
206
9b6c0ed66960
Use the routines factored into dmwav module.
Matti Hamalainen <ccr@tnsp.org>
parents:
185
diff
changeset
|
17 #include "dmwav.h" |
285
245b15cd1919
Don't link libSDL uselessly to utilities that do not actually use it.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
18 #include "dmmutex.h" |
0 | 19 |
20 | |
300 | 21 char *optInFilename = NULL, *optOutFilename = NULL; |
0 | 22 int optOutFormat = JSS_AUDIO_S16, |
23 optOutChannels = 2, | |
24 optOutFreq = 44100, | |
25 optMuteOChannels = -1, | |
26 optStartOrder = -1; | |
27 BOOL optUsePlayTime = FALSE; | |
28 size_t optPlayTime; | |
29 | |
30 | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
31 static const DMOptArg optList[] = |
37 | 32 { |
33 { 0, '?', "help", "Show this help", OPT_NONE }, | |
34 { 2, 'v', "verbose", "Be more verbose", OPT_NONE }, | |
35 { 3, '1', "16bit", "16-bit output", OPT_NONE }, | |
36 { 4, '8', "8bit", "8-bit output", OPT_NONE }, | |
37 { 5, 'm', "mono", "Mono output", OPT_NONE }, | |
38 { 6, 's', "stereo", "Stereo output", OPT_NONE }, | |
39 { 7, 'f', "freq", "Output frequency", OPT_ARGREQ }, | |
40 { 8, 'M', "mute", "Mute other channels than #", OPT_ARGREQ }, | |
41 { 9, 'o', "order", "Start from order #", OPT_ARGREQ }, | |
42 { 10, 't', "time", "Play for # seconds", OPT_ARGREQ }, | |
0 | 43 // {10, 'l', "loop", "Loop for # times", OPT_ARGREQ }, |
44 }; | |
45 | |
46 const int optListN = sizeof(optList) / sizeof(optList[0]); | |
47 | |
48 | |
49 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | |
50 { | |
51 (void) optArg; | |
52 | |
37 | 53 switch (optN) |
54 { | |
55 case 0: | |
56 dmPrintBanner(stdout, dmProgName, | |
57 "[options] [sourcefile] [destfile]"); | |
58 | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
59 dmArgsPrintHelp(stdout, optList, optListN, 0); |
37 | 60 exit(0); |
61 break; | |
0 | 62 |
37 | 63 case 2: |
64 dmVerbosity++; | |
65 break; | |
66 | |
67 case 3: | |
68 optOutFormat = JSS_AUDIO_S16; | |
69 break; | |
0 | 70 |
37 | 71 case 4: |
72 optOutFormat = JSS_AUDIO_U8; | |
73 break; | |
0 | 74 |
37 | 75 case 5: |
76 optOutChannels = JSS_AUDIO_MONO; | |
77 break; | |
0 | 78 |
37 | 79 case 6: |
38
8b04b0b51edc
Oops, fix a stupid bug in -s option .. it was setting the wrong variable. :|
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
80 optOutChannels = JSS_AUDIO_STEREO; |
37 | 81 break; |
0 | 82 |
37 | 83 case 7: |
84 optOutFreq = atoi(optArg); | |
85 break; | |
0 | 86 |
37 | 87 case 8: |
88 optMuteOChannels = atoi(optArg); | |
89 break; | |
90 | |
91 case 9: | |
92 optStartOrder = atoi(optArg); | |
93 break; | |
0 | 94 |
37 | 95 case 10: |
96 optPlayTime = atoi(optArg); | |
97 optUsePlayTime = TRUE; | |
98 break; | |
0 | 99 |
37 | 100 default: |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
101 dmErrorMsg("Unknown argument '%s'.\n", currArg); |
37 | 102 return FALSE; |
0 | 103 } |
104 | |
105 return TRUE; | |
106 } | |
107 | |
108 | |
109 BOOL argHandleFile(char *currArg) | |
110 { | |
300 | 111 if (!optInFilename) |
112 optInFilename = currArg; | |
37 | 113 else |
300 | 114 if (!optOutFilename) |
115 optOutFilename = currArg; | |
37 | 116 else |
117 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
118 dmErrorMsg("Too many filename arguments (only source and dest needed) '%s'\n", currArg); |
0 | 119 return FALSE; |
120 } | |
121 | |
122 return TRUE; | |
123 } | |
124 | |
125 | |
126 int main(int argc, char *argv[]) | |
127 { | |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
128 DMResource *inFile = NULL; |
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
129 FILE *outFile = NULL; |
68 | 130 JSSModule *mod = NULL; |
131 JSSMixer *dev = NULL; | |
132 JSSPlayer *plr = NULL; | |
0 | 133 int result = -1; |
134 size_t bufLen = 1024*4, dataTotal, dataWritten, sampSize; | |
135 Uint8 *mb = NULL; | |
136 | |
137 dmInitProg("mod2wav", "XM/JSSMOD to WAV renderer", "0.2", NULL, NULL); | |
138 dmVerbosity = 1; | |
139 | |
140 // Parse arguments | |
141 if (!dmArgsProcess(argc, argv, optList, optListN, | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
797
diff
changeset
|
142 argHandleOpt, argHandleFile, OPTH_BAILOUT)) |
0 | 143 exit(1); |
144 | |
145 // Check arguments | |
300 | 146 if (optInFilename == NULL || optOutFilename == NULL) |
37 | 147 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
148 dmErrorMsg("Input or output file not specified. Try --help.\n"); |
0 | 149 return 1; |
150 } | |
151 | |
152 // Initialize miniJSS | |
153 jssInit(); | |
154 | |
155 // Open the source file | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
156 if ((result = dmf_create_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
157 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
158 dmErrorMsg("Error opening input file '%s', %d: %s\n", |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
159 optInFilename, result, dmErrorStr(result)); |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
160 return 1; |
0 | 161 } |
162 | |
163 // Read module file | |
1199
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
164 dmMsg(1, "Reading file: %s\n", optInFilename); |
0 | 165 #ifdef JSS_SUP_XM |
797
f066e9dccf29
Oops, fix some inverted booleans.
Matti Hamalainen <ccr@tnsp.org>
parents:
796
diff
changeset
|
166 result = jssLoadXM(inFile, &mod, TRUE); |
0 | 167 #endif |
168 #ifdef JSS_SUP_JSSMOD | |
1199
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
169 dmfreset(inFile); |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
170 if (result != DMERR_OK) |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
171 { |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
730
diff
changeset
|
172 dmMsg(1, "* Trying JSSMOD ...\n"); |
797
f066e9dccf29
Oops, fix some inverted booleans.
Matti Hamalainen <ccr@tnsp.org>
parents:
796
diff
changeset
|
173 result = jssLoadJSSMOD(inFile, &mod, TRUE); |
1199
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
174 dmfreset(inFile); |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
175 if (result == DMERR_OK) |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
176 result = jssLoadJSSMOD(inFile, &mod, FALSE); |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
177 } |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
178 else |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
179 { |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
180 dmMsg(2, "* Trying XM...\n"); |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
181 result = jssLoadXM(inFile, &mod, FALSE); |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
182 } |
0 | 183 #endif |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
184 dmf_close(inFile); |
1199
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
185 |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
186 // Check for errors, we still might have some data tho |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
187 if (result != DMERR_OK) |
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
188 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
189 dmErrorMsg("Error loading module file, %d: %s\n", |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
190 result, dmErrorStr(result)); |
1199
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
191 } |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
192 |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
193 // Check if we have anything |
a79edf59d5d8
Improve use of probing in dumpmod, mod2wav and ppl.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
194 if (mod == NULL) |
15
feec43a3497c
Fix input file reading of mod2wav utility.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
195 return 3; |
0 | 196 |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
197 // Try to convert it |
68 | 198 if ((result = jssConvertModuleForPlaying(mod)) != DMERR_OK) |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
199 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
200 dmErrorMsg("Could not convert module for playing, %d: %s\n", |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
201 result, dmErrorStr(result)); |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
202 return 3; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
203 } |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
204 |
0 | 205 // Open mixer |
68 | 206 dev = jvmInit(optOutFormat, optOutChannels, optOutFreq, JMIX_AUTO); |
207 if (dev == NULL) | |
39 | 208 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
209 dmErrorMsg("jvmInit() returned NULL\n"); |
0 | 210 return 4; |
211 } | |
212 | |
68 | 213 sampSize = jvmGetSampleSize(dev); |
83
6b42aed2745b
Cleanups and correct the dmf_create_stdio() issue here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
68
diff
changeset
|
214 if ((mb = dmMalloc(bufLen * sampSize)) == NULL) |
39 | 215 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
216 dmErrorMsg("Could not allocate mixing buffer\n"); |
0 | 217 return 5; |
218 } | |
219 | |
220 dmMsg(1, "Using fmt=%d, bits=%d, channels=%d, freq=%d [%d / sample]\n", | |
68 | 221 optOutFormat, jvmGetSampleRes(dev), optOutChannels, optOutFreq, |
0 | 222 sampSize); |
223 | |
224 // Initialize player | |
68 | 225 if ((plr = jmpInit(dev)) == NULL) |
226 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
227 dmErrorMsg("jmpInit() returned NULL.\n"); |
0 | 228 return 6; |
229 } | |
230 | |
231 // Set callback | |
68 | 232 jvmSetCallback(dev, jmpExec, plr); |
0 | 233 |
234 // Initialize playing | |
68 | 235 jmpSetModule(plr, mod); |
37 | 236 if (optStartOrder >= 0) |
237 { | |
0 | 238 dmMsg(1, "Starting from song order #%d\n", optStartOrder); |
239 } else | |
240 optStartOrder = 0; | |
37 | 241 |
68 | 242 jmpPlayOrder(plr, optStartOrder); |
185
1f8f4d7cb33b
Increase default global volume.
Matti Hamalainen <ccr@tnsp.org>
parents:
83
diff
changeset
|
243 jvmSetGlobalVol(dev, 150); |
0 | 244 |
68 | 245 if (optMuteOChannels > 0 && optMuteOChannels <= mod->nchannels) |
37 | 246 { |
0 | 247 int i; |
68 | 248 for (i = 0; i < mod->nchannels; i++) |
249 jvmMute(dev, i, TRUE); | |
250 jvmMute(dev, optMuteOChannels - 1, FALSE); | |
0 | 251 } |
252 | |
253 // Open output file | |
300 | 254 if ((outFile = fopen(optOutFilename, "wb")) == NULL) |
37 | 255 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
256 dmErrorMsg("Error opening output file '%s'. (%s)\n", |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
257 optInFilename, strerror(errno)); |
0 | 258 return 7; |
259 } | |
260 | |
261 // Write initial header | |
206
9b6c0ed66960
Use the routines factored into dmwav module.
Matti Hamalainen <ccr@tnsp.org>
parents:
185
diff
changeset
|
262 dmWriteWAVHeader(outFile, jvmGetSampleRes(dev), optOutFreq, optOutChannels, 1024); |
0 | 263 |
264 // Render audio data and output to file | |
265 if (optUsePlayTime) | |
266 dmMsg(1, "Rendering module (%d seconds) ...\n", optPlayTime); | |
267 else | |
268 dmMsg(1, "Rendering module ...\n"); | |
269 | |
270 optPlayTime *= optOutFreq; | |
271 dataTotal = 0; | |
272 dataWritten = 1; | |
68 | 273 while (plr->isPlaying && dataWritten > 0) |
0 | 274 { |
275 size_t writeLen = bufLen; | |
276 if (optUsePlayTime && (writeLen + dataTotal) > optPlayTime) | |
277 writeLen = optPlayTime - dataTotal; | |
278 | |
279 if (writeLen > 0) | |
280 { | |
68 | 281 jvmRenderAudio(dev, mb, writeLen); |
0 | 282 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN) |
283 jssEncodeSample16((Uint16 *)mb, writeLen * optOutChannels, jsampSwapEndianess); | |
284 #endif | |
285 dataWritten = fwrite(mb, sampSize, writeLen, outFile); | |
286 if (dataWritten < writeLen) | |
287 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
288 dmErrorMsg("Error writing data!\n"); |
0 | 289 fclose(outFile); |
290 return 8; | |
291 } | |
292 dataTotal += dataWritten; | |
293 } | |
294 | |
295 if (optUsePlayTime && dataTotal >= optPlayTime) | |
296 break; | |
297 } | |
298 | |
299 // Write the correct header | |
300 if (fseek(outFile, 0L, SEEK_SET) != 0) | |
301 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
302 dmErrorMsg("Error rewinding to header position!\n"); |
0 | 303 return 9; |
304 } | |
305 | |
206
9b6c0ed66960
Use the routines factored into dmwav module.
Matti Hamalainen <ccr@tnsp.org>
parents:
185
diff
changeset
|
306 dmWriteWAVHeader(outFile, jvmGetSampleRes(dev), optOutFreq, optOutChannels, dataTotal); |
0 | 307 |
308 // Done! | |
309 fclose(outFile); | |
68 | 310 |
311 jmpClose(plr); | |
312 jvmClose(dev); | |
313 jssFreeModule(mod); | |
314 jssClose(); | |
315 | |
0 | 316 dmMsg(1, "OK.\n"); |
317 return 0; | |
318 } |