#!/bin/sh
################################################################################
# SmptetUtilities
#   Useful subroutines for working with SMPTE timecodes, for inclusion in
#   pre-read/post-write control scripts.
#
#   $Date: 93/11/15 18:52:32 $
#   $Revision: 11.0 $
#
#   (/bin/sh syntax...)
################################################################################


################################################################################
# Convert a cell number into a SMPTE timecode of the form HH:MM:SS:FF
# args:	
#	$1  Frames per second (30 for NTSC, 25 for PAL, etc...)
#	$2  Frame
#	$3  Field separator   ( : for normal SMPTE style)
# returns:
#	Sets the variable _RESULT to the SMPTE code.
################################################################################
NumToSMPTE( ) 
{ 
    _colon_nm2smt=
    
    _fps_nm2smt=$1
    _cell_nm2smt=$2
    _colon_nm2smt=$3
    
    _fpm_nm2smt=`echo "$_fps_nm2smt * 60" | bc`
    _fph_nm2smt=`echo "$_fpm_nm2smt * 60" | bc`
        
    # Catch a cell number < 0 ...
    if [ $_cell_nm2smt -lt 0 ]
    then
	_RESULT="00"$_colon_nm2smt"00"$_colon_nm2smt"00"$_colon_nm2smt"00"
	return
    fi
    
    # Now use 'bc' to do the math...
    _hours_nm2smt=`echo "$_cell_nm2smt / $_fph_nm2smt" | bc`
    _temp1_nm2smt=`echo "$_cell_nm2smt  - ( $_hours_nm2smt * $_fph_nm2smt )" | bc`
    _minutes_nm2smt=`echo "$_temp1_nm2smt / $_fpm_nm2smt" | bc`
    _temp2_nm2smt=`echo "$_temp1_nm2smt - ( $_minutes_nm2smt * $_fpm_nm2smt )" | bc`
    _seconds_nm2smt=`echo "$_temp2_nm2smt / $_fps_nm2smt" | bc`
    _temp3_nm2smt=`echo "$_temp2_nm2smt - ( $_seconds_nm2smt * $_fps_nm2smt )" | bc`
    _frames_nm2smt=$_temp3_nm2smt

    # Pad the digits with zero padded if less than 10
    if [ $_hours_nm2smt -lt 10 ]
    then
	_hours_nm2smt="0"$_hours_nm2smt
    fi

    if [ $_minutes_nm2smt -lt 10 ]
    then
	_minutes_nm2smt="0"$_minutes_nm2smt
    fi

    if [ $_seconds_nm2smt -lt 10 ]
    then
	_seconds_nm2smt="0"$_seconds_nm2smt
    fi

    if [ $_frames_nm2smt -lt 10 ]
    then
	_frames_nm2smt="0"$_frames_nm2smt
    fi
    
    _RESULT="$_hours_nm2smt$_colon_nm2smt$_minutes_nm2smt$_colon_nm2smt$_seconds_nm2smt$_colon_nm2smt$_frames_nm2smt"
}

