1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#!/bin/bash
#
# dehumanize
#
# Copyright (c) 2006 D. Michael McIntyre <rosegarden.trumpeter@gmail.com>
# Released under the GPL
#
# REQUIRES: sed
#
#
# PURPOSE: replace strings like F#3 with the equivalent MIDI pitch, by crude
# brute force
#
# This should be run on the XML after preset-xmlify.
#
# There are acres (or hectares) of room to make this more elegant. Take the
# ASCII value of the letter and offset it to get a base pitch, add or subtract
# 1 for the accidental, then multiply by the octave. But parsing the string
# in order to get into a position to do that required more effort to figure
# out than seemed worthwhile.
#
#
# C = 0
# C# = 1 Db = 1
# D = 2
# D# = 3 Eb = 3
# E = 4
# F = 5
# F# = 6 Gb = 6
# G = 7
# G# = 8 Ab = 8
# A = 9
# A# = 10 Bb = 10
# B = 11
# This function imported from some old script I wrote years ago, provided here
# since it won't be on anyone else's system to use. It isn't elegant at all,
# but who cares?
replace () {
tmp=/tmp/$RANDOM.$RANDOM.$RANDOM
if (sed "s@$1@$2@g" $3 > $tmp 2> /dev/null); then
if (mv -f $tmp $3); then
# echo "success: replaced $1 with $2 in $3 and updated file"
echo "replaced $1 with $2"
return 0
else
echo "error executing: mv -f $tmp $3"
exit 1
fi
else
echo "error executing: sed \"$1/$2/g\ $3"
exit 1
fi
echo "unexpected failure."
exit 1
}
# respell any flats as sharps
for ((i = 0; i <= 11; i++)); do
# fix octave divide
#!!! no, I think this was stupid; near as I can figure this morning the
# divide problem is at B#/Cb which probably doesn't apply here I hope
# ((y = i + 1))
# replace Db$i C\#$y $1
replace Db$i C\#$i $1
replace Eb$i D\#$i $1
replace Gb$i A\#$i $1
replace Ab$i G\#$i $1
replace Bb$i A\#$i $1
done
# get rid of the + on +3, and only keep the - on -3
replace + "" $1
# piano middle C is MIDI pitch 60
# A0 is MIDI pitch 43
#
pitch=-12
for ((octave = -2; octave < 20; octave++)); do
for note in C C\# D D\# E F F\# G G\# A A\# B; do
if [ $pitch -gt 127 ]; then
continue
fi
replace $note$octave $pitch $1
((pitch++))
done
done
exit 0
|