-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo_fixed.c
101 lines (79 loc) · 2.53 KB
/
demo_fixed.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* Implements a simple square wave generator as might be used in an analog
waveform synthesizer. Runs two square waves together. */
#include "blip_buf.h"
#include "wave_writer.h"
#include <stdlib.h>
enum { sample_rate = 44100 };
/* Use the maximum supported clock rate, which is about one million times
the sample rate, 46 GHz in this case. This gives all the accuracy needed,
even for extremely fine frequency control. */
static const double clock_rate = (double) sample_rate * blip_max_ratio;
static blip_buffer_t* blip;
typedef struct wave_t
{
double frequency; /* cycles per second */
double volume; /* 0.0 to 1.0 */
int phase; /* +1 or -1 */
int time; /* clock time of next delta */
int amp; /* current amplitude in delta buffer */
} wave_t;
static wave_t waves [2];
static void run_wave( wave_t* w, int clocks )
{
/* Clocks for each half of square wave cycle */
int period = (int) (clock_rate / w->frequency / 2 + 0.5);
/* Convert volume to 16-bit sample range (divided by 2 because it's bipolar) */
int volume = (int) (w->volume * 65536 / 2 + 0.5);
/* Add deltas that fall before end time */
for ( ; w->time < clocks; w->time += period )
{
int delta = w->phase * volume - w->amp;
w->amp += delta;
blip_add_delta( blip, w->time, delta );
w->phase = -w->phase;
}
w->time -= clocks; /* adjust for next time frame */
}
/* Generates enough samples to exactly fill out */
static void gen_samples( short out [], int samples )
{
int clocks = blip_clocks_needed( blip, samples );
run_wave( &waves [0], clocks );
run_wave( &waves [1], clocks );
blip_end_frame( blip, clocks );
blip_read_samples( blip, out, samples, 0 );
}
static void init_sound( void )
{
blip = blip_new( sample_rate / 10 );
if ( blip == NULL )
exit( EXIT_FAILURE ); /* out of memory */
blip_set_rates( blip, clock_rate, sample_rate );
}
int main( void )
{
init_sound();
waves [0].phase = 1;
waves [0].volume = 0.0;
waves [0].frequency = 16000;
waves [1].phase = 1;
waves [1].volume = 0.5;
waves [1].frequency = 1000;
wave_open( sample_rate, "out.wav" );
while ( wave_sample_count() < 2 * sample_rate )
{
enum { samples = 1024 };
short temp [samples];
gen_samples( temp, samples );
wave_write( temp, samples );
/* Slowly increase volume and lower pitch */
waves [0].volume += 0.005;
waves [0].frequency *= 0.950;
/* Slowly decrease volume and raise pitch */
waves [1].volume -= 0.002;
waves [1].frequency *= 1.010;
}
wave_close();
blip_delete( blip );
return 0;
}