Chris@40: /* Chris@40: ** Copyright (C) 2007-2011 Erik de Castro Lopo Chris@40: ** Chris@40: ** This program is free software; you can redistribute it and/or modify Chris@40: ** it under the terms of the GNU General Public License as published by Chris@40: ** the Free Software Foundation; either version 2 of the License, or Chris@40: ** (at your option) any later version. Chris@40: ** Chris@40: ** This program is distributed in the hope that it will be useful, Chris@40: ** but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@40: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@40: ** GNU General Public License for more details. Chris@40: ** Chris@40: ** You should have received a copy of the GNU General Public License Chris@40: ** along with this program; if not, write to the Free Software Chris@40: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Chris@40: */ Chris@40: Chris@40: #include Chris@40: #include Chris@40: Chris@40: #include Chris@40: Chris@40: #define BUFFER_LEN 1024 Chris@40: Chris@40: static void Chris@40: create_file (const char * fname, int format) Chris@40: { static short buffer [BUFFER_LEN] ; Chris@40: Chris@40: SndfileHandle file ; Chris@40: int channels = 2 ; Chris@40: int srate = 48000 ; Chris@40: Chris@40: printf ("Creating file named '%s'\n", fname) ; Chris@40: Chris@40: file = SndfileHandle (fname, SFM_WRITE, format, channels, srate) ; Chris@40: Chris@40: memset (buffer, 0, sizeof (buffer)) ; Chris@40: Chris@40: file.write (buffer, BUFFER_LEN) ; Chris@40: Chris@40: puts ("") ; Chris@40: /* Chris@40: ** The SndfileHandle object will automatically close the file and Chris@40: ** release all allocated memory when the object goes out of scope. Chris@40: ** This is the Resource Acquisition Is Initailization idom. Chris@40: ** See : http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization Chris@40: */ Chris@40: } /* create_file */ Chris@40: Chris@40: static void Chris@40: read_file (const char * fname) Chris@40: { static short buffer [BUFFER_LEN] ; Chris@40: Chris@40: SndfileHandle file ; Chris@40: Chris@40: file = SndfileHandle (fname) ; Chris@40: Chris@40: printf ("Opened file '%s'\n", fname) ; Chris@40: printf (" Sample rate : %d\n", file.samplerate ()) ; Chris@40: printf (" Channels : %d\n", file.channels ()) ; Chris@40: Chris@40: file.read (buffer, BUFFER_LEN) ; Chris@40: Chris@40: puts ("") ; Chris@40: Chris@40: /* RAII takes care of destroying SndfileHandle object. */ Chris@40: } /* read_file */ Chris@40: Chris@40: int Chris@40: main (void) Chris@40: { const char * fname = "test.wav" ; Chris@40: Chris@40: puts ("\nSimple example showing usage of the C++ SndfileHandle object.\n") ; Chris@40: Chris@40: create_file (fname, SF_FORMAT_WAV | SF_FORMAT_PCM_16) ; Chris@40: Chris@40: read_file (fname) ; Chris@40: Chris@40: puts ("Done.\n") ; Chris@40: return 0 ; Chris@40: } /* main */ Chris@40: Chris@40: