#------------------------------------------------------------------------
#
# 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 stereo pcm 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]
}

# generate the double volume map
set list {}
for {set i 0} {$i < 256} {incr i} {
    set v [expr $i + ($i-128)]
    if {$v < 0} {
        lappend list 0
    } elseif {$v > 255} {
        lappend list 255
    } else {
        lappend list $v
    }
}
set vol2map [audiomap_8to8_new]
audiomap_8to8_init_custom $list $vol2map

# Read the Audio Buffer from Wave file
set wave [read_wave $inname]
set hdr [lindex $wave 0]
set audio [lindex $wave 1]
set inbs [lindex $wave 2]

# apply double volume map to left channel
set outaudio [audio_8_new [audio_get_num_of_samples $audio]]
audiomap_8to8_apply_some $vol2map $audio 0 2 0 2 $outaudio
audio_8_copy_some $audio $outaudio 1 2 1 2

# Write the Audio Buffer to Wave file
write_wave $hdr $outaudio $outname

wave_hdr_free $hdr
audiomap_free $vol2map
audio_free $audio
audio_free $outaudio
bitstream_free $inbs