comparison tools/mod2wav.c @ 2565:d56a0e86067a

Improve error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 28 Feb 2022 11:49:58 +0200
parents aacf3bd1cceb
children b205c60aa657
comparison
equal deleted inserted replaced
2564:2cf4e995b50c 2565:d56a0e86067a
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 res; 233 int res = DMERR_OK;
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 { 241 goto out;
242 res = 1;
243 goto exit;
244 }
245
246 242
247 // Check arguments 243 // Check arguments
248 if (optInFilename == NULL || optOutFilename == NULL) 244 if (optInFilename == NULL || optOutFilename == NULL)
249 { 245 {
250 res = dmError(DMERR_INVALID_ARGS, 246 res = dmError(DMERR_INVALID_ARGS,
251 "Input or output file not specified. Try --help.\n"); 247 "Input or output file not specified.\n");
252 goto exit; 248 goto out;
253 } 249 }
254 250
255 // Initialize miniJSS 251 // Initialize miniJSS
256 jssInit(); 252 jssInit();
257 253
258 // Open the source file 254 // Open the source file
259 if ((res = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) 255 if ((res = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK)
260 { 256 {
261 dmErrorMsg("Error opening input file '%s': %s\n", 257 dmErrorMsg("Error opening input file '%s': %s\n",
262 optInFilename, dmErrorStr(res)); 258 optInFilename, dmErrorStr(res));
263 goto exit; 259 goto out;
264 } 260 }
265 261
266 // Read module file 262 // Read module file
267 dmMsg(1, "Reading file: %s\n", optInFilename); 263 dmMsg(1, "Reading file: %s\n", optInFilename);
268 #ifdef JSS_SUP_XM 264 #ifdef JSS_SUP_XM
294 // Check for errors, we still might have some data tho 290 // Check for errors, we still might have some data tho
295 if (res != DMERR_OK) 291 if (res != DMERR_OK)
296 { 292 {
297 dmErrorMsg("Error loading module file: %s\n", 293 dmErrorMsg("Error loading module file: %s\n",
298 dmErrorStr(res)); 294 dmErrorStr(res));
299 goto exit; 295 goto out;
300 } 296 }
301 297
302 // Check if we have anything 298 // Check if we have anything
303 if (mod == NULL) 299 if (mod == NULL)
304 { 300 {
305 res = dmError(DMERR_INIT_FAIL, 301 res = dmError(DMERR_INIT_FAIL,
306 "Could not load module file.\n"); 302 "Could not load module file.\n");
307 goto exit; 303 goto out;
308 } 304 }
309 305
310 // Try to convert it 306 // Try to convert it
311 if ((res = jssConvertModuleForPlaying(mod)) != DMERR_OK) 307 if ((res = jssConvertModuleForPlaying(mod)) != DMERR_OK)
312 { 308 {
313 dmErrorMsg("Could not convert module for playing: %s\n", 309 dmErrorMsg("Could not convert module for playing: %s\n",
314 dmErrorStr(res)); 310 dmErrorStr(res));
315 goto exit; 311 goto out;
316 } 312 }
317 313
318 // Open mixer 314 // Open mixer
319 dev = jvmInit(optOutFormat, optOutChannels, optOutFreq, JMIX_AUTO); 315 dev = jvmInit(optOutFormat, optOutChannels, optOutFreq, JMIX_AUTO);
320 if (dev == NULL) 316 if (dev == NULL)
321 { 317 {
322 res = dmError(DMERR_INIT_FAIL, 318 res = dmError(DMERR_INIT_FAIL,
323 "jvmInit() returned NULL\n"); 319 "jvmInit() returned NULL\n");
324 goto exit; 320 goto out;
325 } 321 }
326 322
327 sampSize = jvmGetSampleSize(dev); 323 sampSize = jvmGetSampleSize(dev);
328 if ((dataBuf = dmMalloc(bufLen * sampSize)) == NULL) 324 if ((dataBuf = dmMalloc(bufLen * sampSize)) == NULL)
329 { 325 {
330 res = dmError(DMERR_MALLOC, 326 res = dmError(DMERR_MALLOC,
331 "Could not allocate mixing buffer.\n"); 327 "Could not allocate mixing buffer.\n");
332 goto exit; 328 goto out;
333 } 329 }
334 330
335 dmMsg(1, "Using fmt=%d, bits=%d, channels=%d, freq=%d [%" DM_PRIu_SIZE_T " / sample]\n", 331 dmMsg(1, "Using fmt=%d, bits=%d, channels=%d, freq=%d [%" DM_PRIu_SIZE_T " / sample]\n",
336 optOutFormat, jvmGetSampleRes(dev), optOutChannels, optOutFreq, 332 optOutFormat, jvmGetSampleRes(dev), optOutChannels, optOutFreq,
337 sampSize); 333 sampSize);
338 334
339 // Initialize player 335 // Initialize player
340 if ((plr = jmpInit(dev)) == NULL) 336 if ((plr = jmpInit(dev)) == NULL)
341 { 337 {
342 dmErrorMsg("jmpInit() returned NULL.\n"); 338 dmErrorMsg("jmpInit() returned NULL.\n");
343 goto exit; 339 goto out;
344 } 340 }
345 341
346 // Set callback 342 // Set callback
347 jvmSetCallback(dev, jmpExec, plr); 343 jvmSetCallback(dev, jmpExec, plr);
348 344
373 if ((outFile = fopen(optOutFilename, "wb")) == NULL) 369 if ((outFile = fopen(optOutFilename, "wb")) == NULL)
374 { 370 {
375 res = dmGetErrno(); 371 res = dmGetErrno();
376 dmErrorMsg("Error opening output file '%s': %s.\n", 372 dmErrorMsg("Error opening output file '%s': %s.\n",
377 optInFilename, dmErrorStr(res)); 373 optInFilename, dmErrorStr(res));
378 goto exit; 374 goto out;
379 } 375 }
380 376
381 // Write initial header 377 // Write initial header
382 dmWriteWAVHeader(outFile, jvmGetSampleRes(dev), optOutFreq, optOutChannels, 1024); 378 dmWriteWAVHeader(outFile, jvmGetSampleRes(dev), optOutFreq, optOutChannels, 1024);
383 379
405 dataWritten = fwrite(dataBuf, sampSize, writeLen, outFile); 401 dataWritten = fwrite(dataBuf, sampSize, writeLen, outFile);
406 if (dataWritten < writeLen) 402 if (dataWritten < writeLen)
407 { 403 {
408 res = dmError(DMERR_FWRITE, 404 res = dmError(DMERR_FWRITE,
409 "Error writing audio data!\n"); 405 "Error writing audio data!\n");
410 goto exit; 406 goto out;
411 } 407 }
412 dataTotal += dataWritten; 408 dataTotal += dataWritten;
413 } 409 }
414 410
415 if (optUsePlayTime && dataTotal >= optPlayTime) 411 if (optUsePlayTime && dataTotal >= optPlayTime)
419 // Write the correct header 415 // Write the correct header
420 if (fseek(outFile, 0L, SEEK_SET) != 0) 416 if (fseek(outFile, 0L, SEEK_SET) != 0)
421 { 417 {
422 res = dmError(DMERR_FSEEK, 418 res = dmError(DMERR_FSEEK,
423 "Error rewinding to header position!\n"); 419 "Error rewinding to header position!\n");
424 goto exit; 420 goto out;
425 } 421 }
426 422
427 dmWriteWAVHeader(outFile, jvmGetSampleRes(dev), optOutFreq, optOutChannels, dataTotal); 423 dmWriteWAVHeader(outFile, jvmGetSampleRes(dev), optOutFreq, optOutChannels, dataTotal);
428 424
429 // Done! 425 // Done!
430 dmMsg(1, "OK.\n"); 426 dmMsg(1, "OK.\n");
431 427
432 exit: 428 out:
433 if (outFile != NULL) 429 if (outFile != NULL)
434 fclose(outFile); 430 fclose(outFile);
435 431
436 dmFree(dataBuf); 432 dmFree(dataBuf);
437 jmpClose(plr); 433 jmpClose(plr);