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