Unable to implement \include to create a lilypond stylesheet - lilypond

I've got a working Lilypond file that's about 80% style instructions that I'd like to use as a template for other songs. Rather than copy/paste, I'd rather put it in a stylesheet for obvious reasons. I'm pretty sure this is doable, but following the Lilypond documentation just results in me breaking the file.
\version "2.18.2" % necessary for upgrading to future LilyPond versions.
\header{
title = "Exercise: C, D, E Notes"
tagline = "" % removed
}
\paper{
#(set-paper-size "arch a" 'landscape)
system-system-spacing.basic-distance = #20
system-system-spacing.minimum-distance = #20
score-system-spacing.basic-distance = #20
markup-system-spacing.basic-distance = #15
indent = 0\cm
}
%Text instructions for each section
mark_A = ^\markup { \small Fingering }^\markup { \bold "Treble Clef - Right Hand" }
mark_B = ^\markup { \small Fingering }^\markup { \bold "Bass Clef - Left Hand" }
%Association list of pitches to colors.
#(define color-mapping
(list
(cons (ly:make-pitch 0 0 -1/2) (rgb-color 0 0 0))
(cons (ly:make-pitch 0 0 0) (rgb-color 0 0 0))
(cons (ly:make-pitch 0 0 1/2) (rgb-color 0 0 0))
(cons (ly:make-pitch 0 1 -1/2) (rgb-color 0.8359375 0.64453125 0.796875))
(cons (ly:make-pitch 0 1 0) (rgb-color 0.8359375 0.64453125 0.796875))
(cons (ly:make-pitch 0 1 1/2) (rgb-color 0.8359375 0.64453125 0.796875))
(cons (ly:make-pitch 0 2 -1/2) (rgb-color 0.4453125 0.7421875 0.2655625))
(cons (ly:make-pitch 0 2 0) (rgb-color 0.4453125 0.7421875 0.2655625))
(cons (ly:make-pitch 0 2 1/2) (rgb-color 0.4453125 0.7421875 0.2655625))
(cons (ly:make-pitch 0 3 -1/2) (rgb-color 0.96875 0.6171875 0.15234375))
(cons (ly:make-pitch 0 3 0) (rgb-color 0.96875 0.6171875 0.15234375))
(cons (ly:make-pitch 0 3 1/2) (rgb-color 0.96875 0.6171875 0.15234375))
(cons (ly:make-pitch 0 4 -1/2) (rgb-color 0 0.67578125 0.9296875))
(cons (ly:make-pitch 0 4 0) (rgb-color 0 0.67578125 0.9296875))
(cons (ly:make-pitch 0 4 1/2) (rgb-color 0 0.67578125 0.9296875))
(cons (ly:make-pitch 0 5 -1/2) (rgb-color 0.91796875 0 0.54296875))
(cons (ly:make-pitch 0 5 0) (rgb-color 0.91796875 0 0.54296875))
(cons (ly:make-pitch 0 5 1/2) (rgb-color 0.91796875 0 0.54296875))
(cons (ly:make-pitch 0 6 -1/2) (rgb-color 0.671875 0.39453125 0.0546875))
(cons (ly:make-pitch 0 6 0) (rgb-color 0.671875 0.39453125 0.0546875))
(cons (ly:make-pitch 0 6 1/2) (rgb-color 0.671875 0.39453125 0.0546875))
))
%Compare pitch and alteration (not octave).
#(define (pitch-equals? p1 p2)
(and
(= (ly:pitch-alteration p1) (ly:pitch-alteration p2))
(= (ly:pitch-notename p1) (ly:pitch-notename p2))))
#(define (pitch-to-color pitch)
(let ((color (assoc pitch color-mapping pitch-equals?)))
(if color
(cdr color))))
#(define (color-notehead grob)
(pitch-to-color
(ly:event-property (ly:grob-property grob 'cause) 'pitch)))
%BEGINNING OF ACTUAL SHEET MUSIC
music_A = \relative c' {
\time 4/4
\override Staff.TimeSignature #'style = #'()
\clef "treble"
\override NoteHead #'color = #color-notehead
c4-\mark_A-1 c-1 c-1 c-1 | d-2 d-2 d-2 d-2 | e-3 e-3 e-3 e-3 | e2-3 e-3 | \break
e4-3 e-3 e-3 e-3 | d-2 d-2 d-2 d-2 | c-1 c-1 c-1 c-1 | c2-1 c-1 | \bar "|." \break
}
music_B = \relative c {
\time 4/4
\override Staff.TimeSignature #'style = #'()
\clef "bass"
\override NoteHead #'color = #color-notehead
e4-\mark_B-3 e-3 e-3 e-3 | d-4 d-4 d-4 d-4 | c-5 c-5 c-5 c-5 | c2-5 c-5 | \break
e4-3 e-3 e-3 e-3 | d-4 d-4 d-4 d-4 | c-5 c-5 c-5 c-5 | c2-5 c-5 | \bar "|."
}
%Score Engraving
\score{
\new Staff \music_A
\layout{}
}
\score{
\new Staff \music_B
\layout{}
}
\score{
\new Staff {\music_A \music_B}
\midi{
\tempo 4 = 144
}
}
Ideally I'd like to pull the \paper instructions and the large pitch-to-color section into their own template and include them. I'm sure the solution is obvious, but for some reason Lilypond's documentation doesn't agree with me.

