/usr/share/bse/v0.7.8/scripts/part-harmonic-transposer.scm is in beast 0.7.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | ;; CC0 Public Domain: http://creativecommons.org/publicdomain/zero/1.0/
;;
;; (bse-script-register <func> <options> <category> <blurb> <author> <license> ARGS...)
;;
(bse-script-register 'part-harmonic-transposer
""
(N_ "/Part/Harmonic Transposer")
;; FIXME: the description may be suboptimal; the problem is
;; that I can't precisely describe details of musical theory
;; in english. -- stw
(N_ "The harmonic transposer takes the selection of a part "
"and transposes it to different harmonies. If you for "
"instance have selected a measure filled with C major "
"chords, and enter \"C,Am,F,G\" as harmonic sequence, "
"the result will be four measures, filled with C major, "
"A minor, F major and G major chords. "
"\n\n"
"This also works for melodies, so you can transpose a "
"whole melody written in G major to D minor. The standard "
"scales used in church music (ionian, dorian, phrygian, "
"lydian, mixolydian, aeolian, locrian) are also supported: "
"it is for instance possible to write Ddorian or Caeolian. "
"The aeolian scale is equivalent to minor and the ionian "
"scale is equivalent to major. "
"\n\n"
"Since musically, there is no preference on whether to transpose up or "
"down it is possible to specify the first harmony that will be transposed "
"down (all harmonies below this will be transposed up). It is possible "
"to omit this value. Then all notes will be transposed up.")
"Stefan Westerfeld"
"Provided \"as is\", WITHOUT ANY WARRANTY"
(bse-param-part (N_ "Part"))
(bse-param-string (N_ "Harmony Sequence") "C,Amin,F,G")
(bse-param-string (N_ "Transpose down starting at") "F"))
;; ------------------- parser for harmony strings ------------------------
;; parses a constant string (terminal symbols)
(define (parse-constant input match)
(and
input
(let* ((str (car input))
(strlen (string-length str))
(matchlen (string-length match)))
(and
(<= matchlen strlen)
(string=? (substring str 0 matchlen) match)
(cons (substring str matchlen strlen) (cdr input))))))
;; FIXME: should return the shortest string
(define (parse-multiple input . alternatives)
(if (null? alternatives)
#f
(if (car alternatives)
(car alternatives)
(apply parse-multiple input (cdr alternatives)))))
;; for generating parse results: adds a new element into the parse result list
(define (parse-result input result)
(and input
(cons (car input) (cons result (cdr input)))))
;; change the first element in the parse result list by applying function to it
(define (parse-change-result input function)
(and input
(cons (car input) (cons (function (cadr input)) (cddr input)))))
;; BaseNote := C D E F G A B H
(define (parse-basenote input)
(parse-multiple input
(parse-result (parse-constant input "C") 0)
(parse-result (parse-constant input "D") 2)
(parse-result (parse-constant input "E") 4)
(parse-result (parse-constant input "F") 5)
(parse-result (parse-constant input "G") 7)
(parse-result (parse-constant input "A") 9)
(parse-result (parse-constant input "B") 11)
(parse-result (parse-constant input "H") 11)))
;; FullNote := # <BaseNote> | <BaseNote> # | <BaseNote> is | <BaseNote>
(define (parse-fullnote input)
(parse-multiple input
(parse-change-result (parse-basenote (parse-constant input "#")) 1+)
(parse-change-result (parse-constant (parse-basenote input) "#") 1+)
(parse-change-result (parse-constant (parse-basenote input) "is") 1+)
(parse-basenote input)))
;; Harmony := <FullNote> maj | <FullNote> min | <FullNote> m | <FullNote>
(define (parse-harmony input)
(let ((scale (lambda (shift) (lambda (note) (cons note shift))))
(major (lambda (note) (cons note 0)))
(minor (lambda (note) (cons note 5))))
(parse-multiple input
;; http://en.wikipedia.org/wiki/Musical_mode
;; http://de.wikipedia.org/wiki/Kirchentonart
(parse-change-result (parse-constant (parse-fullnote input) "ionian") (scale 0))
(parse-change-result (parse-constant (parse-fullnote input) "dorian") (scale 1))
(parse-change-result (parse-constant (parse-fullnote input) "phrygian") (scale 2))
(parse-change-result (parse-constant (parse-fullnote input) "lydian") (scale 3))
(parse-change-result (parse-constant (parse-fullnote input) "mixolydian") (scale 4))
(parse-change-result (parse-constant (parse-fullnote input) "aeolian") (scale 5))
(parse-change-result (parse-constant (parse-fullnote input) "locrian") (scale 6))
;; conventional harmonic major/minor (which is the same as ionian/aeolian in our notation)
(parse-change-result (parse-constant (parse-fullnote input) "maj") major)
(parse-change-result (parse-constant (parse-fullnote input) "min") minor)
(parse-change-result (parse-constant (parse-fullnote input) "m") minor)
(parse-change-result (parse-fullnote input) major))))
;; HarmonySeq := <Harmony> , <HarmonySeq> | <Harmony>
(define (parse-harmony-seq input)
(if input
(parse-multiple input
(parse-harmony-seq (parse-constant (parse-harmony input) ","))
(parse-harmony input))
#f))
;; generate a list of harmonies from a string
(define (harmony-list-from-string str)
(let* ((parse-result (parse-harmony-seq (cons str '()))))
(if (and parse-result (= 0 (string-length (car parse-result))))
(reverse (cdr parse-result))
#f)))
;;---------------- harmonic transform functions ---------------------
;; white keys (starting at C) of a keyboard
(define harmony-scale '(0 2 4 5 7 9 11))
;; compute rotated version of white keys (starting at shift, where shift
;; is the number of the white key (i.e. 5 = fifth key = A)
(define (harmony-shift-scale shift)
(map
(lambda (note)
(modulo
(+ (- 12 (list-ref harmony-scale shift))
(list-ref
harmony-scale
(modulo
(+ (list-index harmony-scale note) shift)
(length harmony-scale))))
12))
harmony-scale))
;; all semitones
(define harmony-maj-maj '(0 1 2 3 4 5 6 7 8 9 10 11))
;; computes what to do with each semitone when transposing from
;; harmony srcscale to destscale
(define (harmony-shift srcscale destscale input)
(map
(lambda (note)
(let* ((scale (harmony-shift-scale srcscale))
(len (length scale))
(shift (modulo (- destscale srcscale) len))
(index (list-index scale note))
(delta (modulo
(if index
(- (list-ref scale (modulo (+ shift index) len)) (list-ref scale shift))
note)
12)))
delta))
input))
;; prints a list of things, writing out strings, and displaying other values
;; concludes with a newline
(define (myprint . l)
(if (null? l)
(newline)
(begin
(if (string? (car l))
(map write-char (string->list (car l)))
(display (car l)))
(apply myprint (cdr l)))))
;; transposes one midi note (integer) from one harmony in another
;; down indicates whether to go down rather than up
(define (harmony-transpose note from to down)
(let* ((rnote (modulo (- note (car from)) 12))
(srcscale (cdr from))
(destscale (cdr to))
(stransform (harmony-shift srcscale destscale harmony-maj-maj))
(delta (+
(- (list-ref stransform rnote) rnote)
(modulo (- (car to) (car from)) 12)
(if down -12 0))))
(+ note delta)))
;; harmonic transposer: implementation
(define (part-harmonic-transposer part harmony-string down-string)
(if (not (bse-is-part part))
(bse-exit-error 'text1 (_ "No valid part supplied")))
(let* ((notes (bse-part-list-selected-notes part))
(error-check-1 (if (< (length notes) 1) (bse-exit-error 'text1 (_ "No notes selected"))))
(get-note-start (lambda (rec) (bse-rec-get rec 'tick)))
(get-note-end (lambda (rec) (+ (get-note-start rec) (bse-rec-get rec 'duration))))
(start (apply min (map get-note-start notes)))
(end (apply max (map get-note-end notes)))
(len (- end start))
(harmony-list-all (harmony-list-from-string harmony-string))
(error-check-2 (if (not harmony-list-all) (bse-exit-error 'text1 (_ "Failed to parse harmony list"))))
(base-harmony (car harmony-list-all))
(harmony-list (cdr harmony-list-all))
(error-check-3 (if (< (length harmony-list) 1) (bse-exit-error 'text1 (_ "Harmony list is too short"))))
(down-harmony-list (harmony-list-from-string down-string))
(down-harmony (if down-harmony-list (car (harmony-list-from-string down-string)) #f))
(process-note
(lambda (rec harmony offset)
(bse-part-insert-note-auto
part
(+ offset (bse-rec-get rec 'tick))
(bse-rec-get rec 'duration)
(harmony-transpose (bse-rec-get rec 'note) base-harmony harmony
(>=
(modulo (- (car harmony) (car base-harmony)) 12)
(if down-harmony
(modulo (- (car harmony) (car down-harmony)) 12)
12)))
(bse-rec-get rec 'fine-tune)
(bse-rec-get rec 'velocity))
)))
(letrec ((process-harmonies (lambda (note harmony-list offset)
(if (null? harmony-list)
#t
(begin
(process-note note (car harmony-list) offset)
(process-harmonies note (cdr harmony-list) (+ offset len)))))))
(begin
(bse-item-group-undo part "part-harmonic-transposer")
(for-each
(lambda (note)
(process-harmonies note harmony-list len))
notes)
(bse-item-ungroup-undo part)))))
;; vim:set sw=2 sts=2 ts=8:
|