Skip to content

Commit

Permalink
Migrate to npm/commonjs/webpack, Part 3
Browse files Browse the repository at this point in the history
  • Loading branch information
mmontag committed Jun 22, 2015
1 parent 8322112 commit ec5bc5b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 31 deletions.
2 changes: 1 addition & 1 deletion index.html
Expand Up @@ -173,6 +173,6 @@
</div>
</div>
</div>
<script src="dist/bundle.js"></script>
<script src="bundle.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -4,7 +4,7 @@
"description": "DX7 FM synthesis using the Web Audio and Web MIDI API.",
"main": "app.js",
"scripts": {
"start": "webpack",
"start": "webpack --optimize-minimize --optimize-dedupe",
"dev": "webpack-dev-server --progress --colors --port 9090 --inline --hot"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/app.js
Expand Up @@ -142,7 +142,7 @@ setTimeout(function() {
}, 100);

// Visualizer
var visualizer = new Visualizer("analysis", 256, 35, 0xcee048, 0x2f3409, audioContext);
var visualizer = new Visualizer("analysis", 256, 35, 0xcee048, 0x2f3409, audioContext );
scriptProcessor.connect(visualizer.getAudioNode());

// Polyphony counter
Expand Down
13 changes: 13 additions & 0 deletions src/config.js
@@ -0,0 +1,13 @@
var SAMPLE_RATE = 44100;
var LFO_RATE = 441;
var LFO_SAMPLE_PERIOD = Math.floor(SAMPLE_RATE / LFO_RATE);
var PERIOD = Math.PI * 2;

var Config = {
sampleRate: SAMPLE_RATE,
lfoRate: LFO_RATE,
lfoSamplePeriod: LFO_SAMPLE_PERIOD,
period: PERIOD
};

module.exports = Config;
15 changes: 11 additions & 4 deletions src/lfo-dx7.js
@@ -1,6 +1,12 @@
// Constants
// see https://github.com/smbolton/hexter/blob/621202b4f6ac45ee068a5d6586d3abe91db63eaf/src/dx7_voice.c#L1002
var LFO_FREQUENCY_TABLE = [
var config = require('./config');

var PARAMS = {};
var PERIOD = config.period;
var PERIOD_HALF = config.period / 2;
var PERIOD_RECIP = 1 / config.period;
var LFO_RATE = config.lfoRate;
var LFO_SAMPLE_PERIOD = config.lfoSamplePeriod;
var LFO_FREQUENCY_TABLE = [ // see https://github.com/smbolton/hexter/tree/master/src/dx7_voice.c#L1002
0.062506, 0.124815, 0.311474, 0.435381, 0.619784,
0.744396, 0.930495, 1.116390, 1.284220, 1.496880,
1.567830, 1.738994, 1.910158, 2.081322, 2.252486,
Expand Down Expand Up @@ -63,7 +69,8 @@ var delayTimes = [0, 0, 0];
var delayIncrements = [0, 0, 0];
var delayVals = [0, 0, 1];

function LfoDX7(opIdx) {
function LfoDX7(opIdx, params) {
PARAMS = params;
this.operatorIndex = opIdx;
this.phase = 0;
this.pitchVal = 0;
Expand Down
6 changes: 6 additions & 0 deletions src/operator.js
@@ -1,5 +1,9 @@
var config = require('./config');

// http://www.chipple.net/dx7/fig09-4.gif
var OCTAVE_1024 = 1.0006771307; //Math.exp(Math.log(2)/1024);
var PERIOD = config.period;
var SAMPLE_RATE = config.sampleRate;

function Operator(params, baseFrequency, envelope, lfo, pitchEnvelope) {
this.phase = 0;
Expand Down Expand Up @@ -35,3 +39,5 @@ Operator.prototype.noteOff = function() {
Operator.prototype.isFinished = function() {
return this.envelope.isFinished();
};

module.exports = Operator;
15 changes: 4 additions & 11 deletions src/synth.js
Expand Up @@ -5,21 +5,19 @@ var PITCH_BEND_RANGE = 2; // semitones (in each direction)
var MIDI_CC_MODULATION = 1,
MIDI_CC_SUSTAIN_PEDAL = 64;

// TODO: Probably reduce responsibility to voice management; rename VoiceManager, MIDIChannel, etc.
function Synth(voiceClass) {
this.voices = [];
this.voiceClass = voiceClass;
this.sustainPedalDown = false;
this.bend = 0;
this.aftertouch = 0;
this.mod = 0;
}

Synth.prototype.controller = function(controlNumber, value) {
// see http://www.midi.org/techspecs/midimessages.php#3
switch (controlNumber) {
case MIDI_CC_MODULATION:
this.mod = value;
this.updateMod();
this.voiceClass.modulationWheel(value);
break;
case MIDI_CC_SUSTAIN_PEDAL:
this.sustainPedal(value > 0.5);
Expand All @@ -28,13 +26,7 @@ Synth.prototype.controller = function(controlNumber, value) {
};

Synth.prototype.channelAftertouch = function(value) {
this.aftertouch = value;
this.updateMod();
};

Synth.prototype.updateMod = function() {
var aftertouch = PARAMS.aftertouchEnabled ? this.aftertouch : 0;
PARAMS.controllerModVal = Math.min(1.27, aftertouch + this.mod); // Allow 27% overdrive
this.voiceClass.channelAftertouch(value);
};

Synth.prototype.sustainPedal = function(down) {
Expand Down Expand Up @@ -78,6 +70,7 @@ Synth.prototype.noteOff = function(note) {
};

Synth.prototype.panic = function() {
this.sustainPedalDown = false;
for (var i = 0, l = this.voices.length; i < l; i++) {
if (this.voices[i])
this.voices[i].noteOff();
Expand Down
34 changes: 25 additions & 9 deletions src/voice-dx7.js
@@ -1,14 +1,11 @@
var Operator = require('./operator');
var EnvelopeDX7 = require('./envelope-dx7');
var LfoDX7 = require('./lfo-dx7');
var config = require('./config');

// DX7 Synth Globals
var SAMPLE_RATE = 44100;
var LFO_RATE = 441;
var LFO_SAMPLE_PERIOD = Math.floor(SAMPLE_RATE / LFO_RATE);
var PERIOD = Math.PI * 2;
var PERIOD_HALF = Math.PI;
var PERIOD_RECIP = 1 / PERIOD;
var PARAMS = {};
var SAMPLE_RATE = config.sampleRate;
var PERIOD = config.period;

var OUTPUT_LEVEL_TABLE = [
0.000000, 0.000337, 0.000476, 0.000674, 0.000952, 0.001235, 0.001602, 0.001905, 0.002265, 0.002694,
Expand Down Expand Up @@ -86,10 +83,11 @@ function FMVoice(note, velocity, bend) {
// https://github.com/asb2m10/dexed/blob/1eda313316411c873f8388f971157664827d1ac9/Source/msfa/dx7note.cc#L55
// https://groups.yahoo.com/neo/groups/YamahaDX/conversations/messages/15919
var params = PARAMS.operators[i];
var op = new Operator(params,
var op = new Operator(
params,
this.frequency,
new EnvelopeDX7(params.levels, params.rates),
new LfoDX7(i)
new LfoDX7(i, PARAMS)
//new EnvelopeDX7(PARAMS.pitchEnvelope.levels, PARAMS.pitchEnvelope.rates, true)
);
// TODO: DX7 accurate velocity sensitivity map
Expand All @@ -99,6 +97,9 @@ function FMVoice(note, velocity, bend) {
this.pitchBend(bend);
}

FMVoice.aftertouch = 0;
FMVoice.mod = 0;

FMVoice.frequencyFromNoteNumber = function(note) {
return 440 * Math.pow(2,(note-69)/12);
};
Expand Down Expand Up @@ -140,6 +141,21 @@ FMVoice.mapOutputLevel = function(input) {
return OUTPUT_LEVEL_TABLE[idx] * 1.27;
};

FMVoice.channelAftertouch = function(value) {
FMVoice.aftertouch = value;
FMVoice.updateMod();
};

FMVoice.modulationWheel = function(value) {
FMVoice.mod = value;
FMVoice.updateMod();
};

FMVoice.updateMod = function() {
var aftertouch = PARAMS.aftertouchEnabled ? FMVoice.aftertouch : 0;
PARAMS.controllerModVal = Math.min(1.27, aftertouch + FMVoice.mod); // Allow 27% overdrive
};

FMVoice.prototype.render = function() {
var algorithmIdx = PARAMS.algorithm - 1;
var modulationMatrix = ALGORITHMS[algorithmIdx].modulationMatrix;
Expand Down
7 changes: 3 additions & 4 deletions webpack.config.js
Expand Up @@ -2,13 +2,12 @@ var webpack = require('webpack');

module.exports = {
entry: [
'webpack-dev-server/client?http://localhost:9090',
'webpack/hot/only-dev-server',
//'webpack-dev-server/client?http://localhost:9090',
//'webpack/hot/dev-server',
'./src/app.js'
],
output: {
path: './dist',
publicPath: '/',
path: './',
filename: 'bundle.js'
},
module: {
Expand Down

0 comments on commit ec5bc5b

Please sign in to comment.