Skip to content

Commit

Permalink
Fix sample rate
Browse files Browse the repository at this point in the history
Remove hard coded 44100 sample rate.
Get sample rate from audio context.
  • Loading branch information
mmontag committed Nov 30, 2021
1 parent c626487 commit 1e63b89
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ var Visualizer = require('./visualizer');
var config = require('./config');
var defaultPresets = require('./default-presets');

var BUFFER_SIZE_MS = 1000 * config.bufferSize / config.sampleRate;
var MS_PER_SAMPLE = 1000 / config.sampleRate;
var PARAM_START_MANIPULATION = 'param-start-manipulation';
var PARAM_STOP_MANIPULATION = 'param-stop-manipulation';
var PARAM_CHANGE = 'param-change';
Expand All @@ -25,6 +23,9 @@ var app = Angular.module('synthApp', ['ngStorage']);
var synth = new Synth(FMVoice, config.polyphony);
var midi = new MIDI(synth);
var audioContext = new (window.AudioContext || window.webkitAudioContext)();
config.sampleRate = audioContext.sampleRate;
var BUFFER_SIZE_MS = 1000 * config.bufferSize / config.sampleRate;
var MS_PER_SAMPLE = 1000 / config.sampleRate;
var visualizer = new Visualizer("analysis", 256, 35, 0xc0cf35, 0x2f3409, audioContext);
var scriptProcessor = null;

Expand Down
11 changes: 3 additions & 8 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
var SAMPLE_RATE = 44100;
var LFO_RATE = 441;
var LFO_SAMPLE_PERIOD = Math.floor(SAMPLE_RATE / LFO_RATE);
var PERIOD = Math.PI * 2;
var LFO_SAMPLE_PERIOD = 100;
var BUFFER_SIZE = 1024;
var POLYPHONY = 12;

Expand All @@ -11,12 +8,10 @@ if (/iPad|iPhone|iPod|Android/.test(navigator.userAgent)) {
}

var Config = {
sampleRate: SAMPLE_RATE,
lfoRate: LFO_RATE,
sampleRate: 44100, // gets updated with audio context rate
lfoSamplePeriod: LFO_SAMPLE_PERIOD,
period: PERIOD,
bufferSize: BUFFER_SIZE,
polyphony: POLYPHONY
};

module.exports = Config;
module.exports = Config;
18 changes: 9 additions & 9 deletions src/lfo-dx7.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
var config = require('./config');

var PERIOD = config.period;
var PERIOD_HALF = config.period / 2;
var PERIOD_RECIP = 1 / config.period;
var LFO_RATE = config.lfoRate;
var PERIOD = Math.PI * 2;
var PERIOD_HALF = PERIOD / 2;
var PERIOD_RECIP = 1 / PERIOD;
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,
Expand Down Expand Up @@ -84,7 +83,7 @@ function LfoDX7(opParams) {

LfoDX7.prototype.render = function() {
var amp;
if (this.counter % LFO_SAMPLE_PERIOD == 0) {
if (this.counter % LFO_SAMPLE_PERIOD === 0) {
switch (params.lfoWaveform) {
case LFO_MODE_TRIANGLE:
if (this.phase < PERIOD_HALF)
Expand Down Expand Up @@ -154,12 +153,13 @@ LfoDX7.setParams = function(globalParams) {

LfoDX7.update = function() {
var frequency = LFO_FREQUENCY_TABLE[params.lfoSpeed];
phaseStep = PERIOD * frequency/LFO_RATE; // radians per sample
var lfoRate = config.sampleRate/LFO_SAMPLE_PERIOD;
phaseStep = PERIOD * frequency/lfoRate; // radians per sample
ampModDepth = params.lfoAmpModDepth * 0.01;
// ignoring amp mod table for now. it seems shallow LFO_AMP_MOD_TABLE[params.lfoAmpModDepth];
delayTimes[LFO_DELAY_ONSET] = (LFO_RATE * 0.001753 * Math.pow(params.lfoDelay, 3.10454) + 169.344 - 168) / 1000;
delayTimes[LFO_DELAY_RAMP] = (LFO_RATE * 0.321877 * Math.pow(params.lfoDelay, 2.01163) + 494.201 - 168) / 1000;
delayTimes[LFO_DELAY_ONSET] = (lfoRate * 0.001753 * Math.pow(params.lfoDelay, 3.10454) + 169.344 - 168) / 1000;
delayTimes[LFO_DELAY_RAMP] = (lfoRate * 0.321877 * Math.pow(params.lfoDelay, 2.01163) + 494.201 - 168) / 1000;
delayIncrements[LFO_DELAY_RAMP] = 1 / (delayTimes[LFO_DELAY_RAMP] - delayTimes[LFO_DELAY_ONSET]);
};

module.exports = LfoDX7;
module.exports = LfoDX7;
7 changes: 3 additions & 4 deletions src/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ 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;
var PERIOD = Math.PI * 2;

function Operator(params, baseFrequency, envelope, lfo) {
this.phase = 0;
Expand All @@ -20,7 +19,7 @@ Operator.prototype.updateFrequency = function(baseFrequency) {
var frequency = this.params.oscMode ?
this.params.freqFixed :
baseFrequency * this.params.freqRatio * Math.pow(OCTAVE_1024, this.params.detune);
this.phaseStep = PERIOD * frequency / SAMPLE_RATE; // radians per sample
this.phaseStep = PERIOD * frequency / config.sampleRate; // radians per sample
};

Operator.prototype.render = function(mod) {
Expand All @@ -41,4 +40,4 @@ Operator.prototype.isFinished = function() {
return this.envelope.isFinished();
};

module.exports = Operator;
module.exports = Operator;

0 comments on commit 1e63b89

Please sign in to comment.