Turns out it IS really simple. The documentation references includes in two sections - one (the one I found first) runs through a very convoluted scenario and involves defining variables, etc. The second (much simpler), simply uses:
\include "filename.ly"
and does a paste-in-place of whatever is in that file. Sorry for the time-waste, but hopefully someone down the road who has the same brain-fart I just did will find this helpful.

Related

Function to count number of 0 in given arguments in lisp

I want to create a function in LISP
to count the number of 0 in given arguments
Ex
(count_number_of_0 '(1 0 5 9 0 0 0 7 1 0) )
Output : 5
Here is an implementation in Racket, which is a lisp-family language. It would be quite easy to translate into Common Lisp (but a little more verbose in CL):
(define make-counter
(λ (v same?)
(λ (l)
((λ (c)
(c c 0 l))
(λ (c a t)
(if (null? t)
a
(c c (if (same? (first t) v) (+ a 1) a) (rest t))))))))
(define count-zeros
(make-counter 0 =))
And now
> (count-zeros '(1 2 0 3 4 0))
2
one way is:
(defun count-number-of-0 (lst &optional (cnt 0)) ;counter starts at zero
(if lst
(if (and (numberp (car lst)) ;better verify that element is a number
(= 0 (car lst)))
(progn
(setq cnt (+ cnt 1))
(count-number-of-0 (cdr lst) cnt))
(count-number-of-0 (cdr lst) cnt))
cnt)) ;return counter
This should work in all implementations of common-lisp.

Reverse engineering python .exe, can not find strings

I started with reverse engineering and using the IDA disassembler tool.
I wrote some programs in C++, made an .exe and reversed it in IDA to "hack" my own programs.
Now I wanted to do the same with a python program. As a start a made this simple program:
inp = input("What is your name?\n")
print("Hi", inp)
Then I built an .exe using:
pyinstaller main.py
Loaded it into IDA and searched for strings. IDA gives me a lot of strings but "What is your name" is not in its list. Why is that?
Yes, pyinstaller builds an executable but it is not a "normal" executable. Your Python code is actually in a compressed archive.
$ readelf -S main
There are 30 section headers, starting at offset 0x1ad400:
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .interp PROGBITS 00000000000002a8 000002a8
000000000000001c 0000000000000000 A 0 0 1
[ 2] .note.ABI-tag NOTE 00000000000002c4 000002c4
0000000000000020 0000000000000000 A 0 0 4
[ 3] .note.gnu.bu[...] NOTE 00000000000002e4 000002e4
0000000000000024 0000000000000000 A 0 0 4
[ 4] .gnu.hash GNU_HASH 0000000000000308 00000308
0000000000000034 0000000000000000 A 5 0 8
[ 5] .dynsym DYNSYM 0000000000000340 00000340
00000000000007b0 0000000000000018 A 6 1 8
[ 6] .dynstr STRTAB 0000000000000af0 00000af0
0000000000000348 0000000000000000 A 0 0 1
[ 7] .gnu.version VERSYM 0000000000000e38 00000e38
00000000000000a4 0000000000000002 A 5 0 2
[ 8] .gnu.version_r VERNEED 0000000000000ee0 00000ee0
00000000000000a0 0000000000000000 A 6 3 8
[ 9] .rela.dyn RELA 0000000000000f80 00000f80
0000000000000198 0000000000000018 A 5 0 8
[10] .rela.plt RELA 0000000000001118 00001118
00000000000006d8 0000000000000018 AI 5 24 8
[11] .init PROGBITS 0000000000002000 00002000
0000000000000017 0000000000000000 AX 0 0 4
[12] .plt PROGBITS 0000000000002020 00002020
00000000000004a0 0000000000000010 AX 0 0 16
[13] .plt.got PROGBITS 00000000000024c0 000024c0
0000000000000008 0000000000000008 AX 0 0 8
[14] .text PROGBITS 00000000000024d0 000024d0
0000000000003f21 0000000000000000 AX 0 0 16
[15] .fini PROGBITS 00000000000063f4 000063f4
0000000000000009 0000000000000000 AX 0 0 4
[16] .rodata PROGBITS 0000000000007000 00007000
0000000000001618 0000000000000000 A 0 0 8
[17] .eh_frame_hdr PROGBITS 0000000000008618 00008618
000000000000025c 0000000000000000 A 0 0 4
[18] .eh_frame PROGBITS 0000000000008878 00008878
0000000000000e90 0000000000000000 A 0 0 8
[19] .init_array INIT_ARRAY 000000000000ad70 00009d70
0000000000000008 0000000000000008 WA 0 0 8
[20] .fini_array FINI_ARRAY 000000000000ad78 00009d78
0000000000000008 0000000000000008 WA 0 0 8
[21] .data.rel.ro PROGBITS 000000000000ad80 00009d80
0000000000000040 0000000000000000 WA 0 0 32
[22] .dynamic DYNAMIC 000000000000adc0 00009dc0
0000000000000210 0000000000000010 WA 6 0 8
[23] .got PROGBITS 000000000000afd0 00009fd0
0000000000000028 0000000000000008 WA 0 0 8
[24] .got.plt PROGBITS 000000000000b000 0000a000
0000000000000260 0000000000000008 WA 0 0 8
[25] .data PROGBITS 000000000000b260 0000a260
0000000000000010 0000000000000000 WA 0 0 8
[26] .bss NOBITS 000000000000b280 0000a270
00000000000172c0 0000000000000000 WA 0 0 32
[27] .comment PROGBITS 0000000000000000 0000a270
000000000000001c 0000000000000001 MS 0 0 1
[28] pydata PROGBITS 0000000000000000 0000a28c
00000000001a3066 0000000000000000 0 0 1
[29] .shstrtab STRTAB 0000000000000000 001ad2f2
000000000000010b 0000000000000000 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
l (large), p (processor specific)
$
Note the pydata section!
You can use archive_viewer.py to view the archives in main:
$ python archive_viewer.py main
pos, length, uncompressed, iscompressed, type, name
[(0, 215, 285, 1, 'm', 'struct'),
(215, 1059, 1792, 1, 'm', 'pyimod01_os_path'),
(1274, 4080, 8938, 1, 'm', 'pyimod02_archive'),
(5354, 5518, 13005, 1, 'm', 'pyimod03_importers'),
(10872, 1825, 4051, 1, 's', 'pyiboot01_bootstrap'),
(12697, 1161, 2135, 1, 's', 'pyi_rth_multiprocessing'),
(13858, 125, 143, 1, 's', 'main'),
(13983, 1701919, 1701919, 0, 'z', 'PYZ-00.pyz')]
? x main
to filename? main.x
? q
$ xxd main.x
00000000: e300 0000 0000 0000 0000 0000 0000 0000 ................
00000010: 0003 0000 0040 0000 0073 1600 0000 6500 .....#...s....e.
00000020: 6400 8301 5a01 6502 6401 6501 8302 0100 d...Z.e.d.e.....
00000030: 6402 5300 2903 7a13 5768 6174 2069 7320 d.S.).z.What is
00000040: 796f 7572 206e 616d 653f 0ada 0248 694e your name?...HiN
00000050: 2903 da05 696e 7075 74da 0369 6e70 da05 )...input..inp..
00000060: 7072 696e 74a9 0072 0500 0000 7205 0000 print..r....r...
00000070: 007a 076d 6169 6e2e 7079 da08 3c6d 6f64 .z.main.py..<mod
00000080: 756c 653e 0100 0000 7302 0000 0008 01 ule>....s......
$
As you can see from the above output the string you are looking for is stored in a compressed archive called main which in turn is part of the main executable build by pyinstaller.

