Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 23 Aug 1998 21:53:15 -0500
From:      Dan Nelson <dnelson@emsphone.com>
To:        David Kott <dakott@alpha.delta.edu>, System Administrator <igor@ns.online.samara.ru>
Cc:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: sound card programming
Message-ID:  <19980823215315.B24497@emsphone.com>
In-Reply-To: <Pine.BSF.3.96.980821185937.26738B-200000@kott.my.domain>; from "David Kott" on Fri Aug 21 19:03:25 GMT 1998
References:  <ABt6OtrODQ@ns.online.samara.ru> <Pine.BSF.3.96.980821185937.26738B-200000@kott.my.domain>

next in thread | previous in thread | raw e-mail | index | archive | help

--SLDf9lqlvOQaIe6s
Content-Type: text/plain; charset=us-ascii

In the last episode (Aug 21), David Kott said:
> On Fri, 21 Aug 1998, System Administrator wrote:
> > I need play file on the sound card and record to file from sound
> > card. Where I can find examples of source code for this ? I have
> > fbsd 2.2.6
> 
> Here is a slightly modified piece of source that I found on one of
> the mailing list archives that does precisely 1/2 of your request. 
> It was written by one of the FreeBSD gurus/core team (forgive me, but
> I don't remember precisely who) and did almost exactly what I wanted.

And here are mine; both play and listen default to 8khz, 8bit, mono,
but that's easy enough to change, either via the commandline options,
or changing the constants in the code.  Play will also play to the PC
speaker, if you use -s and have enabled the pca device in the kernel.

	-Dan Nelson
	dnelson@emsphone.com

--SLDf9lqlvOQaIe6s
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="listen.c"

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

main(int argc, char *argv[])
{
	int fd = open("/dev/dsp", O_RDONLY);
	int rate = 8012;
	int bits = 8;
	int channels = 1;
	int c;
	int len;
	char buf[1024];

	while ((c = getopt(argc, argv, "hr:b:c:")) != EOF)
	{
		switch (c)
		{
		    case 'r':
			rate = atoi(optarg);
			break;
		    case 'b':
			bits = atoi(optarg);
			break;
		    case 'c':
			channels = atoi(optarg);
			break;
			case '?':
			case 'h':
			printf ("listen -r rate -b bits -c channels\n");
			exit(1);
			break;
		}
	}

	ioctl(fd, SNDCTL_DSP_SETFMT, &bits);
	ioctl(fd, SOUND_PCM_WRITE_RATE, &rate);
	ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &channels);

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

--SLDf9lqlvOQaIe6s
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="play.c"

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

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]\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);
}

--SLDf9lqlvOQaIe6s--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message



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