Recognizes if input is ogg or not.
[xiph.git] / ao / doc / ao_example.c
blobf346c4042879fba8c7d1aadde084894d1bc0f431
1 /*
3 * ao_example.c
5 * Written by Stan Seibert - July 2001
7 * Legal Terms:
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.
14 * Function:
16 * This program opens the default driver and plays a 440 Hz tone for
17 * one second.
19 * Compilation command line (for Linux systems):
21 * gcc -lao -ldl -lm -o ao_example ao_example.c
25 #include <stdio.h>
26 #include <ao/ao.h>
27 #include <math.h>
29 #define BUF_SIZE 4096
31 int main(int argc, char **argv)
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;
42 /* -- Initialize -- */
44 fprintf(stderr, "libao example program\n");
46 ao_initialize();
48 /* -- Setup for default driver -- */
50 default_driver = ao_default_driver_id();
52 format.bits = 16;
53 format.channels = 2;
54 format.rate = 44100;
55 format.byte_format = AO_FMT_LITTLE;
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;
64 /* -- Play some stuff -- */
65 buf_size = format.bits/8 * format.channels * format.rate;
66 buffer = calloc(buf_size,
67 sizeof(char));
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)));
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;
77 ao_play(device, buffer, buf_size);
79 /* -- Close and shutdown -- */
80 ao_close(device);
82 ao_shutdown();
84 return (0);