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