if 0 {Richard Suchenwirth 2002-09-01 - Everything is a string - so why not music? Last summer I demonstrated how a simple, ASCII-based (string) input language can be interpreted and rendered as notes on a canvas. But music is most of all auditory - one wants to hear it too. As every note allows calculation of its pitch (frequency) and length in time units, it is comparably easy to convert a note-string to instructions for sound output, e.g. with Snack. One can also directly link mouseclicks to sound output, as in A toy piano - if playing with one stiff finger is enough for you... What was missing still, was the ability to "quantize" the piano keyclicks, to determine their length in terms of eighth/quarter/half/full bar note, which is done in the code below. Then one has the possibility of clicking a melody together, recording the quantized notes, editing the resulting string, and replay it, save it as "sheet music", whatever - round-trip conversion from sounds to strings and back.

Piecewise refinements on a toy piano (see Playing with sound effects) led to many new features, but also to untidy code. Thus it was time for a redesign under the name of TclMusic, which started with the following sketchy spec of the music package:}

if 0 {If ms is -1, the note starts playing (e.g. after pressing a key). The sound is turned off again by calling with ms = 0. "Note" above refers to a string consisting of maximally four parts:

base note: [A-Ga-g], cover two octaves; x for pause

optional sign: [#bB]: b only after b, B only after B

optional octave marker: 1,2 go down, one to three 's go up

optional length marker: + double, - half, .: 1.5 times

"Scores" or "tunes" are lists of such notes, plus

"x" for pauses

">", "<" for piano/forte (low or high amplitude) changes

"/" signs (bars) have no effect, except of aiding the reader

Test the UI demo, which comes up if this file is toplevel script, with the sample tune in the entry widget, and try to guess which popular song I meant ;-) Portions of the tune can also be played by selecting them, and right-clicking. Activating "Record" will copy the names of played notes into the entry for editing (lengths must be fixed manually) and replay. As additional educational goodie, the currently replayed note is highlighted in green on the keyboard. Right-clicking on a piano key displays its frequency in the bottom info label, in addition to the note name that is always shown.

The canvas note display still leaves wishes open. #/b signs are just drawn as text, the little "factor 1.5" dots as commas (periods were too small). Very high or low notes just wander out of the picture.. This was just yet another weekend project, but I hope that some of you can enjoy it, and improve on it. Care was taken to isolate Tcl-based music processing from Tk widgetry, so the demo at end runs also in a tclsh - it plays the tune and displays the current note on stderr.

A final "note": I don't know if it's Snack or the many afters that I'm planting, but this plaything did not so rarely hang or crash on my W95 box, when keyboard or mouse events occur while sounds are playing. Looks like this is sort of a stress test. Be warned, and ready to kill the thing in order to stop runaway sounds. }

Brian Theado 14Aug04 - Here is an alternate music generator that makes use of the tclmidi package (see midi) which works in Windows. On my computer, the midi synthesizer in the sound card sounds excellent (to my untrained ear). The default instrument is a piano.