[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Looking for C code reading /dev/dsp value



 > I want to measure the average amplitude value of an audio noise
 > spectrum put into the sound card of a laptop (I need it for automatic
 > noise bridge project to be built into tlf) . Could someone point me to
 > some SIMPLE C-code which does nothing but read one actual value from
 > /dev/dsp? Is there a register or memory location I could read? I am no
 > hardware expert, and I don't have time to become one :)

These fragments come from a digilogger program I built.  The odd
variable initialization made sense at the time.  It reads a 0.1 second
8kHz mu-law sample from the sound card in a loop.  OSS, of course.

I just walked through one section of the OSS Programmer's Guide
(http://www.opensound.com/pguide/oss.pdf) in a matter of 45 minutes or
so to build this thing.

Dennis Boone
KB8ZQZ

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <fcntl.h>

#define ABUFSIZE          800

...

    char afile[MAXSTR+1];               /* Audio device name */
    int sndfmt;                         /* Encoding of audio from */
    int channels;                       /* Number of channels to record */
    int speed;                          /* Sample rate of audio */
    int rc;                             /* Return value from subs */
    unsigned char abuf[ABUFSIZE];       /* Audio data buffer */

...

    sndfmt = AFMT_MU_LAW;
    channels = 1;
    speed = 8000;

...

/* Audio device setup */

    if ((afd = open(afile, O_RDONLY, 0)) == -1)
    {
        perror(afile);
        exit(errno);
    }

    if (ioctl(afd, SNDCTL_DSP_SETFMT, &sndfmt) == -1)
    {
        perror("mu law");
        exit(errno);
    }

    if (ioctl(afd, SNDCTL_DSP_CHANNELS, &channels) == -1)
    {
        perror("mono");
        exit(errno);
    }

    if (ioctl(afd, SNDCTL_DSP_SPEED, &speed) == -1)
    {
        perror("8000 sps");
        exit(errno);
    }

/* Main loop */

    while (1)
    {
        if ((rc = read(afd, abuf, ABUFSIZE)) == -1)
        {
            perror("audio read");
            exit(errno);
        }
    }



Reply to: