Commit | Line | Data |
---|---|---|
21c4e167 H |
1 | /* |
2 | * | |
3 | * ao_example.c | |
4 | * | |
5 | * Written by Stan Seibert - July 2001 | |
6 | * | |
7 | * Legal Terms: | |
8 | * | |
9 | * This source file is released into the public domain. It is | |
10 | * distributed without any warranty; without even the implied | |
11 | * warranty * of merchantability or fitness for a particular | |
12 | * purpose. | |
13 | * | |
14 | * Function: | |
15 | * | |
16 | * This program opens the default driver and plays a 440 Hz tone for | |
17 | * one second. | |
18 | * | |
19 | * Compilation command line (for Linux systems): | |
20 | * | |
21 | * gcc -lao -ldl -lm -o ao_example ao_example.c | |
22 | * | |
23 | */ | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <ao/ao.h> | |
27 | #include <math.h> | |
28 | ||
29 | #define BUF_SIZE 4096 | |
30 | ||
31 | int main(int argc, char **argv) | |
32 | { | |
33 | ao_device *device; | |
34 | ao_sample_format format; | |
35 | int default_driver; | |
36 | char *buffer; | |
37 | int buf_size; | |
38 | int sample; | |
39 | float freq = 440.0; | |
40 | int i; | |
41 | ||
42 | /* -- Initialize -- */ | |
43 | ||
44 | fprintf(stderr, "libao example program\n"); | |
45 | ||
46 | ao_initialize(); | |
47 | ||
48 | /* -- Setup for default driver -- */ | |
49 | ||
50 | default_driver = ao_default_driver_id(); | |
51 | ||
52 | format.bits = 16; | |
53 | format.channels = 2; | |
54 | format.rate = 44100; | |
55 | format.byte_format = AO_FMT_LITTLE; | |
56 | ||
57 | /* -- Open driver -- */ | |
58 | device = ao_open_live(default_driver, &format, NULL /* no options */); | |
59 | if (device == NULL) { | |
60 | fprintf(stderr, "Error opening device.\n"); | |
61 | return 1; | |
62 | } | |
63 | ||
64 | /* -- Play some stuff -- */ | |
65 | buf_size = format.bits/8 * format.channels * format.rate; | |
66 | buffer = calloc(buf_size, | |
67 | sizeof(char)); | |
68 | ||
69 | for (i = 0; i < format.rate; i++) { | |
70 | sample = (int)(0.75 * 32768.0 * | |
71 | sin(2 * M_PI * freq * ((float) i/format.rate))); | |
72 | ||
73 | /* Put the same stuff in left and right channel */ | |
74 | buffer[4*i] = buffer[4*i+2] = sample & 0xff; | |
75 | buffer[4*i+1] = buffer[4*i+3] = (sample >> 8) & 0xff; | |
76 | } | |
77 | ao_play(device, buffer, buf_size); | |
78 | ||
79 | /* -- Close and shutdown -- */ | |
80 | ao_close(device); | |
81 | ||
82 | ao_shutdown(); | |
83 | ||
84 | return (0); | |
85 | } |