c9a1931aeacff3627c0e9254eb424a06d4a8ee5c
2 ** Copyright (C) 2007-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #define BUFFER_LEN 1024
27 create_file (const char * fname
, int format
)
28 { static short buffer
[BUFFER_LEN
] ;
34 printf ("Creating file named '%s'\n", fname
) ;
36 file
= SndfileHandle (fname
, SFM_WRITE
, format
, channels
, srate
) ;
38 memset (buffer
, 0, sizeof (buffer
)) ;
40 file
.write (buffer
, BUFFER_LEN
) ;
44 ** The SndfileHandle object will automatically close the file and
45 ** release all allocated memory when the object goes out of scope.
46 ** This is the Resource Acquisition Is Initailization idom.
47 ** See : http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
52 read_file (const char * fname
)
53 { static short buffer
[BUFFER_LEN
] ;
57 file
= SndfileHandle (fname
) ;
59 printf ("Opened file '%s'\n", fname
) ;
60 printf (" Sample rate : %d\n", file
.samplerate ()) ;
61 printf (" Channels : %d\n", file
.channels ()) ;
63 file
.read (buffer
, BUFFER_LEN
) ;
67 /* RAII takes care of destroying SndfileHandle object. */
72 { const char * fname
= "test.wav" ;
74 puts ("\nSimple example showing usage of the C++ SndfileHandle object.\n") ;
76 create_file (fname
, SF_FORMAT_WAV
| SF_FORMAT_PCM_16
) ;