feat: added Tune scales to Xen, improved documentation re uzu/strudel#1944
This commit is contained in:
parent
47ad4bb171
commit
14cc5d989b
4 changed files with 37 additions and 14 deletions
|
|
@ -7,12 +7,16 @@ This program is free software: you can redistribute it and/or modify it under th
|
||||||
import Tune from './tunejs.js';
|
import Tune from './tunejs.js';
|
||||||
import { register } from '@strudel/core';
|
import { register } from '@strudel/core';
|
||||||
|
|
||||||
|
// Tune.scale seems to be in ratio format
|
||||||
|
//
|
||||||
|
|
||||||
export const tune = register('tune', (scale, pat) => {
|
export const tune = register('tune', (scale, pat) => {
|
||||||
const tune = new Tune();
|
const tune = new Tune();
|
||||||
if (!tune.isValidScale(scale)) {
|
if (!tune.isValidScale(scale)) {
|
||||||
throw new Error('not a valid tune.js scale name: "' + scale + '". See http://abbernie.github.io/tune/scales.html');
|
throw new Error('not a valid tune.js scale name: "' + scale + '". See http://abbernie.github.io/tune/scales.html');
|
||||||
}
|
}
|
||||||
tune.loadScale(scale);
|
tune.loadScale(scale);
|
||||||
|
// if the tonic is a frequency, why are we putting in "1"
|
||||||
tune.tonicize(1);
|
tune.tonicize(1);
|
||||||
return pat.withHap((hap) => {
|
return pat.withHap((hap) => {
|
||||||
return hap.withValue(() => tune.note(hap.value));
|
return hap.withValue(() => tune.note(hap.value));
|
||||||
|
|
|
||||||
|
|
@ -78,14 +78,21 @@ Tune.prototype.frequency = function(stepIn, octaveIn) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// which scale degree (0 - scale length) is our input
|
// which scale degree (0 - scale length) is our input
|
||||||
|
// 60 % 12 = 0
|
||||||
var scaleDegree = stepIn % this.scale.length
|
var scaleDegree = stepIn % this.scale.length
|
||||||
|
|
||||||
|
// what's this doing
|
||||||
|
// 0 scaleDegree = 12
|
||||||
|
// seems to simply be for a negative result from above, maybe another way to do it, but ok for now
|
||||||
while (scaleDegree < 0) {
|
while (scaleDegree < 0) {
|
||||||
scaleDegree += this.scale.length
|
scaleDegree += this.scale.length
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tonic is currently always 1
|
||||||
|
// so this is 1*scale[scaledegree]
|
||||||
var freq = this.tonic*this.scale[scaleDegree]
|
var freq = this.tonic*this.scale[scaleDegree]
|
||||||
|
|
||||||
|
// map it to octave
|
||||||
freq = freq*(Math.pow(2,octave))
|
freq = freq*(Math.pow(2,octave))
|
||||||
|
|
||||||
// truncate irrational numbers
|
// truncate irrational numbers
|
||||||
|
|
@ -137,10 +144,13 @@ Tune.prototype.MIDI = function(stepIn,octaveIn) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Load a new scale */
|
/* Load a new scale */
|
||||||
|
// sets .scale to ratios
|
||||||
|
|
||||||
Tune.prototype.loadScale = function(scale){
|
Tune.prototype.loadScale = function(scale){
|
||||||
|
|
||||||
/* load the scale */
|
/* load the scale */
|
||||||
|
let name
|
||||||
|
if (typeof scale === 'string') name = scale;
|
||||||
var freqs = isArrayOfNumbers(scale) ? scale : TuningList[scale].frequencies
|
var freqs = isArrayOfNumbers(scale) ? scale : TuningList[scale].frequencies
|
||||||
this.scale = []
|
this.scale = []
|
||||||
for (var i=0;i<freqs.length-1;i++) {
|
for (var i=0;i<freqs.length-1;i++) {
|
||||||
|
|
@ -148,10 +158,10 @@ Tune.prototype.loadScale = function(scale){
|
||||||
}
|
}
|
||||||
|
|
||||||
/* visualize in console */
|
/* visualize in console */
|
||||||
/* console.log(" ");
|
// console.log(" ");
|
||||||
console.log("LOADED "+name);
|
// console.log("LOADED "+name);
|
||||||
console.log(TuningList[name].description);
|
// console.log(TuningList[name].description);
|
||||||
console.log(this.scale); */
|
// console.log(this.scale);
|
||||||
var vis = [];
|
var vis = [];
|
||||||
for (var i=0;i<100;i++) {
|
for (var i=0;i<100;i++) {
|
||||||
vis[i] = " ";
|
vis[i] = " ";
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ This program is free software: you can redistribute it and/or modify it under th
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { register, _mod, parseNumeral, removeUndefineds } from '@strudel/core';
|
import { register, _mod, parseNumeral, removeUndefineds } from '@strudel/core';
|
||||||
|
import Tune from './tunejs.js'
|
||||||
|
|
||||||
// returns a list of frequency ratios for given edo scale
|
// returns a list of frequency ratios for given edo scale
|
||||||
export function edo(name) {
|
export function edo(name) {
|
||||||
|
|
@ -30,12 +30,18 @@ const defaultBase = 220;
|
||||||
|
|
||||||
// Assumes a base of 220. Returns a filtered scale based on 'indices'
|
// Assumes a base of 220. Returns a filtered scale based on 'indices'
|
||||||
function getXenScale(scale, indices) {
|
function getXenScale(scale, indices) {
|
||||||
|
let tune = new Tune()
|
||||||
if (typeof scale === 'string') {
|
if (typeof scale === 'string') {
|
||||||
if (/^[1-9]+[0-9]*edo$/.test(scale)) {
|
if (/^[1-9]+[0-9]*edo$/.test(scale)) {
|
||||||
scale = edo(scale);
|
scale = edo(scale);
|
||||||
} else if (presets[scale]) {
|
} else if (presets[scale]) {
|
||||||
scale = presets[scale];
|
scale = presets[scale];
|
||||||
} else {
|
}
|
||||||
|
else if (tune.isValidScale(scale)) {
|
||||||
|
tune.loadScale(scale)
|
||||||
|
scale = tune.scale
|
||||||
|
}
|
||||||
|
else {
|
||||||
throw new Error('unknown scale name: "' + scale + '"');
|
throw new Error('unknown scale name: "' + scale + '"');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -69,6 +75,7 @@ function xenOffset(xenScale, offset, index = 0) {
|
||||||
* @param {(string | number[] )} scaleNameOrRatios
|
* @param {(string | number[] )} scaleNameOrRatios
|
||||||
* @tags music_theory
|
* @tags music_theory
|
||||||
* @example
|
* @example
|
||||||
|
* // A major tried in 31edo:
|
||||||
* "0 8 18".xen("31edo").freq().piano()
|
* "0 8 18".xen("31edo").freq().piano()
|
||||||
* @example
|
* @example
|
||||||
* // You can also use xen with frequency ratios.
|
* // You can also use xen with frequency ratios.
|
||||||
|
|
@ -78,8 +85,15 @@ function xenOffset(xenScale, offset, index = 0) {
|
||||||
* Math.pow(2, 8/31),
|
* Math.pow(2, 8/31),
|
||||||
* Math.pow(2, 18/31),
|
* Math.pow(2, 18/31),
|
||||||
* ]).freq().piano()
|
* ]).freq().piano()
|
||||||
|
* @example
|
||||||
|
* // xen also supports all scale names that
|
||||||
|
* // tune does:
|
||||||
|
* "0 1 2 3 4 5".xen("hexany15").freq()
|
||||||
|
* // equiv to:
|
||||||
|
* // "0 1 2 3 4 5".tune("hexany15").mul("220").freq()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// TODO how do you reference another function in jsdoc (tune, above)
|
||||||
// TODO support tunings defined in './tunejs.js'
|
// TODO support tunings defined in './tunejs.js'
|
||||||
export const xen = register('xen', function (scaleNameOrRatios, pat) {
|
export const xen = register('xen', function (scaleNameOrRatios, pat) {
|
||||||
return pat.withHaps((haps) => {
|
return pat.withHaps((haps) => {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ import { JsDoc } from '../../docs/JsDoc';
|
||||||
|
|
||||||
# Xenharmonic Functions (experimental)
|
# Xenharmonic Functions (experimental)
|
||||||
|
|
||||||
|
{/* TODO expand explanation of xenharmony */}
|
||||||
|
|
||||||
These functions allow the use of scales other than your typical chromatic 12 based ones.
|
These functions allow the use of scales other than your typical chromatic 12 based ones.
|
||||||
|
|
||||||
### tune(scale)
|
### tune(scale)
|
||||||
|
|
@ -96,11 +98,4 @@ The `tranh3` tuning has a similar set of notes, with two clashing. You might try
|
||||||
|
|
||||||
### xen(scaleOrRatios)
|
### xen(scaleOrRatios)
|
||||||
|
|
||||||
<JsDoc client:idle name="Pattern.xen" h={0} />
|
<JsDoc client:idle name="Pattern.xen" h={0} />
|
||||||
|
|
||||||
{/* <MiniRepl
|
|
||||||
client:idle
|
|
||||||
tune={`
|
|
||||||
"0 8 18".xen("31edo").freq().piano()
|
|
||||||
`}
|
|
||||||
/> */}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue