Music with aplay

15 Jan 2022


I decided to tinker with piping random files into aplay when I had a thought.

What if I programmatically outputted a correct number of characters to create a quarter note (.25 seconds)

Actually doing it?

I made a rust program and put an arbitrary amount of b's in there, 40 * 40 to be exact.

It produced a nice sounding tone that changed speeds between different sampling rates.

I figured out that to produce a 0.4 second tone in these I'd have to produce 40 characters / 50 Hz

This means that to produce a quarter note, I would need to produce 25 characters / 50 Hz.

I put in 25 characters per loop pass of an string append loop I had (one loop / 50 Hz)

I made it do 100 loops (5000/50 = 100) and piped it into aplay at 5 kHz S16_LE.

An (almost) perfect 0.255 second note.

Making it actually responsive

I had to allow the program to take input before I could have it produce perfect 0.25 notes for each hertz.

I added the code to auto divide by fifty and stuff to allow for the notes to actually be generated correctly.

How to use.

cargo run -q 5000 | aplay -r 5000

You can double the hertz that are seen by the rust program to make the note twice as long.

You can also half it and it halfs the note length.

I've uploaded a git repository for this little toy if you'd like to play around with it. It has some goodies in the songs folder if you'd like to have some songs pre-played to you like the Bit Waltz (the test song to see if it worked properly which it did!)

Sidenote

I've included an extra script in the songs directory called utility.sh.

This utility will take a notes actual frequency and converts it into the aplay frequency. This is cool!


All content on this website is licensed CC BY-ND 4.0 unless otherwise stated. Copyright © abbieoverflight 2021-2024
Powered by ewfm. This website features no AI-generated content.