Skip to content

Instantly share code, notes, and snippets.

@dheaney
Created August 31, 2013 18:13
Show Gist options
  • Select an option

  • Save dheaney/6399780 to your computer and use it in GitHub Desktop.

Select an option

Save dheaney/6399780 to your computer and use it in GitHub Desktop.
NBC sound with libao and C
/*
*
* nbc.c
*
* Modified by DJ Heaney - August 2013
* Original code (public domain) 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:
*
* Plays the famous NBC thing.
*
* Compilation:
*
* gcc -lao -ldl -lm -o nbc nbc.c
*
*/
#include <stdio.h>
#include <ao/ao.h>
#include <math.h>
#include <string.h>
#define BUF_SIZE 4096
int main(int argc, char **argv)
{
ao_device *device;
ao_sample_format format;
int default_driver;
char *buffer;
int buf_size;
int sample;
float freqs[3] = { 261.6, 440.0, 349.2 };
int i, j;
/* -- Initialize -- */
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;
}
/* -- Play some stuff -- */
buf_size = format.bits/8 * format.channels * format.rate;
buffer = calloc(buf_size,
sizeof(char));
for(j = 0; j < 3; j++) {
for (i = 0; i < format.rate; i++) {
sample = (int)(0.75 * 32768.0 *
sin(2 * M_PI * freqs[j] * ((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);
}
/* -- Close and shutdown -- */
ao_close(device);
ao_shutdown();
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment