annotate src/libsndfile-1.0.27/examples/sndfilehandle.cc @ 84:08ae793730bd

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