login
Header Space

 
 

Re: OpenBSD Sound

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Alexandre Ratchov <alex@...>
Cc: OpenBSD <misc@...>
Date: Saturday, November 3, 2007 - 8:40 am

On Wed, Oct 31, 2007 at 05:48:20PM +0100, Alexandre Ratchov wrote:

You don't need arts/esd/jack because of this. This can be solved in kernel.
The kernel opens the audio device for the highest common sampling rate
from those requested, or, if the rate cannot be switched without an
audible glitch, for the highest hardware available (48 or 96kHz, or
user-configured if it should be too much burden).

Then if the kernel has it 48 and the app opens 44.1, the kernel resamples.
According to Nyquist theorem, you first need to emulate the reconstruction
lowpass filter with 22.05kHz programatically, and then resample the
output stream at 48kHz and send it out. This requires an intermediate
stream at least common multiple, for this ugly case it's 2352 kHz. But
you actually don't have to shove data at 2352 kHz sampling rate in the
kernel, since you use only every 49th sample for the output. So some nifty
math or multipass filter does the job, for the expense of your brain exploding.

Now how the lowpass is done. It has to be really sharp otherwise you get
an 8-bit-era-like ringing in the sound. And it must not have large delay
(you don't want to hear the explosion in your Quake a second later) and
also not computationally expensive.

The suitable type of filter is called finite impulse response (FIR) and
it's just a naiive convolution with a short kernel. Now how to calculate
this kernel to get the best response? You make your kernel and imagine
it's cyclically wrapped. Then you calculate through FFT the ideal response
into it - that's perfectly sharp. But now since the response won't be
cyclically wrapped but occurs just once in the time and have zero strecthing
into both infinities, we have to fix this.

You take a Hann window http://en.wikipedia.org/wiki/Hann_window and apply that
and you got it. Hann window is just one cycle of a sine wave plus minus some
pushing around. You don't even need a sin for this, you can calculate it with
complex multiplication of one pre-calculated complex number.

How long do you make your kernel? The longer the kernel, the more
computationally intensive, but the sharper the transition so you lose less of
the high frequencies.

mplayer has to do this stuff all the time so it's full of this code. It does it
not only to accomodate for various sample rates, but also when you slow down or
speed up your video.  Maybe the code could be taken from mplayer. 

CL<

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
OpenBSD Sound, Samuel Proulx, (Wed Oct 31, 9:17 am)
Re: OpenBSD Sound, Karel Kulhavy, (Sat Nov 3, 8:09 am)
Re: OpenBSD Sound, Jacob Meuser, (Sat Nov 3, 4:38 pm)
Re: OpenBSD Sound, Tomas Bodzar, (Wed Oct 31, 10:51 am)
Re: OpenBSD Sound, Brian A Seklecki (Mobile)..., (Wed Oct 31, 11:06 am)
Re: OpenBSD Sound, Jacob Meuser, (Wed Oct 31, 5:30 pm)
Re: OpenBSD Sound, Nick Guenther, (Wed Oct 31, 11:23 am)
Re: OpenBSD Sound, Alexandre Ratchov, (Wed Oct 31, 12:48 pm)
Re: OpenBSD Sound, Karel Kulhavy, (Sat Nov 3, 8:40 am)
Re: OpenBSD Sound, Alexandre Ratchov, (Sat Nov 3, 12:36 pm)
Re: OpenBSD Sound, Stuart Henderson, (Sat Nov 3, 10:21 am)
Re: OpenBSD Sound, Karel Kulhavy, (Sat Nov 3, 11:40 am)
False OpenBSD Sound, chefren, (Sat Nov 3, 6:14 pm)
Re: OpenBSD Sound, Jussi Peltola, (Sat Nov 3, 12:29 pm)
Re: OpenBSD Sound, Edd Barrett, (Sat Nov 3, 9:44 am)
Re: OpenBSD Sound, Karel Kulhavy, (Sat Nov 3, 10:23 am)
Re: OpenBSD Sound , Theo de Raadt, (Sat Nov 3, 12:03 pm)
Re: OpenBSD Sound, Steve Shockley, (Sun Nov 4, 1:55 pm)
Re: OpenBSD Sound, Ted Unangst, (Wed Oct 31, 12:40 pm)
Re: OpenBSD Sound, Nick Guenther, (Wed Oct 31, 10:47 am)
Re: OpenBSD Sound, Karel Kulhavy, (Sat Nov 3, 8:21 am)
Re: OpenBSD Sound, Samuel Proulx, (Wed Oct 31, 11:11 am)
Re: OpenBSD Sound, Benjamin M. A'Lee, (Wed Oct 31, 10:19 am)
speck-geostationary