Skip to content

Instantly share code, notes, and snippets.

@pyropeter
Created November 21, 2010 21:43
Show Gist options
  • Save pyropeter/709179 to your computer and use it in GitHub Desktop.
Save pyropeter/709179 to your computer and use it in GitHub Desktop.
My "Hello, OSS!" application
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <stropts.h>
#include <string.h>
#include <linux/soundcard.h>
//#include <soundcard.h>
/*
gcc -Wall sndtest.c -o sndtest && ./sndtest
old:
gcc -Wall -I/usr/lib/oss/include/sys/ sndtest.c -o sndtest && ./sndtest
*/
int main(int argc, char *argv[]) {
int tmp, tmp2, control, i, j;
int chunklen = 16 * 1024;
int fragsize = chunklen * 2;
int16_t * chunk = malloc(fragsize);
int dspfd;
//oss_audioinfo ai;
//ai.dev = -1;
long long samplesWritten = 0;
int channels = 1;
int rate = 44100;
int amplitude = 8000;
int frequency = 100;
if ((dspfd = open("/dev/dsp", O_WRONLY/* | O_NONBLOCK*/)) == -1) return EXIT_FAILURE;
//if (ioctl (dspfd, SNDCTL_ENGINEINFO, &ai) == -1) return EXIT_FAILURE;
//printf ("\nUsing audio engine #%d: %s\n\n", ai.dev, ai.name);
tmp = channels;
if (ioctl(dspfd, SNDCTL_DSP_CHANNELS, &tmp) == -1) return EXIT_FAILURE;
if (tmp != channels) return EXIT_FAILURE;
tmp = AFMT_S16_NE;
if (ioctl(dspfd, SNDCTL_DSP_SETFMT, &tmp) == -1) return EXIT_FAILURE;
tmp = rate;
if (ioctl(dspfd, SNDCTL_DSP_SPEED, &tmp) == -1) return EXIT_FAILURE;
if (tmp != rate) return EXIT_FAILURE;
for (i = 0 ; 1 ; i++) {
memset(chunk, 0, fragsize);
for (j = 0 ; j < chunklen ; j++) {
control = ((samplesWritten + j) % frequency) * 4 * amplitude / frequency;
chunk[j] = control < 2 * amplitude
? control - amplitude
: 3 * amplitude - control;
}
ioctl(dspfd, SNDCTL_DSP_GETODELAY, &tmp);
if (write(dspfd, chunk, fragsize) != fragsize) return EXIT_FAILURE;
ioctl(dspfd, SNDCTL_DSP_GETODELAY, &tmp2);
samplesWritten = samplesWritten + chunklen;
printf("Chunk: %3i, Delay: %5i, in s: %5.2f, II: %5i\n", i, tmp, (float)tmp / rate / 2, tmp2);
fflush(stdout);
}
close(dspfd);
return EXIT_SUCCESS;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment