-
Notifications
You must be signed in to change notification settings - Fork 2
/
little_star.c
174 lines (144 loc) · 3.51 KB
/
little_star.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
*
* ao_example.c
*
* Written by Stan Seibert - July 2001
*
* Legal Terms:
*
* This source file is released into the public domain. It is
* distributed without any warranty; without even the implied
* warranty * of merchantability or fitness for a particular
* purpose.
*
* Function:
*
* This program opens the default driver and plays a 440 Hz tone for
* one second.
*
* Compilation command line (for Linux systems):
*
* gcc -o ao_example ao_example.c -lao -ldl -lm
*
*/
#include <stdio.h>
#include <string.h>
#include <ao/ao.h>
#include <math.h>
#include <unistd.h>
#define BUF_SIZE 4096
#define A 440
#define B 494
#define C 261
#define D 294
#define E 329
#define F 349
#define G 392
void playTone(int freq, ao_device *device, float keep_time);
void playTone(int freq, ao_device *device, float keep_time)
{
char *buffer;
int sample;
int i;
int buf_size;
int play_bits;
ao_sample_format format;
memset(&format, 0, sizeof(format));
format.bits = 16;
format.channels = 2;
format.rate = 44100;
format.byte_format = AO_FMT_LITTLE;
play_bits = (int)(format.rate * keep_time);
buf_size = format.bits / 8 * format.channels * play_bits;
buffer = calloc(buf_size,
sizeof(char));
for (i = 0; i < play_bits; i++)
{
sample = (int)(0.75 * 32768.0 *
sin(2 * M_PI * freq * ((float)i / format.rate)));
/* Put the same stuff in left and right channel */
buffer[4 * i] = buffer[4 * i + 2] = sample & 0xff;
buffer[4 * i + 1] = buffer[4 * i + 3] = (sample >> 8) & 0xff;
}
ao_play(device, buffer, buf_size);
usleep(130000);
}
int main(int argc, char **argv)
{
ao_device *device;
ao_sample_format format;
int default_driver;
char *buffer;
int buf_size;
int sample;
float freq = 423.0;
int i;
/* -- Initialize -- */
fprintf(stderr, "libao example program\n");
ao_initialize();
/* -- Setup for default driver -- */
default_driver = ao_default_driver_id();
memset(&format, 0, sizeof(format));
format.bits = 16;
format.channels = 2;
format.rate = 44100;
format.byte_format = AO_FMT_LITTLE;
/* -- Open driver -- */
device = ao_open_live(default_driver, &format, NULL /* no options */);
if (device == NULL)
{
fprintf(stderr, "Error opening device.\n");
return 1;
}
playTone(C, device, 0.5);
playTone(C, device, 0.5);
playTone(G, device, 0.5);
playTone(G, device, 0.5);
playTone(A, device, 0.5);
playTone(A, device, 0.5);
playTone(G, device, 0.5);
usleep(300000);
playTone(F, device, 0.5);
playTone(F, device, 0.5);
playTone(E, device, 0.5);
playTone(E, device, 0.5);
playTone(D, device, 0.5);
playTone(D, device, 0.5);
playTone(C, device, 0.5);
usleep(300000);
playTone(G, device, 0.5);
playTone(G, device, 0.5);
playTone(F, device, 0.5);
playTone(F, device, 0.5);
playTone(E, device, 0.5);
playTone(E, device, 0.5);
playTone(D, device, 0.5);
usleep(300000);
playTone(G, device, 0.5);
playTone(G, device, 0.5);
playTone(F, device, 0.5);
playTone(F, device, 0.5);
playTone(E, device, 0.5);
playTone(E, device, 0.5);
playTone(D, device, 0.5);
usleep(300000);
playTone(C, device, 0.5);
playTone(C, device, 0.5);
playTone(G, device, 0.5);
playTone(G, device, 0.5);
playTone(A, device, 0.5);
playTone(A, device, 0.5);
playTone(G, device, 0.5);
usleep(300000);
playTone(F, device, 0.5);
playTone(F, device, 0.5);
playTone(E, device, 0.5);
playTone(E, device, 0.5);
playTone(D, device, 0.5);
playTone(D, device, 0.5);
playTone(C, device, 0.5);
/* -- Close and shutdown -- */
ao_close(device);
ao_shutdown();
return (0);
}