5c7db2884317ca1700f6094b401a5401029fe837
2 ** Copyright (C) 2001-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
4 ** All rights reserved.
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are
10 ** * Redistributions of source code must retain the above copyright
11 ** notice, this list of conditions and the following disclaimer.
12 ** * Redistributions in binary form must reproduce the above copyright
13 ** notice, this list of conditions and the following disclaimer in
14 ** the documentation and/or other materials provided with the
16 ** * Neither the author nor the names of any contributors may be used
17 ** to endorse or promote products derived from this software without
18 ** specific prior written permission.
20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 /* Include this header file to use functions from libsndfile. */
38 /* This will be the length of the buffer used to hold.frames while
41 #define BUFFER_LEN 1024
43 /* libsndfile can handle more than 6 channels but we'll restrict it to 6. */
44 #define MAX_CHANNELS 6
46 /* Function prototype. */
47 static void process_data (double *data
, int count
, int channels
) ;
52 { /* This is a buffer of double precision floating point values
53 ** which will hold our data while we process it.
55 static double data
[BUFFER_LEN
] ;
57 /* A SNDFILE is very much like a FILE in the Standard C library. The
58 ** sf_open function return an SNDFILE* pointer when they sucessfully
59 ** open the specified file.
61 SNDFILE
*infile
, *outfile
;
63 /* A pointer to an SF_INFO stutct is passed to sf_open.
64 ** On read, the library fills this struct with information about the file.
65 ** On write, the struct must be filled in before calling sf_open.
69 const char *infilename
= "input.wav" ;
70 const char *outfilename
= "output.wav" ;
72 /* Here's where we open the input file. We pass sf_open the file name and
73 ** a pointer to an SF_INFO struct.
74 ** On successful open, sf_open returns a SNDFILE* pointer which is used
75 ** for all subsequent operations on that file.
76 ** If an error occurs during sf_open, the function returns a NULL pointer.
78 ** If you are trying to open a raw headerless file you will need to set the
79 ** format and channels fields of sfinfo before calling sf_open(). For
80 ** instance to open a raw 16 bit stereo PCM file you would need the following
83 ** sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16 ;
84 ** sfinfo.channels = 2 ;
86 if (! (infile
= sf_open (infilename
, SFM_READ
, &sfinfo
)))
87 { /* Open failed so print an error message. */
88 printf ("Not able to open input file %s.\n", infilename
) ;
89 /* Print the error message from libsndfile. */
90 puts (sf_strerror (NULL
)) ;
94 if (sfinfo
.channels
> MAX_CHANNELS
)
95 { printf ("Not able to process more than %d channels\n", MAX_CHANNELS
) ;
98 /* Open the output file. */
99 if (! (outfile
= sf_open (outfilename
, SFM_WRITE
, &sfinfo
)))
100 { printf ("Not able to open output file %s.\n", outfilename
) ;
101 puts (sf_strerror (NULL
)) ;
105 /* While there are.frames in the input file, read them, process
106 ** them and write them to the output file.
108 while ((readcount
= sf_read_double (infile
, data
, BUFFER_LEN
)))
109 { process_data (data
, readcount
, sfinfo
.channels
) ;
110 sf_write_double (outfile
, data
, readcount
) ;
113 /* Close input and output files. */
121 process_data (double *data
, int count
, int channels
)
122 { double channel_gain
[MAX_CHANNELS
] = { 0.5, 0.8, 0.1, 0.4, 0.4, 0.9 } ;
125 /* Process the data here.
126 ** If the soundfile contains more then 1 channel you need to take care of
127 ** the data interleaving youself.
128 ** Current we just apply a channel dependant gain.
131 for (chan
= 0 ; chan
< channels
; chan
++)
132 for (k
= chan
; k
< count
; k
+= channels
)
133 data
[k
] *= channel_gain
[chan
] ;