stereorize.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /****************************************************************************
  2. *
  3. * Programs for processing sound files in raw- or WAV-format.
  4. * -- Merge two mono WAV-files to one stereo WAV-file.
  5. *
  6. * Name: stereorize.c
  7. * Version: 1.1
  8. * Author: Mark Roberts <mark@manumark.de>
  9. * Michael Labuschke <michael@labuschke.de>
  10. *
  11. ****************************************************************************/
  12. /*** MODULEINFO
  13. <support_level>extended</support_level>
  14. ***/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <time.h>
  20. #include <assert.h>
  21. #include "frame.h"
  22. static char *Version = "stereorize 1.1, November 5th 2000";
  23. static char *Usage =
  24. "Usage: stereorize [options] infile-left infile-right outfile\n\n"
  25. "Example:\n"
  26. " stereorize left.wav right.wav stereo.wav -h\n\n"
  27. "Creates stereo.wav (with WAV-header, option -h) from data in mono files\n"
  28. "left.wav and right.wav.\n"
  29. ;
  30. int main( int argcount, char *args[])
  31. {
  32. int i, k[2], maxk, stdin_in_use=FALSE;
  33. short *leftsample, *rightsample, *stereosample;
  34. FILE *channel[2];
  35. char *filename[2], *tempname;
  36. version = Version;
  37. usage = Usage;
  38. channel[0] = NULL;
  39. channel[1] = NULL;
  40. parseargs( argcount, args, NOFILES | NOCOMPLAIN);
  41. for (i = 0; i < 2; i++)
  42. {
  43. filename[i] = parsefilearg( argcount, args);
  44. if (filename[i] == NULL)
  45. argerrornum( NULL, ME_NOTENOUGHFILES);
  46. if (strcmp (filename[i], "-") == 0)
  47. {
  48. if (stdin_in_use)
  49. argerrortxt( filename[i] + 1,
  50. "Cannot use <stdin> for both input files");
  51. filename[i] = "<stdin>";
  52. channel[i] = stdin;
  53. stdin_in_use = TRUE;
  54. }
  55. else
  56. {
  57. channel[i] = fopen(filename[i], "rb");
  58. }
  59. if (channel[i] == NULL)
  60. fatalerror( "Error opening input file '%s': %s\n", filename[i],strerror(errno));
  61. else
  62. inform("Using file '%s' as input\n", filename[i]);
  63. }
  64. for (i = 0; i < 2; i++)
  65. {
  66. assert ( channel[i] != NULL);
  67. readwavheader( channel[i]);
  68. if (iswav && channels != 1)
  69. inform("Warning: '%s' is no mono file\n", filename[i]);
  70. }
  71. outfilename = parsefilearg( argcount, args);
  72. if (outfilename == NULL) argerrornum( NULL, ME_NOOUTFILE);
  73. if (strcmp (outfilename, "-") == 0)
  74. {
  75. outfilename = "<stdout>";
  76. out = stdout;
  77. }
  78. else
  79. {
  80. out = fopen(outfilename, "wb");
  81. }
  82. if (out == NULL)
  83. fatalerror( "Error opening output file '%s': %s\n", outfilename,strerror(errno));
  84. else
  85. inform("Using file '%s' as output\n", outfilename);
  86. if ((tempname = parsefilearg( argcount, args)) != NULL)
  87. argerrornum( tempname, ME_TOOMANYFILES);
  88. checknoargs(argcount, args); /* Check that no arguments are left */
  89. leftsample = malloc( sizeof(*leftsample) * BUFFSIZE);
  90. rightsample = malloc( sizeof(*leftsample) * BUFFSIZE);
  91. stereosample = malloc( sizeof(*leftsample) * 2 * BUFFSIZE);
  92. if (leftsample == NULL || rightsample == NULL || stereosample == NULL)
  93. fatalperror ("");
  94. channels = 2; /* Output files are stereo */
  95. if (wavout)
  96. {
  97. if ((strcmp(outfilename,"<stdout>")!=0) && (fseek( out, 0, SEEK_SET) != 0))
  98. fatalerror("Couldn't navigate output file '%s': %s\n",outfilename, strerror(errno));
  99. makewavheader();
  100. }
  101. startstopwatch();
  102. while (TRUE)
  103. {
  104. maxk = 0;
  105. for (i = 0; i < 2; i++)
  106. {
  107. k[i] = fread(i==0? leftsample : rightsample,
  108. sizeof(*leftsample),
  109. BUFFSIZE,
  110. channel[i]);
  111. if (k[i] == -1)
  112. fatalerror("Error reading file '%s': %s\n", filename[i],strerror(errno));
  113. if (k[i] > maxk)
  114. maxk = k[i];
  115. }
  116. if (maxk == 0)
  117. myexit (0);
  118. /*-------------------------------------------------*
  119. * First the left channel as far as it goes ... *
  120. *-------------------------------------------------*/
  121. for (i = 0; i < k[0]; i++)
  122. stereosample[2 * i] = leftsample[i];
  123. /*-------------------------------------------------*
  124. * ... and fill up till the end of this buffer. *
  125. *-------------------------------------------------*/
  126. for (; i < maxk; i++)
  127. stereosample[2 * i] = 0;
  128. /*-------------------------------------------------*
  129. * Next the right channel as far as it goes ... *
  130. *-------------------------------------------------*/
  131. for (i = 0; i < k[1]; i++)
  132. stereosample[2 * i + 1] = rightsample[i];
  133. /*-------------------------------------------------*
  134. * ... and fill up till the end of this buffer. *
  135. *-------------------------------------------------*/
  136. for (; i < maxk; i++)
  137. stereosample[2 * i + 1] = 0;
  138. if (!fwrite(stereosample, sizeof(*leftsample), 2 * maxk, out)) {
  139. fatalerror("Error writing to file '%s': %s\n",
  140. outfilename, strerror(errno));
  141. }
  142. }
  143. /* That was an endless loop. This point is never reached. */
  144. free(leftsample);
  145. free(rightsample);
  146. free(stereosample);
  147. }