/piano

Primary LanguagePerl

Piano

A low dependency terminal piano player writen in Perl.

Many perl sound examples I have come across have needed /dev/dsp/ support. This requires OSS compatibility support and is not automatically available in my distro. Audio::DSP fails. While it can be installed, I have explored ways I may bypass this. padsp is one option. It can be used to start a program to give it OSS compatibility and access to /dev/dsp etc.

In Attempt 1 Twinkle.pl. I have combined some code found in a code golf site, and combined it with a pipe into padsp and then tee to a /dev/dsp. (see refs below). A previous bug had been that the data in the piano aplication would only produce outputs on alternate calles. It appears that the piping/processing of the sounds waits until 1024 bytes are sent...

open (my $dsp,"|padsp tee /dev/audio > /dev/null") or   die qq(Couldn't execute for piping);

Attempt 2 maps 96 key piano to notes and frequencies and a sinewave generator

SampleMaker (Attempt 3)

  • Draws the piano and notes with reference a keyboard.
  • creates and saves a sample file so does not need to be recreated (using storable)
  • outputs keys data as keys are pressed ( key pressed, note represented, and frequency) as debug.
  • realised that octaves dont go A..G, but C..B !!
  • works but not for the A keys!!!..Help!!

Final version PIANO.pl

(Enable sound in the video)

PIANO.webm

Frequency to data conversion

key to frequency converter

Number of keys to middleA= keys_to_A4 Frequency of middle A (note A4, key keys_to_A4) is 440hz; all other keys can be derived from this freqency=440 * 2 ** ((KeyNumber - keys_to_A4)/12)

simple sin wave

$sps=8000       #  sample size
$resolution=128 #  peak to peak
@sample_data=map {$resolution *  $_ *  2 * $freq * pi/$sps} (0..$sps-1)

Resources

Twinkle Twinkle Little Star, Code Golf

padsp

Check out morsify

Pure Perl Beep using ioctl

Music notes