#!/usr/bin/python
#
# Bctodao: Generates file.trk from file.htal and file.wav.
#
# Originally written by: Jon Corbet
# Modified for my own uses: Forrest Cook
#
# usage: bctodao name
#
# The htal-file is the file out of bcast, which we chew on
# to make the track file.
# The wav-file's name is stuffed into the trk file.
#
#For a short tape, you could feed the output directly to cdrdao.  For longer
#ones, or if you want to remove pieces, you'll need to edit the file; it's
#pretty straightforward.  Just be sure any resulting toc file for cdrdao has
#the "CD_DA" line at the top. (my experience says just the opposite, remove
#the CD_DA line (FC))
#
#> How do I get from there to a CD?
#
#First, split it into multiple toc files if need be (i.e. one per disk).
#Then I use:
#
#  cdrdao write --device 0,4,0 --driver generic-mmc -n toc-file
#
#to actually write a disk.  You may have to mess with the parameters, of
#course; the '0,4,0' tells it where to find the cdr on the SCSI bus (mine
#is target 4).
#
#> In the jgb tape, there are a couple of sections that are just
#> audience noise, does everything turn into a .wav file at
#> some point so the noise ones can be deleted?
#
#The best thing is to set labels around stuff you want to delete, and remove
#the corresponding entries in the TOC file.  It helps to keep notes as you
#set the labels, so you know which tracks to delete.
#

import sys
import string

#
# Print a time in samples.
#
def ptime (samples):
    seconds = samples/44100
    frames = ((samples % 44100)*75)/44100
    return '%d:%02d:%02d' % (seconds/60, seconds % 60, frames)


if len(sys.argv) != 2:
#    print 'usage: %s bcast-file wav-file tracks-file' % sys.argv[0]
    print 'usage: %s name' % sys.argv[0]
    print 'This program requires files name.htal, name.wav and outputs name.trk'
    sys.exit (1)

#
# Open up the bcast TOC and verify it.
#
tocfile = sys.argv[1] + ".htal"
wavfile = sys.argv[1] + ".wav"
trkfile = sys.argv[1] + ".trk"

print "Generating " + trkfile + " from " + tocfile + " and " + wavfile + "."

#
# Open up the bcast TOC and verify it.
#
bcast = open (tocfile, 'r')
first = bcast.readline ()
if first[:6] != '<HTAL>':
    print "Hmm...this really doesn't look like a bcast file"
    sys.exit (1)

#
# Snarf out the labels.
#
labelline = None
for line in bcast.readlines ():
    if line[:7] == '<LABEL ':
	labelline = string.split (line, '>')
	break;
if labelline == None:
    print "Bummer, I couldn't find the label line!"
    sys.exit (1)
bcast.close ()
#
# Time to split them all out.
#
labels = [ ]
for label in labelline:
    slabel = string.split (label, '=')
    if len (slabel) != 2:
	continue
    labels.append (string.atoi (slabel[1]))
print 'Found %d labels' % len (labels)

#
# Create the output file.
#
Track = """
TRACK AUDIO
COPY
FILE "%s" %s %s\t// %s
"""

output = open (trkfile, 'w')

# This just gets removed later, could be done with a command line switch.
#output.write ('CD_DA\n')
for i in range (0, len (labels) - 1):
    delta = labels[i+1] - labels[i]
    output.write (Track % (wavfile, ptime (labels[i]), ptime (delta),
			   ptime (labels[i + 1] - labels[0])))

output.close ()
