annotate src/libsndfile-1.0.27/examples/sfprocess.c @ 40:1df64224f5ac

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