Clear questions and runnable code
get the best and fastest answer
Answer: How can I play music like Basic's SOUND or PLAY?by igoryonya (Scribe)
|on Dec 07, 2009 at 16:18 UTC||Need Help??|
Hi, long time ago, I've made a music playing script. It generates sound waves then sends them to a sound card. I did it as an experiment that sound generation could be done in pure perl without external apps. I was determined to write my own pure perl mp3 player and this player was just the beginning of the project, which got stale for the lack of time, but it doesn't mean that I don't still want to eventually complete that. I actually want to try to create a pure perl mp3 decoder which will read in mp3 files, decode them internally without any external codec by just perl itself into waves and send them to the sound card.
In my script, I added a wave visualization, which you can disable by setting 0 on $displayWave variable; from the script and just leave the sound playing section.You can also save the output wave to a wav file. I have it commented out at the end.
It accepts a file with notes and plays them.
This prog accepts a file with notes. File spec is simple. Just type notes in sequence, separated by space. Notes are defined are written in the form:
(Note)(isSharp)(Octave)length in seconds or fractions of seconds, optional. Default = 1 secondNotes are: a b c d e f g
Sharp Notes are: a# c# d# f# g#An octave is appended to a note to define it:
a2 d#4In the above example were used a note from the octave 2 and d sharp note from the octave 4
You can specify their lengths in complete or fractional seconds. Fraction is defined by a slash and a seconds value:a2/2 d#4/.5 b5
In the above example: a note, octave, 2 seconds; d sharp note, 4th octave, half a second; b from the 5th octave, 1 second (the default, because the length was omitted).There is a p note, which is a pause
An example Music file:
The note frequencies are defined in FqC. They are probably not the frequencies used in real instruments, but I chose them by manually listening and testing sounds. I didn't have a time to look for the info on the real frequencies for each note used. If anybody knows the real frequencies for each note in each octave, they can be changed in that variable.