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