mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 14:02:29 -05:00
Pass with eslint --fix
This commit is contained in:
parent
cd699dbc0f
commit
ee8462f53f
15 changed files with 102 additions and 110 deletions
|
@ -1,6 +1,6 @@
|
||||||
var ArrayBufferStream = require('./ArrayBufferStream');
|
const ArrayBufferStream = require('./ArrayBufferStream');
|
||||||
var Tone = require('tone');
|
const Tone = require('tone');
|
||||||
var log = require('./log');
|
const log = require('./log');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decode wav audio files that have been compressed with the ADPCM format.
|
* Decode wav audio files that have been compressed with the ADPCM format.
|
||||||
|
@ -21,27 +21,27 @@ function ADPCMSoundDecoder () {
|
||||||
*/
|
*/
|
||||||
ADPCMSoundDecoder.prototype.decode = function (audioData) {
|
ADPCMSoundDecoder.prototype.decode = function (audioData) {
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise((resolve, reject) => {
|
||||||
var stream = new ArrayBufferStream(audioData);
|
const stream = new ArrayBufferStream(audioData);
|
||||||
|
|
||||||
var riffStr = stream.readUint8String(4);
|
const riffStr = stream.readUint8String(4);
|
||||||
if (riffStr != 'RIFF') {
|
if (riffStr != 'RIFF') {
|
||||||
log.warn('incorrect adpcm wav header');
|
log.warn('incorrect adpcm wav header');
|
||||||
reject();
|
reject();
|
||||||
}
|
}
|
||||||
|
|
||||||
var lengthInHeader = stream.readInt32();
|
const lengthInHeader = stream.readInt32();
|
||||||
if ((lengthInHeader + 8) != audioData.byteLength) {
|
if ((lengthInHeader + 8) != audioData.byteLength) {
|
||||||
log.warn('adpcm wav length in header: ' + lengthInHeader + ' is incorrect');
|
log.warn(`adpcm wav length in header: ${lengthInHeader} is incorrect`);
|
||||||
}
|
}
|
||||||
|
|
||||||
var wavStr = stream.readUint8String(4);
|
const wavStr = stream.readUint8String(4);
|
||||||
if (wavStr != 'WAVE') {
|
if (wavStr != 'WAVE') {
|
||||||
log.warn('incorrect adpcm wav header');
|
log.warn('incorrect adpcm wav header');
|
||||||
reject();
|
reject();
|
||||||
}
|
}
|
||||||
|
|
||||||
var formatChunk = this.extractChunk('fmt ', stream);
|
const formatChunk = this.extractChunk('fmt ', stream);
|
||||||
this.encoding = formatChunk.readUint16();
|
this.encoding = formatChunk.readUint16();
|
||||||
this.channels = formatChunk.readUint16();
|
this.channels = formatChunk.readUint16();
|
||||||
this.samplesPerSecond = formatChunk.readUint32();
|
this.samplesPerSecond = formatChunk.readUint32();
|
||||||
|
@ -52,18 +52,18 @@ ADPCMSoundDecoder.prototype.decode = function (audioData) {
|
||||||
this.samplesPerBlock = formatChunk.readUint16();
|
this.samplesPerBlock = formatChunk.readUint16();
|
||||||
this.adpcmBlockSize = ((this.samplesPerBlock - 1) / 2) + 4; // block size in bytes
|
this.adpcmBlockSize = ((this.samplesPerBlock - 1) / 2) + 4; // block size in bytes
|
||||||
|
|
||||||
var samples = this.imaDecompress(this.extractChunk('data', stream), this.adpcmBlockSize);
|
const samples = this.imaDecompress(this.extractChunk('data', stream), this.adpcmBlockSize);
|
||||||
|
|
||||||
// todo: this line is the only place Tone is used here, should be possible to remove
|
// todo: this line is the only place Tone is used here, should be possible to remove
|
||||||
var buffer = Tone.context.createBuffer(1, samples.length, this.samplesPerSecond);
|
const buffer = Tone.context.createBuffer(1, samples.length, this.samplesPerSecond);
|
||||||
|
|
||||||
// todo: optimize this? e.g. replace the divide by storing 1/32768 and multiply?
|
// todo: optimize this? e.g. replace the divide by storing 1/32768 and multiply?
|
||||||
for (var i=0; i<samples.length; i++) {
|
for (let i = 0; i < samples.length; i++) {
|
||||||
buffer.getChannelData(0)[i] = samples[i] / 32768;
|
buffer.getChannelData(0)[i] = samples[i] / 32768;
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(buffer);
|
resolve(buffer);
|
||||||
}.bind(this));
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -95,14 +95,14 @@ ADPCMSoundDecoder.prototype.indexTable = [
|
||||||
ADPCMSoundDecoder.prototype.extractChunk = function (chunkType, stream) {
|
ADPCMSoundDecoder.prototype.extractChunk = function (chunkType, stream) {
|
||||||
stream.position = 12;
|
stream.position = 12;
|
||||||
while (stream.position < (stream.getLength() - 8)) {
|
while (stream.position < (stream.getLength() - 8)) {
|
||||||
var typeStr = stream.readUint8String(4);
|
const typeStr = stream.readUint8String(4);
|
||||||
var chunkSize = stream.readInt32();
|
const chunkSize = stream.readInt32();
|
||||||
if (typeStr == chunkType) {
|
if (typeStr == chunkType) {
|
||||||
var chunk = stream.extract(chunkSize);
|
const chunk = stream.extract(chunkSize);
|
||||||
return chunk;
|
return chunk;
|
||||||
} else {
|
|
||||||
stream.position += chunkSize;
|
|
||||||
}
|
}
|
||||||
|
stream.position += chunkSize;
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -114,17 +114,17 @@ ADPCMSoundDecoder.prototype.extractChunk = function (chunkType, stream) {
|
||||||
* @return {Int16Array} the uncompressed audio samples
|
* @return {Int16Array} the uncompressed audio samples
|
||||||
*/
|
*/
|
||||||
ADPCMSoundDecoder.prototype.imaDecompress = function (compressedData, blockSize) {
|
ADPCMSoundDecoder.prototype.imaDecompress = function (compressedData, blockSize) {
|
||||||
var sample, step, code, delta;
|
let sample, step, code, delta;
|
||||||
var index = 0;
|
let index = 0;
|
||||||
var lastByte = -1; // -1 indicates that there is no saved lastByte
|
let lastByte = -1; // -1 indicates that there is no saved lastByte
|
||||||
var out = [];
|
const out = [];
|
||||||
|
|
||||||
// Bail and return no samples if we have no data
|
// Bail and return no samples if we have no data
|
||||||
if (!compressedData) return out;
|
if (!compressedData) return out;
|
||||||
|
|
||||||
compressedData.position = 0;
|
compressedData.position = 0;
|
||||||
var a = 0;
|
const a = 0;
|
||||||
while (a==0) {
|
while (a == 0) {
|
||||||
if (((compressedData.position % blockSize) == 0) && (lastByte < 0)) { // read block header
|
if (((compressedData.position % blockSize) == 0) && (lastByte < 0)) { // read block header
|
||||||
if (compressedData.getBytesAvailable() == 0) break;
|
if (compressedData.getBytesAvailable() == 0) break;
|
||||||
sample = compressedData.readInt16();
|
sample = compressedData.readInt16();
|
||||||
|
@ -159,7 +159,7 @@ ADPCMSoundDecoder.prototype.imaDecompress = function (compressedData, blockSize)
|
||||||
out.push(sample);
|
out.push(sample);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var samples = Int16Array.from(out);
|
const samples = Int16Array.from(out);
|
||||||
return samples;
|
return samples;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -19,8 +19,8 @@ function ArrayBufferStream (arrayBuffer) {
|
||||||
* @return {ArrayBufferStream} the extracted stream
|
* @return {ArrayBufferStream} the extracted stream
|
||||||
*/
|
*/
|
||||||
ArrayBufferStream.prototype.extract = function (length) {
|
ArrayBufferStream.prototype.extract = function (length) {
|
||||||
var slicedArrayBuffer = this.arrayBuffer.slice(this.position, this.position+length);
|
const slicedArrayBuffer = this.arrayBuffer.slice(this.position, this.position + length);
|
||||||
var newStream = new ArrayBufferStream(slicedArrayBuffer);
|
const newStream = new ArrayBufferStream(slicedArrayBuffer);
|
||||||
return newStream;
|
return newStream;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ ArrayBufferStream.prototype.getBytesAvailable = function () {
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
ArrayBufferStream.prototype.readUint8 = function () {
|
ArrayBufferStream.prototype.readUint8 = function () {
|
||||||
var val = new Uint8Array(this.arrayBuffer, this.position, 1)[0];
|
const val = new Uint8Array(this.arrayBuffer, this.position, 1)[0];
|
||||||
this.position += 1;
|
this.position += 1;
|
||||||
return val;
|
return val;
|
||||||
};
|
};
|
||||||
|
@ -55,10 +55,10 @@ ArrayBufferStream.prototype.readUint8 = function () {
|
||||||
* @return {String} a String made by concatenating the chars in the input
|
* @return {String} a String made by concatenating the chars in the input
|
||||||
*/
|
*/
|
||||||
ArrayBufferStream.prototype.readUint8String = function (length) {
|
ArrayBufferStream.prototype.readUint8String = function (length) {
|
||||||
var arr = new Uint8Array(this.arrayBuffer, this.position, length);
|
const arr = new Uint8Array(this.arrayBuffer, this.position, length);
|
||||||
this.position += length;
|
this.position += length;
|
||||||
var str = '';
|
let str = '';
|
||||||
for (var i=0; i<arr.length; i++) {
|
for (let i = 0; i < arr.length; i++) {
|
||||||
str += String.fromCharCode(arr[i]);
|
str += String.fromCharCode(arr[i]);
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
|
@ -69,7 +69,7 @@ ArrayBufferStream.prototype.readUint8String = function (length) {
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
ArrayBufferStream.prototype.readInt16 = function () {
|
ArrayBufferStream.prototype.readInt16 = function () {
|
||||||
var val = new Int16Array(this.arrayBuffer, this.position, 1)[0];
|
const val = new Int16Array(this.arrayBuffer, this.position, 1)[0];
|
||||||
this.position += 2; // one 16 bit int is 2 bytes
|
this.position += 2; // one 16 bit int is 2 bytes
|
||||||
return val;
|
return val;
|
||||||
};
|
};
|
||||||
|
@ -79,7 +79,7 @@ ArrayBufferStream.prototype.readInt16 = function () {
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
ArrayBufferStream.prototype.readUint16 = function () {
|
ArrayBufferStream.prototype.readUint16 = function () {
|
||||||
var val = new Uint16Array(this.arrayBuffer, this.position, 1)[0];
|
const val = new Uint16Array(this.arrayBuffer, this.position, 1)[0];
|
||||||
this.position += 2; // one 16 bit int is 2 bytes
|
this.position += 2; // one 16 bit int is 2 bytes
|
||||||
return val;
|
return val;
|
||||||
};
|
};
|
||||||
|
@ -89,7 +89,7 @@ ArrayBufferStream.prototype.readUint16 = function () {
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
ArrayBufferStream.prototype.readInt32 = function () {
|
ArrayBufferStream.prototype.readInt32 = function () {
|
||||||
var val = new Int32Array(this.arrayBuffer, this.position, 1)[0];
|
const val = new Int32Array(this.arrayBuffer, this.position, 1)[0];
|
||||||
this.position += 4; // one 32 bit int is 4 bytes
|
this.position += 4; // one 32 bit int is 4 bytes
|
||||||
return val;
|
return val;
|
||||||
};
|
};
|
||||||
|
@ -99,7 +99,7 @@ ArrayBufferStream.prototype.readInt32 = function () {
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
ArrayBufferStream.prototype.readUint32 = function () {
|
ArrayBufferStream.prototype.readUint32 = function () {
|
||||||
var val = new Uint32Array(this.arrayBuffer, this.position, 1)[0];
|
const val = new Uint32Array(this.arrayBuffer, this.position, 1)[0];
|
||||||
this.position += 4; // one 32 bit int is 4 bytes
|
this.position += 4; // one 32 bit int is 4 bytes
|
||||||
return val;
|
return val;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
var SoundPlayer = require('./SoundPlayer');
|
const SoundPlayer = require('./SoundPlayer');
|
||||||
var Tone = require('tone');
|
const Tone = require('tone');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A prototype for the drum sound functionality that can load drum sounds, play, and stop them.
|
* A prototype for the drum sound functionality that can load drum sounds, play, and stop them.
|
||||||
|
@ -9,8 +9,8 @@ var Tone = require('tone');
|
||||||
function DrumPlayer (outputNode) {
|
function DrumPlayer (outputNode) {
|
||||||
this.outputNode = outputNode;
|
this.outputNode = outputNode;
|
||||||
|
|
||||||
var baseUrl = 'https://raw.githubusercontent.com/LLK/scratch-audio/develop/sound-files/drums/';
|
const baseUrl = 'https://raw.githubusercontent.com/LLK/scratch-audio/develop/sound-files/drums/';
|
||||||
var fileNames = [
|
const fileNames = [
|
||||||
'SnareDrum(1)',
|
'SnareDrum(1)',
|
||||||
'BassDrum(1b)',
|
'BassDrum(1b)',
|
||||||
'SideStick(1)',
|
'SideStick(1)',
|
||||||
|
@ -33,8 +33,8 @@ function DrumPlayer (outputNode) {
|
||||||
|
|
||||||
this.drumSounds = [];
|
this.drumSounds = [];
|
||||||
|
|
||||||
for (var i=0; i<fileNames.length; i++) {
|
for (let i = 0; i < fileNames.length; i++) {
|
||||||
var url = baseUrl + fileNames[i] + '_22k.wav';
|
const url = `${baseUrl + fileNames[i]}_22k.wav`;
|
||||||
this.drumSounds[i] = new SoundPlayer(this.outputNode);
|
this.drumSounds[i] = new SoundPlayer(this.outputNode);
|
||||||
this.drumSounds[i].setBuffer(new Tone.Buffer(url));
|
this.drumSounds[i].setBuffer(new Tone.Buffer(url));
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ DrumPlayer.prototype.play = function (drum, outputNode) {
|
||||||
* Stop all drum sounds.
|
* Stop all drum sounds.
|
||||||
*/
|
*/
|
||||||
DrumPlayer.prototype.stopAll = function () {
|
DrumPlayer.prototype.stopAll = function () {
|
||||||
for (var i=0; i<this.drumSounds.length; i++) {
|
for (let i = 0; i < this.drumSounds.length; i++) {
|
||||||
this.drumSounds[i].stop();
|
this.drumSounds[i].stop();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
var Tone = require('tone');
|
const Tone = require('tone');
|
||||||
var Soundfont = require('soundfont-player');
|
const Soundfont = require('soundfont-player');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A prototype for the instrument sound functionality that can play notes.
|
* A prototype for the instrument sound functionality that can play notes.
|
||||||
|
@ -37,13 +37,13 @@ function InstrumentPlayer (outputNode) {
|
||||||
* @param {number} vol - a volume level (0-100%)
|
* @param {number} vol - a volume level (0-100%)
|
||||||
*/
|
*/
|
||||||
InstrumentPlayer.prototype.playNoteForSecWithInstAndVol = function (note, sec, instrumentNum, vol) {
|
InstrumentPlayer.prototype.playNoteForSecWithInstAndVol = function (note, sec, instrumentNum, vol) {
|
||||||
var gain = vol / 100;
|
const gain = vol / 100;
|
||||||
this.loadInstrument(instrumentNum)
|
this.loadInstrument(instrumentNum)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
this.instruments[instrumentNum].play(
|
this.instruments[instrumentNum].play(
|
||||||
note, Tone.context.currentTime, {
|
note, Tone.context.currentTime, {
|
||||||
duration : sec,
|
duration: sec,
|
||||||
gain : gain
|
gain: gain
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -57,20 +57,20 @@ InstrumentPlayer.prototype.playNoteForSecWithInstAndVol = function (note, sec, i
|
||||||
InstrumentPlayer.prototype.loadInstrument = function (instrumentNum) {
|
InstrumentPlayer.prototype.loadInstrument = function (instrumentNum) {
|
||||||
if (this.instruments[instrumentNum]) {
|
if (this.instruments[instrumentNum]) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
} else {
|
}
|
||||||
return Soundfont.instrument(Tone.context, this.instrumentNames[instrumentNum])
|
return Soundfont.instrument(Tone.context, this.instrumentNames[instrumentNum])
|
||||||
.then((inst) => {
|
.then(inst => {
|
||||||
inst.connect(this.outputNode);
|
inst.connect(this.outputNode);
|
||||||
this.instruments[instrumentNum] = inst;
|
this.instruments[instrumentNum] = inst;
|
||||||
});
|
});
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop all notes being played on all instruments
|
* Stop all notes being played on all instruments
|
||||||
*/
|
*/
|
||||||
InstrumentPlayer.prototype.stopAll = function () {
|
InstrumentPlayer.prototype.stopAll = function () {
|
||||||
for (var i=0; i<this.instruments.length; i++) {
|
for (let i = 0; i < this.instruments.length; i++) {
|
||||||
if (this.instruments[i]) {
|
if (this.instruments[i]) {
|
||||||
this.instruments[i].stop();
|
this.instruments[i].stop();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
var Tone = require('tone');
|
const Tone = require('tone');
|
||||||
var log = require('./log');
|
const log = require('./log');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A SoundPlayer stores an audio buffer, and plays it
|
* A SoundPlayer stores an audio buffer, and plays it
|
||||||
|
@ -74,8 +74,8 @@ SoundPlayer.prototype.start = function () {
|
||||||
* @return {Promise} a Promise that resolves when the sound finishes playing
|
* @return {Promise} a Promise that resolves when the sound finishes playing
|
||||||
*/
|
*/
|
||||||
SoundPlayer.prototype.finished = function () {
|
SoundPlayer.prototype.finished = function () {
|
||||||
var storedContext = this;
|
const storedContext = this;
|
||||||
return new Promise(function (resolve) {
|
return new Promise(resolve => {
|
||||||
storedContext.bufferSource.onended = function () {
|
storedContext.bufferSource.onended = function () {
|
||||||
this.isPlaying = false;
|
this.isPlaying = false;
|
||||||
resolve();
|
resolve();
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
var Tone = require('tone');
|
const Tone = require('tone');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An echo effect (aka 'delay effect' in audio terms)
|
* An echo effect (aka 'delay effect' in audio terms)
|
||||||
|
@ -36,8 +36,8 @@ EchoEffect.prototype.set = function (val) {
|
||||||
this.wet.value = 0.5;
|
this.wet.value = 0.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
var feedback = (this.value / 100) * 0.75;
|
const feedback = (this.value / 100) * 0.75;
|
||||||
this.delay.feedback.rampTo(feedback, 1/60);
|
this.delay.feedback.rampTo(feedback, 1 / 60);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -59,4 +59,3 @@ EchoEffect.prototype.clamp = function (input, min, max) {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = EchoEffect;
|
module.exports = EchoEffect;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
var Tone = require('tone');
|
const Tone = require('tone');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A fuzz effect (aka 'distortion effect' in audio terms)
|
* A fuzz effect (aka 'distortion effect' in audio terms)
|
||||||
|
@ -49,4 +49,3 @@ FuzzEffect.prototype.clamp = function (input, min, max) {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = FuzzEffect;
|
module.exports = FuzzEffect;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
var Tone = require('tone');
|
const Tone = require('tone');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A pan effect, which moves the sound to the left or right between the speakers
|
* A pan effect, which moves the sound to the left or right between the speakers
|
||||||
|
@ -50,4 +50,3 @@ PanEffect.prototype.clamp = function (input, min, max) {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = PanEffect;
|
module.exports = PanEffect;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
var Tone = require('tone');
|
const Tone = require('tone');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A pitch change effect, which changes the playback rate of the sound in order
|
* A pitch change effect, which changes the playback rate of the sound in order
|
||||||
|
@ -71,7 +71,7 @@ PitchEffect.prototype.updatePlayer = function (player) {
|
||||||
PitchEffect.prototype.updatePlayers = function (players) {
|
PitchEffect.prototype.updatePlayers = function (players) {
|
||||||
if (!players) return;
|
if (!players) return;
|
||||||
|
|
||||||
for (var md5 in players) {
|
for (const md5 in players) {
|
||||||
if (players.hasOwnProperty(md5)) {
|
if (players.hasOwnProperty(md5)) {
|
||||||
this.updatePlayer(players[md5]);
|
this.updatePlayer(players[md5]);
|
||||||
}
|
}
|
||||||
|
@ -79,4 +79,3 @@ PitchEffect.prototype.updatePlayers = function (players) {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = PitchEffect;
|
module.exports = PitchEffect;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
var Tone = require('tone');
|
const Tone = require('tone');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A reverb effect, simulating reverberation in a room
|
* A reverb effect, simulating reverberation in a room
|
||||||
|
@ -50,4 +50,3 @@ ReverbEffect.prototype.clamp = function (input, min, max) {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = ReverbEffect;
|
module.exports = ReverbEffect;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
|
const Tone = require('tone');
|
||||||
var Tone = require('tone');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A "robotic" effect that adds a low-pitched buzzing to the sound, reminiscent of the
|
* A "robotic" effect that adds a low-pitched buzzing to the sound, reminiscent of the
|
||||||
|
@ -17,7 +16,7 @@ function RoboticEffect () {
|
||||||
|
|
||||||
this.value = 0;
|
this.value = 0;
|
||||||
|
|
||||||
var time = this._delayTimeForValue(100);
|
const time = this._delayTimeForValue(100);
|
||||||
this.feedbackCombFilter = new Tone.FeedbackCombFilter(time, 0.9);
|
this.feedbackCombFilter = new Tone.FeedbackCombFilter(time, 0.9);
|
||||||
|
|
||||||
this.effectSend.chain(this.feedbackCombFilter, this.effectReturn);
|
this.effectSend.chain(this.feedbackCombFilter, this.effectReturn);
|
||||||
|
@ -40,8 +39,8 @@ RoboticEffect.prototype.set = function (val) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// set delay time using the value
|
// set delay time using the value
|
||||||
var time = this._delayTimeForValue(this.value);
|
const time = this._delayTimeForValue(this.value);
|
||||||
this.feedbackCombFilter.delayTime.rampTo(time, 1/60);
|
this.feedbackCombFilter.delayTime.rampTo(time, 1 / 60);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -60,10 +59,9 @@ RoboticEffect.prototype.changeBy = function (val) {
|
||||||
* @returns {number} a delay time in seconds
|
* @returns {number} a delay time in seconds
|
||||||
*/
|
*/
|
||||||
RoboticEffect.prototype._delayTimeForValue = function (val) {
|
RoboticEffect.prototype._delayTimeForValue = function (val) {
|
||||||
var midiNote = ((val - 100) / 10) + 36;
|
const midiNote = ((val - 100) / 10) + 36;
|
||||||
var freq = Tone.Frequency(midiNote, 'midi').eval();
|
const freq = Tone.Frequency(midiNote, 'midi').eval();
|
||||||
return 1 / freq;
|
return 1 / freq;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = RoboticEffect;
|
module.exports = RoboticEffect;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
var Tone = require('tone');
|
const Tone = require('tone');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A wobble effect. In audio terms, it sounds like tremolo.
|
* A wobble effect. In audio terms, it sounds like tremolo.
|
||||||
|
@ -36,7 +36,7 @@ WobbleEffect.prototype.set = function (val) {
|
||||||
|
|
||||||
this.wet.value = this.value / 100;
|
this.wet.value = this.value / 100;
|
||||||
|
|
||||||
this.wobbleLFO.frequency.rampTo(this.value / 10, 1/60);
|
this.wobbleLFO.frequency.rampTo(this.value / 10, 1 / 60);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,4 +58,3 @@ WobbleEffect.prototype.clamp = function (input, min, max) {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = WobbleEffect;
|
module.exports = WobbleEffect;
|
||||||
|
|
||||||
|
|
52
src/index.js
52
src/index.js
|
@ -1,18 +1,18 @@
|
||||||
var log = require('./log');
|
const log = require('./log');
|
||||||
var Tone = require('tone');
|
const Tone = require('tone');
|
||||||
|
|
||||||
var PitchEffect = require('./effects/PitchEffect');
|
const PitchEffect = require('./effects/PitchEffect');
|
||||||
var PanEffect = require('./effects/PanEffect');
|
const PanEffect = require('./effects/PanEffect');
|
||||||
|
|
||||||
var RoboticEffect = require('./effects/RoboticEffect');
|
const RoboticEffect = require('./effects/RoboticEffect');
|
||||||
var FuzzEffect = require('./effects/FuzzEffect');
|
const FuzzEffect = require('./effects/FuzzEffect');
|
||||||
var EchoEffect = require('./effects/EchoEffect');
|
const EchoEffect = require('./effects/EchoEffect');
|
||||||
var ReverbEffect = require('./effects/ReverbEffect');
|
const ReverbEffect = require('./effects/ReverbEffect');
|
||||||
|
|
||||||
var SoundPlayer = require('./SoundPlayer');
|
const SoundPlayer = require('./SoundPlayer');
|
||||||
var ADPCMSoundDecoder = require('./ADPCMSoundDecoder');
|
const ADPCMSoundDecoder = require('./ADPCMSoundDecoder');
|
||||||
var InstrumentPlayer = require('./InstrumentPlayer');
|
const InstrumentPlayer = require('./InstrumentPlayer');
|
||||||
var DrumPlayer = require('./DrumPlayer');
|
const DrumPlayer = require('./DrumPlayer');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fileOverview Scratch Audio is divided into a single AudioEngine,
|
* @fileOverview Scratch Audio is divided into a single AudioEngine,
|
||||||
|
@ -35,7 +35,7 @@ function AudioEngine () {
|
||||||
|
|
||||||
// chain the global effects to the output
|
// chain the global effects to the output
|
||||||
this.input = new Tone.Gain();
|
this.input = new Tone.Gain();
|
||||||
this.input.chain (
|
this.input.chain(
|
||||||
this.roboticEffect, this.fuzzEffect, this.echoEffect, this.reverbEffect,
|
this.roboticEffect, this.fuzzEffect, this.echoEffect, this.reverbEffect,
|
||||||
Tone.Master
|
Tone.Master
|
||||||
);
|
);
|
||||||
|
@ -70,7 +70,7 @@ function AudioEngine () {
|
||||||
*/
|
*/
|
||||||
AudioEngine.prototype.decodeSound = function (sound) {
|
AudioEngine.prototype.decodeSound = function (sound) {
|
||||||
|
|
||||||
var loaderPromise = null;
|
let loaderPromise = null;
|
||||||
|
|
||||||
switch (sound.format) {
|
switch (sound.format) {
|
||||||
case '':
|
case '':
|
||||||
|
@ -83,12 +83,12 @@ AudioEngine.prototype.decodeSound = function (sound) {
|
||||||
return log.warn('unknown sound format', sound.format);
|
return log.warn('unknown sound format', sound.format);
|
||||||
}
|
}
|
||||||
|
|
||||||
var storedContext = this;
|
const storedContext = this;
|
||||||
return loaderPromise.then(
|
return loaderPromise.then(
|
||||||
function (decodedAudio) {
|
decodedAudio => {
|
||||||
storedContext.audioBuffers[sound.md5] = new Tone.Buffer(decodedAudio);
|
storedContext.audioBuffers[sound.md5] = new Tone.Buffer(decodedAudio);
|
||||||
},
|
},
|
||||||
function (error) {
|
error => {
|
||||||
log.warn('audio data could not be decoded', error);
|
log.warn('audio data could not be decoded', error);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -112,7 +112,7 @@ AudioEngine.prototype.loadSounds = function () {
|
||||||
* @return {Promise} a Promise that resolves after the duration has elapsed
|
* @return {Promise} a Promise that resolves after the duration has elapsed
|
||||||
*/
|
*/
|
||||||
AudioEngine.prototype.playNoteForBeatsWithInstAndVol = function (note, beats, inst, vol) {
|
AudioEngine.prototype.playNoteForBeatsWithInstAndVol = function (note, beats, inst, vol) {
|
||||||
var sec = this.beatsToSec(beats);
|
const sec = this.beatsToSec(beats);
|
||||||
this.instrumentPlayer.playNoteForSecWithInstAndVol(note, sec, inst, vol);
|
this.instrumentPlayer.playNoteForSecWithInstAndVol(note, sec, inst, vol);
|
||||||
return this.waitForBeats(beats);
|
return this.waitForBeats(beats);
|
||||||
};
|
};
|
||||||
|
@ -132,9 +132,9 @@ AudioEngine.prototype.beatsToSec = function (beats) {
|
||||||
* @return {Promise} a Promise that resolves after the duration has elapsed
|
* @return {Promise} a Promise that resolves after the duration has elapsed
|
||||||
*/
|
*/
|
||||||
AudioEngine.prototype.waitForBeats = function (beats) {
|
AudioEngine.prototype.waitForBeats = function (beats) {
|
||||||
var storedContext = this;
|
const storedContext = this;
|
||||||
return new Promise(function (resolve) {
|
return new Promise(resolve => {
|
||||||
setTimeout(function () {
|
setTimeout(() => {
|
||||||
resolve();
|
resolve();
|
||||||
}, storedContext.beatsToSec(beats) * 1000);
|
}, storedContext.beatsToSec(beats) * 1000);
|
||||||
});
|
});
|
||||||
|
@ -170,9 +170,9 @@ AudioEngine.prototype.getLoudness = function () {
|
||||||
}
|
}
|
||||||
if (this.mic && this.mic.state == 'started') {
|
if (this.mic && this.mic.state == 'started') {
|
||||||
return this.micMeter.value * 100;
|
return this.micMeter.value * 100;
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
return -1;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -245,7 +245,7 @@ AudioPlayer.prototype.playSound = function (md5) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new soundplayer to play the sound
|
// create a new soundplayer to play the sound
|
||||||
var player = new SoundPlayer();
|
const player = new SoundPlayer();
|
||||||
player.setBuffer(this.audioEngine.audioBuffers[md5]);
|
player.setBuffer(this.audioEngine.audioBuffers[md5]);
|
||||||
player.connect(this.effectsNode);
|
player.connect(this.effectsNode);
|
||||||
this.pitchEffect.updatePlayer(player);
|
this.pitchEffect.updatePlayer(player);
|
||||||
|
@ -255,7 +255,7 @@ AudioPlayer.prototype.playSound = function (md5) {
|
||||||
this.activeSoundPlayers[md5] = player;
|
this.activeSoundPlayers[md5] = player;
|
||||||
|
|
||||||
// remove sounds that are not playing from the active sound players array
|
// remove sounds that are not playing from the active sound players array
|
||||||
for (var id in this.activeSoundPlayers) {
|
for (const id in this.activeSoundPlayers) {
|
||||||
if (this.activeSoundPlayers.hasOwnProperty(id)) {
|
if (this.activeSoundPlayers.hasOwnProperty(id)) {
|
||||||
if (!this.activeSoundPlayers[id].isPlaying) {
|
if (!this.activeSoundPlayers[id].isPlaying) {
|
||||||
delete this.activeSoundPlayers[id];
|
delete this.activeSoundPlayers[id];
|
||||||
|
@ -283,7 +283,7 @@ AudioPlayer.prototype.playDrumForBeats = function (drum, beats) {
|
||||||
*/
|
*/
|
||||||
AudioPlayer.prototype.stopAllSounds = function () {
|
AudioPlayer.prototype.stopAllSounds = function () {
|
||||||
// stop all active sound players
|
// stop all active sound players
|
||||||
for (var md5 in this.activeSoundPlayers) {
|
for (const md5 in this.activeSoundPlayers) {
|
||||||
this.activeSoundPlayers[md5].stop();
|
this.activeSoundPlayers[md5].stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
var minilog = require('minilog');
|
const minilog = require('minilog');
|
||||||
minilog.enable();
|
minilog.enable();
|
||||||
|
|
||||||
module.exports = minilog('scratch-audioengine');
|
module.exports = minilog('scratch-audioengine');
|
||||||
|
|
|
@ -2,7 +2,7 @@ var path = require('path');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
entry: {
|
entry: {
|
||||||
'dist': './src/index.js'
|
dist: './src/index.js'
|
||||||
},
|
},
|
||||||
output: {
|
output: {
|
||||||
path: __dirname,
|
path: __dirname,
|
||||||
|
|
Loading…
Reference in a new issue