«Show a chord in music notation in a sclang window» by 56228375

on 26 Aug'23 15:17 in guinotation

showing a chord in a supercollider window in music notation using the Fosc quark

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
// this code relies on the Fosc quark. Very capable, but mostly undocumented.
// Here's the result of half a day of searching and grepping... :)
(
var svg;
var img;
var options;
var window;
var score;
var chord;
var staff;
var chordview;

// configure path to lilypond
Fosc.lilypondPath = "/usr/bin/lilypond";

// make a chord
chord = FoscChord("c' e' fs' gs' bf'", 1); //or: chord = FoscChord([60, 67, 69], 1);
// display the chord with easy heads (comment out for normal note heads)
chord.attach(FoscLilyPondLiteral("\\easyHeadsOn"));
// embed the chord in a staff
staff = FoscStaff([
	FoscVoice([
		chord
	])
]);
// don't show time signature, clef and bar lines
staff.removeCommands.add('Time_signature_engraver');
staff.removeCommands.add('Clef_engraver');
staff.removeCommands.add('Bar_engraver');

// make sure the produced svg file is cropped; use a large staff size for sufficient resolution
// use a hack to make lilypond crop the svg file since Fosc doesn't expose lilypond command line arguments, and I didn't find another way to insert raw lilypond code
score = FoscLilyPondFile(["#(ly:set-option 'crop #t)", staff], staffSize:40);
// write chord.svg and also chord.cropped.svg
svg = score.writeSVG(path:PathName.tmp +/+ "chord.svg", clean:false);
// open the cropped svg
img = Image.openSVG(PathName.tmp +/+ "chord.cropped.svg");

// embed the generated svg in a window
window = Window.new("test", Rect(100,100,150,100)).front;
window.view.background_(Color.new(153,255, 102));
chordview = UserView(window);
chordview.drawFunc = {
	|uview|
	var viewwidth = uview.bounds.width;
	var viewheight = uview.bounds.height;
	var imgwidth = img.width;
	var imgheight = img.height;
	var left = (viewwidth - imgwidth)/2;
	var top = (viewheight - imgheight)/2;
	img.drawAtPoint(left@top); // draw with fixed size centered in user view
};
a = ({ Button(window) } ! 3) ++ chordview.minSize_(100@100) ++ ({ Button(window) } ! 3);
window.layout = HLayout(*a);
)
raw 2012 chars (focus & ctrl+a+c to copy)
reception
comments