1 /****************************************************************************
3 * Programs for processing sound files in raw- or WAV-format.
4 * -- Merge two mono WAV-files to one stereo WAV-file.
8 * Author: Mark Roberts <mark@manumark.de>
9 * Michael Labuschke <michael@labuschke.de>
11 ****************************************************************************/
14 <support_level>extended</support_level>
25 static char *Version = "stereorize 1.1, November 5th 2000";
27 "Usage: stereorize [options] infile-left infile-right outfile\n\n"
30 " stereorize left.wav right.wav stereo.wav -h\n\n"
32 "Creates stereo.wav (with WAV-header, option -h) from data in mono files\n"
33 "left.wav and right.wav.\n"
36 int main( int argcount, char *args[])
38 int i, k[2], maxk, stdin_in_use=FALSE;
39 short *leftsample, *rightsample, *stereosample;
41 char *filename[2], *tempname;
49 parseargs( argcount, args, NOFILES | NOCOMPLAIN);
51 for (i = 0; i < 2; i++)
53 filename[i] = parsefilearg( argcount, args);
54 if (filename[i] == NULL)
55 argerrornum( NULL, ME_NOTENOUGHFILES);
56 if (strcmp (filename[i], "-") == 0)
59 argerrortxt( filename[i] + 1,
60 "Cannot use <stdin> for both input files");
61 filename[i] = "<stdin>";
67 channel[i] = fopen(filename[i], "rb");
69 if (channel[i] == NULL)
70 fatalerror( "Error opening input file '%s': %s\n", filename[i],strerror(errno));
72 inform("Using file '%s' as input\n", filename[i]);
74 for (i = 0; i < 2; i++)
76 assert ( channel[i] != NULL);
77 readwavheader( channel[i]);
78 if (iswav && channels != 1)
79 inform("Warning: '%s' is no mono file\n", filename[i]);
82 outfilename = parsefilearg( argcount, args);
83 if (outfilename == NULL) argerrornum( NULL, ME_NOOUTFILE);
84 if (strcmp (outfilename, "-") == 0)
86 outfilename = "<stdout>";
91 out = fopen(outfilename, "wb");
94 fatalerror( "Error opening output file '%s': %s\n", outfilename,strerror(errno));
96 inform("Using file '%s' as output\n", outfilename);
98 if ((tempname = parsefilearg( argcount, args)) != NULL)
99 argerrornum( tempname, ME_TOOMANYFILES);
101 checknoargs(argcount, args); /* Check that no arguments are left */
103 leftsample = malloc( sizeof(*leftsample) * BUFFSIZE);
104 rightsample = malloc( sizeof(*leftsample) * BUFFSIZE);
105 stereosample = malloc( sizeof(*leftsample) * 2 * BUFFSIZE);
106 if (leftsample == NULL || rightsample == NULL || stereosample == NULL)
109 channels = 2; /* Output files are stereo */
112 if ((strcmp(outfilename,"<stdout>")!=0) && (fseek( out, 0, SEEK_SET) != 0))
113 fatalerror("Couldn't navigate output file '%s': %s\n",outfilename, strerror(errno));
121 for (i = 0; i < 2; i++)
123 k[i] = fread(i==0? leftsample : rightsample,
128 fatalerror("Error reading file '%s': %s\n", filename[i],strerror(errno));
135 /*-------------------------------------------------*
136 * First the left channel as far as it goes ... *
137 *-------------------------------------------------*/
138 for (i = 0; i < k[0]; i++)
139 stereosample[2 * i] = leftsample[i];
140 /*-------------------------------------------------*
141 * ... and fill up till the end of this buffer. *
142 *-------------------------------------------------*/
143 for (; i < maxk; i++)
144 stereosample[2 * i] = 0;
146 /*-------------------------------------------------*
147 * Next the right channel as far as it goes ... *
148 *-------------------------------------------------*/
149 for (i = 0; i < k[1]; i++)
150 stereosample[2 * i + 1] = rightsample[i];
151 /*-------------------------------------------------*
152 * ... and fill up till the end of this buffer. *
153 *-------------------------------------------------*/
154 for (; i < maxk; i++)
155 stereosample[2 * i + 1] = 0;
157 if (!fwrite(stereosample, sizeof(*leftsample), 2 * maxk, out)) {
158 fatalerror("Error writing to file '%s': %s\n",
159 outfilename, strerror(errno));
162 /* That was an endless loop. This point is never reached. */