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 ****************************************************************************/
21 static char *Version
= "stereorize 1.1, November 5th 2000";
23 "Usage: stereorize [options] infile-left infile-right outfile\n\n"
26 " stereorize left.wav right.wav stereo.wav -h\n\n"
28 "Creates stereo.wav (with WAV-header, option -h) from data in mono files\n"
29 "left.wav and right.wav.\n"
32 int main( int argcount
, char *args
[])
34 int i
, k
[2], maxk
, stdin_in_use
=FALSE
;
35 short *leftsample
, *rightsample
, *stereosample
;
37 char *filename
[2], *tempname
;
45 parseargs( argcount
, args
, NOFILES
| NOCOMPLAIN
);
47 for (i
= 0; i
< 2; i
++)
49 filename
[i
] = parsefilearg( argcount
, args
);
50 if (filename
[i
] == NULL
)
51 argerrornum( NULL
, ME_NOTENOUGHFILES
);
52 if (strcmp (filename
[i
], "-") == 0)
55 argerrortxt( filename
[i
] + 1,
56 "Cannot use <stdin> for both input files");
57 filename
[i
] = "<stdin>";
63 channel
[i
] = fopen(filename
[i
], "rb");
65 if (channel
[i
] == NULL
)
66 fatalerror( "Error opening input file '%s': %s\n", filename
[i
],strerror(errno
));
68 inform("Using file '%s' as input\n", filename
[i
]);
70 for (i
= 0; i
< 2; i
++)
72 assert ( channel
[i
] != NULL
);
73 readwavheader( channel
[i
]);
74 if (iswav
&& channels
!= 1)
75 inform("Warning: '%s' is no mono file\n", filename
[i
]);
78 outfilename
= parsefilearg( argcount
, args
);
79 if (outfilename
== NULL
) argerrornum( NULL
, ME_NOOUTFILE
);
80 if (strcmp (outfilename
, "-") == 0)
82 outfilename
= "<stdout>";
87 out
= fopen(outfilename
, "wb");
90 fatalerror( "Error opening output file '%s': %s\n", outfilename
,strerror(errno
));
92 inform("Using file '%s' as output\n", outfilename
);
94 if ((tempname
= parsefilearg( argcount
, args
)) != NULL
)
95 argerrornum( tempname
, ME_TOOMANYFILES
);
97 checknoargs(argcount
, args
); /* Check that no arguments are left */
99 leftsample
= malloc( sizeof(*leftsample
) * BUFFSIZE
);
100 rightsample
= malloc( sizeof(*leftsample
) * BUFFSIZE
);
101 stereosample
= malloc( sizeof(*leftsample
) * 2 * BUFFSIZE
);
102 if (leftsample
== NULL
|| rightsample
== NULL
|| stereosample
== NULL
)
105 channels
= 2; /* Output files are stereo */
108 if ((strcmp(outfilename
,"<stdout>")!=0) && (fseek( out
, 0, SEEK_SET
) != 0))
109 fatalerror("Couldn't navigate output file '%s': %s\n",outfilename
, strerror(errno
));
117 for (i
= 0; i
< 2; i
++)
119 k
[i
] = fread(i
==0? leftsample
: rightsample
,
124 fatalerror("Error reading file '%s': %s\n", filename
[i
],strerror(errno
));
131 /*-------------------------------------------------*
132 * First the left channel as far as it goes ... *
133 *-------------------------------------------------*/
134 for (i
= 0; i
< k
[0]; i
++)
135 stereosample
[2 * i
] = leftsample
[i
];
136 /*-------------------------------------------------*
137 * ... and fill up till the end of this buffer. *
138 *-------------------------------------------------*/
139 for (; i
< maxk
; i
++)
140 stereosample
[2 * i
] = 0;
142 /*-------------------------------------------------*
143 * Next the right channel as far as it goes ... *
144 *-------------------------------------------------*/
145 for (i
= 0; i
< k
[1]; i
++)
146 stereosample
[2 * i
+ 1] = rightsample
[i
];
147 /*-------------------------------------------------*
148 * ... and fill up till the end of this buffer. *
149 *-------------------------------------------------*/
150 for (; i
< maxk
; i
++)
151 stereosample
[2 * i
+ 1] = 0;
153 fwrite(stereosample
, sizeof(*leftsample
), 2 * maxk
, out
);
154 if (ferror( out
) != 0)
155 fatalerror("Error writing to file '%s': %s\n",
156 outfilename
, strerror(errno
));
158 /* That was an endless loop. This point is never reached. */