#!/bin/bash -
#===========================================================================
#
# FILE: serial.sh
#
# USAGE: ./serial.sh
#
# DESCRIPTION: generates tonerow randomly, compiles on Lilypond
# and creates static html page.
# OPTIONS: ---
# REQUIREMENTS: lilypond, shuf
# BUGS: ---
# NOTES: ---
# AUTHOR: Jonathan Kulp (),
# ORGANIZATION:
# CREATED: 03/20/2013 07:35:41 AM CDT
# REVISION: ---
#===========================================================================
pitches=/tmp/pitches.ily
random=/tmp/random.ily
lilyfile=/tmp/tonerow.ly
midifile=/tmp/tonerow.midi
stem=$(readlink -f $lilyfile | sed -e 's/\..*$//')
withrhythms=/tmp/withrhythms.ily
chordOne=/tmp/chord1.ily
chordTwo=/tmp/chord2.ily
chordThree=/tmp/chord3.ily
getpitches (){
# first make a list of all 12 chromatic pitches
# using lilypond's naming conventions
# one per line b/c it's easier to shuffle, add rhythms, etc
cat > $pitches << EOFallpitches
c'
cis'
d'
dis'
e'
f'
fis'
g'
gis'
a'
bes'
b'
EOFallpitches
# now run them through the shuf command to put them in random order
shuf $pitches > $random
} # ------------ end of getpitches ---------------
add_rhythms(){
# todo: randomize the rhythms
sed -f - $random > $withrhythms << EOFrhythms
1s/$/4/
3s/$/4../
4s/$/16/
5s/$/4/
6s/$/4./
7s/$/8/
8s/$/8./
12s/$/2./
EOFrhythms
} # ------------ end of add_rhythms ---------------
## On web version I'm not running this function
chords(){
# stack up the notes of the row in 3 chords of
# 4 notes each
cat $random | sed -n '1,4p' \
| sed -e :a -e '$!N;s/\n/ /;ta' -e 'P;D' \
| sed -e 's/^/</' | sed -e 's/$/>2/' > $chordOne
cat $random | sed -n '5,8p' \
| sed -e :a -e '$!N;s/\n/ /;ta' -e 'P;D' \
| sed -e 's/^/</' | sed -e 's/$/>4/' > $chordTwo
cat $random | sed -n '9,12p' \
| sed -e :a -e '$!N;s/\n/ /;ta' -e 'P;D' \
| sed -e 's/^/</' | sed -e 's/$/>2./' > $chordThree
} # ------------ end of chords ---------------
make_lily_file (){
# determine lilypond version for later inclusion
version=$(lilypond --version | grep LilyPond | cut -d " " -f3)
# make it cat the random list of pitches
lilypitches=$(cat $withrhythms $chordOne $chordTwo $chordThree)
# assemble the lilypond source file, sticking the
# pitches in at the right place
cat > $lilyfile << EOFscore
\\score {
{
\\version "$version"
\\time 3/4
#(set-accidental-style 'dodecaphonic)
#(set-global-staff-size 24)
\\set Staff.midiInstrument = "violin"
\\partial 4
$lilypitches
\\bar "|."
}
\\layout {} \\midi {\\tempo 4 = 120}
}
EOFscore
} # ------------ end of chords ---------------
runlily(){
# compile the lilypond file
lilycmd="lilypond -dno-point-and-click -ddelete-intermediate-files -dpreview"
$lilycmd $lilyfile &> /dev/null
} # ------------ end of runlily ---------------
## create static html page for my website
html(){
web="$stem".html
image="$stem".preview.png
midi="$stem".midi
cat > $web << EOFhtml
<meta http-equiv=Content-Type content="text/html; charset=UTF-8">
<title>12-Tone Row of the Day</title>
<h2>Random 12-Tone Row of the Day</h2>
<p>
<img src="tonerow.preview.png" alt="randomly generated 12-tone
row"
title="randomly-generated 12-tone row">
</p>
<a href="tonerow.midi">Midi file</a>
<p>
Generated with <a href="12toneweb.html" target="_blank">bash fu</a> and <a href="http://lilypond.org"
target="_blank">Lilypond</a>
</p>
EOFhtml
} # ------------ end of html ---------------
# RUN ALL FUNCTIONS HERE
cd /tmp
getpitches
#chords
add_rhythms
make_lily_file
runlily
# sleep for half a second to make sure Lilypond has time to compile
sleep .5
html
#clean stuff up
rm /tmp/*.ily /tmp/*.eps