annotate src/libsndfile-1.0.27/examples/make_sine.c @ 40:1df64224f5ac

Current libsndfile source
author Chris Cannam
date Tue, 18 Oct 2016 13:22:47 +0100
parents
children
rev   line source
Chris@40 1 /*
Chris@40 2 ** Copyright (C) 1999-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@40 3 **
Chris@40 4 ** All rights reserved.
Chris@40 5 **
Chris@40 6 ** Redistribution and use in source and binary forms, with or without
Chris@40 7 ** modification, are permitted provided that the following conditions are
Chris@40 8 ** met:
Chris@40 9 **
Chris@40 10 ** * Redistributions of source code must retain the above copyright
Chris@40 11 ** notice, this list of conditions and the following disclaimer.
Chris@40 12 ** * Redistributions in binary form must reproduce the above copyright
Chris@40 13 ** notice, this list of conditions and the following disclaimer in
Chris@40 14 ** the documentation and/or other materials provided with the
Chris@40 15 ** distribution.
Chris@40 16 ** * Neither the author nor the names of any contributors may be used
Chris@40 17 ** to endorse or promote products derived from this software without
Chris@40 18 ** specific prior written permission.
Chris@40 19 **
Chris@40 20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@40 21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
Chris@40 22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Chris@40 23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Chris@40 24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Chris@40 25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Chris@40 26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Chris@40 27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Chris@40 28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Chris@40 29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Chris@40 30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@40 31 */
Chris@40 32
Chris@40 33 #include <stdio.h>
Chris@40 34 #include <stdlib.h>
Chris@40 35 #include <string.h>
Chris@40 36 #include <math.h>
Chris@40 37
Chris@40 38 #include <sndfile.h>
Chris@40 39
Chris@40 40 #ifndef M_PI
Chris@40 41 #define M_PI 3.14159265358979323846264338
Chris@40 42 #endif
Chris@40 43
Chris@40 44 #define SAMPLE_RATE 44100
Chris@40 45 #define SAMPLE_COUNT (SAMPLE_RATE * 4) /* 4 seconds */
Chris@40 46 #define AMPLITUDE (1.0 * 0x7F000000)
Chris@40 47 #define LEFT_FREQ (344.0 / SAMPLE_RATE)
Chris@40 48 #define RIGHT_FREQ (466.0 / SAMPLE_RATE)
Chris@40 49
Chris@40 50 int
Chris@40 51 main (void)
Chris@40 52 { SNDFILE *file ;
Chris@40 53 SF_INFO sfinfo ;
Chris@40 54 int k ;
Chris@40 55 int *buffer ;
Chris@40 56
Chris@40 57 if (! (buffer = malloc (2 * SAMPLE_COUNT * sizeof (int))))
Chris@40 58 { printf ("Malloc failed.\n") ;
Chris@40 59 exit (0) ;
Chris@40 60 } ;
Chris@40 61
Chris@40 62 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 63
Chris@40 64 sfinfo.samplerate = SAMPLE_RATE ;
Chris@40 65 sfinfo.frames = SAMPLE_COUNT ;
Chris@40 66 sfinfo.channels = 2 ;
Chris@40 67 sfinfo.format = (SF_FORMAT_WAV | SF_FORMAT_PCM_24) ;
Chris@40 68
Chris@40 69 if (! (file = sf_open ("sine.wav", SFM_WRITE, &sfinfo)))
Chris@40 70 { printf ("Error : Not able to open output file.\n") ;
Chris@40 71 free (buffer) ;
Chris@40 72 return 1 ;
Chris@40 73 } ;
Chris@40 74
Chris@40 75 if (sfinfo.channels == 1)
Chris@40 76 { for (k = 0 ; k < SAMPLE_COUNT ; k++)
Chris@40 77 buffer [k] = AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI) ;
Chris@40 78 }
Chris@40 79 else if (sfinfo.channels == 2)
Chris@40 80 { for (k = 0 ; k < SAMPLE_COUNT ; k++)
Chris@40 81 { buffer [2 * k] = AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI) ;
Chris@40 82 buffer [2 * k + 1] = AMPLITUDE * sin (RIGHT_FREQ * 2 * k * M_PI) ;
Chris@40 83 } ;
Chris@40 84 }
Chris@40 85 else
Chris@40 86 { printf ("makesine can only generate mono or stereo files.\n") ;
Chris@40 87 exit (1) ;
Chris@40 88 } ;
Chris@40 89
Chris@40 90 if (sf_write_int (file, buffer, sfinfo.channels * SAMPLE_COUNT) !=
Chris@40 91 sfinfo.channels * SAMPLE_COUNT)
Chris@40 92 puts (sf_strerror (file)) ;
Chris@40 93
Chris@40 94 sf_close (file) ;
Chris@40 95 free (buffer) ;
Chris@40 96 return 0 ;
Chris@40 97 } /* main */
Chris@40 98