###############################################################################
# Sound Studio
# A Sound Sample Utility
# by Paul D. Sharpe
# A 3rd Year Project at the University of Leeds;
# Department of Electronic and Electrical Engineering.
 
# This is the main module
# 7/3/95
# Revision:2
# Date: 10/4/95
 

###################################################################
# Global Variables
#
# The Global variables are grouped into arrays.
# These arrays and there function is declared and explained here.

global ConfigSet
# This array contains the resource data. This is used mainly in setting
# up the widgets.

global ConfigInfo
# This is related to ConfigSet in that it has the same array names. 
# It contains a written description of the items in the ConfigSet array.
# It is used in the user interface for changing colours.

global FILE
# This array contains the filenames and paths for the various components
# of sound studio. These are defined here:
 # The directory containing Sound Studio files
   set FILE(SRC) $MYFILES
 # The configuration File
   set FILE(CFG) ~/.studio.cfg
 # The Mixer program.
   set FILE(Mixer) $FILE(SRC)/studio_mixer
 # The recording/playback program.
   set FILE(Tool) "$FILE(SRC)/studio_tool"
 # The Conversion Utility
   set FILE(Sox) $SOX_DIR/sox
 # Temporary files
   # Use system clock for uniqueness
   set PID [exec date +%s]
   set FILE(TMP1) $TEMP_DIR/SoundStudio.tmp$PID.01.main
   set FILE(TMP2) $TEMP_DIR/SoundStudio.tmp$PID.02.clip
   set FILE(TMP3) $TEMP_DIR/SoundStudio.tmp$PID.03.soxerr
   set FILE(TMP4) $TEMP_DIR/SoundStudio.tmp$PID.04.undo
   set FILE(TMP5) $TEMP_DIR/SoundStudio.tmp$PID.05.temp
 # The file name of the loaded file.
   set FILE(CURR) {}

global HELP
# This array contains globals specific to the Help Module.

global CARD
# This array contains information about the sound card.
# For example the sample rate limits.

global SAMPLE
# This array contains the sampling parameters of the file.
# Default Settings.

global FLAG
# This array contains the flags indicating the various states.

global PLAY
# This array contains globals specific to playpanel.tk

global PACK
# This array contains the packing order of the Sound Studio sections.
# There is an array item for each section which contains the path of the
# section it is to be packed after.
# This allows sections to be removed view and then restored to view in its
# original place.
# The following contains the list of names of the sound studio sections.
  set PACK(list) { MenuBar PlayPanel  PlotBox EffectBar Mixer  InfoBar }
# The following contains the paths of the sound studio sections.
  set PACK(path) { "" .menubar  .playpanel .plotbox .effect  .mixer  .infobar }

global MIXER
# This array contains globals specific to the mixer module. 

global DialogCfg
# This global contains a resource list to be used for the dialog boxes.


###################################################################
# Load the modules.
  source $FILE(SRC)/menu.tk
  source $FILE(SRC)/playpanel.tk
  source $FILE(SRC)/option.tk
  source $FILE(SRC)/infobar.tk
  source $FILE(SRC)/plot.tk
  source $FILE(SRC)/mixer.tk

  source $FILE(SRC)/file.tk
  source $FILE(SRC)/edit.tk
  source $FILE(SRC)/effects.tk
  source $FILE(SRC)/help.tk
  source $FILE(SRC)/misc.tk


###################################################################
# Load the Configuration File.
  LoadSettings
# Store the dialog box configuration list.
  set DialogCfg [list 	$ConfigSet(cBG1) $ConfigSet(cFG1)\
			$ConfigSet(cABG) $ConfigSet(cAFG)\
			$ConfigSet(FNT_L)]

CARDSetter
foreach dev $MIXER(All) {
	set temp [lindex $MIXER(noshow) [expr $dev+1]]
	set lab [lindex $MIXER(labels) $dev]
	set MIXER(show$lab) [expr {$temp==-1}]
# not ($temp...) - thanks Jason Adams <jadams@mosqito.frcc.cccoes.edu>
	unset temp
	unset lab
}

# Override the user setting of the view mixer flag, based on the existence of 
# the mixer.
set FLAG(Vw_Mixer) [expr $FLAG(Mixer_Exist) && $FLAG(Vw_Mixer)]

# Set PACK array with the order of sound studio sections.
OrderPacking

# Set globals to their initial values.
  New init

###################################################################
# Setup the Toplevel window for Sound Studio.
  # Set the title.
    wm title . "Sound Studio"
  # Set the name to be found on the icon.
    wm iconname . Studio
  # Set the bitmap to be the icon for Sound Studio.
    wm iconbitmap . @$FILE(SRC)/mic.ico
  # Set the window background colour.
    . configure -background $ConfigSet(cBG1)
  # Set some safety devices for an unexected termination of the program.
    wm protocol . WM_DELETE_WINDOW {
	puts "studio has been exited"
	Exit
    }
    wm protocol . WM_SAVE_YOURSELF {
	puts "studio has been exited"
	Exit
    }

###################################################################
# Initialise the Sound Studio sections. (Create Widgets)
  PlayPanel init
  MenuBar init
  InfoBar init 
  PlotBox init
  EffectBar init
  Mixer init

  # Disable Mixer Board functionality if there is no mixer present.
    if {!$FLAG(Mixer_Exist)} { 
	MenuItemDisable .menubar.options.menu.view Mixer 1 
	MenuItemDisable .menubar.options.menu "Mixer Channels" 1
    }

###################################################################
# Display the Sound Studio Sections that are requested.
  # Loop through the sections.
    foreach item $PACK(list) {
	# if the section is requested to be displayed, display it.
	if {$FLAG(Vw_$item)} "$item show "
    }


###################################################################
# Clear Globals from the Start-up script.
  unset MYFILES
  unset TEMP_DIR
  unset SOX_DIR

#Check if a sample is to be loaded
if {($argc==1) } {
	if {[file exist $argv]}  {
 		#Load Sample
		Load default $argv
		Play
	} else {
		dialog .loaderr "Load Error" "The file\n$argv\nis not found" $DialogCfg {error} 0 OK
	}
}\
else {
	# Start New Sample.
	New
}
###################################################################
#END OF INITIALISATION -> ENTER EVENT LOOP
