annotate src/libsndfile-1.0.27/examples/sndfilehandle.cc @ 127:7867fa7e1b6b

Current fftw source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:40:26 +0100
parents cd6cdf86811e
children
rev   line source
cannam@125 1 /*
cannam@125 2 ** Copyright (C) 2007-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
cannam@125 3 **
cannam@125 4 ** This program is free software; you can redistribute it and/or modify
cannam@125 5 ** it under the terms of the GNU General Public License as published by
cannam@125 6 ** the Free Software Foundation; either version 2 of the License, or
cannam@125 7 ** (at your option) any later version.
cannam@125 8 **
cannam@125 9 ** This program is distributed in the hope that it will be useful,
cannam@125 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@125 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@125 12 ** GNU General Public License for more details.
cannam@125 13 **
cannam@125 14 ** You should have received a copy of the GNU General Public License
cannam@125 15 ** along with this program; if not, write to the Free Software
cannam@125 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
cannam@125 17 */
cannam@125 18
cannam@125 19 #include <cstdio>
cannam@125 20 #include <cstring>
cannam@125 21
cannam@125 22 #include <sndfile.hh>
cannam@125 23
cannam@125 24 #define BUFFER_LEN 1024
cannam@125 25
cannam@125 26 static void
cannam@125 27 create_file (const char * fname, int format)
cannam@125 28 { static short buffer [BUFFER_LEN] ;
cannam@125 29
cannam@125 30 SndfileHandle file ;
cannam@125 31 int channels = 2 ;
cannam@125 32 int srate = 48000 ;
cannam@125 33
cannam@125 34 printf ("Creating file named '%s'\n", fname) ;
cannam@125 35
cannam@125 36 file = SndfileHandle (fname, SFM_WRITE, format, channels, srate) ;
cannam@125 37
cannam@125 38 memset (buffer, 0, sizeof (buffer)) ;
cannam@125 39
cannam@125 40 file.write (buffer, BUFFER_LEN) ;
cannam@125 41
cannam@125 42 puts ("") ;
cannam@125 43 /*
cannam@125 44 ** The SndfileHandle object will automatically close the file and
cannam@125 45 ** release all allocated memory when the object goes out of scope.
cannam@125 46 ** This is the Resource Acquisition Is Initailization idom.
cannam@125 47 ** See : http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
cannam@125 48 */
cannam@125 49 } /* create_file */
cannam@125 50
cannam@125 51 static void
cannam@125 52 read_file (const char * fname)
cannam@125 53 { static short buffer [BUFFER_LEN] ;
cannam@125 54
cannam@125 55 SndfileHandle file ;
cannam@125 56
cannam@125 57 file = SndfileHandle (fname) ;
cannam@125 58
cannam@125 59 printf ("Opened file '%s'\n", fname) ;
cannam@125 60 printf (" Sample rate : %d\n", file.samplerate ()) ;
cannam@125 61 printf (" Channels : %d\n", file.channels ()) ;
cannam@125 62
cannam@125 63 file.read (buffer, BUFFER_LEN) ;
cannam@125 64
cannam@125 65 puts ("") ;
cannam@125 66
cannam@125 67 /* RAII takes care of destroying SndfileHandle object. */
cannam@125 68 } /* read_file */
cannam@125 69
cannam@125 70 int
cannam@125 71 main (void)
cannam@125 72 { const char * fname = "test.wav" ;
cannam@125 73
cannam@125 74 puts ("\nSimple example showing usage of the C++ SndfileHandle object.\n") ;
cannam@125 75
cannam@125 76 create_file (fname, SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
cannam@125 77
cannam@125 78 read_file (fname) ;
cannam@125 79
cannam@125 80 puts ("Done.\n") ;
cannam@125 81 return 0 ;
cannam@125 82 } /* main */
cannam@125 83
cannam@125 84