comparison tools/mod2wav.c @ 2530:aacf3bd1cceb

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 16 May 2020 06:38:52 +0300
parents bc05bcfc4598
children d56a0e86067a
comparison
equal deleted inserted replaced
2529:fddee4b6a427 2530:aacf3bd1cceb
228 JSSModule *mod = NULL; 228 JSSModule *mod = NULL;
229 JSSMixer *dev = NULL; 229 JSSMixer *dev = NULL;
230 JSSPlayer *plr = NULL; 230 JSSPlayer *plr = NULL;
231 size_t bufLen = 1024*4, dataTotal, dataWritten, sampSize; 231 size_t bufLen = 1024*4, dataTotal, dataWritten, sampSize;
232 Uint8 *dataBuf = NULL; 232 Uint8 *dataBuf = NULL;
233 int result = -1; 233 int res;
234 234
235 dmInitProg("mod2wav", "XM/JSSMOD to WAV renderer", "0.2", NULL, NULL); 235 dmInitProg("mod2wav", "XM/JSSMOD to WAV renderer", "0.2", NULL, NULL);
236 dmVerbosity = 1; 236 dmVerbosity = 1;
237 237
238 // Parse arguments 238 // Parse arguments
239 if (!dmArgsProcess(argc, argv, optList, optListN, 239 if (!dmArgsProcess(argc, argv, optList, optListN,
240 argHandleOpt, argHandleFile, OPTH_BAILOUT)) 240 argHandleOpt, argHandleFile, OPTH_BAILOUT))
241 return 1; 241 {
242 res = 1;
243 goto exit;
244 }
245
242 246
243 // Check arguments 247 // Check arguments
244 if (optInFilename == NULL || optOutFilename == NULL) 248 if (optInFilename == NULL || optOutFilename == NULL)
245 { 249 {
246 dmErrorMsg("Input or output file not specified. Try --help.\n"); 250 res = dmError(DMERR_INVALID_ARGS,
247 return 1; 251 "Input or output file not specified. Try --help.\n");
252 goto exit;
248 } 253 }
249 254
250 // Initialize miniJSS 255 // Initialize miniJSS
251 jssInit(); 256 jssInit();
252 257
253 // Open the source file 258 // Open the source file
254 if ((result = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) 259 if ((res = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK)
255 { 260 {
256 dmErrorMsg("Error opening input file '%s', %d: %s\n", 261 dmErrorMsg("Error opening input file '%s': %s\n",
257 optInFilename, result, dmErrorStr(result)); 262 optInFilename, dmErrorStr(res));
258 goto exit; 263 goto exit;
259 } 264 }
260 265
261 // Read module file 266 // Read module file
262 dmMsg(1, "Reading file: %s\n", optInFilename); 267 dmMsg(1, "Reading file: %s\n", optInFilename);
263 #ifdef JSS_SUP_XM 268 #ifdef JSS_SUP_XM
264 result = jssLoadXM(inFile, &mod, TRUE); 269 if (mod == NULL)
270 {
271 dmMsg(2, "* Trying XM...\n");
272 dmfreset(inFile);
273 if ((res = jssLoadXM(inFile, &mod, TRUE)) == DMERR_OK)
274 {
275 dmfreset(inFile);
276 res = jssLoadXM(inFile, &mod, FALSE);
277 }
278 }
265 #endif 279 #endif
266 #ifdef JSS_SUP_JSSMOD 280 #ifdef JSS_SUP_JSSMOD
267 dmfreset(inFile); 281 if (mod == NULL)
268 if (result != DMERR_OK)
269 { 282 {
270 dmMsg(1, "* Trying JSSMOD ...\n"); 283 dmMsg(1, "* Trying JSSMOD ...\n");
271 result = jssLoadJSSMOD(inFile, &mod, TRUE);
272 dmfreset(inFile); 284 dmfreset(inFile);
273 if (result == DMERR_OK) 285 if ((res = jssLoadJSSMOD(inFile, &mod, TRUE)) == DMERR_OK)
274 result = jssLoadJSSMOD(inFile, &mod, FALSE); 286 {
275 } 287 dmfreset(inFile);
276 else 288 res = jssLoadJSSMOD(inFile, &mod, FALSE);
277 { 289 }
278 dmMsg(2, "* Trying XM...\n");
279 result = jssLoadXM(inFile, &mod, FALSE);
280 } 290 }
281 #endif 291 #endif
282 dmf_close(inFile); 292 dmf_close(inFile);
283 293
284 // Check for errors, we still might have some data tho 294 // Check for errors, we still might have some data tho
285 if (result != DMERR_OK) 295 if (res != DMERR_OK)
286 { 296 {
287 dmErrorMsg("Error loading module file, %d: %s\n", 297 dmErrorMsg("Error loading module file: %s\n",
288 result, dmErrorStr(result)); 298 dmErrorStr(res));
289 goto exit; 299 goto exit;
290 } 300 }
291 301
292 // Check if we have anything 302 // Check if we have anything
293 if (mod == NULL) 303 if (mod == NULL)
294 goto exit; 304 {
305 res = dmError(DMERR_INIT_FAIL,
306 "Could not load module file.\n");
307 goto exit;
308 }
295 309
296 // Try to convert it 310 // Try to convert it
297 if ((result = jssConvertModuleForPlaying(mod)) != DMERR_OK) 311 if ((res = jssConvertModuleForPlaying(mod)) != DMERR_OK)
298 { 312 {
299 dmErrorMsg("Could not convert module for playing, %d: %s\n", 313 dmErrorMsg("Could not convert module for playing: %s\n",
300 result, dmErrorStr(result)); 314 dmErrorStr(res));
301 goto exit; 315 goto exit;
302 } 316 }
303 317
304 // Open mixer 318 // Open mixer
305 dev = jvmInit(optOutFormat, optOutChannels, optOutFreq, JMIX_AUTO); 319 dev = jvmInit(optOutFormat, optOutChannels, optOutFreq, JMIX_AUTO);
306 if (dev == NULL) 320 if (dev == NULL)
307 { 321 {
308 dmErrorMsg("jvmInit() returned NULL\n"); 322 res = dmError(DMERR_INIT_FAIL,
323 "jvmInit() returned NULL\n");
309 goto exit; 324 goto exit;
310 } 325 }
311 326
312 sampSize = jvmGetSampleSize(dev); 327 sampSize = jvmGetSampleSize(dev);
313 if ((dataBuf = dmMalloc(bufLen * sampSize)) == NULL) 328 if ((dataBuf = dmMalloc(bufLen * sampSize)) == NULL)
314 { 329 {
315 dmErrorMsg("Could not allocate mixing buffer\n"); 330 res = dmError(DMERR_MALLOC,
316 return 5; 331 "Could not allocate mixing buffer.\n");
332 goto exit;
317 } 333 }
318 334
319 dmMsg(1, "Using fmt=%d, bits=%d, channels=%d, freq=%d [%" DM_PRIu_SIZE_T " / sample]\n", 335 dmMsg(1, "Using fmt=%d, bits=%d, channels=%d, freq=%d [%" DM_PRIu_SIZE_T " / sample]\n",
320 optOutFormat, jvmGetSampleRes(dev), optOutChannels, optOutFreq, 336 optOutFormat, jvmGetSampleRes(dev), optOutChannels, optOutFreq,
321 sampSize); 337 sampSize);
332 348
333 // Initialize playing 349 // Initialize playing
334 jmpSetModule(plr, mod); 350 jmpSetModule(plr, mod);
335 if (optStartOrder >= 0) 351 if (optStartOrder >= 0)
336 { 352 {
337 dmMsg(1, "Starting from song order #%d\n", optStartOrder); 353 dmMsg(1, "Starting from song order #%d\n",
338 } else 354 optStartOrder);
355 }
356 else
357 {
339 optStartOrder = 0; 358 optStartOrder = 0;
359 }
340 360
341 jmpPlayOrder(plr, optStartOrder); 361 jmpPlayOrder(plr, optStartOrder);
342 jvmSetGlobalVol(dev, 150); 362 jvmSetGlobalVol(dev, 150);
343 363
344 if (optMuteOChannels > 0 && optMuteOChannels <= mod->nchannels) 364 if (optMuteOChannels > 0 && optMuteOChannels <= mod->nchannels)
345 { 365 {
346 int i; 366 for (int i = 0; i < mod->nchannels; i++)
347 for (i = 0; i < mod->nchannels; i++)
348 jvmMute(dev, i, TRUE); 367 jvmMute(dev, i, TRUE);
368
349 jvmMute(dev, optMuteOChannels - 1, FALSE); 369 jvmMute(dev, optMuteOChannels - 1, FALSE);
350 } 370 }
351 371
352 // Open output file 372 // Open output file
353 if ((outFile = fopen(optOutFilename, "wb")) == NULL) 373 if ((outFile = fopen(optOutFilename, "wb")) == NULL)
354 { 374 {
355 int err = dmGetErrno(); 375 res = dmGetErrno();
356 dmErrorMsg("Error opening output file '%s' #%d: %s.\n", 376 dmErrorMsg("Error opening output file '%s': %s.\n",
357 optInFilename, err, dmErrorStr(err)); 377 optInFilename, dmErrorStr(res));
358 goto exit; 378 goto exit;
359 } 379 }
360 380
361 // Write initial header 381 // Write initial header
362 dmWriteWAVHeader(outFile, jvmGetSampleRes(dev), optOutFreq, optOutChannels, 1024); 382 dmWriteWAVHeader(outFile, jvmGetSampleRes(dev), optOutFreq, optOutChannels, 1024);
378 398
379 if (writeLen > 0) 399 if (writeLen > 0)
380 { 400 {
381 jvmRenderAudio(dev, dataBuf, writeLen); 401 jvmRenderAudio(dev, dataBuf, writeLen);
382 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN) 402 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
383 jssEncodeSample16((Uint16 *)dataBuf, writeLen * optOutChannels, jsampSwapEndianess); 403 jssEncodeSample16((Uint16 *) dataBuf, writeLen * optOutChannels, jsampSwapEndianess);
384 #endif 404 #endif
385 dataWritten = fwrite(dataBuf, sampSize, writeLen, outFile); 405 dataWritten = fwrite(dataBuf, sampSize, writeLen, outFile);
386 if (dataWritten < writeLen) 406 if (dataWritten < writeLen)
387 { 407 {
388 dmErrorMsg("Error writing data!\n"); 408 res = dmError(DMERR_FWRITE,
409 "Error writing audio data!\n");
389 goto exit; 410 goto exit;
390 } 411 }
391 dataTotal += dataWritten; 412 dataTotal += dataWritten;
392 } 413 }
393 414
396 } 417 }
397 418
398 // Write the correct header 419 // Write the correct header
399 if (fseek(outFile, 0L, SEEK_SET) != 0) 420 if (fseek(outFile, 0L, SEEK_SET) != 0)
400 { 421 {
401 dmErrorMsg("Error rewinding to header position!\n"); 422 res = dmError(DMERR_FSEEK,
423 "Error rewinding to header position!\n");
402 goto exit; 424 goto exit;
403 } 425 }
404 426
405 dmWriteWAVHeader(outFile, jvmGetSampleRes(dev), optOutFreq, optOutChannels, dataTotal); 427 dmWriteWAVHeader(outFile, jvmGetSampleRes(dev), optOutFreq, optOutChannels, dataTotal);
406 428
415 jmpClose(plr); 437 jmpClose(plr);
416 jvmClose(dev); 438 jvmClose(dev);
417 jssFreeModule(mod); 439 jssFreeModule(mod);
418 jssClose(); 440 jssClose();
419 441
420 return 0; 442 return res;
421 } 443 }