-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDreamScaler.control.js
More file actions
449 lines (394 loc) · 17.5 KB
/
Copy pathDreamScaler.control.js
File metadata and controls
449 lines (394 loc) · 17.5 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/**
* DreamScaler - Bitwig Studio Controller Script
*
* Tracks the current musical key (root note + scale type) via the
* controller settings panel. On every change, shows a popup notification
* and sends an OSC message to the DreamScaler LED bridge on localhost:9001.
*
* Scale Info section shows feelings, genre and usage of the selected scale.
* Browse Scales section allows filtering by category, mood and genre, and
* navigating through matching scales with Prev / Next buttons.
*
* LED Bridge: python/osc_bridge.py (start separately)
* OSC message: /dreamscaler/scale <rootIndex: int 0-11> <scaleName: string>
*/
loadAPI(25)
host.setShouldFailOnDeprecatedUse(true)
host.defineController('DreamScaler', 'DreamScaler', '1.0', 'd8e89ef4-f1c0-4c3a-a456-1a2b3c4d5e6f', 'DreamScaler')
const listRootNotes = ['C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'G#', 'A', 'Bb', 'B']
// ── Scales and scale data — generated from scales.json (single source of truth) ──
// Edit scales.json, then run: python generate_scales_js.py
host.load('scales_data.js')
// ── Browse / filter options ───────────────────────────────────────────────────
const CATEGORY_FILTER_OPTIONS = ['All', 'Common', 'Blues', 'Modal', 'Jazz', 'Exotic', 'Unusual']
const MOOD_FILTER_OPTIONS = [
'All', 'Bright', 'Dark', 'Melancholic', 'Dreamy',
'Bluesy', 'Groovy', 'Jazzy', 'Exotic', 'Oriental',
'Dramatic', 'Tense', 'Meditative'
]
const GENRE_FILTER_OPTIONS = [
'All', 'Rock / Blues', 'Jazz / Funk', 'Classical',
'Folk / Country', 'Film / Ambient', 'World / Ethnic',
'Metal', 'Pop', 'Experimental'
]
// Maps a Genre filter label to the genre_tags it matches
function genreOptionToTags(option) {
var map = {
'Rock / Blues': ['rock', 'blues'],
'Jazz / Funk': ['jazz', 'funk'],
'Classical': ['classical'],
'Folk / Country': ['folk', 'country'],
'Film / Ambient': ['film', 'ambient'],
'World / Ethnic': ['world', 'flamenco'],
'Metal': ['metal'],
'Pop': ['pop'],
'Experimental': ['experimental']
}
return map[option] || []
}
// ── Localisation ─────────────────────────────────────────────────────────────
var _CS_CATEGORY_MAP = {
'Vše': 'All', 'Běžné': 'Common', 'Blues': 'Blues',
'Modální': 'Modal', 'Jazz': 'Jazz', 'Exotické': 'Exotic', 'Neobvyklé': 'Unusual'
}
var _CS_GENRE_MAP = {
'Vše': 'All', 'Rock / Blues': 'Rock / Blues', 'Jazz / Funk': 'Jazz / Funk',
'Klasická': 'Classical', 'Folk / Country': 'Folk / Country',
'Film / Ambient': 'Film / Ambient', 'Světová / Etnická': 'World / Ethnic',
'Metal': 'Metal', 'Pop': 'Pop', 'Experimentální': 'Experimental'
}
var _CS_MOOD_MAP = {
'Vše': 'All', 'Světlý': 'Bright', 'Temný': 'Dark',
'Melancholický': 'Melancholic', 'Snivý': 'Dreamy',
'Bluesový': 'Bluesy', 'Groovy': 'Groovy', 'Jazzový': 'Jazzy',
'Exotický': 'Exotic', 'Orientální': 'Oriental', 'Dramatický': 'Dramatic',
'Napjatý': 'Tense', 'Meditativní': 'Meditative'
}
function makeLabels(lang) {
if (lang === 'Čeština') {
return {
sectionScale: 'Stupnice',
rootNote: 'Základní nota',
scaleType: 'Typ stupnice',
scaleDisplay: 'Zobrazení stupnice',
displayOn: 'Zap',
displayOff: 'Vyp',
sectionInfo: 'Info o stupnici',
feelings: 'Pocity',
genre: 'Žánr',
usage: 'Použití',
scalesRef: 'Přehled stupnic',
scalesUrl: 'https://github.com/tomasmark79/DreamScaler/blob/main/scales_cs.md',
scalesBtnLabel: 'Otevřít přehled stupnic ↗',
sectionBrowse: 'Procházet stupnice',
category: 'Kategorie',
mood: 'Nálada',
genreFilter: 'Žánr filtru',
prevScale: 'Předchozí stupnice',
nextScale: 'Další stupnice',
prevBtn: 'Předchozí',
nextBtn: 'Další',
filteredScales: 'Odpovídající stupnice',
sectionLED: 'LED',
ledIntensity: 'Intenzita LED',
notePlaying: 'Intenzita LED hraných not',
categoryOptions: ['Vše', 'Běžné', 'Blues', 'Modální', 'Jazz', 'Exotické', 'Neobvyklé'],
moodOptions: ['Vše', 'Světlý', 'Temný', 'Melancholický', 'Snivý',
'Bluesový', 'Groovy', 'Jazzový', 'Exotický', 'Orientální',
'Dramatický', 'Napjatý', 'Meditativní'],
genreOptions: ['Vše', 'Rock / Blues', 'Jazz / Funk', 'Klasická',
'Folk / Country', 'Film / Ambient', 'Světová / Etnická',
'Metal', 'Pop', 'Experimentální'],
mapCategory: function (v) { return _CS_CATEGORY_MAP[v] || v },
mapMood: function (v) { return _CS_MOOD_MAP[v] || v },
mapGenre: function (v) { return _CS_GENRE_MAP[v] || v }
}
}
return {
sectionScale: 'Scale',
rootNote: 'Root Note',
scaleType: 'Scale Type',
scaleDisplay: 'Scale Display',
displayOn: 'On',
displayOff: 'Off',
sectionInfo: 'Scale Info',
feelings: 'Feelings',
genre: 'Genre',
usage: 'Usage',
scalesRef: 'Scale Reference',
scalesUrl: 'https://github.com/tomasmark79/DreamScaler/blob/main/scales_en.md',
scalesBtnLabel: 'Open Scale Reference ↗',
sectionBrowse: 'Browse Scales',
category: 'Category',
mood: 'Mood',
genreFilter: 'Genre',
prevScale: 'Previous Scale',
nextScale: 'Next Scale',
prevBtn: 'Prev',
nextBtn: 'Next',
filteredScales: 'Matching Scales',
sectionLED: 'LEDs',
ledIntensity: 'LED Intensity',
notePlaying: 'Note Playing LED Intensity',
categoryOptions: CATEGORY_FILTER_OPTIONS,
moodOptions: MOOD_FILTER_OPTIONS,
genreOptions: GENRE_FILTER_OPTIONS,
mapCategory: function (v) { return v },
mapMood: function (v) { return v },
mapGenre: function (v) { return v }
}
}
// ── Active language — set in init() ──────────────────────────────────────────
var activeScales = SCALES_EN
var activeScaleData = SCALE_DATA_EN
// ── Filter state ──────────────────────────────────────────────────────────────
var filterCategory = 'All'
var filterMood = 'All'
var filterGenre = 'All'
// Reference to the scaleType setting (set in init, used by navigateScale)
var scaleTypeRef = null
// ── Filter helpers ────────────────────────────────────────────────────────────
function getFilteredScales() {
var result = []
for (var i = 0; i < SCALES_EN.length; i++) {
var name = SCALES_EN[i]
var data = SCALE_DATA_EN[name]
if (!data) continue
if (filterCategory !== 'All' && data.category !== filterCategory) continue
if (filterMood !== 'All') {
var moodTag = filterMood.toLowerCase()
if (data.mood_tags.indexOf(moodTag) === -1) continue
}
if (filterGenre !== 'All') {
var tags = genreOptionToTags(filterGenre)
var found = false
for (var t = 0; t < tags.length; t++) {
if (data.genre_tags.indexOf(tags[t]) !== -1) { found = true; break }
}
if (!found) continue
}
result.push(name)
}
return result
}
function navigateScale(direction) {
if (!scaleTypeRef) return
var filtered = getFilteredScales()
if (filtered.length === 0) {
println('DreamScaler: No scales match the current filter.')
return
}
// Resolve current display name to English
var currentDisplay = scaleTypeRef.get()
var currentENIdx = activeScales.indexOf(currentDisplay)
var currentEN = (currentENIdx >= 0) ? SCALES_EN[currentENIdx] : currentDisplay
var filteredIdx = filtered.indexOf(currentEN)
var nextIdx
if (filteredIdx === -1) {
nextIdx = (direction === 1) ? 0 : filtered.length - 1
} else {
nextIdx = (filteredIdx + direction + filtered.length) % filtered.length
}
var nextEN = filtered[nextIdx]
// Map to active display language
var nextDisplay = nextEN
if (activeScales !== SCALES_EN) {
var enIdx = SCALES_EN.indexOf(nextEN)
if (enIdx >= 0) nextDisplay = activeScales[enIdx]
}
scaleTypeRef.set(nextDisplay)
println('DreamScaler: [' + (nextIdx + 1) + '/' + filtered.length + '] ' + nextEN)
}
// ── Playing-notes state ───────────────────────────────────────────────────────
var _prevNotes = {} // { midiPitch: velocity }
// ── OSC bridge ────────────────────────────────────────────────────────────────
const OSC_HOST = '127.0.0.1'
const OSC_PORT = 9001
var oscOut = null
// ── init ──────────────────────────────────────────────────────────────────────
function init() {
println('-- DreamScaler Go! --')
println('DreamScaler: Loaded ' + SCALES_EN.length + ' scales')
const prefs = host.getPreferences()
const langSetting = prefs.getEnumSetting('Language / Jazyk', 'DreamScaler', ['English', 'Čeština'], 'English')
const lang = langSetting.get()
if (lang === 'Čeština') {
activeScales = SCALES_CS
activeScaleData = SCALE_DATA_CS
} else {
activeScales = SCALES_EN
activeScaleData = SCALE_DATA_EN
}
println('DreamScaler: Language = ' + lang)
const L = makeLabels(lang)
langSetting.addValueObserver(function (newLang) {
println('DreamScaler: Language changed to "' + newLang + '" – please reload the controller to apply.')
})
// OSC output connection to LED bridge
try {
const oscModule = host.getOscModule()
oscOut = oscModule.connectToUdpServer(OSC_HOST, OSC_PORT, oscModule.createAddressSpace())
println('DreamScaler: OSC connected to ' + OSC_HOST + ':' + OSC_PORT)
} catch (e) {
println('DreamScaler: OSC init failed – ' + e)
}
const documentState = host.getDocumentState()
// ── Scale selection (registered first so section appears at top) ─────────
const rootNote = documentState.getEnumSetting(L.rootNote, L.sectionScale, listRootNotes, 'C')
const scaleType = documentState.getEnumSetting(L.scaleType, L.sectionScale, activeScales, activeScales[0])
scaleTypeRef = scaleType
const scaleDisplay = documentState.getEnumSetting(L.scaleDisplay, L.sectionScale, [L.displayOn, L.displayOff], L.displayOn)
// ── Scale Info (auto-updated display fields) ──────────────────────────────
const displayFeelings = documentState.getStringSetting(L.feelings, L.sectionInfo, 80, '-')
const displayGenre = documentState.getStringSetting(L.genre, L.sectionInfo, 80, '-')
const displayUsage = documentState.getStringSetting(L.usage, L.sectionInfo, 100, '-')
const scalesRefBtn = documentState.getSignalSetting(L.scalesRef, L.sectionInfo, L.scalesBtnLabel)
scalesRefBtn.addSignalObserver(function () {
println('DreamScaler: open_scales_ref clicked, oscOut=' + (oscOut !== null ? 'ok' : 'null'))
if (oscOut !== null) {
oscOut.sendMessage('/dreamscaler/open_scales_ref', L.scalesUrl)
println('DreamScaler: sent open_scales_ref -> ' + L.scalesUrl)
} else {
println('DreamScaler: OSC not connected, cannot open URL')
}
})
// ── Browse Scales — filters ───────────────────────────────────────────────
const categoryFilter = documentState.getEnumSetting(L.category, L.sectionBrowse, L.categoryOptions, L.categoryOptions[0])
const moodFilter = documentState.getEnumSetting(L.mood, L.sectionBrowse, L.moodOptions, L.moodOptions[0])
const genreFilter = documentState.getEnumSetting(L.genreFilter, L.sectionBrowse, L.genreOptions, L.genreOptions[0])
// ── Browse Scales — matching scales display (read-only text) ──────────────
const displayFilteredScales = documentState.getStringSetting(L.filteredScales, L.sectionBrowse, 300, '')
function updateFilterDisplay() {
var filtered = getFilteredScales()
var n = filtered.length
if (n === 0) {
displayFilteredScales.set('–')
return
}
var names = []
for (var i = 0; i < filtered.length; i++) {
var enName = filtered[i]
if (activeScales !== SCALES_EN) {
var enIdx = SCALES_EN.indexOf(enName)
names.push(enIdx >= 0 ? activeScales[enIdx] : enName)
} else {
names.push(enName)
}
}
var full = n + ': ' + names.join(', ')
if (full.length > 298) { full = full.substring(0, 295) + '...' }
displayFilteredScales.set(full)
}
// ── Browse Scales — navigation buttons ───────────────────────────────────
const prevSignal = documentState.getSignalSetting(L.prevScale, L.sectionBrowse, L.prevBtn)
const nextSignal = documentState.getSignalSetting(L.nextScale, L.sectionBrowse, L.nextBtn)
// ── LED brightness settings ───────────────────────────────────────────────
const ledIntensity = documentState.getNumberSetting(L.ledIntensity, L.sectionLED, 1, 30, 1, '', 1)
const notePlaying = documentState.getNumberSetting(L.notePlaying, L.sectionLED, 1, 50, 1, '', 10)
ledIntensity.addRawValueObserver(function (value) {
if (oscOut !== null) {
oscOut.sendMessage('/dreamscaler/led_intensity', Math.round(value))
}
})
notePlaying.addRawValueObserver(function (value) {
if (oscOut !== null) {
oscOut.sendMessage('/dreamscaler/note_playing_intensity', Math.round(value))
}
})
function updateInfoDisplay(displayName, root) {
var data = activeScaleData[displayName]
if (!data) {
var idx = activeScales.indexOf(displayName)
if (idx >= 0) data = SCALE_DATA_EN[SCALES_EN[idx]]
}
if (data) {
var feelings = data.feelings
if (root && data.feelings_by_root && data.feelings_by_root[root]) {
feelings = data.feelings_by_root[root]
}
displayFeelings.set(feelings.charAt(0).toLowerCase() + feelings.slice(1))
displayGenre.set(data.genre)
displayUsage.set(data.usage || '')
}
}
rootNote.addValueObserver(function (root) {
updateInfoDisplay(scaleType.get(), root)
notifyScale(root, scaleType.get())
})
scaleType.addValueObserver(function (type) {
updateInfoDisplay(type, rootNote.get())
notifyScale(rootNote.get(), type)
})
scaleDisplay.addValueObserver(function (val) {
var enabled = (val === L.displayOn) ? 1 : 0
println('DreamScaler: Scale Display = ' + val)
if (oscOut !== null) {
oscOut.sendMessage('/dreamscaler/scale_display', enabled)
}
})
categoryFilter.addValueObserver(function (val) {
filterCategory = L.mapCategory(val)
println('DreamScaler: Filter → Category=' + filterCategory + ' matches=' + getFilteredScales().length)
updateFilterDisplay()
})
moodFilter.addValueObserver(function (val) {
filterMood = L.mapMood(val)
println('DreamScaler: Filter → Mood=' + filterMood + ' matches=' + getFilteredScales().length)
updateFilterDisplay()
})
genreFilter.addValueObserver(function (val) {
filterGenre = L.mapGenre(val)
println('DreamScaler: Filter → Genre=' + filterGenre + ' matches=' + getFilteredScales().length)
updateFilterDisplay()
})
prevSignal.addSignalObserver(function () { navigateScale(-1) })
nextSignal.addSignalObserver(function () { navigateScale(1) })
// ── Playing notes observer ────────────────────────────────────────────────
var cursorTrack = host.createCursorTrack(0, 0)
var playingNotes = cursorTrack.playingNotes()
playingNotes.markInterested()
playingNotes.addValueObserver(function (notes) {
updateNotes(notes)
})
}
/**
* Diffs the new playing-notes snapshot against the previous one and sends
* individual note_on / note_off OSC messages to the LED bridge.
*/
function updateNotes(notes) {
if (oscOut === null) return
var currNotes = {}
for (var i = 0; i < notes.length; i++) {
var p = notes[i].pitch()
var v = notes[i].velocity()
currNotes[p] = v
}
for (var pitch in currNotes) {
oscOut.sendMessage('/dreamscaler/note_on', parseInt(pitch, 10), currNotes[pitch])
}
for (var pitch in _prevNotes) {
if (!(pitch in currNotes)) {
oscOut.sendMessage('/dreamscaler/note_off', parseInt(pitch, 10))
}
}
_prevNotes = currNotes
}
function flush() { }
function exit() {
println('-- DreamScaler Bye! --')
}
function notifyScale(root, type) {
// Popup supports ASCII only — always use English names and feelings
const scaleIndex = activeScales.indexOf(type)
const enName = (scaleIndex >= 0) ? SCALES_EN[scaleIndex] : type
const enInfo = SCALE_DATA_EN[enName]
var feelings = enInfo ? enInfo.feelings : ''
if (enInfo && enInfo.feelings_by_root && enInfo.feelings_by_root[root]) {
feelings = enInfo.feelings_by_root[root]
}
println('DreamScaler: ' + root + ' ' + enName + (feelings ? ' - ' + feelings : ''))
if (oscOut !== null) {
const rootIndex = listRootNotes.indexOf(root)
oscOut.sendMessage('/dreamscaler/scale', rootIndex, enName)
}
}