Debian Codegraph @

texmf-dist/doc/latex/songbook/contrib/modulate

raw
1 #!/usr/bin/perl -w
2 ###
3 # modulate - Modulate a song from one key to another.
4 #
5 # @(#)$Id: modulate,v 1.6 2006/10/15 15:48:10 rathc Exp $
6 # Copyright 1998--2002 Christopher Rath <[email protected]>
7 #
8 # This package is free software; you can redistribute it and/or modify
9 # it under the terms of version 2.1 of the GNU Lesser General Public
10 # License as published by the Free Software Foundation.
11 #
12 # This package is distributed in the hope that it will be
13 # useful, but WITHOUT ANY WARRANTY; without even the implied
14 # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 # PURPOSE. See the GNU Lesser General Public License for more
16 # details.
17 #
18 # USAGE: modulate [+-]<semitones> [filename]
19 #
20 # DESCRIPTION:
21 #
22 # Modulate a song from one key to another. The destination key is
23 # required to determine how to handle some troublesome note names (e.g.,
24 # is F to be named E# or F.
25 #
26 # This program is hard-wired to my own songbook coding style, some
27 # customization will be required if your coding style differs much from
28 # mine.
29 #
30 # To use this program, feed a song into the modulate program. The
31 # beginning of the song MUST begin with a \begin{song} statement. Any
32 # text preceeding the \begin{song} will be ignored.
33 #
34 # For example, feeding this:
35 #
36 # \begin{song}{What A Mighty God We Serve}{C}
37 # {Public Domain}
38 # {Unknown}
39 # {Isaiah 9:6}
40 # {\NotCCLIed}
41 #
42 # % Copyright verified by: Christopher Rath
43 # % Words verified by: Christopher Rath
44 # % Chords verified by: Priscilla Gruver
45 # \renewcommand{\RevDate}{February~11,~1993}
46 # \SBRef{Hosanna! Music Book~I}{\#93}
47 #
48 # \Ch{C}{What} a mighty God we serve,
49 #
50 # What a mighty God we \Ch{G7}{serve},
51 #
52 # will result in:
53 #
54 # \STitle{What A Mighty God We Serve}{D}
55 #
56 # \Ch{D}{What} a mighty God we serve,
57 #
58 # What a mighty God we \Ch{A7}{serve},
59 #
60 # being emitted by modulate.
61 ###
62
63 ###
64 # Specify constants and variables.
65 #
66 local($keyStr,
67 $oldKey,
68 $title);
69
70 local(%rawKeys) = (
71 '0', 'A',
72 '1', 'Bb',
73 '2', 'B',
74 '3', 'C',
75 '4', 'C#',
76 '5', 'D',
77 '6', 'Eb',
78 '7', 'E',
79 '8', 'F',
80 '9', 'F#',
81 '10', 'G',
82 '11', 'Ab'
83 );
84
85 local(%rawNotes) = (
86 'A', '0',
87 'A#', '1', 'Bb', '1',
88 'B', '2', 'Cb', '2',
89 'B#', '3', 'C', '3',
90 'C#', '4', 'Db', '4',
91 'D', '5',
92 'D#', '6', 'Eb', '6',
93 'E', '7', 'Fb', '7',
94 'E#', '8', 'F', '8',
95 'F#', '9', 'Gb', '9',
96 'G', '10',
97 'G#', '11', 'Ab', '11'
98 );
99
100 ###
101 # Test chord: A#modifier/Fbtrailer
102 sub modChord {
103 local($oldCh, $modVal) = (@_);
104 local($newCh);
105
106 if ($oldCh =~ /^[\{\[\]]/) {
107 $newCh = $oldCh;
108 } elsif ($oldCh =~ m%(^[A-G#b]+)([^/]*)/?([A-G#b]*)(.*)$%) {
109 local($chord, $modifier, $bass, $trailer) = ($1, $2, $3, $4);
110
111 $newCh = modNote($chord, $modVal);
112
113 if (length($modifier)) {
114 $newCh .= $modifier;
115 }
116
117 if (length($bass)) {
118 $newCh .= '/';
119 $newCh .= modNote($bass, $modVal);
120
121 if (length($trailer)) {
122 $newCh .= $trailer;
123 }
124 }
125 } else {
126 $newCh = $oldCh;
127 }
128
129 return $newCh;
130 }
131
132
133 sub modKey {
134 local($oldKey, $modVal) = (@_);
135 local($newKey) = (0);
136
137 $oldKey =~ s/\$\\sharp\$/\#/g;
138 $oldKey =~ s/\$\\flat\$/b/g;
139
140 $newKey = modChord($oldKey, $modVal);
141
142 $newKey =~ s/\#/\$\\sharp\$/g;
143 $newKey =~ s/b/\$\\flat\$/g;
144
145 return($newKey);
146 }
147
148
149 sub modNote {
150 local($oldNote, $modVal) = (@_);
151 local($newNote) = (0);
152
153 $newNote = $rawKeys{($rawNotes{$oldNote} + $modVal)%12};
154
155 return($newNote);
156 }
157
158
159 ###
160 # setKeyArray() - Patch %rawKeys for the key we're working in.
161 #
162 sub setKeyArray {
163 local($newKey) = (@_);
164
165 if ($newKey =~ /^E/) {
166 $rawKeys{'6'} = 'D#';
167 $rawKeys{'11'} = 'G#';
168 }
169 }
170
171
172 ###
173 # Get command line parameters.
174 #
175 if ($#ARGV < 0) {
176 $semitones = 2;
177 # die "USAGE: modulate [+-]<semitones> [inputFile]\n";
178 } else {
179 $semitones = $ARGV[0];
180 shift;
181 }
182
183 if (@ARGV) {
184 $inFile = $ARGV[0];
185 } else {
186 $inFile = '-';
187 }
188
189 open(INFILE, $inFile) || die "Couldn't open input file; aborting.\n";
190
191
192 ###
193 # Skip up to the \song macro; then scan the macro and figure out what key the
194 # song is in. Then skip ahead to the first line containing a \Ch macro.
195 #
196 while (<INFILE>)
197 {
198 if (/\\begin\{song\}\{([^\}]*)\}\{([^\}]*)\}/) {
199
200 $title = $1;
201 $oldKey = $2;
202 $keyStr = modKey($2, $semitones);
203
204 print '\\STitle{'."$title".'}{'."$keyStr".'}'."\n";
205 print "\n";
206
207 setKeyArray($keyStr);
208
209 last;
210 }
211 }
212
213 if (eof()) {
214 print "$0: ERROR: couldn't find `\\begin{song}{}{}' block, aborting.";
215 } else {
216 local($chord,
217 $prefix,
218 $suffix);
219
220 while (<INFILE>) {
221 if (/^\s+\{/
222 || /^\s+%/
223 || /^\s+\\renewcommand/
224 || /^\s+\\FLineIdx/
225 || /^\s+\\SBRef/
226 || /^\s+$/) {
227 next;
228 } else {
229 chop;
230 last;
231 }
232 }
233
234 ###
235 # Split the line into 3 pieces: a prefix, the chord itself (i.e., the first
236 # parameter to the \Ch command) and a suffix. We then modulate the chord
237 # itself, output the prefix followed by the chord; then we reset $_ to the
238 # suffix and re-check it for more \Ch macros. When we've run out of \Ch
239 # macros, get the next line of the file.
240 #
241 # Test line: \Ch{C}{What} a mighty God we \Ch{C}{serve!}\Ch{[}{}\Ch{F}{} \Ch{C}{}\Ch{]}{}
242 while (1) {
243 if (/\\end\{song\}/) {
244 last;
245 } elsif (/(\\Ch[rX]*\{)([^\}]*)/) {
246 $prefix = $`.$1;
247 $chord = $2;
248 $suffix = $';
249
250 $chord = modChord($chord, $semitones);
251
252 printf("%s%s", $prefix, $chord);
253
254 $_ = $suffix;
255 } else {
256 printf("%s\n", $_);
257
258 if (eof(INFILE)) {
259 last;
260 } else {
261 $_ = <INFILE>;
262 chop;
263 }
264 }
265 }
266 }
267