Play a WAV through /dev/console

0

Is there a way to play a WAV through /dev/console? (without being root)

The same way the shell command

/dev/urandom > /dev/console

produces a random sound

cat music.wav > /dev/console

does not work.

I can use /dev/dsp but the sound does not come from the internal PC speaker.

anonymous

Posted 2011-04-03T15:37:44.517

Reputation: 11

It's sounding like you might want to play a sound file located on one system remotely on another system, is this the case? – LawrenceC – 2012-03-28T01:19:04.693

Answers

2

TL; DR

No, it's not possible. Use aplay or mplayer.

Why not?

There are two problems with your idea. The first is that writing to /dev/console results in the written characters showing up in the system console, which is commonly used for showing error notifications to the user (I believe), and not for playing sounds. If you somehow got sound from writing to it, I would guess it is due to the 1/256 chance of every character being , which should make a short beep.

The second problem is that a .wav is not a raw, unambiguous audio stream, even if it is less complex and (generally) not compressed like many other formats. In fact (iirc) there can never be such a thing as a raw, unambiguous audio stream. You will always (at the very least) need a header specifying sample resolution and rate in order to do anything useful with the data. I would be very surprised if it was decided that one of the responsibilities of the Linux kernel was to decode various audio formats. It is typically a thing best done by a program in userspace.

Digression:

The Windows 9x line (at least part of it) did image decoding in the kernel for icons. That turned out to be a less-than-stellar idea, and caused many, many bluescreens.

Proposed solution

If you wanted to play your wav from the command line, there are a range of programs that will decode the file for you and communicate it to the kernel, generally using the ALSA interface. I suggest aplay, which is available on most systems and is rather simplistic, or mplayer which is also widely distributed, but with far larger complexity. If your system supports sound, chances are at least one of those are installed.

Eroen

Posted 2011-04-03T15:37:44.517

Reputation: 5 615

Actually, .wav does support compression. It just isn't used often. – Ignacio Vazquez-Abrams – 2012-03-27T22:15:50.587

Yes, yes it does. My point being, it is not to audio as utf-8 plain files are to text. – Eroen – 2012-03-27T22:25:51.433

0

it should be possible but wether you'd agree to the quality produced is another story.

first of all a wav is pulse code modulated (pcm), the pc speaker is pretty much an on-or-off story so there are 2 options... analyze the frequency the wav aims to archieve (gonna be hard if there are multiple frequencies at once) then make the speaker reproduce that frequency, or live with the fact that it can only be 'on' or 'off' and just 'turn it on or off' if the data in the wav reaches a certain pre-set volume/pcm threshold.

also the wav would have a much higher samplerate than the speaker hardware probably can handle (not sure on that, but not being able to set other amplitudes than just 'as much voltage as the thing can handle' or 'zero' with nothing in between is the bigger problem here.

now... the things you want to look into for method 1 is ioctl(x,KIOCSOUND,1193180/desired-freq-in-hz); with 0 obviously being off. KDMKTONE ioctl on an fd on /dev/console seems to be supposed to produce specified tones for a period specified in jiffies.

but if you want to do it properly i'd pick up the datasheet of the chip the speaker is connected to and just address the thing directly without even bothering with /dev/console or any of the linux stuff... so you can indeed, just turn on the voltage, or turn it off. which would give a pcm modulated sound... just without any amplitude variation between 'loud' and 'nothing'.

probably all methods sound like crap anyway...but analyzing the frequency first and then playing back a frequency won't work for music which usually contains multiple frequencies at the same time.

the whole ioctl KIOCSOUND/KDMKTONE seem to rely on the clocktick settings in the kernel, as well as the being present of a /dev/console and a file descriptor to that being open as well. they don't seem to 'just address the i/o pin of the chip the pc beeper is connected to'.

programmable interrupt timer (PIT) intel 8259 i/o port $0042 r/w PIT counter 2, cassette & speaker (XT, AT, PS/2)

basically the usual approach would be to program it with a desired frequency and then it would produce beeps but what you'd want is just 'on' or 'off' so basically 0hz or very low hz, then reprogram the PIT when the data from the wav goes from '1' to 0 to turn it off again... and so on. handling the sample rate of the wav file is up to software like anything else. (take the average or just skip samples if it's too high - it's analog anyway, and you can only produce digital output ;)

HRH Sven Olaf von CyberBunker

Posted 2011-04-03T15:37:44.517

Reputation: 21

it's actually pretty much the same as producing pcm(wav/mp3) data on pretty much any 8 bit microcomputer's sound chip, as those too were usually made to produce -frequencies- not -volumes- ...(just that those -can- produce other voltages than +5 or whatever and 0 volt - as they do have full DAC's - just a bit too much other hardware to do what -you- want with them nowadays ;) – HRH Sven Olaf von CyberBunker – 2015-12-23T19:55:31.330

i'd say it can be done. but it's gonna sound like doing voip over 300 baud. :P – HRH Sven Olaf von CyberBunker – 2015-12-23T20:07:37.527

to give an idea: i remember that that speech intro actually was played over the pc speaker (we never had soundcards/adlib ;) https://www.youtube.com/watch?v=2WlUfUBosgs

– HRH Sven Olaf von CyberBunker – 2015-12-23T20:10:27.323