├── .gitignore ├── test.html ├── package.json ├── bower.json ├── modularscale.min.js ├── modularscale.js └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "modularscale-js", 3 | "version": "3.0.1", 4 | "description": "A modular scale calculator written in JavaScript", 5 | "homepage": "http://www.modularscale.com/", 6 | "bugs": { 7 | "url": "https://github.com/modularscale/modularscale-js/issues" 8 | }, 9 | "license": "MIT", 10 | "author": "Scott Kellum (http://scottkellum.com/)", 11 | "main": "modularscale.js", 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/modularscale/modularscale-js.git" 15 | } 16 | } -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "modularscale-js", 3 | "version": "3.0.1", 4 | "homepage": "http://modularscale.com/", 5 | "authors": [ 6 | "scottkellum " 7 | ], 8 | "description": "A modular scale calculator written in JavaScript", 9 | "main": "modularscale.js", 10 | "keywords": [ 11 | "modularscale", 12 | "modular", 13 | "scale", 14 | "calculator", 15 | "typography", 16 | "type", 17 | "design" 18 | ], 19 | "license": "MIT", 20 | "ignore": [ 21 | "**/.*", 22 | "node_modules", 23 | "bower_components", 24 | "test", 25 | "tests" 26 | ] 27 | } 28 | -------------------------------------------------------------------------------- /modularscale.min.js: -------------------------------------------------------------------------------- 1 | function msFunction(a,b){void 0===b&&(b=modularscale);var c=b.base,d=b.ratio;if(void 0===d&&(d=modularscale.ratio),void 0===c&&(c=modularscale.base),!Array.isArray(c)||1===c.length)return Math.pow(d,a)*c;for(var e=Math.pow(d,1)*c[0],f=1;f=e/1;)c[f]=Math.pow(d,-1)*c[f]}c.sort();var g=Math.round((a/c.length-Math.floor(a/c.length))*c.length);return Math.pow(d,Math.floor(a/c.length))*c[g]}function ms(a,b){return msFunction(a,b)}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};module.exports=ms; -------------------------------------------------------------------------------- /modularscale.js: -------------------------------------------------------------------------------- 1 | // Values 2 | var minorSecond = 16/15; 3 | var majorSecond = 1.125; 4 | var minorThird = 1.2; 5 | var majorThird = 1.25; 6 | var perfectFourth = 4/3; 7 | var augFourth = 1.414; 8 | var perfectFifth = 1.5; 9 | var minorSixth = 1.6; 10 | var goldenSection = 1.61803398875; 11 | var majorSixth = 5/3; 12 | var minorSeventh = 16/9; 13 | var majorSeventh = 1.875; 14 | var octave = 2; 15 | var majorTenth = 2.5; 16 | var majorEleventh = 8/3; 17 | var majorTwelfth = 3; 18 | var doubleOctave = 4; 19 | 20 | // Function settings 21 | var modularscale = { 22 | base: 16, 23 | ratio: 1.5, 24 | }; 25 | 26 | // Function 27 | function msFunction(v,settings) { 28 | 29 | // Parse settings 30 | // Write initial settings if undefined 31 | if (settings === undefined) { 32 | settings = modularscale; 33 | } 34 | // Initiate values 35 | var base = settings.base; 36 | var ratio = settings.ratio; 37 | // Fill in the blanks with default values 38 | if (ratio === undefined) { 39 | ratio = modularscale.ratio; 40 | } 41 | if (base === undefined) { 42 | base = modularscale.base; 43 | } 44 | 45 | // Fast calc if not multi stranded 46 | if (!Array.isArray(base) || base.length === 1) { 47 | return (Math.pow(ratio,v) * base); 48 | } 49 | 50 | // Normalize bases 51 | // Find the upper bounds for base values 52 | var baseHigh = Math.pow(ratio,1) * base[0]; 53 | for (var i = 1; i < base.length; i++) { 54 | // shift up if value too low 55 | while (base[i]/1 < base[0]/1) { 56 | base[i] = Math.pow(ratio,1) * base[i]; 57 | } 58 | // Shift down if too high 59 | while (base[i]/1 >= baseHigh/1) { 60 | base[i] = Math.pow(ratio,-1) * base[i]; 61 | } 62 | } 63 | // Sort bases 64 | base.sort(); 65 | 66 | // Figure out what base to use with modulo 67 | var rBase = Math.round((v / base.length - Math.floor(v/base.length)) * base.length); 68 | 69 | // Return 70 | return Math.pow(ratio,Math.floor(v/base.length)) * base[rBase]; 71 | }; 72 | 73 | function ms(v,settings) { 74 | return msFunction(v,settings); 75 | } 76 | 77 | module.exports = ms; -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Modularscale 2 | 3 | This is a JavaScript modular scale calculator so you can create and use modular scales in your project. This calculator was built in tandem with the new [modularscale.com](http://www.modularscale.com/) website both as its calculation engine and as an external tool you can use in your own projects. 4 | 5 | ### Installation 6 | 7 | You can install modularscale-js as an NPM or Bower package to include in your project. 8 | 9 | *NPM:* 10 | 11 | ``` 12 | $ npm install modularscale-js 13 | ``` 14 | 15 | *Bower:* 16 | 17 | ``` 18 | $ bower install modularscale-js 19 | ``` 20 | 21 | Alternatively you can download the [latest release](https://github.com/modularscale/modularscale-js/releases) and add it to your project. 22 | 23 | ### Using modularscale-js 24 | 25 | First, you will want to set up your scale. You can go to [modularscale.com](http://www.modularscale.com) and click on the `js` tab to generate a config, or define it yourself. 26 | 27 | There is a configuration object that contains the settings for modular scale, inside it is an array of bases and the ratio value. 28 | 29 | 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. 30 | 31 | ```js 32 | $modularscale: { 33 | base: [1], 34 | ratio: 1.5 35 | }; 36 | ``` 37 | 38 | You can add multiple bases by adding values to the array 39 | 40 | ```js 41 | $modularscale: { 42 | base: [12,14,16], 43 | ratio: 1.5 44 | }; 45 | ``` 46 | 47 | ## Ratios 48 | 49 | Modular scale includes functions for a number of classic design and musical scale ratios. You can add your own ratios as well. 50 | 51 | By default, the ratio is set to `1.5` (fifth). 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 |
VariableDecimal value
minorSecond 1.067
majorSecond 1.125
minorThird 1.2
majorThird 1.25
perfectFourth 1.333
augFourth 1.414
perfectFifth 1.5
minorSixth 1.6
goldenSection 1.618
majorSixth 1.667
minorSeventh 1.778
majorSeventh 1.875
octave 2
majorTenth 2.5
majorEleventh 2.667
majorTwelfth 3
doubleOctave 4
76 | 77 | Add your own ratio by setting a variable. 78 | 79 | ```js 80 | $modularscale: { 81 | ratio: 1.234 82 | }; 83 | ``` 84 | 85 | ## Multiple threads 86 | 87 | You may notice you can have multiple scales at once on modularscale.com formatted like this: 88 | 89 | ```js 90 | $modularscale: { 91 | base: [1], 92 | ratio: 1.5, 93 | a: { 94 | base: [1], 95 | ratio: 1.2 96 | } 97 | }; 98 | ``` 99 | 100 | 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. 101 | 102 | ### Licence 103 | 104 | The MIT License (MIT) 105 | 106 | Copyright © 2015 Scott Kellum 107 | 108 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 109 | 110 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 111 | 112 | **The software is provided “as is”, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non-infringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.** 113 | --------------------------------------------------------------------------------