Csound Csound-dev Csound-tekno Search About

text-to-music toy

Date1999-01-29 10:00
FromMatti Koskinen
Subjecttext-to-music toy
hello all

here's a little toy to generate score from ascii text file.
pitch is based on character's ascii code, rythm and duration
are based on chars/word and words/line. amplitude is some
combination of these.
i advise not to convert Encyclopedia Britannica :-)
output is to stdout, so redirect to file.
pitch is best converted with cpsoct in orc, amp can be used as is.


-matti
mjkoskin@sci.fi


-------------------cut--------------------------

#!/usr/bin/tclsh
# text-to-music? by mjkoskin@sci.fi

proc parse {ins line} {
 global tim
    set sco ""

    set txtlen [string length $line]
    set wc [llength $line]
    if {$wc == 0} {
        return "\n"
    }
    set rythm [expr $wc.0 / $txtlen.0 ]
    foreach word $line {
        set wl [string length $word]
        set amp [expr $wl * 2000 ]
        set dur [expr $rythm / $wl.0 * 10.0 ]
        set dur [format "%3.3f" $dur]
        for {set i 0} {$i < $wl} {incr i} {
            set word [string toupper $word]
            scan [string index $word $i] "%c" pitch
            if {$pitch > 128} {
              set pitch [expr $pitch - 128]
            }
            set pitch [expr $pitch.0 / 32.0 + 5.0 ]
            append sco "$ins $tim $dur $pitch $amp\n"
            set tim [expr $tim + $dur]
        }
    }
    return $sco
}

proc gensco { file ins} {
global tim

    set tim 0
    if { [catch {open $file} fin] } {
        puts stderr "can't open file\n"
        return ""
    }
    set score ""
    while {![eof $fin]} {
        gets $fin line
        append score [parse $ins  $line]
    }
    close $fin
    return $score
}

if {$argc == 0} {
    puts stderr "usage: text-file "
    exit
}
if {$argc == 1} {
    set ins "i1"
} else {
    set ins [lindex $argv 1]
}
puts stdout "f1 0 8192 10 1 0.5 0.33 0.66 0.25 0.001"
puts stdout [gensco [lindex $argv 0] $ins]
puts stdout "e"