2
  • cat /dev/urandom generates a random sequence of all possibles "values".
  • cat /dev/urandom | padsp tee /dev/audio > /dev/null directs these "values" to your audio device, turning them into "random noise" or "random tones" (see: Generating random noise for fun in /dev/snd/)

But how can I do the same but instead of random noise/tones I pick a single value out of all possibles values and cat that to the audio device indefinitely (creating a sequence of the same value rather than random values)?

This should produce a single consistent tone.

Interface

You can manually experiment with different values, but one imaginary "interface" to make it easier to pick/hit the value you want could be:

  • Frequency (Hz) e.g. 440
  • Amplitude (0 - 1) e.g. 0.8

I'd rather not use an audio file, e.g. file.wav, file.mp3, file.ogg, etc. Just a bash script and defaultish cli applications (e.g. cat, padsp, etc).

01AutoMonkey
  • 145
  • 5

1 Answers1

4

You can play around with anything that can do maths (sin especially) and write a number as a character to stdout. For example:

awk --characters-as-bytes 'BEGIN { freq=2200; amp=0.3; for (i=0; i>=0; i++) { printf "%c", 127+ amp*(127.0*sin(2*3.14159265/44100*i*freq)); } }' | padsp tee /dev/audio > /dev/null

Depending on how you set freq it sounds more like a siren... perhaps that's something to play with, depending on your use case.

The amplitude is adjusted with amp, max 1.0.

Please note that I am using GNU awk, therefore --characters-as-bytes works. You do not want characters to be UTF-8 encoded when written to stdout!

Also, depending on your system you may want to replace 44100 by 48000 or a different number if the default sample rate differs.

Ned64
  • 8,486
  • 9
  • 48
  • 86
  • Can you make an interface/option for "volume" as well? Amplitude kind of functions as volume, but not exactly. In my testing amplitude kind of functions as volume from about 0.0 to 0.5, but beyond that it doesn't make much difference in terms of "perceived loudness". – 01AutoMonkey Mar 29 '20 at 00:09
  • @01AutoMonkey Funny, on my system the difference between 0.5 and 1.0 is very noticeable indeed! 1.0 is very loud, though, so unless you have your mixer tuned down a little you may hear distortions from clipping. – Ned64 Mar 29 '20 at 00:56