annotate src/libsndfile-1.0.25/examples/sfprocess.c @ 23:619f715526df sv_v2.1

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