| 1 |
/* |
| 2 |
** Copyright (C) 1999-2012 Erik de Castro Lopo <[email protected]> |
| 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 ("Error : Malloc failed.\n") ; |
| 59 |
return 1 ; |
| 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 |
free (buffer) ; |
| 72 |
return 1 ; |
| 73 |
} ; |
| 74 |
|
| 75 |
if (sfinfo.channels == 1) |
| 76 |
{ for (k = 0 ; k < SAMPLE_COUNT ; k++) |
| 77 |
buffer [k] = (int) (AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI)) ; |
| 78 |
} |
| 79 |
else if (sfinfo.channels == 2) |
| 80 |
{ for (k = 0 ; k < SAMPLE_COUNT ; k++) |
| 81 |
{ buffer [2 * k] = (int) (AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI)) ; |
| 82 |
buffer [2 * k + 1] = (int) (AMPLITUDE * sin (RIGHT_FREQ * 2 * k * M_PI)) ; |
| 83 |
} ; |
| 84 |
} |
| 85 |
else |
| 86 |
{ printf ("Error : make_sine can only generate mono or stereo files.\n") ; |
| 87 |
sf_close (file) ; |
| 88 |
free (buffer) ; |
| 89 |
return 1 ; |
| 90 |
} ; |
| 91 |
|
| 92 |
if (sf_write_int (file, buffer, sfinfo.channels * SAMPLE_COUNT) != |
| 93 |
sfinfo.channels * SAMPLE_COUNT) |
| 94 |
puts (sf_strerror (file)) ; |
| 95 |
|
| 96 |
sf_close (file) ; |
| 97 |
free (buffer) ; |
| 98 |
return 0 ; |
| 99 |
} /* main */ |
| 100 |
|