Alsa-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Florian Schmidt <mista.tapas@gmx.net>
To: alsa-devel@alsa-project.org
Subject: possible fix for alsa-oss fopen() [Patch]
Date: Fri, 11 Jun 2004 01:27:53 +0200	[thread overview]
Message-ID: <20040611012753.360c48ae@mango.fruits.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 1530 bytes --]


Hi,

i attached to this email a patch which blatantly rips parts out of the  artsdsp program from the KDE artsd. Namely the file stdioemu.c which provides the mechanism to hook fopen and related functions. See the email by James Courtier-Dutton with subject "Possible solution for the alsa-oss fopen problem" for details. The changes to alsa-oss.c are minimal. See patch..

I also uploaded a tarball to 

http://www.affenbande.org/~tapas/alsa-oss-1.0.5-artsdsp-rip.tar.bz2

The relevant parts of artsdsp.c and stdioemu.c are both LGPL. I copied the file stdioemu.c unchanged including the copyright notice.

Please test it and tell me, if it works for you.. A test case is the following command:

aoss sox ~/test.wav -t ossdsp -w -s /dev/dsp

and define your pcm.dsp0 device in the .asoundrc to something which differs from normal output. I use a ladspa plugin for this:

pcm.dsp0 {type plug slave.pcm "ladspa"}
pcm.ladspa { 
	type ladspa 
	slave.pcm "plughw:0,0"  
	path "/usr/lib/ladspa" 
	plugins [ {label delay_5s  input { controls [0.8 0.3] } } ] 
}

you need the delay_5s plugin for this test, of course.. Now, with the attached patch i get a nice delay on my test.wav playback with sox.. With the original alsa-oss, sox is not interceptable and therefore does not play the delay. Teamspeak seems to work with aoss now, too...

If your soundcard does not do hw mixing, a good test case is to use 

pcm.dsp {type plug slave.pcm "dmix"} 

and try two instances of the aoss sox command..

Florian Schmidt

-- 
Palimm Palimm!


