c9a1931aeacff3627c0e9254eb424a06d4a8ee5c
[Faustine.git] / interpretor / libsndfile-1.0.25 / examples / sndfilehandle.cc
1 /*
2 ** Copyright (C) 2007-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
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.
8 **
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.
13 **
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.
17 */
18
19 #include <cstdio>
20 #include <cstring>
21
22 #include <sndfile.hh>
23
24 #define BUFFER_LEN 1024
25
26 static void
27 create_file (const char * fname, int format)
28 { static short buffer [BUFFER_LEN] ;
29
30 SndfileHandle file ;
31 int channels = 2 ;
32 int srate = 48000 ;
33
34 printf ("Creating file named '%s'\n", fname) ;
35
36 file = SndfileHandle (fname, SFM_WRITE, format, channels, srate) ;
37
38 memset (buffer, 0, sizeof (buffer)) ;
39
40 file.write (buffer, BUFFER_LEN) ;
41
42 puts ("") ;
43 /*
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
48 */
49 } /* create_file */
50
51 static void
52 read_file (const char * fname)
53 { static short buffer [BUFFER_LEN] ;
54
55 SndfileHandle file ;
56
57 file = SndfileHandle (fname) ;
58
59 printf ("Opened file '%s'\n", fname) ;
60 printf (" Sample rate : %d\n", file.samplerate ()) ;
61 printf (" Channels : %d\n", file.channels ()) ;
62
63 file.read (buffer, BUFFER_LEN) ;
64
65 puts ("") ;
66
67 /* RAII takes care of destroying SndfileHandle object. */
68 } /* read_file */
69
70 int
71 main (void)
72 { const char * fname = "test.wav" ;
73
74 puts ("\nSimple example showing usage of the C++ SndfileHandle object.\n") ;
75
76 create_file (fname, SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
77
78 read_file (fname) ;
79
80 puts ("Done.\n") ;
81 return 0 ;
82 } /* main */
83
84