App-MusicTools

 view release on metacpan or  search on metacpan

bin/ly-fu  view on Meta::CPAN

my $tmp_ly = File::Temp->new(
    DIR      => $work_dir,
    SUFFIX   => '.ly',
    TEMPLATE => 'ly-fu.XXXXXXXXXX',
    UNLINK   => 0,
);
$tmp_ly->print($lilypond_input);
$tmp_ly->flush;
$tmp_ly->sync;

my $ly_filename = $tmp_ly->filename;
( my $score_filename = $ly_filename ) =~ s/\.ly/\.pdf/;
( my $midi_filename  = $ly_filename ) =~ s/\.ly/\.midi/;
close $tmp_ly;

say $ly_filename if $Flag_Generate_Score;

# lilypond is noisy by default
my ( $stdout, $stderr, $status ) = capture {
    system @Lilypond_Cmd, $ly_filename;
};
if ( $status != 0 ) {
    print STDERR $stderr;
    exit 1;
}
print STDERR $stderr if $Flag_Verbose;

if ($Flag_Show_Score) {
    local $SIG{CHLD} = 'IGNORE';
    my $pid = fork();
    die "ly-fu: could not fork score reader: $!\n" unless defined $pid;
    unless ($pid) {
        # a vague effort at daemonization; may be necessary if the PDF
        # view being open holds ly-fu open which then prevents a text
        # editor from moving on
        open( STDIN,  '<', '/dev/null' );
        open( STDOUT, '>', '/dev/null' );
        ( setsid() == -1 ) and die "ly-fu: setsid failed: $!\n";
        open( STDERR, '>&', STDOUT );
        exec( @Score_Reader, $score_filename )
          or die "ly-fu: could not exec score reader '@Score_Reader': $!\n";
    }
}

unless ($Flag_No_MIDI) {
    if ($Flag_Generate_Score) {
        # we're keeping the files, so there's nothing more this script
        # needs to do
        exec( @MIDI_Player, $midi_filename )
          or die
          "ly-fu: could not exec MIDI player '@MIDI_Player $midi_filename': $!\n";
    } else {
        system( @MIDI_Player, $midi_filename ) == 0
          or die
          "ly-fu: could not run MIDI player '@MIDI_Player $midi_filename': $!\n";
    }
}

if ($Flag_Generate_Score) {
    # the files might be shared, so open up the permissions
    chmod 0644, $ly_filename, $midi_filename, $score_filename;
} else {
    # Preview.app got increasingly slower to actually start over time,
    # hence this kluge. Eventually I switched to mupdf, and eventually
    # the 2009 macbook died in late 2022.
    sleep $Flag_Sleep_Kluge if $Flag_Sleep_Kluge > 0;
    unlink $ly_filename, $midi_filename, $score_filename;
}

########################################################################
#
# SUBROUTINES

sub emit_help {
    warn <<"END_HELP";
Usage: $0 [options] "lilypond code ..."

The lilypond input will likely need to be quoted as lilypond input may
clash with various shell metacharacters.

  --absolute      Notation assumed to be absolute.
  --instrument    Set MIDI instrument (see lilypond docs).
  --partial|p     Lilypond fragment played once at beginning.
  --relative|b    Specify what note input is relative to.
  --repeats|r     How many times to repeat the lilypond input.
  --tempo|t       Set a tempo for the music.

  --layout|l      Save the MIDI and other various files.
  --open          Show the score in some sort of viewer.
  --show-code     Print the lilypond data to stdout and exit.
  --silent|s      Skip generating MIDI.
  --sleep|S       Sleep before unlink of various tmp files.
  --verbose       Show noise from lilypond, MIDI player.

END_HELP
    exit 64;
}
__END__

=head1 NAME

ly-fu - play or display lilypond snippets

=head1 SYNOPSIS

Set the MIDI_EDITOR and SCORE_VIEWER environment variables to
suitable MIDI playing and PDF opening utilities, or adjust the source
to your liking.

  $ export MIDI_EDITOR=timidity
  $ export SCORE_VIEWER=xpdf
  $ ly-fu --instrument=banjo c des ees c des bes c aes
  $ ly-fu  -i=trumpet --open "c8 g'4 c,8 g'4 c,8 g'2"
  $ echo c e g | ly-fu -

Or, to instead save the generated lilypond somewhere:

  $ ly-fu --show-code e e e c1 > masterpiece.ly

=head1 DESCRIPTION



( run in 1.286 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )