Chris@40: /* Chris@40: ** Copyright (C) 2001-2013 Erik de Castro Lopo Chris@40: ** Chris@40: ** All rights reserved. Chris@40: ** Chris@40: ** Redistribution and use in source and binary forms, with or without Chris@40: ** modification, are permitted provided that the following conditions are Chris@40: ** met: Chris@40: ** Chris@40: ** * Redistributions of source code must retain the above copyright Chris@40: ** notice, this list of conditions and the following disclaimer. Chris@40: ** * Redistributions in binary form must reproduce the above copyright Chris@40: ** notice, this list of conditions and the following disclaimer in Chris@40: ** the documentation and/or other materials provided with the Chris@40: ** distribution. Chris@40: ** * Neither the author nor the names of any contributors may be used Chris@40: ** to endorse or promote products derived from this software without Chris@40: ** specific prior written permission. Chris@40: ** Chris@40: ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@40: ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED Chris@40: ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR Chris@40: ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR Chris@40: ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@40: ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@40: ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; Chris@40: ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, Chris@40: ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR Chris@40: ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF Chris@40: ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@40: */ Chris@40: Chris@40: #include Chris@40: #include Chris@40: Chris@40: /* Include this header file to use functions from libsndfile. */ Chris@40: #include Chris@40: Chris@40: /* This will be the length of the buffer used to hold.frames while Chris@40: ** we process them. Chris@40: */ Chris@40: #define BUFFER_LEN 1024 Chris@40: Chris@40: /* libsndfile can handle more than 6 channels but we'll restrict it to 6. */ Chris@40: #define MAX_CHANNELS 6 Chris@40: Chris@40: /* Function prototype. */ Chris@40: static void process_data (double *data, int count, int channels) ; Chris@40: Chris@40: Chris@40: int Chris@40: main (void) Chris@40: { /* This is a buffer of double precision floating point values Chris@40: ** which will hold our data while we process it. Chris@40: */ Chris@40: static double data [BUFFER_LEN] ; Chris@40: Chris@40: /* A SNDFILE is very much like a FILE in the Standard C library. The Chris@40: ** sf_open function return an SNDFILE* pointer when they sucessfully Chris@40: ** open the specified file. Chris@40: */ Chris@40: SNDFILE *infile, *outfile ; Chris@40: Chris@40: /* A pointer to an SF_INFO struct is passed to sf_open. Chris@40: ** On read, the library fills this struct with information about the file. Chris@40: ** On write, the struct must be filled in before calling sf_open. Chris@40: */ Chris@40: SF_INFO sfinfo ; Chris@40: int readcount ; Chris@40: const char *infilename = "input.wav" ; Chris@40: const char *outfilename = "output.wav" ; Chris@40: Chris@40: /* The SF_INFO struct must be initialized before using it. Chris@40: */ Chris@40: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@40: Chris@40: /* Here's where we open the input file. We pass sf_open the file name and Chris@40: ** a pointer to an SF_INFO struct. Chris@40: ** On successful open, sf_open returns a SNDFILE* pointer which is used Chris@40: ** for all subsequent operations on that file. Chris@40: ** If an error occurs during sf_open, the function returns a NULL pointer. Chris@40: ** Chris@40: ** If you are trying to open a raw headerless file you will need to set the Chris@40: ** format and channels fields of sfinfo before calling sf_open(). For Chris@40: ** instance to open a raw 16 bit stereo PCM file you would need the following Chris@40: ** two lines: Chris@40: ** Chris@40: ** sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16 ; Chris@40: ** sfinfo.channels = 2 ; Chris@40: */ Chris@40: if (! (infile = sf_open (infilename, SFM_READ, &sfinfo))) Chris@40: { /* Open failed so print an error message. */ Chris@40: printf ("Not able to open input file %s.\n", infilename) ; Chris@40: /* Print the error message from libsndfile. */ Chris@40: puts (sf_strerror (NULL)) ; Chris@40: return 1 ; Chris@40: } ; Chris@40: Chris@40: if (sfinfo.channels > MAX_CHANNELS) Chris@40: { printf ("Not able to process more than %d channels\n", MAX_CHANNELS) ; Chris@40: return 1 ; Chris@40: } ; Chris@40: /* Open the output file. */ Chris@40: if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo))) Chris@40: { printf ("Not able to open output file %s.\n", outfilename) ; Chris@40: puts (sf_strerror (NULL)) ; Chris@40: return 1 ; Chris@40: } ; Chris@40: Chris@40: /* While there are.frames in the input file, read them, process Chris@40: ** them and write them to the output file. Chris@40: */ Chris@40: while ((readcount = sf_read_double (infile, data, BUFFER_LEN))) Chris@40: { process_data (data, readcount, sfinfo.channels) ; Chris@40: sf_write_double (outfile, data, readcount) ; Chris@40: } ; Chris@40: Chris@40: /* Close input and output files. */ Chris@40: sf_close (infile) ; Chris@40: sf_close (outfile) ; Chris@40: Chris@40: return 0 ; Chris@40: } /* main */ Chris@40: Chris@40: static void Chris@40: process_data (double *data, int count, int channels) Chris@40: { double channel_gain [MAX_CHANNELS] = { 0.5, 0.8, 0.1, 0.4, 0.4, 0.9 } ; Chris@40: int k, chan ; Chris@40: Chris@40: /* Process the data here. Chris@40: ** If the soundfile contains more then 1 channel you need to take care of Chris@40: ** the data interleaving youself. Chris@40: ** Current we just apply a channel dependant gain. Chris@40: */ Chris@40: Chris@40: for (chan = 0 ; chan < channels ; chan ++) Chris@40: for (k = chan ; k < count ; k+= channels) Chris@40: data [k] *= channel_gain [chan] ; Chris@40: Chris@40: return ; Chris@40: } /* process_data */ Chris@40: