Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Jan 2010 10:57:17 -0600
From:      Dan Nelson <dnelson@allantgroup.com>
To:        Gary Kline <kline@thought.org>
Cc:        Glen Barber <glen.j.barber@gmail.com>, freebsd-questions@freebsd.org
Subject:   Re: any port use /dev/dsp directly?
Message-ID:  <20100114165717.GA5651@dan.emsphone.com>
In-Reply-To: <20100114024242.GA9744@thought.org>
References:  <20100114012059.GA3921@thought.org> <20100114013746.GB67999@orion.hsd1.pa.comcast.net> <20100114024242.GA9744@thought.org>

next in thread | previous in thread | raw e-mail | index | archive | help
In the last episode (Jan 13), Gary Kline said:
> On Wed, Jan 13, 2010 at 08:37:46PM -0500, Glen Barber wrote:
> > Gary Kline wrote: 
> > > 	I have a couple short programs where I mess with /dev/dsp.  I'll
> > > 	open check to be sure the speed is right, open in mono or stereo,
> > > 	&c.  is there anything is ports that uses this dev by opening, doing
> > > 	ioctls and so forth?
> > > 
> > > 	I think I may need to flush my data before closing the FILE *FP. Not
> > > 	sure; just guessing.
> > > 
> > 
> > I don't know if this directly answers your question, but from sound(4):
> > 
> > hw.snd.default_unit
> > 	Default sound card for systems with multiple sound cards.  When
> > 	using devfs(5), the default device for /dev/dsp.  Equivalent to a
> > 	symlink from /dev/dsp to /dev/dsp${hw.snd.default_unit}.
> > 
> > FWIW, www/linux-f10-flashplugin10 is using /dev/dsp0.0 on my system at the
> > moment.
> 
> Thanks, but I already read the sound man page.  I am trying to emulate 
> 
> /bin/cat WAVEFILE > /dev/dsp
> 
> which works well by opening /dev/dsp, making sure everything is set, the
> writing the bytes of the WAVEFILE thru/into the device with a write()
> call.  It works, the sound echoes, but at the end is an ugly HISSing or
> FIZZZZ sound.

You're probably playing an mp3-style tag at the end of the file, or some
other metadata encoded in the wav file format.  /dev/dsp takes raw bytes,
and doesn't parse a file headers at all.

A better way to play wav files would be to install the sox port and use its
included "play" command, which will parse the wav file format and only send
the audio data to /dev/dsp.  It'll also play compressed audio files (mp3, or
other non-raw wav encodings).

If you want a simple example of how to play a raw sound file, try this.  You
can tell its age by the fact that it can play through /dev/pcaudio, but it
still works :)


#include <sys/soundcard.h>
#include <machine/pcaudioio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int fd;
    int rate = 8012;
    int bits = 8;
    int speaker = 0;
    int channels = 1;
    int c;
    int len;
    char buf[1024];

    while ((c = getopt(argc, argv, "r:b:c:s")) != EOF)
    {
        switch (c)
        {
            case 's':
            speaker = 1;
            break;
            case 'r':
            rate = atoi(optarg);
            break;
            case 'c':
            channels = atoi(optarg);
            break;
            case 'b':
            bits = atoi(optarg);
            break;
            case '?':
            case 'h':
            printf ("play [-s] [-c channels] [-r rate] [-b bits] < audio_file\n");
            exit(1);
            break;
        }
    }

    if (speaker)
    {
        audio_info_t ait;
        ait.play.sample_rate = rate;
        ait.play.encoding = AUDIO_ENCODING_RAW;
        ait.play.gain = 150;
        ait.play.pause = -1;
        fd = open("/dev/pcaudio", O_WRONLY);
        ioctl(fd, AUDIO_SETINFO, &ait);    
    } else
    {
        fd = open("/dev/dsp", O_WRONLY);
        ioctl(fd, SNDCTL_DSP_SETFMT, &bits);
        ioctl(fd, SOUND_PCM_WRITE_RATE, &rate);
        ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &channels);
    }

    while ((len = read(fileno(stdin), buf, sizeof(buf))) > 0)
        write(fd, buf, len);

    return 0;
}


-- 
	Dan Nelson
	dnelson@allantgroup.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20100114165717.GA5651>