#!/usr/bin/perl
#
# Make copies of the individual JPG files, 
#     make as many copies as each as required to fill the song's beats.
#
# Place all the resulting frames into the "frame_dir" subdirectory,
#     with a numbering scheme like:
#
#              frame_0001.jpg
#
# To turn the resulting large number of frames into a movie, use
#
#  mencoder "mf://*.jpg" -mf fps=25 -o output.avi -ovc lavc -lavcopts vcodec=mpeg4
#
# MWR 3/13/2008

$beats_per_frame = 10;
$frame_dir = "./frame_dir";
$frame_base = "frame_";


$num_frames = 0;

# this for the lead in
for ($i = 0; $i < 169; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 0, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}


for ($i = 0; $i < $beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 0, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < $beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 1, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < $beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 2, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < $beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 3, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 2*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 4, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < $beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 5, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < $beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 6, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < $beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 7, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 2*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 8, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < $beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 9, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 0.5*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 10, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 0.5*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 11, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < $beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 12, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 0.5*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 13, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 0.5*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 14, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 2*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 15, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < $beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 16, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 2*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 17, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 2*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 18, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 2*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 19, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 2*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 20, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 2*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 21, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < $beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 22, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 2*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 23, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}
for ($i = 0; $i < 4*$beats_per_frame; $i++) {
  $cmd = sprintf "cp te_song_c_%d.jpg %s/%s%04d.jpg", 24, $frame_dir, $frame_base,
                 $num_frames;
  $ret = exec_cmd($cmd);
  $num_frames++;
}



exit 0;





##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}