[-- Attachment #2: alsa-oss-fopen.patch --]
[-- Type: application/octet-stream, Size: 4622 bytes --]

diff -uNr alsa-oss-1.0.5/alsa/alsa-oss.c alsa-oss-1.0.5.new/alsa/alsa-oss.c
--- alsa-oss-1.0.5/alsa/alsa-oss.c	2004-02-26 19:34:58.000000000 +0100
+++ alsa-oss-1.0.5.new/alsa/alsa-oss.c	2004-06-11 00:16:36.000000000 +0200
@@ -73,6 +73,8 @@
 void *(*_mmap)(void *addr, size_t len, int prot, int flags, int fd, off_t offset);
 int (*_munmap)(void* addr, size_t len);
 
+FILE *(*_fopen)(const char *path, const char *mode);
+
 typedef struct ops {
 	int (*close)(int fd);
 	ssize_t (*write)(int fd, const void *buf, size_t n);
@@ -630,6 +632,24 @@
 	return count1;
 }
 
+static int is_sound_device(const char *pathname)
+{
+	if(!pathname) return 0;
+	if(strcmp(pathname,"/dev/dsp") == 0) return 1;
+	if(strcmp(pathname,"/dev/sound/dsp") == 0) return 1;
+	return 0;
+}
+
+
+#include "stdioemu.c"
+
+FILE *fopen(const char* path, const char *mode) {
+	if(!is_sound_device(path)) 
+		return _fopen (path, mode);
+	
+	return fake_fopen(path, mode);
+}
+
 #if 1
 # define strong_alias(name, aliasname) \
   extern __typeof (name) aliasname __attribute__ ((alias (#name)));
@@ -643,6 +663,7 @@
 strong_alias(munmap, __munmap);
 strong_alias(poll, __poll);
 strong_alias(select, __select);
+strong_alias(fopen, __fopen);
 #else
 int dup(int fd)
 {
@@ -712,4 +733,5 @@
 	_munmap = dlsym(RTLD_NEXT, "munmap");
 	_select = dlsym(RTLD_NEXT, "select");
 	_poll = dlsym(RTLD_NEXT, "poll");
+	_fopen = dlsym(RTLD_NEXT, "fopen");
 }
diff -uNr alsa-oss-1.0.5/alsa/stdioemu.c alsa-oss-1.0.5.new/alsa/stdioemu.c
--- alsa-oss-1.0.5/alsa/stdioemu.c	1970-01-01 01:00:00.000000000 +0100
+++ alsa-oss-1.0.5.new/alsa/stdioemu.c	2004-06-10 23:56:40.000000000 +0200
@@ -0,0 +1,96 @@
+    /*
+
+    Copyright (C) 2000 Stefan Westerfeld
+                       stefan@space.twc.de
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Library General Public
+    License as published by the Free Software Foundation; either
+    version 2 of the License, or (at your option) any later version.
+  
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Library General Public License for more details.
+   
+    You should have received a copy of the GNU Library General Public License
+    along with this library; see the file COPYING.LIB.  If not, write to
+    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+    Boston, MA 02111-1307, USA.
+
+    */
+
+/*
+ * This source only exists because some very special programs think that
+ * it is a very special idea to access /dev/dsp by the means of stdio, so
+ * we need to fake FILE* access for artsdsp as well.
+ *
+ * To do so, it relies on glibc internals, so that it will probably not work
+ * on other systems - but then again, it might not be necessary on other
+ * systems, when fopen properly calls open, it might as well work unchanged.
+ */
+
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include <stdio.h>
+#include <libio.h>
+
+struct fd_cookie {
+	int fd;
+};
+
+static ssize_t fdc_read (void *cookie, char *buffer, size_t size)
+{
+	struct fd_cookie *fdc = (struct fd_cookie *)cookie;
+	return read(fdc->fd, buffer, size);
+}
+
+static ssize_t fdc_write (void *cookie, const char *buffer, size_t size)
+{
+	struct fd_cookie *fdc = (struct fd_cookie *)cookie;
+	return write(fdc->fd, buffer, size);
+}
+
+static int fdc_seek (void* cookie, off64_t* position, int whence)
+{
+	return -1;
+}
+
+static int fdc_clean (void *cookie)
+{
+	struct fd_cookie *fdc = (struct fd_cookie *)cookie;
+	int result = close(fdc->fd);
+	free(cookie);
+	return result;
+}
+
+static FILE *fake_fopen(const char *path, const char *mode)
+{
+	cookie_io_functions_t fns = { fdc_read, fdc_write, fdc_seek, fdc_clean };
+	struct fd_cookie *fdc =
+		(struct fd_cookie *)malloc(sizeof(struct fd_cookie));
+	const char *mptr;
+	int open_mode = 0;
+	FILE *result = 0;
+
+	for(mptr = mode; *mptr; mptr++)
+	{
+		if(*mptr == 'r') open_mode |= 1; /* 1 = read */
+		if(*mptr == 'w') open_mode |= 2; /* 2 = write */
+		if(*mptr == '+') open_mode |= 3; /* 3 = readwrite */
+		if(*mptr == 'a') open_mode |= 2; /* append -> write */
+  	}
+  	if(open_mode == 1) fdc->fd = open(path,O_RDONLY,0666);
+  	if(open_mode == 2) fdc->fd = open(path,O_WRONLY,0666);
+  	if(open_mode == 3) fdc->fd = open(path,O_RDWR,0666);
+
+	if(open_mode && fdc->fd > 0)
+	{
+		result = fopencookie (fdc,"w", fns);
+		result->_fileno = fdc->fd;		/* ugly patchy slimy kludgy hack */
+	}
+	return result;
+}

             reply	other threads:[~2004-06-10 23:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-10 23:27 Florian Schmidt [this message]
2004-06-11  0:13 ` possible fix for alsa-oss fopen() [Patch] Florian Schmidt
2004-06-11 18:06 ` Florian Schmidt
2004-06-14 12:55   ` Florian Schmidt
2004-06-15 17:24     ` Florian Schmidt
2004-06-16  9:32       ` Takashi Iwai
2004-06-16 10:19         ` Florian Schmidt
2004-06-25 16:27           ` Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040611012753.360c48ae@mango.fruits.de \
    --to=mista.tapas@gmx.net \
    --cc=alsa-devel@alsa-project.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox