aboutsummaryrefslogtreecommitdiffstats
path: root/utils/stereorize.c
blob: 31f1b1fbed1ad8275ebb507673eead7834c33e68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/****************************************************************************
 *
 * Programs for processing sound files in raw- or WAV-format.
 * -- Merge two mono WAV-files to one stereo WAV-file.
 *
 * Name:    stereorize.c
 * Version: 1.1
 * Author:  Mark Roberts <mark@manumark.de>
 *	    Michael Labuschke <michael@labuschke.de>
 *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <assert.h>
#include "frame.h"

static char *Version = "stereorize 1.1, November 5th 2000";
static char *Usage =
"Usage: stereorize [options] infile-left infile-right outfile\n\n"

"Example:\n"
" stereorize left.wav right.wav stereo.wav -h\n\n"

"Creates stereo.wav (with WAV-header, option -h) from data in mono files\n"
"left.wav and right.wav.\n"
;

int main( int argcount, char *args[])
{
   int i, k[2], maxk, stdin_in_use=FALSE;
   short *leftsample, *rightsample, *stereosample;
   FILE *channel[2];
   char *filename[2], *tempname;

   version = Version;
   usage = Usage;

   channel[0] = NULL;
   channel[1] = NULL;

   parseargs( argcount, args, NOFILES | NOCOMPLAIN);

   for (i = 0; i < 2; i++)
     {
       filename[i] = parsefilearg( argcount, args);
       if (filename[i] == NULL)
	 argerrornum( NULL, ME_NOTENOUGHFILES);
       if (strcmp (filename[i], "-") == 0)
	 {
	   if (stdin_in_use)
	     argerrortxt( filename[i] + 1,
			  "Cannot use <stdin> for both input files");
	   filename[i] = "<stdin>";
	   channel[i] = stdin;
	   stdin_in_use = TRUE;
	 }
       else
	 {
	   channel[i] = fopen(filename[i], "rb");
	 }
       if (channel[i] == NULL)
	   fatalerror( "Error opening input file '%s': %s\n", filename[i],strerror(errno));
       else
	 inform("Using file '%s' as input\n", filename[i]);
     }
   for (i = 0; i < 2; i++)
     {
       assert ( channel[i] != NULL);
       readwavheader( channel[i]);
       if (iswav && channels != 1)
	 inform("Warning: '%s' is no mono file\n", filename[i]);
     }

   outfilename = parsefilearg( argcount, args);
   if (outfilename == NULL) argerrornum( NULL, ME_NOOUTFILE);
   if (strcmp (outfilename, "-") == 0)
     {
       outfilename = "<stdout>";
       out = stdout;
     }
   else
     {
       out = fopen(outfilename, "wb");
     }
   if (out == NULL)
     fatalerror( "Error opening output file '%s': %s\n", outfilename,strerror(errno));
   else
     inform("Using file '%s' as output\n", outfilename);

   if ((tempname = parsefilearg( argcount, args)) != NULL)
     argerrornum( tempname, ME_TOOMANYFILES);

   checknoargs(argcount, args);      /* Check that no arguments are left */

   leftsample = malloc( sizeof(*leftsample) * BUFFSIZE);
   rightsample = malloc( sizeof(*leftsample) * BUFFSIZE);
   stereosample = malloc( sizeof(*leftsample) * 2 * BUFFSIZE);
   if (leftsample == NULL || rightsample == NULL || stereosample == NULL)
     fatalperror ("");

   channels = 2;   /* Output files are stereo */
   if (wavout)
     {
       if ((strcmp(outfilename,"<stdout>")!=0) && (fseek( out, 0, SEEK_SET) != 0)) 
    	 fatalerror("Couldn't navigate output file '%s': %s\n",outfilename, strerror(errno));
       makewavheader();
     }

   startstopwatch();
   while (TRUE)
   {
      maxk = 0;
      for (i = 0; i < 2; i++)
	{
	  k[i] = fread(i==0? leftsample : rightsample,
		       sizeof(*leftsample),
		       BUFFSIZE,
		       channel[i]);
	  if (k[i] == -1)
	    fatalerror("Error reading file '%s': %s\n", filename[i],strerror(errno));
	  if (k[i] > maxk)
	    maxk = k[i];
	}
      if (maxk == 0)
	myexit (0);

      /*-------------------------------------------------*
       * First the left channel as far as it goes ...    *
       *-------------------------------------------------*/
      for (i = 0; i < k[0]; i++)
	stereosample[2 * i] = leftsample[i];
      /*-------------------------------------------------*
       * ... and fill up till the end of this buffer.    *
       *-------------------------------------------------*/
      for (; i < maxk; i++)
	stereosample[2 * i] = 0;

      /*-------------------------------------------------*
       * Next the right channel as far as it goes ...    *
       *-------------------------------------------------*/
      for (i = 0; i < k[1]; i++)
	stereosample[2 * i + 1] = rightsample[i];
      /*-------------------------------------------------*
       * ... and fill up till the end of this buffer.    *
       *-------------------------------------------------*/
      for (; i < maxk; i++)
	stereosample[2 * i + 1] = 0;

      if (!fwrite(stereosample, sizeof(*leftsample), 2 * maxk, out)) {
	      fatalerror("Error writing to file '%s': %s\n",
			 outfilename, strerror(errno));
      }
   }
   /* That was an endless loop. This point is never reached. */
   free(leftsample);
   free(rightsample);
   free(stereosample);
}