8

How can I pipe any data to audio output? For example, i want to listen to a file -- an archive, a drive backup, a program. Or I want to listen to my HDD -- I vaguely remember reading something about this being possible about 7 years ago, but can't find anything now.

So, files, disk reads, even network connections -- I want to be able to listen to anything. I know that it's definitely possible with Linux. How can I do it? Using Lubuntu 20.04

2 Answers2

18

I find piping things into aplay works well.

journalctl | aplay doesn't sound pretty but does work surprisingly well.

Here's an example from aplay(1):

aplay -c 1 -t raw -r 22050 -f mu_law foobar
              will play the raw file "foobar" as a 22050-Hz, mono, 8-bit, Mu-Law .au file.

It can be found as part of the alsa-utils package on debian/ubuntu.

Here's a 1-liner that I like which echos a small C program into gcc, and runs the compiled version, piping it to aplay. The result is a surprisingly nice 15-minute repeating song.

echo "g(i,x,t,o){return((3&x&(i*((3&i>>16?\"BY}6YB6$\":\"Qj}6jQ6%\")[t%8]+51)>>o))<<4);};main(i,n,s){for(i=0;;i++)putchar(g(i,1,n=i>>14,12)+g(i,s=i>>17,n^i>>13,10)+g(i,s/3,n+((i>>11)%3),10)+g(i,s/5,8+n-((i>>10)%3),9));}"|gcc -xc -&&./a.out|aplay
Stewart
  • 12,628
  • 1
  • 37
  • 80
  • 17
    `Here's a 1-liner that I like [...]` Sooo ... just *how* long have you been in lockdown ?-) – TomRoche May 16 '20 at 20:49
  • 1
    @TomRoche you have to give it a bit of time, it starts out slow then it gets kind of catchy :) – Clumsy cat May 17 '20 at 10:55
  • 1
    I first read "... and pipes the compiled version to aplay", and wondered what compiler hacks you were using to get a compiled program sound nice. – Paŭlo Ebermann May 17 '20 at 15:47
  • Holy hell, it sounds amazing!!! Oh I'm so glad I asked this question. Have any more? – Роман Мавроян May 20 '20 at 17:35
  • @PaŭloEbermann actually he does not "pipe" the compiled version like `cat a.out |...`, he runs the program and pipe its output to aplay `./a.out |...` – bew Nov 11 '20 at 16:53
  • 1
    @bew yes, I got this on the second read, that's why I wrote "I first read". – Paŭlo Ebermann Nov 11 '20 at 19:40
  • > Got any more? This one improvises major scale notes using `urandom`: `cat /dev/urandom | hexdump -v -e '/1 "%u\n"' | awk '{ split("0,2,4,5,7,9,11,12",a,","); for (i = 0; i < 1; i+= 0.0001) printf("%08X\n", 100*sin(1382*exp((a[$1 % 8]/12)*log(2))*i)) }' | xxd -r -p | aplay -c 2 -f S32_LE -r 16000 ` – Stewart Dec 20 '20 at 20:47
  • `Cannot open output wave file aplay: read_header:2950: read error` – Salem F Jul 23 '22 at 17:27
11

It was possible with /dev/dsp, which is part of OSS, which hasn't been part of the Linux kernel a very long time. It used to be as easy as cat some_file >/dev/dsp or some_program >/dev/dsp.

PulseAudio provides padsp.

padsp starts the specified program and redirects its access to OSS compatible audio devices (/dev/dsp and auxiliary devices) to a PulseAudio sound server.

(source)


Examples:

  • random data

    </dev/urandom padsp tee /dev/dsp >/dev/null
    
  • regular file

    </etc/fstab padsp tee /dev/dsp >/dev/null
    
  • network activity

    sudo tcpdump | padsp tee /dev/dsp >/dev/null
    
  • block device

    sudo cat /dev/sda | padsp tee /dev/dsp >/dev/null
    

In my Ubuntu 18.04.4 LTS padsp is from the pulseaudio-utils package.

Kamil Maciorowski
  • 19,242
  • 1
  • 50
  • 94