Lilypond: Change color of notes below and above a certain pitch

While writing a Lilypond score for recorders (flutes), I wish I could automatically mark notes with pitches beyond the range of an instrument by changing its color.
The idea is that, for example, all absolute pitches below f and all pitches above g'' are colored red for the bass instrument. The same for tenor, alt and soprano instruments.
I found a helpful question on coloring notes, but there remains a piece of code I cannot write:
#(define (ambitus-notehead-alt grob)
( **code_i_cannot_write** )
#(define (ambitus-notehead-tenor grob)
( **code_i_cannot_write** )
#(define (ambitus-notehead-bass grob)
( **code_i_cannot_write** )
\score {
\new Staff \relative c' {
\override NoteHead #'color = #ambitus-notehead-alt
\music_altrecorder
}
\new Staff \relative c' {
\override NoteHead #'color = #ambitus-notehead-tenor
\music_tenorrecorder
}
\new Staff \relative c' {
\override NoteHead #'color = #ambitus-notehead-bass
\music_bassrecorder
}
}
Here is a function that does what you want:
\version "2.19.82"
#(define (colour-out-of-range grob)
(let* ((pch (ly:event-property (event-cause grob) 'pitch))
(semitones (ly:pitch-semitones pch)))
(cond ((< semitones 0) red)
((> semitones 24) red)
(else black))))
\score {
\new Staff \relative c' {
\override NoteHead.color = #colour-out-of-range
g8 a b c d e f g a b c d e f g a b c d e f g
}
}
Producing:
To customize it for your instrument's range, change the values of (< semitones 0) and (> semitones 24). The value 0 is the middle C (C4), and increments of 1 are equal to one semitone. So in the case above, the range is between C4-C6. You need to use negative values for pitches below middle C (e.g. -5 is G3).

