#!/usr/bin/perl # # File: cue2toc # Version: 0.1 # Author: Patrick C. McGinty # Date: 9/01/04 # Description: Given a .CUE input file, the script creates # a useable *.TOC output file # use warnings; use strict; # require 'shellwords.pl'; use Audio::Wav; # sub print_help; sub sample_to_index; sub index_to_sample; # my @cue_lines; #input file data my @words; #words in each line my @next_words; #words in line ahead of current my $track_count = 0; #current track in file my @data; #array of hashes of track data my $file; #current working wave file in TOC my $wav; #wave object holder my $read; #pointer to wave being read my $max_samples; #total number of samples in wave file my $toc_file; #name of TOC file my $diff_time; #temp variable for sample length calculation my $cue_dir = ""; #temp variable for file paths print "--- Cue2Toc Converter, (c) Patrick C. McGinty ---\n"; if( not $ARGV[0] ){ exit print_help(); } #get directory path if( $ARGV[0] =~ m#^(.*/)[\w].*$# ){ $cue_dir = $1; } #open cue file #save file to array #remove white space from array values open( CUE_FILE, $ARGV[0] ); @cue_lines = ; chomp( @cue_lines ); #for each line #split the line and look for keywords #if a keyword is found, save the value in a hash for( my $i=0; $i <= $#cue_lines; $i++ ){ @words = shellwords($cue_lines[$i]); if( $words[0] ){ $track_count = $words[1] if( $words[0] eq 'TRACK' ); if( $words[0] eq 'FILE' ){ @next_words = shellwords($cue_lines[$i+1]); if( $next_words[0] eq 'TRACK'){ $data[$next_words[1]]{file} = $words[1]; } else{ $data[$track_count]{file} = $words[1]; } } $data[$track_count]{performer} = $words[1] if( $words[0] eq 'PERFORMER' ); $data[$track_count]{title} = $words[1] if( $words[0] eq 'TITLE' ); $data[$track_count]{"index$words[1]"} = $words[2] if( $words[0] eq 'INDEX' ); $data[$track_count]{isrc} = $words[1] if( $words[0] eq 'ISRC' ); } } close( CUE_FILE ); exit print "Error: No WAV file delcaration found in CUE file.\n" if( not $data[1]{file} ); #create an output file $toc_file = $ARGV[0]; $toc_file =~ s/cue$/toc/i; print( "Converting $ARGV[0] to $toc_file\n" ); print( "Found performer:\t$data[0]{performer}\n" ) if $data[0]{performer}; print( "Found title:\t\t$data[0]{title}\n" ) if $data[0]{title}; open( TOC_FILE, ">$toc_file" ); print TOC_FILE "//File generated by cue2toc script (c) Patrick C. McGinty\n\n"; print TOC_FILE "CD_DA\n"; print TOC_FILE "CD_TEXT {\n"; print TOC_FILE "\tLANGUAGE_MAP {\n"; print TOC_FILE "\t\t0 : EN\n"; print TOC_FILE "\t}\n"; print TOC_FILE "\tLANGUAGE 0 {\n"; print TOC_FILE "\t\tTITLE \"$data[0]{title}\"\n" if $data[0]{title}; print TOC_FILE "\t\tPERFORMER \"$data[0]{performer}\"\n" if $data[0]{performer}; print TOC_FILE "\t}\n"; print TOC_FILE "}\n"; for( my $i=1; $i<=$track_count; ) { $file = $data[$i]{file}; #open wave file to determin the length of the file $wav = new Audio::Wav; $read = $wav->read( "$cue_dir$file" ); $max_samples = $read->length_samples(); for( ; ( (not $data[$i]{file}) || ($data[$i]{file} eq $file) ) && ($i<=$track_count); $i++ ) { $data[$i]{file} = $file; $data[$i]{max_samp} = $max_samples; print TOC_FILE "\nTRACK AUDIO\n"; print TOC_FILE "CD_TEXT {\n"; print TOC_FILE "\tLANGUAGE 0 {\n"; print TOC_FILE "\t\tTITLE \"$data[$i]{title}\"\n" if $data[$i]{title}; print TOC_FILE "\t\tPERFORMER \"$data[$i]{performer}\"\n" if $data[$i]{performer}; print TOC_FILE "\t\tISRC \"$data[$i]{isrc}\"\n" if $data[$i]{isrc}; print TOC_FILE "\t}\n"; print TOC_FILE "}\n"; if( $data[$i]{index00} ) { $data[$i]{start_index} = $data[$i]{index00}; $data[$i]{start_samp} = index_to_sample($data[$i]{index00}); $data[$i]{mid_samp} = index_to_sample($data[$i]{index01}); $diff_time = $data[$i]{mid_samp} - $data[$i]{start_samp}; if( $diff_time > 0 ) { print TOC_FILE "START " . sample_to_index( $diff_time ) . "\n"; } else { print TOC_FILE "FILE \"$data[$i-1]{file}\" " . "$data[$i-1]{stop_index} " . sample_to_index( $data[$i-1]{max_samp} - $data[$i-1]{stop_samp}) . "\n"; print TOC_FILE "START\n"; $data[$i]{start_index} = $data[$i]{index01}; $data[$i]{start_samp} = $data[$i]{mid_samp}; } } else { $data[$i]{start_index} = $data[$i]{index01}; $data[$i]{start_samp} = index_to_sample($data[$i]{index01}); } if( (not $data[$i+1]{file}) && ($data[$i+1]{index00} || $data[$i+1]{index01})) { if( $data[$i+1]{index00} ) { $data[$i]{stop_samp} = index_to_sample($data[$i+1]{index00}); } elsif( $data[$i+1]{index01} ) { $data[$i]{stop_samp} = index_to_sample($data[$i+1]{index01}); } } elsif( ($data[$i+1]{index00} && $data[$i+1]{index01}) && (index_to_sample( $data[$i+1]{index00} ) > index_to_sample( $data[$i+1]{index01} )) ) { $data[$i]{stop_samp} = index_to_sample( $data[$i+1]{index00} ); } else { $data[$i]{stop_samp} = $max_samples; } print( "Adding track $i:\t$file\n" ); $data[$i]{stop_index} = sample_to_index( $data[$i]{stop_samp} ); print TOC_FILE "FILE \"$file\" $data[$i]{start_index} " . sample_to_index( $data[$i]{stop_samp} - $data[$i]{start_samp} ) . "\n"; } } close( TOC_FILE ); sub print_help { print <