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