Asterisk - The Open Source Telephony Project GIT-master-d5a0626
stereorize.c
Go to the documentation of this file.
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
13/*** MODULEINFO
14 <support_level>extended</support_level>
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
25static char *Version = "stereorize 1.1, November 5th 2000";
26static 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
36int 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
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)
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);
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); /* Check that no arguments are left */
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; /* Output files are stereo */
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));
115 }
116
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 * 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;
145
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;
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 /* That was an endless loop. This point is never reached. */
163 free(leftsample);
164 free(rightsample);
165 free(stereosample);
166}
#define TRUE
Definition: app_minivm.c:524
#define FALSE
Definition: app_minivm.c:527
static char version[AST_MAX_EXTENSION]
Definition: chan_ooh323.c:391
static struct channel_usage channels
char * malloc()
void free()
int errno
#define NULL
Definition: resample.c:96
static char * Version
Definition: stereorize.c:25
int main(int argcount, char *args[])
Definition: stereorize.c:36
static char * Usage
Definition: stereorize.c:26
const char * args
Time-related functions and macros.
void argerrornum(char *s, Errornum code)
Definition: utils/frame.c:635
void fatalperror(const char *string)
Definition: utils/frame.c:1020
int myexit(int value)
Definition: utils/frame.c:922
FILE * out
Definition: utils/frame.c:33
int iswav
Definition: utils/frame.c:32
void readwavheader(FILE *anyin)
Definition: utils/frame.c:126
int wavout
Definition: utils/frame.c:31
void argerrortxt(char *s, char *message)
Definition: utils/frame.c:704
void fatalerror(const char *format,...)
Definition: utils/frame.c:1010
char * usage
Definition: utils/frame.c:37
int inform(const char *format,...)
Definition: utils/frame.c:985
void checknoargs(int argcount, char *args[])
Definition: utils/frame.c:717
char * outfilename
Definition: utils/frame.c:34
char * parsefilearg(int argcount, char *args[])
Definition: utils/frame.c:419
void parseargs(int argcount, char *args[], int fileswitch)
Definition: utils/frame.c:744
void startstopwatch(void)
Definition: utils/frame.c:307
void makewavheader(void)
Definition: utils/frame.c:219
#define BUFFSIZE
Definition: utils/frame.h:54
#define NOCOMPLAIN
Definition: utils/frame.h:32
#define NOFILES
Definition: utils/frame.h:31
@ ME_TOOMANYFILES
Definition: utils/frame.h:67
@ ME_NOOUTFILE
Definition: utils/frame.h:70
@ ME_NOTENOUGHFILES
Definition: utils/frame.h:75