# Pi Reciter
# eveander@eveander.com, February 23, 2001
# you may use this software free under the GNU General Public License

# Get the form variables sent by the previous page.  We are expecting: n_digits.
set_form_variables

# Determine pi.
# This function will return n_digits of pi.
set pi_substring [pi_digits $n_digits]

# Prepare the <audio> tag(s).
# We can't just write out <audio>$pi_substring</audio> because:
# 1) the upper limit for audio tag length is 256 characters, so we need to
#    break the substring into multiple audio tags if necessary;
# 2) we need to put spaces between the digits to stop the speech synthesizer
#    from pronouncing 6375678 as "six million three hundred seventy five thousand...."

set audio_tag_string ""

for {set audio_tag_counter 0} {$audio_tag_counter <= [string length $pi_substring]} {set audio_tag_counter [expr $audio_tag_counter + 100]} {

    # the next (up to) 100 digits of pi
    set pi_sub_substring [string range $pi_substring $audio_tag_counter [expr $audio_tag_counter + 99]]

    # vxml_insert_spaces_between_digits is a little proc I wrote to add a space between every character in a string
    append audio_tag_string "\n<audio>[vxml_insert_spaces_between_digits $pi_sub_substring]</audio>"
}


# Just for fun, if they happen to choose 6 digits,
# forego the audio string and replace it with a wav file.
if { $n_digits == 6 } {
    set audio_tag_string "<audio src=\"3point14159.wav\"/>"
}


# Assemble the VXML content.

set content "<vxml>
  <form id=\"pi_digits\">
    <block>
      <audio>You have requested $n_digits digits</audio>
      <pause>150</pause>$audio_tag_string
    <goto next=\"_home\"/>
    </block>
  </form>
</vxml>"

# Write out the content.

# Tellme fails if we use ns_return; it turns out, it's due to a ^@
# at the end of its output.  So use ns_write instead.

set header_time [ns_httptime [ns_time]]
set content_length [expr [string length $content] + 1]

ns_write "HTTP/1.0 200 OK
MIME-Version: 1.0
Date: $header_time
Server: NaviServer/2.0 AOLserver/2.3.2
Content-Type: */*
Content-Length: $content_length

$content"