[music-dsp] mac osx [or other os?] sound?

douglas irving repetto douglas at music.columbia.edu
Mon Jan 8 18:14:05 EST 2001


"Matt J. Ingalls" wrote:

> im about to start a new project [a performance instrument for personal use
> only] and trying to determine if its best to continue on with BeOS or port
> my existing stuff to MacOS/ASIO, OSX Beta, or Linux [which i know nothing
> about]
> 
> what i want is to have in under 10 lines of code [like BeOS "old" media
> kit] setup an input and output callback that i can just read/write
> buffers (of n-channels)
> 


i've found linux to be quite usable, especially for personal performance
type projects. as far as releasing stuff that large numbers of other
people are supposed to be able to use easily, i have my doubts. but for
my own stuff it's been great, not as cozy as the BeOS media apis were,
but just fine nonetheless. here are 100 lines that will do a simple pass
through on linux. most of the code is setting up the soundcard, which
you do in pretty much exactly the same way every time, so i usually
stick it in a function library somewhere. this is the just super raw
version.

so i'd say linux is definitely good for diy projects. the primary reason
i'm interested in what's happening MaxOSX-wise is the built-in hardware
compatibility that comes along with it. that's one place where linux is
still a big pain in the butt. 


.d



/*

soundthru.c

gcc soundthru.c -o soundthru 
should compile cleanly


*/

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

#define BUFFER_SIZE 1024
#define FORMAT AFMT_S16_LE
#define CHANNELS 1 // 0=mono 1=stereo
#define SAMPLE_RATE 44100

int soundthru()
{
	//file i/o
	short* in_ptr;
	int bytes_read = 0;
	int bytes_written = 0;
	int handle;
	int channels;
	int format;
	int sample_rate;
	int setting;
	short read_size;
	handle = -1;
	//format info
	channels = CHANNELS;
	format = FORMAT;
	sample_rate = SAMPLE_RATE;
	setting = 0x0003000C; // 3 fragments 4kb buffer
	//1024 * 2 channels * 2 bytes per sample
	read_size = BUFFER_SIZE * 2 * 2L;
	
	if ( (handle = open("/dev/dsp",O_RDWR))  == -1 ) {
		perror("open /dev/dsp");
		return -1;
	}
	
	if ( ioctl(handle, SNDCTL_DSP_SETFRAGMENT, &setting) == -1 ) {
		perror("ioctl set fragment");
		return errno;
	}
	if ( ioctl(handle, SNDCTL_DSP_STEREO, &channels) == -1 ) {
		perror("ioctl stereo");
		return errno;
	}
	if ( ioctl(handle, SNDCTL_DSP_SETFMT, &format) == -1 ) {
		perror("ioctl format");
		return errno;
	}
	if ( ioctl(handle, SNDCTL_DSP_SPEED, &sample_rate) == -1 ) {
		perror("ioctl sample rate");
		return errno; 
	}

	in_ptr = (short*)malloc(read_size);
	memset(in_ptr, 0, read_size);

	while (1)
	{	
		
		//get input samples
		bytes_read = read(handle, in_ptr, read_size);
		if (bytes_read < 0)
		{
			printf("problem reading input. goodbye.\n");
			return -1;
		}
		else if (bytes_read < read_size)
			printf("read: %d of %d\n", bytes_read, read_size);
			
		//write them back out
		bytes_written = write(handle, in_ptr, read_size);
		if (bytes_written < 0)
		{
			printf("problem writing output. goodbye.\n");
			return -1;
		}
		else if (bytes_written < read_size)
			printf("wrote: %d of %d\n", bytes_read, read_size);
	}
 
	return 0; 
}

int main()
{
	soundthru();
	return 0; 
 }




-- 
                        douglas irving repetto 
                  http://music.columbia.edu/~douglas 
                  http://shoko.calarts.edu/musicdsp
               http://music.columbia.edu/cmc/dorkbotnyc

dupswapdrop -- the music-dsp mailing list and website: subscription info,
FAQ, source code archive, list archive, book reviews, dsp links
http://shoko.calarts.edu/musicdsp/




More information about the music-dsp mailing list