| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include <stdio.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include <errno.h> |
| 21 |
#include <time.h> |
| 22 |
#include <assert.h> |
| 23 |
#include "frame.h" |
| 24 |
|
| 25 |
static char *Version = "stereorize 1.1, November 5th 2000"; |
| 26 |
static char *Usage = |
| 27 |
"Usage: stereorize [options] infile-left infile-right outfile\n\n" |
| 28 |
|
| 29 |
"Example:\n" |
| 30 |
" stereorize left.wav right.wav stereo.wav -h\n\n" |
| 31 |
|
| 32 |
"Creates stereo.wav (with WAV-header, option -h) from data in mono files\n" |
| 33 |
"left.wav and right.wav.\n" |
| 34 |
; |
| 35 |
|
| 36 |
int main( int argcount, char *args[]) |
| 37 |
{ |
| 38 |
int i, k[2], maxk, stdin_in_use=FALSE; |
| 39 |
short *leftsample, *rightsample, *stereosample; |
| 40 |
FILE *channel[2]; |
| 41 |
char *filename[2], *tempname; |
| 42 |
|
| 43 |
version = Version; |
| 44 |
usage = Usage; |
| 45 |
|
| 46 |
channel[0] = NULL; |
| 47 |
channel[1] = NULL; |
| 48 |
|
| 49 |
parseargs( argcount, args, NOFILES | NOCOMPLAIN); |
| 50 |
|
| 51 |
for (i = 0; i < 2; i++) |
| 52 |
{ |
| 53 |
filename[i] = parsefilearg( argcount, args); |
| 54 |
if (filename[i] == NULL) |
| 55 |
argerrornum( NULL, ME_NOTENOUGHFILES); |
| 56 |
if (strcmp (filename[i], "-") == 0) |
| 57 |
{ |
| 58 |
if (stdin_in_use) |
| 59 |
argerrortxt( filename[i] + 1, |
| 60 |
"Cannot use <stdin> for both input files"); |
| 61 |
filename[i] = "<stdin>"; |
| 62 |
channel[i] = stdin; |
| 63 |
stdin_in_use = TRUE; |
| 64 |
} |
| 65 |
else |
| 66 |
{ |
| 67 |
channel[i] = fopen(filename[i], "rb"); |
| 68 |
} |
| 69 |
if (channel[i] == NULL) |
| 70 |
fatalerror( "Error opening input file '%s': %s\n", filename[i],strerror(errno)); |
| 71 |
else |
| 72 |
inform("Using file '%s' as input\n", filename[i]); |
| 73 |
} |
| 74 |
for (i = 0; i < 2; i++) |
| 75 |
{ |
| 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]); |
| 80 |
} |
| 81 |
|
| 82 |
outfilename = parsefilearg( argcount, args); |
| 83 |
if (outfilename == NULL) argerrornum( NULL, ME_NOOUTFILE); |
| 84 |
if (strcmp (outfilename, "-") == 0) |
| 85 |
{ |
| 86 |
outfilename = "<stdout>"; |
| 87 |
out = stdout; |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
out = fopen(outfilename, "wb"); |
| 92 |
} |
| 93 |
if (out == NULL) |
| 94 |
fatalerror( "Error opening output file '%s': %s\n", outfilename,strerror(errno)); |
| 95 |
else |
| 96 |
inform("Using file '%s' as output\n", outfilename); |
| 97 |
|
| 98 |
if ((tempname = parsefilearg( argcount, args)) != NULL) |
| 99 |
argerrornum( tempname, ME_TOOMANYFILES); |
| 100 |
|
| 101 |
checknoargs(argcount, args); |
| 102 |
|
| 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) |
| 107 |
fatalperror (""); |
| 108 |
|
| 109 |
channels = 2; |
| 110 |
if (wavout) |
| 111 |
{ |
| 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)); |
| 114 |
makewavheader(); |
| 115 |
} |
| 116 |
|
| 117 |
startstopwatch(); |
| 118 |
while (TRUE) |
| 119 |
{ |
| 120 |
maxk = 0; |
| 121 |
for (i = 0; i < 2; i++) |
| 122 |
{ |
| 123 |
k[i] = fread(i==0? leftsample : rightsample, |
| 124 |
sizeof(*leftsample), |
| 125 |
BUFFSIZE, |
| 126 |
channel[i]); |
| 127 |
if (k[i] == -1) |
| 128 |
fatalerror("Error reading file '%s': %s\n", filename[i],strerror(errno)); |
| 129 |
if (k[i] > maxk) |
| 130 |
maxk = k[i]; |
| 131 |
} |
| 132 |
if (maxk == 0) |
| 133 |
myexit (0); |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
for (i = 0; i < k[0]; i++) |
| 139 |
stereosample[2 * i] = leftsample[i]; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
for (; i < maxk; i++) |
| 144 |
stereosample[2 * i] = 0; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
for (i = 0; i < k[1]; i++) |
| 150 |
stereosample[2 * i + 1] = rightsample[i]; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
for (; i < maxk; i++) |
| 155 |
stereosample[2 * i + 1] = 0; |
| 156 |
|
| 157 |
if (!fwrite(stereosample, sizeof(*leftsample), 2 * maxk, out)) { |
| 158 |
fatalerror("Error writing to file '%s': %s\n", |
| 159 |
outfilename, strerror(errno)); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
free(leftsample); |
| 164 |
free(rightsample); |
| 165 |
free(stereosample); |
| 166 |
} |