$NetBSD: patch-ab,v 1.3 2005/12/11 22:24:03 joerg Exp $

--- streams.c.orig	2000-05-23 18:08:00.000000000 +0000
+++ streams.c
@@ -36,7 +36,11 @@
 #ifdef HAVE_SYS_SOUNDCARD_H
 #include <sys/soundcard.h>
 #else
+#ifdef __NetBSD__
+#include <sys/audioio.h>
+#else
 #include <machine/soundcard.h>
+#endif /* __NetBSD__ */
 #endif
 #endif
 #include <sys/ioctl.h>
@@ -44,8 +48,6 @@
 #define BUF_SIZE BUF_LENGTH*2
 #define BUFLEN 1024*32
 
-extern int errno;
-
 FILE *ds;
 
 /* suggested legal sample rates */
@@ -77,11 +79,23 @@ void check_soundcard(void)
 {
         int format,stereo,speed,caps;
 #ifdef SOUNDCARD_SUPPORT
+
+#ifdef __NetBSD__
+	audio_info_t ctlinfo;
+#endif /* __NetBSD__ */
+
 	fprintf(stderr,"Initialising Soundcard\n");
 
+#ifdef __NetBSD__
+	if((g_conf.audioctl_fd=open(g_conf.netbsd_audioctl_file,O_RDWR))==-1)
+	        fatal("Failed to open audioctl device");
+	if((g_conf.audio_fd=open(g_conf.netbsd_audio_file,O_RDONLY))==-1)
+#else
 	if((g_conf.audio_fd=open(g_conf.sound_input_file,O_RDWR))==-1)
+#endif /* __NetBSD__ */
 	        fatal("Failed to open sound device");
 
+#ifndef __NetBSD__
         /* see if the card can do full_duplex */
         if(g_conf.full_duplex){
                 ioctl(g_conf.audio_fd, SNDCTL_DSP_GETCAPS, &caps);
@@ -97,7 +111,33 @@ void check_soundcard(void)
 			g_conf.full_duplex=0;
 		}
 	}
+#endif /* __NetBSD__ */
+
+#ifdef __NetBSD__
+	if(ioctl(g_conf.audioctl_fd, AUDIO_GETINFO, &(g_conf.ctlrestore)) < 0)
+		fatal("ioctl(AUDIO_GETINFO) failed");
+
+	/* in theory, we should pull ctlrestore.record.buffer_size and use
+	   that to malloc() our audio buffer, but in practice, this has 
+	   shown to be unneccesary.  This will minimize impact to the 
+	   original source and reduce the amount of patching we need.
+	*/
+
+	/* somewhere in here, we should be able to check/set full duplex,
+	   but I don't know enough about the internals of this driver yet.
+	*/
+	g_conf.full_duplex = 0;
+
+	AUDIO_INITINFO(&ctlinfo);
+	ctlinfo.record.sample_rate = g_conf.sample_rate;
+	ctlinfo.record.channels = (g_conf.stereo ? 2 : 1);
+	ctlinfo.record.precision = 16;
+	ctlinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
+	ctlinfo.mode = AUMODE_RECORD;
 
+	if(ioctl(g_conf.audioctl_fd, AUDIO_SETINFO, &ctlinfo) < 0)
+		fatal("ioctl(AUDIO_SETINFO) failed");
+#else
 	ioctl(g_conf.audio_fd, SNDCTL_DSP_GETFMTS, &format);
 	if(!(format&AFMT_S16_LE))
 		fatal("16bit mode not supported by driver");
@@ -113,6 +153,7 @@ void check_soundcard(void)
 	speed=g_conf.sample_rate;
 	if(ioctl(g_conf.audio_fd,SNDCTL_DSP_SPEED,&speed)==-1)
 		fatal("Speed Setting failed\n");
+#endif /* __NetBSD__ */
 
 	
 	fprintf(stderr,"16Bit %dHz ",g_conf.sample_rate);
@@ -121,8 +162,10 @@ void check_soundcard(void)
 	if(g_conf.full_duplex)
 	 	  fprintf(stderr,"Full Duplex ");
 	fprintf(stderr,"\n");
+#ifndef __NetBSD__
 	close(g_conf.audio_fd);
 	g_conf.audio_fd=0;
+#endif /* __NetBSD__ */
 #else
 	write_message("This executable Doesn't Support Soundcards",0);
 #endif
@@ -136,9 +179,10 @@ void open_soundcard(int flags){
 #ifdef SOUNDCARD_SUPPORT
        write_message("Opening Soundcard",1);
         
+#ifndef __NetBSD__
 	if((g_conf.audio_fd=open(g_conf.sound_input_file,flags))==-1)
 	        fatal("Failed to open sound device");
-	
+
         if(g_conf.full_duplex)
 #ifdef HAVE_SYS_SOUNDCARD_H
 	        ioctl(g_conf.audio_fd, SNDCTL_DSP_SETDUPLEX, 0);  
@@ -157,6 +201,7 @@ void open_soundcard(int flags){
 	speed=g_conf.sample_rate;
 	if(ioctl(g_conf.audio_fd,SNDCTL_DSP_SPEED,&speed)==-1)
 		fatal("Speed Setting failed\n");
+#endif /* __NetBSD__ */
 #else
 	write_message("This executable Doesn't Support Soundcards",0);
 #endif
@@ -166,8 +211,21 @@ void close_soundcard(void)
 {
 #ifdef SOUNDCARD_SUPPORT
 	write_message("Closing Soundcard",1);
+#ifdef __NetBSD__
+	close(g_conf.audio_fd);
+
+	/* In theory, we should reset the device.  In practice, this 
+	   segfaults.  I haven't figured out why.
+
+	if(ioctl(g_conf.audioctl_fd, AUDIO_SETINFO, &(g_conf.ctlrestore)) < 0)
+		fatal("ioctl(AUDIO_SETINFO) failed");
+	*/
+
+	close(g_conf.audioctl_fd);
+#else
 	ioctl(g_conf.audio_fd, SNDCTL_DSP_RESET, 0);
         close(g_conf.audio_fd);
+#endif /* __NetBSD__ */
 #else
 	write_message("This executable Doesn't Support Soundcards",0);
 #endif