Multiplication of Binary List in Scheme

I'm trying to implement an algorithm to multiply two bit-lists of 1s and 0s as a simulation to binary multiplication. It should return a like list, but I am having a hard time building on what I already have. Some help would be appreciated...
;;Function designed to accept two bit-list binary numbers (reverse order) and produce their product, a bitlist in reverse order.
;;Example: (multiply '(0 1 1 0 1) '(1 0 1)) should produce '(0 1 1 1 0 1 1)
(define (multiply x y)
(cond
;[(= null? y) 0]
[(zero? y) 0]
(#t (let ((z (multiply x (rest y )))) (cond
[(num_even? y) (cons 0 z)]
(#t (addWithCarry x (cons 0 z) 1)))))))
;This is to check if the current value of parameter x is the number 0
(define (zero? x)
(cond
((null? x) #t)
((=(first x) 1) #f)
(#t (zero? (rest x)))))
;This is to check if the current parameter x is 0 (even) or not.
(define (num_even? x)
(cond
[(null? x) #t]
[(=(first x) 0)#t]
[#t (num_even? (rest x))]))
;To add two binary numbers
(define(addWithCarry x y carry)
(cond
((and (null? x) (null? y)) (if (= carry 0) '( ) '(1)))
((null? x) (addWithCarry '(0) y carry))
((null? y) (addWithCarry x '(0) carry))
(#t (let ((bit1 (first x))
(bit2 (first y)))
(cond
((=(+ bit1 bit2 carry) 0) (cons 0 (addWithCarry (rest x)(rest y) 0)))
((=(+ bit1 bit2 carry) 1) (cons 1 (addWithCarry (rest x)(rest y) 0)))
((=(+ bit1 bit2 carry) 2) (cons 0 (addWithCarry (rest x)(rest y) 1)))
(#t (cons 1 (addWithCarry (rest x) (rest y) 1))))))))
Based on my previous answer for a base-10 multiplication, here's a solution that works for binary numbers (in the correct order):
(define base 2)
(define (car0 lst)
(if (empty? lst)
0
(car lst)))
(define (cdr0 lst)
(if (empty? lst)
empty
(cdr lst)))
(define (apa-add l1 l2) ; apa-add (see https://stackoverflow.com/a/19597007/1193075)
(let loop ((l1 (reverse l1))
(l2 (reverse l2))
(carry 0)
(res '()))
(if (and (null? l1) (null? l2) (= 0 carry))
res
(let* ((d1 (car0 l1))
(d2 (car0 l2))
(ad (+ d1 d2 carry))
(dn (modulo ad base)))
(loop (cdr0 l1)
(cdr0 l2)
(quotient (- ad dn) base)
(cons dn res))))))
(define (mult1 n lst) ; multiply a list by one digit
(let loop ((lst (reverse lst))
(carry 0)
(res '()))
(if (and (null? lst) (= 0 carry))
res
(let* ((c (car0 lst))
(m (+ (* n c) carry))
(m0 (modulo m base)))
(loop (cdr0 lst)
(quotient (- m m0) base)
(cons m0 res))))))
(define (apa-multi l1 l2) ; full multiplication
(let loop ((l2 (reverse l2))
(app '())
(res '()))
(if (null? l2)
res
(let* ((d2 (car l2))
(m (mult1 d2 l1))
(r (append m app)))
(loop (cdr l2)
(cons '0 app)
(apa-add r res))))))
so that
(apa-multi '(1 0 1 1 0) '(1 0 1))
=> '(1 1 0 1 1 1 0)

LISP function to remove nils

I want to write a function in LISP that will completely remove all NILS in a list. The list may be nested, meaning it can contain other lists inside. For example the list '((state L L L L) NIL (state L L R L) NIL) should be tranformed into '((STATE L L L L) (STATE L L R L)).
(defun remove-nil-recursively (x)
(if (listp x)
(mapcar #'remove-nil-recursively
(remove nil x))
x))
Works for your example:
[1]> (remove-nil-recursively '((state L L L L) NIL (state L L R L) NIL))
((STATE L L L L) (STATE L L R L))
And with nested lists:
[2]> (remove-nil-recursively '(NIL (state L L nil R L) NIL))
((STATE L L R L))
But watch out:
[3]> (remove-nil-recursively '(NIL (state L L (nil) R L) NIL))
((STATE L L NIL R L))
Paul Graham calls this function (recurring into sublists remove-if) "prune" in On Lisp, p. 49. It is one of the utility functions.
(defun prune (test tree)
(labels ((rec (tree acc)
(cond
((null tree) (nreverse acc))
((consp (car tree))
(rec (cdr tree)
(cons (rec (car tree) nil) acc)))
(t (rec (cdr tree)
(if (funcall test (car tree))
acc
(cons (car tree) acc)))))))
(rec tree nil)))
(prune #'evenp '(1 2 (3 (4 5) 6) 7 8 (9)))
(1 (3 (5)) 7 (9))
A generic function in the style of remove-if:
(defun remove-all (predic seq &optional res)
(if (null seq)
(reverse res)
(cond ((and (not (null (car seq))) (listp (car seq)))
(remove-all predic (cdr seq)
(cons (remove-all predic (car seq)) res)))
((funcall predic (car seq))
(remove-all predic (cdr seq) res))
(t (remove-all predic (cdr seq) (cons (car seq) res))))))
Examples:
> (remove-all #'null (list 1 2 'nil 3))
=> (1 2 3)
> (remove-all #'null (list 1 2 'nil '(4 5 nil 6) 3))
=> (1 2 (4 5 6) 3)
> (remove-all #'(lambda (x) (oddp x)) '(1 2 (3 4) 5 6 (7 8 (9 10))))
=> (2 (4) 6 (8 (10)))
(defun remove-if-nil (list) (remove-if-not 'identity list))
remove-if-not takes a predicate and a list, and removes all the items in the list that do not satisfy the predicate, that is, that return nil when evaluated in the predicate. identity, as you can guess, returns exactly the same thing it takes, so (remove-if-not 'identity list) removes every element in list that is nil.