Hello all,
I'm struggling a bit with getting libao to work in C on Debian (Squeeze,
kernel 2.6.38.5, driver is Alsa). I've got this slightly modified test
script and it seems to work fine on my internal soundcard (it opens the
device and plays a test tone). However, when trying to do the same trick
on an external USB-dac (tried two different models), it gives an
"Input/output error". I've tried different settings, but as far as I can
see there is not really that much to set (especially if it plays on one
soundcard but not the other, dev=hw:1 should suffice).
Does anyone have a suggestion on how to get this to work?
Kind regards, Maarte
/*
*
* 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 -lao -ldl -lm -o ao_example ao_example.c
*
*/
#include <stdio.h>
#include <ao/ao.h>
#include <math.h>
#include <string.h>
#include <errno.h>
#define BUF_SIZE 4096
int main(int argc, char *argv[])
{
ao_device *device;
ao_sample_format format;
ao_option option;
int default_driver;
char *buffer;
int buf_size;
int sample;
float freq = 440.0;
int i;
char *option_key = "dev";
char *option_value = "hw:0";
option.key = option_key;
option.value = option_value;
option.next = NULL;
if (argc >= 1)
option.value = argv[1];
/* -- Initialize -- */
fprintf(stderr, "libao example program\n");
ao_initialize();
/* -- Setup for default driver -- */
default_driver = ao_default_driver_id();
char *format_matrix = "L,R";
format.matrix = format_matrix;
format.bits = 16;
format.channels = 2;
format.rate = 44100;
format.byte_format = AO_FMT_LITTLE;
/* -- Open driver -- */
device = ao_open_live(default_driver, &format, &option);
if (device == NULL) {
fprintf(stderr, "Error opening device: %s\n", strerror (errno));
return 1;
}
/* -- Play some stuff -- */
buf_size = format.bits/8 * format.channels * format.rate;
buffer = calloc(buf_size,
sizeof(char));
for (i = 0; i < format.rate; 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);
/* -- Close and shutdown -- */
ao_close(device);
ao_shutdown();
return (0);
}