#------------------------------------------------------------------------
#
# Copyright (c) 1997-1998 by Cornell University.
# 
# See the file "license.txt" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
#------------------------------------------------------------------------
package require DvmBasic
package require DvmAmap
package require DvmWave

source ../lib/wavelib.tcl

if {$argc != 2} {
    puts "enter input 8 bit ulaw wav filename :"
    set inname [gets stdin]
    puts "enter output wav filename :"
    set outname [gets stdin]
} else {
    set inname [lindex $argv 0]
    set outname [lindex $argv 1]
}

### u-law to pcm mapping
## read u-law audio in buffer
set wave [read_wave $inname]
set hdr  [lindex $wave 0]
set audio [lindex $wave 1]
set inbs  [lindex $wave 2]

# Apply map to convert u-law into PCM
set utolinmap [audiomap_8to16_new]
audiomap_8to16_init_ulaw_to_linear $utolinmap
set outaudio [audio_16_new [audio_get_num_of_samples $audio]]
audiomap_8to16_apply $utolinmap $audio $outaudio

# Change hdr to be a valid PCM Wave header
wave_hdr_set_format $hdr 1
wave_hdr_set_bits_per_sample $hdr 16
wave_hdr_set_bytes_per_sec $hdr [expr [wave_hdr_get_bytes_per_sec $hdr]*2]
wave_hdr_set_block_align $hdr [expr [wave_hdr_get_block_align $hdr] * 2]
wave_hdr_set_data_len $hdr [expr [wave_hdr_get_data_len $hdr] * 2]

# Write the PCM audio in Wave format
write_wave $hdr $outaudio $outname

# Wrap up
wave_hdr_free $hdr
bitstream_free $inbs
audio_free $outaudio
audiomap_free $utolinmap