

package require sound
# (sub-package of snack)

namespace eval DTMF {}

# Set up the frequencies
array set DTMF::low {
    0   941
    1   697
    2   697
    3   697
    4   770
    5   770
    6   770
    7   852
    8   852
    9   852
    *   941
    #   941
}
array set DTMF::high {
    0   1336
    1   1209
    2   1336
    3   1477
    4   1209
    5   1336
    6   1477
    7   1209
    8   1336
    9   1477
    *   1209
    #   1477
}

set DTMF::dutLength 3500
set DTMF::gap 1200

proc DTMF::dialNumber { number } {
    variable low 
    variable high 
    variable dutLength
    variable gap
    
    # In Europe and many other places:
    regsub {^\+} $number "00" number
    # In North America:
    # regsub {^+} $number "011" number
    
    # We only consider digits:
    regsub -all {\D} $number "" number
    set nList [split $number ""]

    snack::sound S
    S length [expr 5* $gap]

    ::snack::sound tempLow
    ::snack::sound temp
    foreach n $nList {
        temp flush
	tempLow flush
	tempLow filter [::snack::filter generator $low($n) 30000 0.0 sine $dutLength]
	temp filter [::snack::filter generator $high($n) 30000 0.0 sine $dutLength]
	temp mix tempLow
	S concatenate temp
	S length [expr [S length] + $gap]
    }
    # There is some strange extra beep coming up after the specified
    # tones.  Dunno what that is, but it can be cleared by specifying
    # a ridculous length:
    S length [expr [S length] + 50*$gap]

    S play -command [list S destroy]

    temp destroy
    tempLow destroy
}
