annotate src/libsndfile-1.0.25/examples/sndfilehandle.cc @ 169:223a55898ab9 tip default

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