modularscale-js
Advanced tools
Comparing version
{ | ||
"name": "modularscale-js", | ||
"version": "0.0.1", | ||
"version": "3.0.0", | ||
"homepage": "http://modularscale.com/", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -1,133 +0,75 @@ | ||
var msValue = 0; | ||
var msBases = 1; | ||
var msRatios = (1+ Math.sqrt(5))/2; | ||
// Values | ||
var minorSecond = 1.067; | ||
var minorSecond = 16/15; | ||
var majorSecond = 1.125; | ||
var minorThird = 1.2; | ||
var majorThird = 1.25; | ||
var perfectFourth = 1.333; | ||
var augFourth = 1.414; | ||
var perfectFourth = 4/3; | ||
var augFourth = 1.414; | ||
var perfectFifth = 1.5; | ||
var minorSixth = 1.6; | ||
var goldenSection = 1.618; | ||
var majorSixth = 1.667; | ||
var minorSeventh = 1.778; | ||
var goldenSection = 1.61803398875; | ||
var majorSixth = 5/3; | ||
var minorSeventh = 16/9; | ||
var majorSeventh = 1.875; | ||
var octave = 2; | ||
var majorTenth = 2.5; | ||
var majorEleventh = 2.667; | ||
var majorEleventh = 8/3; | ||
var majorTwelfth = 3; | ||
var doubleOctave = 4; | ||
// Function settings | ||
var modularscale = { | ||
base: 16, | ||
ratio: 1.5, | ||
}; | ||
// Unique via http://jsfiddle.net/gabrieleromanato/BrLfv/ | ||
var msUnique = function(origArr) { | ||
// Function | ||
function msFunction(v,settings) { | ||
origArr = origArr.sort(function(a,b) { | ||
var x = a[0]; | ||
var y = b[0]; | ||
return x-y; | ||
}); | ||
newArr = []; | ||
var lastVal = null; | ||
for (var i = 0; i < origArr.length; i++) { | ||
var currentVal = origArr[i][0]; | ||
if (currentVal != lastVal) { | ||
newArr.push(origArr[i]); | ||
}; | ||
lastVal = currentVal; | ||
} | ||
return newArr; | ||
} | ||
// Main function | ||
function ms(value, bases, ratios) { | ||
if (typeof value === 'string') { | ||
value = 1; | ||
// Parse settings | ||
// Write initial settings if undefined | ||
if (settings === undefined) { | ||
settings = modularscale; | ||
} | ||
if (value == undefined) { | ||
value = msValue; | ||
// Initiate values | ||
var base = settings.base; | ||
var ratio = settings.ratio; | ||
// Fill in the blanks with default values | ||
if (ratio === undefined) { | ||
ratio = modularscale.ratio; | ||
} | ||
if (bases == undefined) { | ||
bases = msBases; | ||
if (base === undefined) { | ||
base = modularscale.base; | ||
} | ||
if (ratios == undefined) { | ||
ratios = msRatios; | ||
} | ||
// Error hangling | ||
if (bases <= 0) { | ||
bases = 1; | ||
// Fast calc if not multi stranded | ||
if (!Array.isArray(base) || base.length === 1) { | ||
return (Math.pow(ratio,v) * base); | ||
} | ||
if (typeof Math.abs(bases[0]) != 'number') { | ||
bases = 1; | ||
} | ||
// Make arrays | ||
var bases = (''+bases).split(','); | ||
var ratios = (''+ratios).split(','); | ||
// Seed return array | ||
var r = []; | ||
var strand = null; | ||
for (var ratio = 0; ratio < ratios.length; ratio++) { | ||
for (var base = 0; base < bases.length; base++) { | ||
strand = (base + ratio); | ||
// Seed list with an initial value | ||
// r.push(bases[base]); | ||
// Find values on a positive scale | ||
if (value >= 0) { | ||
// Find lower values on the scale | ||
var i = 0; | ||
while((Math.pow(ratios[ratio], i) * bases[base]) >= bases[0]) { | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]); | ||
i--; | ||
} | ||
// Find higher possible values on the scale | ||
var i = 0; | ||
while(Math.pow(ratios[ratio], i) * bases[base] <= Math.pow(ratios[ratio], value + 1) * bases[base]) { | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]); | ||
i++; | ||
} | ||
} else { | ||
// Find values on a negitve scale | ||
var i = 0; | ||
while((Math.pow(ratios[ratio], i) * bases[base]) <= bases[0]) { | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]); | ||
i++; | ||
} | ||
// // Find higher possible values on the scale | ||
var i = 0; | ||
while((Math.pow(ratios[ratio], i) * bases[base]) >= (Math.pow(ratios[ratio], value - 1) * bases[base])) { | ||
if (Math.pow(ratios[ratio], i) * bases[base] <= bases[0]) { | ||
r.push([Math.pow(ratios[ratio], i) * bases[base], strand]); | ||
} | ||
i--; | ||
} | ||
} | ||
// Normalize bases | ||
// Find the upper bounds for base values | ||
var baseHigh = Math.pow(ratio,1) * base[0]; | ||
for (var i = 1; i < base.length; i++) { | ||
// shift up if value too low | ||
while (base[i]/1 < base[0]/1) { | ||
base[i] = Math.pow(ratio,1) * base[i]; | ||
} | ||
// Shift down if too high | ||
while (base[i]/1 >= baseHigh/1) { | ||
base[i] = Math.pow(ratio,-1) * base[i]; | ||
} | ||
} | ||
// Sort bases | ||
base.sort(); | ||
r = msUnique(r); | ||
// Figure out what base to use with modulo | ||
var rBase = Math.round((v / base.length - Math.floor(v/base.length)) * base.length); | ||
// reverse array if value is negitive | ||
if(value < 0) { | ||
r = r.reverse(); | ||
} | ||
// Return | ||
return Math.pow(ratio,Math.floor(v/base.length)) * base[rBase]; | ||
}; | ||
return r[Math.abs(value)][0]; | ||
function ms(v,settings) { | ||
return msFunction(v,settings); | ||
} |
@@ -1,1 +0,1 @@ | ||
function ms(r,a,t){"string"==typeof r&&(r=1),void 0==r&&(r=msValue),void 0==a&&(a=msBases),void 0==t&&(t=msRatios),0>=a&&(a=1),"number"!=typeof Math.abs(a[0])&&(a=1);for(var a=(""+a).split(","),t=(""+t).split(","),o=[],s=null,e=0;e<t.length;e++)for(var h=0;h<a.length;h++)if(s=h+e,r>=0){for(var n=0;Math.pow(t[e],n)*a[h]>=a[0];)o.push([Math.pow(t[e],n)*a[h],s]),n--;for(var n=0;Math.pow(t[e],n)*a[h]<=Math.pow(t[e],r+1)*a[h];)o.push([Math.pow(t[e],n)*a[h],s]),n++}else{for(var n=0;Math.pow(t[e],n)*a[h]<=a[0];)o.push([Math.pow(t[e],n)*a[h],s]),n++;for(var n=0;Math.pow(t[e],n)*a[h]>=Math.pow(t[e],r-1)*a[h];)Math.pow(t[e],n)*a[h]<=a[0]&&o.push([Math.pow(t[e],n)*a[h],s]),n--}return o=msUnique(o),0>r&&(o=o.reverse()),o[Math.abs(r)]}var msValue=0,msBases=1,msRatios=(1+Math.sqrt(5))/2,msUnique=function(r){r=r.sort(function(r,a){var t=r[0],o=a[0];return t-o}),newArr=[];for(var a=null,t=0;t<r.length;t++){var o=r[t][0];o!=a&&newArr.push(r[t]),a=o}return newArr}; | ||
function msFunction(o,r){void 0===r&&(r=modularscale);var t=r.base,a=r.ratio;if(void 0===a&&(a=modularscale.ratio),void 0===t&&(t=modularscale.base),!Array.isArray(t)||1===t.length)return Math.pow(a,o)*t;for(var e=Math.pow(a,1)*t[0],n=1;n<t.length;n++){for(;t[n]/1<t[0]/1;)t[n]=Math.pow(a,1)*t[n];for(;t[n]/1>=e/1;)t[n]=Math.pow(a,-1)*t[n]}t.sort();var h=Math.round((o/t.length-Math.floor(o/t.length))*t.length);return Math.pow(a,Math.floor(o/t.length))*t[h]}function ms(o,r){return msFunction(o,r)}var minorSecond=16/15,majorSecond=1.125,minorThird=1.2,majorThird=1.25,perfectFourth=4/3,augFourth=1.414,perfectFifth=1.5,minorSixth=1.6,goldenSection=1.61803398875,majorSixth=5/3,minorSeventh=16/9,majorSeventh=1.875,octave=2,majorTenth=2.5,majorEleventh=8/3,majorTwelfth=3,doubleOctave=4,modularscale={base:16,ratio:1.5}; |
{ | ||
"name": "modularscale-js", | ||
"version": "0.0.1", | ||
"version": "3.0.0", | ||
"description": "A modular scale calculator written in JavaScript", | ||
@@ -5,0 +5,0 @@ "homepage": "http://www.modularscale.com/", |
@@ -7,5 +7,13 @@ # Modularscale | ||
You can install modularscale-js as a bower package to include in your project. | ||
You can install modularscale-js as an NPM or Bower package to include in your project. | ||
*NPM:* | ||
``` | ||
$ npm install modularscale-js | ||
``` | ||
*Bower:* | ||
``` | ||
$ bower install modularscale-js | ||
@@ -20,11 +28,22 @@ ``` | ||
There are two variables that need to be redefined by you, `msBases` is an array of a base, or multiple base values. `msRatios` holds the ratios that your bases will be multiplied by to create your scale. | ||
There is a configuration object that contains the settings for modular scale, inside it is an array of bases and the ratio value. | ||
Call the function with either `msFunction(n)` or `ms(n)` where `n` is the point on your scale. You can pass settings in as a second variable. | ||
```js | ||
msBases = [1]; | ||
msRatios = [1.5]; | ||
$modularscale: { | ||
base: [1], | ||
ratio: 1.5 | ||
}; | ||
``` | ||
Now you can use the function `ms()` throughout your project. Pass a number into this function to get the value for that position on the scale. For example, `ms(5)` with the above configuration will return the result `7.594`. | ||
You can add multiple bases by adding values to the array | ||
```js | ||
$modularscale: { | ||
base: [12,14,16], | ||
ratio: 1.5 | ||
}; | ||
``` | ||
## Ratios | ||
@@ -63,11 +82,24 @@ | ||
```js | ||
msRatios = [1.234]; | ||
$modularscale: { | ||
ratio: 1.234 | ||
}; | ||
``` | ||
You can also pass more than one ratio | ||
## Multiple threads | ||
You may notice you can have multiple scales at once on modularscale.com formatted like this: | ||
```js | ||
msRatios = [1.234, augFourth]; | ||
$modularscale: { | ||
base: [1], | ||
ratio: 1.5, | ||
a: { | ||
base: [1], | ||
ratio: 1.2 | ||
} | ||
}; | ||
``` | ||
To use a scale with the `$modularscale.a` settings pass the settings in `msFunction(n,$modularscale.a)` where `n` is the point on the scale you wish to find a value for. You can break this variable down and pass it through via its own object but it is consolodated in the output of modularscale.com. | ||
### Licence | ||
@@ -74,0 +106,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
-100%112
40%8033
-9.15%93
-31.62%