Soundgenerator

dzach 2007-6-27: A sound generator for beeps, clicks and noises can be created using Snack. Here is a way to produce such sounds, using snack's filters:


package req snack

namespace eval ::soundgen {
        variable var
}
proc ::soundgen::init {} {
        variable var
        # define sinewaves
        set var(low) [snack::filter generator 880 32767 0.0 sine]
        set var(high) [snack::filter generator 990 32767 0.0 sine]
        set var(veryhigh) [snack::filter generator 1650 32767 0.0 sine]
        # define some noise
        set var(noisy) [snack::filter generator 0 2000 0.0 noise]

        # define sound objects
        set var(beep) [snack::sound -channels 1]
        set var(chaos) [snack::sound -channels 1]
        # create sounds
        $var(beep) filter $var(low) -start 0 -end 500
        $var(beep) filter $var(high) -start 500 -end 750
        $var(beep) filter $var(low) -start 750 -end 1250
        $var(beep) filter $var(veryhigh) -start 1250 -end 1750
        $var(beep) filter $var(noisy) -start 1750

        $var(chaos) filter $var(noisy) -end 300
}
proc ::soundgen::beep {type args} {
        variable var

        # use catch in case the sound device is busy.
        # on windows this might not be necessary
        catch {

        switch -- $type {
                raise {
                        eval $var(beep) play -start 250 -end 750 $args
                }
                drop {
                        eval $var(beep) play -start 500 -end 1200 $args
                }
                trill {
                        eval $var(beep) play -start 0 -end 1250 $args
                }
                lowbeep {
                        eval $var(beep) play -start 750 -end 1250 $args
                }
                highbeep {
                        eval $var(beep) play -start 1250 -end 1750 $args
                }
                chaos {
                        eval $var(chaos) play -end 500 $args
                }
                beep -
                default {
                        eval $var(beep) play -start 500 -end 750 $args
                }
        }
        }
}

# test code
# create a sound clock that counts 5sec, 10sec, 30sec and 1min

proc tick {} {set clock [clock sec]
        if {$clock % 60 == 0} {
                # cascade two sounds
                soundgen::beep trill -command {soundgen::beep chaos -end 2000}
        } elseif {$clock % 30 == 0} {
                soundgen::beep trill
        } elseif {$clock % 10 == 0} {
                soundgen::beep drop
        } elseif {$clock % 5 == 0} {
                soundgen::beep lowbeep
        } {
                soundgen::beep highbeep
        }
        after 1000 tick
}

# start test
soundgen::init
tick

ZB 20100726 - the above produces only one very short squeak. So many lines - and that's it? Or perhaps my Snack is broken? dzach Your snack might be broken. With a properly working snack, this code produces ticks every second, with different sounds for 5", 10", 30" and 60". You can change the sounds by editing the filter parameters in the init proc.