«parse scale files into Tuning» by LFSaw

on 28 Jul'23 16:56 in tuningparserscala

A "parser" for Scala tunings as suggested here: https://scsynth.org/t/scales-and-tunings-without-scale-or-tuning/8014/7

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
// read all scales provided at
// https://www.huygens-fokker.org/microtonality/scales.html
// turn into a dictionary with keys equal to filenames

// a "parser" based on description at
// https://www.huygens-fokker.org/scala/seq_format.html
// 2023, Till Bovermann | https://tai-studio.org


// download scl files from https://www.huygens-fokker.org/docs/scales.zip
// unzip with
// unzip -aa scales.zip to a directory

// example path:
var path = "/Users/tboverma/Downloads/scl";

t = "%/*.scl".format(path).pathMatch.collectAs({|path|
	var rawContents, description, numKeys, degrees, octaveRatio, tuning, a, b;

	// read file, ignore comments
	rawContents = File.readAllString(path).split($\n).select{|row| row.first != $! };


	// description is in first line
	description = rawContents[0].replace(13.asAscii, "");
	// number of elements is in second line
	numKeys = rawContents[1].asInteger;

	// read only numKeys elements
	degrees = numKeys.collect{|i|
		// strip leading white space and ignore everything after first white space
		var val = rawContents[2 + i].stripWhiteSpace.split($ ).first;

		val.includes($.).if({
			// we see cents
			val.asFloat/100
		}, {
			// we see fractions
			val.includes($/).if({
				#a, b = val.split($/).collect(_.asInteger);
				a/b
			}, {
				val.asInteger
			}) - 1 * 12
		})
	};

	// octave ratio is last element
	octaveRatio = degrees.last / 12 + 1;

	// a Tuning object requires a leading 0 and omits the octave jump
	degrees = [0] ++ degrees.drop(-1);

	// create a Tuning object
	tuning = Tuning(degrees, octaveRatio, description);
	
	path.basename.splitext.first.asSymbol -> tuning
}, Event);
raw 1699 chars (focus & ctrl+a+c to copy)
reception
comments