Chris@0: /* Chris@0: ** Copyright (C) 1999-2011 Erik de Castro Lopo Chris@0: ** Chris@0: ** All rights reserved. Chris@0: ** Chris@0: ** Redistribution and use in source and binary forms, with or without Chris@0: ** modification, are permitted provided that the following conditions are Chris@0: ** met: Chris@0: ** Chris@0: ** * Redistributions of source code must retain the above copyright Chris@0: ** notice, this list of conditions and the following disclaimer. Chris@0: ** * Redistributions in binary form must reproduce the above copyright Chris@0: ** notice, this list of conditions and the following disclaimer in Chris@0: ** the documentation and/or other materials provided with the Chris@0: ** distribution. Chris@0: ** * Neither the author nor the names of any contributors may be used Chris@0: ** to endorse or promote products derived from this software without Chris@0: ** specific prior written permission. Chris@0: ** Chris@0: ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@0: ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED Chris@0: ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR Chris@0: ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR Chris@0: ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@0: ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@0: ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; Chris@0: ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, Chris@0: ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR Chris@0: ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF Chris@0: ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@0: */ Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #ifndef M_PI Chris@0: #define M_PI 3.14159265358979323846264338 Chris@0: #endif Chris@0: Chris@0: #define SAMPLE_RATE 44100 Chris@0: #define SAMPLE_COUNT (SAMPLE_RATE * 4) /* 4 seconds */ Chris@0: #define AMPLITUDE (1.0 * 0x7F000000) Chris@0: #define LEFT_FREQ (344.0 / SAMPLE_RATE) Chris@0: #define RIGHT_FREQ (466.0 / SAMPLE_RATE) Chris@0: Chris@0: int Chris@0: main (void) Chris@0: { SNDFILE *file ; Chris@0: SF_INFO sfinfo ; Chris@0: int k ; Chris@0: int *buffer ; Chris@0: Chris@0: if (! (buffer = malloc (2 * SAMPLE_COUNT * sizeof (int)))) Chris@0: { printf ("Malloc failed.\n") ; Chris@0: exit (0) ; Chris@0: } ; Chris@0: Chris@0: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@0: Chris@0: sfinfo.samplerate = SAMPLE_RATE ; Chris@0: sfinfo.frames = SAMPLE_COUNT ; Chris@0: sfinfo.channels = 2 ; Chris@0: sfinfo.format = (SF_FORMAT_WAV | SF_FORMAT_PCM_24) ; Chris@0: Chris@0: if (! (file = sf_open ("sine.wav", SFM_WRITE, &sfinfo))) Chris@0: { printf ("Error : Not able to open output file.\n") ; Chris@0: return 1 ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.channels == 1) Chris@0: { for (k = 0 ; k < SAMPLE_COUNT ; k++) Chris@0: buffer [k] = AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI) ; Chris@0: } Chris@0: else if (sfinfo.channels == 2) Chris@0: { for (k = 0 ; k < SAMPLE_COUNT ; k++) Chris@0: { buffer [2 * k] = AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI) ; Chris@0: buffer [2 * k + 1] = AMPLITUDE * sin (RIGHT_FREQ * 2 * k * M_PI) ; Chris@0: } ; Chris@0: } Chris@0: else Chris@0: { printf ("makesine can only generate mono or stereo files.\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sf_write_int (file, buffer, sfinfo.channels * SAMPLE_COUNT) != Chris@0: sfinfo.channels * SAMPLE_COUNT) Chris@0: puts (sf_strerror (file)) ; Chris@0: Chris@0: sf_close (file) ; Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: