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