#!/usr/bin/perl -w ########################################################################### # Copyright (C) 2007 by Reid Swanson # # reid@reidswanson.com # # # # Final Class Project in ISE575 / EE675 / CSCI575 / PSYCH675 # #   University of Southern California, Spring 2007 # #   Topics in Engineering Approaches to Music Cognition # #   Human-Centered Computing in Generating Music # ########################################################################### # Extracts voices from a MuseData kern read from the standard input. # You can either specify which voice to extract by specifying a voice number # or by default extract them all. # This script requires that you have Humdrum install and that you set the # variable $bin to the Humdrum bin directory use strict; use IPC::Open2; my $bin = "/home/reid/downloads/software/humdrum/bin"; if (scalar(@ARGV) > 1) { print STDERR "usage:\n\textract-voices.pl [voice-number]?\n"; exit; } my $voiceNumber = -1; if (scalar(@ARGV) == 1) { $voiceNumber = shift; if (!($voiceNumber =~ /\d+/) || $voiceNumber <= 0) { print STDERR "usage:\n\textract-voices.pl [voice-number]?\n"; exit; } } my $data = ""; my $numVoices = 0; while (defined(my $line = )) { $data .= $line; next if ($line =~ /^\s*$/); next if ($line =~ /^!/); chomp $line; if ($line =~ /^\*\*kern/) { my @tokens = split(/\t/, $line); $numVoices = scalar(@tokens); } } if ($voiceNumber > $numVoices) { print STDERR "The voice number must be less than the number of voices\n"; exit; } if ($voiceNumber == -1) { for (my $i = 1; $i <= $numVoices; $i++) { local (*READ, *WRITE); open2(\*READ, \*WRITE, "$bin/extract -p $i"); print WRITE "$data"; close(WRITE); while (my $line = ) { next if ($line =~ /((^!)|(^\*)|(^\s*$))/); chomp $line; my @tokens = split(/\t/, $line); print "$tokens[0] "; } close(READ); print "\n"; } } else { local (*READ, *WRITE); open2(\*READ, \*WRITE, "$bin/extract -p $voiceNumber"); print WRITE "$data"; close(WRITE); while (my $line = ) { next if ($line =~ /((^!)|(^\*)|(^\s*$))/); chomp $line; my @tokens = split(/\t/, $line); print "$tokens[0] "; } close(READ); print "\n"; }