2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
4 ** All rights reserved.
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are
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
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.
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.
41 #define M_PI 3.14159265358979323846264338
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)
57 if (! (buffer
= malloc (2 * SAMPLE_COUNT
* sizeof (int))))
58 { printf ("Malloc failed.\n") ;
62 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
64 sfinfo
.samplerate
= SAMPLE_RATE
;
65 sfinfo
.frames
= SAMPLE_COUNT
;
67 sfinfo
.format
= (SF_FORMAT_WAV
| SF_FORMAT_PCM_24
) ;
69 if (! (file
= sf_open ("sine.wav", SFM_WRITE
, &sfinfo
)))
70 { printf ("Error : Not able to open output file.\n") ;
74 if (sfinfo
.channels
== 1)
75 { for (k
= 0 ; k
< SAMPLE_COUNT
; k
++)
76 buffer
[k
] = AMPLITUDE
* sin (LEFT_FREQ
* 2 * k
* M_PI
) ;
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
) ;
85 { printf ("makesine can only generate mono or stereo files.\n") ;
89 if (sf_write_int (file
, buffer
, sfinfo
.channels
* SAMPLE_COUNT
) !=
90 sfinfo
.channels
* SAMPLE_COUNT
)
91 puts (sf_strerror (file
)) ;