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