annotate src/libsndfile-1.0.25/examples/sfprocess.c @ 85:545efbb81310

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