include <stdio.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/soundcard.h>

#define RATE 8000

double sol_fa[] = {880.0, 987.0, 523.0, 587.0, 659.0, 698.0, 784.0};

void play(int fd, const char *str){
    int i, j;
    unsigned char buf[RATE];
    double freq;
    int len = strlen(str);
    for(i=0; i<len; i++){
        if('a' <= str[i] && str[i] <= 'g')
            freq = sol_fa[str[i] - 'a'];
        else
            freq = 0;
        for (j = 0;j < RATE; j++)
            buf[j] = 255 * sin(2.0 * M_PI * freq * j / RATE);
        write(fd, buf, RATE);
    }
}

int main( void )
{
    int fd;
    int fmt = AFMT_U8;
    int channels = 1;
    int rate = RATE;

    fd = open("/dev/dsp", O_WRONLY);
    ioctl(fd, SOUND_PCM_SETFMT, &fmt);
    ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &channels);
    ioctl(fd, SOUND_PCM_WRITE_RATE, &rate);
    play(fd, "cdefedcrefgagfercrcrcrcrcdefedcr");
    close(fd);
    return 0;
}
