annotate src/libsndfile-1.0.27/examples/make_sine.c @ 168:ceec0dd9ec9c

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