Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 5 Nov 1996 17:22:46 -0500 (EST)
From:      "John S. Dyson" <toor@dyson.iquest.net>
To:        gjm@hal2.soas.ac.uk (geoff martindale)
Cc:        freebsd-multimedia@freebsd.org
Subject:   Re: Rec/play on the SoundBlaster
Message-ID:  <199611052222.RAA01853@dyson.iquest.net>
In-Reply-To: <9611060312.AA11990@soas.ac.uk> from "geoff martindale" at Nov 5, 96 08:12:52 pm

next in thread | previous in thread | raw e-mail | index | archive | help
> 
> Hello,
> 
> Does anyone know of any tools/source code
> for doing record/playback on a SoundBlaster 16?
> We want to sample speech at 16kHz, linear PCM.
> Currently we can playback by using sox and
> 'cat speech.voc > /dev/dsp0'.
> 
> System - FreeBSD 2.1.5 and Pentium Pro
> 
Here is my "stupid" record program, and following,
my "stupid" play program, you can change the
parameters as needed, and note that these
programs only do raw file formats:


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


#define DSP "/dev/dsp"

int dspfd;
int filefd;

main(int argc, char *argv[]) {
	char buffer[256];
	int readcount;
	int toread;
	int parm;

	dspfd = open(DSP, O_RDONLY);
	if (dspfd < 0) {
		perror("dspopen");
		exit(1);
	}

	parm = 16;
	if (ioctl(dspfd, SOUND_PCM_READ_BITS, &parm)) {
		perror("ioctlrd16");
		exit(1);
	}

	parm = 16;
	if (ioctl(dspfd, SOUND_PCM_WRITE_BITS, &parm)) {
		perror("ioctlwr16");
		exit(1);
	}

	parm = 2;
	if (ioctl(dspfd, SOUND_PCM_READ_CHANNELS, &parm)) {
		perror("ioctlrd2");
		exit(1);
	}

	parm = 2;
	if (ioctl(dspfd, SOUND_PCM_WRITE_CHANNELS, &parm)) {
		perror("ioctlwr2");
		exit(1);
	}

	parm = 44100;
	if (ioctl(dspfd, SOUND_PCM_READ_RATE, &parm)) {
		perror("ioctlsp44100");
		exit(1);
	}

	parm = 44100;
	if (ioctl(dspfd, SOUND_PCM_WRITE_RATE, &parm)) {
		perror("ioctlsp44100");
		exit(1);
	}

	filefd = dup(1);

	toread = atoi(argv[1]) * 44100 * 4;
	fprintf(stderr,"%d samples\n", toread);
	while ( toread > 0) {
		readcount = sizeof buffer;
		if (readcount > toread)
			readcount = toread;
		readcount = read(dspfd, buffer, readcount);
		if (readcount < 0) {
			perror("dspread");
			exit(1);
		}		
		write(filefd, buffer, readcount);
		toread -= readcount;
	}

	exit(0);
}

Play:

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


#define DSP "/dev/dsp"

int dspfd;
int filefd;

main(int argc, char *argv[]) {
	char buffer[1024];
	int writecount;
	int toread;
	int parm;
	int sfptr;
	char *readb;
	struct stat statbuf;

	dspfd = open(DSP, O_RDWR);
	if (dspfd < 0) {
		perror("dspopen");
		exit(1);
	}
	
	parm = 16;
	if (ioctl(dspfd, SOUND_PCM_READ_BITS, &parm)) {
		perror("ioctlrd16");
		exit(1);
	}

	parm = 16;
	if (ioctl(dspfd, SOUND_PCM_WRITE_BITS, &parm)) {
		perror("ioctlwr16");
		exit(1);
	}

	parm = 2;
	if (ioctl(dspfd, SOUND_PCM_READ_CHANNELS, &parm)) {
		perror("ioctlrd2");
		exit(1);
	}

	parm = 2;
	if (ioctl(dspfd, SOUND_PCM_WRITE_CHANNELS, &parm)) {
		perror("ioctlwr2");
		exit(1);
	}

	parm = 44100;
	if (ioctl(dspfd, SOUND_PCM_READ_RATE, &parm)) {
		perror("ioctlsp44100");
		exit(1);
	}

	parm = 44100;
	if (ioctl(dspfd, SOUND_PCM_WRITE_RATE, &parm)) {
		perror("ioctlsp44100");
		exit(1);
	}

	sfptr = 0;
	while ( 1) {
		toread = read(0, buffer, sizeof buffer);
		if (toread <= 0)
			break;
		writecount = write(dspfd, buffer, toread);
		if (writecount < 0) {
			perror("dspwrite");
			exit(1);
		}		
	}

	exit(0);
}



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