Skip to content

Commit 117637d

Browse files
authored
decoupled helpers method from p5 in helper.js and coupled them separately in app.js (#540)
1 parent b6b1562 commit 117637d

File tree

2 files changed

+70
-30
lines changed

2 files changed

+70
-30
lines changed

src/app.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,40 @@ p5.prototype.userStartAudio = userStartAudio;
77

88
import './master';
99

10-
import { freqToMidi, saveSound } from './helpers';
10+
11+
import {
12+
sampleRate,
13+
freqToMidi,
14+
midiToFreq,
15+
noteToFreq,
16+
soundFormats,
17+
disposeSound,
18+
_checkFileFormats,
19+
_mathChain,
20+
convertToWav,
21+
interleave,
22+
writeUTFBytes,
23+
safeBufferSize,
24+
} from './helpers';
25+
p5.prototype.sampleRate = sampleRate;
1126
p5.prototype.freqToMidi = freqToMidi;
27+
p5.prototype.midiToFreq = midiToFreq;
28+
p5.prototype.noteToFreq = noteToFreq;
29+
p5.prototype.soundFormats = soundFormats;
30+
p5.prototype.disposeSound = disposeSound;
31+
p5.prototype._checkFileFormats = _checkFileFormats;
32+
p5.prototype._mathChain = _mathChain;
33+
p5.prototype.convertToWav = convertToWav;
34+
p5.prototype.interleave = interleave;
35+
p5.prototype.writeUTFBytes = writeUTFBytes;
36+
p5.prototype.safeBufferSize = safeBufferSize;
1237
p5.prototype.saveSound = saveSound;
1338

39+
// register removeSound to dispose of p5sound SoundFiles, Convolvers,
40+
// Oscillators etc when sketch ends
41+
p5.prototype.registerMethod('remove', p5.prototype.disposeSound);
42+
43+
1444
import './errorHandler';
1545
import './audioWorklet';
1646

@@ -76,6 +106,7 @@ p5.Delay = Delay;
76106

77107

78108

109+
79110
import { Reverb, Convolver, createConvolver } from './reverb';
80111
p5.Reverb = Reverb;
81112
p5.Convolver = Convolver;
@@ -102,15 +133,16 @@ import Compressor from './compressor';
102133
p5.Compressor = Compressor;
103134

104135

136+
105137
import peakDetect from './peakDetect';
106138
p5.peakDetect = peakDetect;
107139

108140

109141

142+
110143
import SoundRecorder from './soundRecorder';
111144
p5.SoundRecorder = SoundRecorder;
112145

113-
114146
import Distortion from './distortion';
115147
p5.Distortion = Distortion;
116148

src/helpers.js

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import processorNames from './audioWorklet/processorNames';
1414
* @method sampleRate
1515
* @return {Number} samplerate samples per second
1616
*/
17-
p5.prototype.sampleRate = function () {
17+
function sampleRate() {
1818
return p5sound.audiocontext.sampleRate;
19-
};
19+
}
2020

2121
/**
2222
* Returns the closest MIDI note value for
@@ -27,11 +27,11 @@ p5.prototype.sampleRate = function () {
2727
* above Middle C is 440Hz
2828
* @return {Number} MIDI note value
2929
*/
30-
export const freqToMidi = function (f) {
30+
function freqToMidi(f) {
3131
var mathlog2 = Math.log(f / 440) / Math.log(2);
3232
var m = Math.round(12 * mathlog2) + 69;
3333
return m;
34-
};
34+
}
3535

3636
/**
3737
* Returns the frequency value of a MIDI note value.
@@ -77,12 +77,12 @@ export const freqToMidi = function (f) {
7777
* }
7878
* </code></div>
7979
*/
80-
export var midiToFreq = (p5.prototype.midiToFreq = function (m) {
80+
function midiToFreq(m) {
8181
return 440 * Math.pow(2, (m - 69) / 12.0);
82-
});
82+
}
8383

8484
// This method converts ANSI notes specified as a string "C4", "Eb3" to a frequency
85-
export var noteToFreq = function (note) {
85+
function noteToFreq(note) {
8686
if (typeof note !== 'string') {
8787
return note;
8888
}
@@ -102,7 +102,7 @@ export var noteToFreq = function (note) {
102102
break;
103103
}
104104
return midiToFreq(value);
105-
};
105+
}
106106

107107
/**
108108
* List the SoundFile formats that you will include. LoadSound
@@ -133,7 +133,8 @@ export var noteToFreq = function (note) {
133133
* }
134134
* </code></div>
135135
*/
136-
p5.prototype.soundFormats = function () {
136+
137+
function soundFormats() {
137138
// reset extensions array
138139
p5sound.extensions = [];
139140
// add extensions
@@ -145,19 +146,15 @@ p5.prototype.soundFormats = function () {
145146
throw arguments[i] + ' is not a valid sound format!';
146147
}
147148
}
148-
};
149+
}
149150

150-
p5.prototype.disposeSound = function () {
151+
function disposeSound() {
151152
for (var i = 0; i < p5sound.soundArray.length; i++) {
152153
p5sound.soundArray[i].dispose();
153154
}
154-
};
155-
156-
// register removeSound to dispose of p5sound SoundFiles, Convolvers,
157-
// Oscillators etc when sketch ends
158-
p5.prototype.registerMethod('remove', p5.prototype.disposeSound);
155+
}
159156

160-
p5.prototype._checkFileFormats = function (paths) {
157+
function _checkFileFormats(paths) {
161158
var path;
162159
// if path is a single string, check to see if extension is provided
163160
if (typeof paths === 'string') {
@@ -215,12 +212,12 @@ p5.prototype._checkFileFormats = function (paths) {
215212
}
216213
}
217214
return path;
218-
};
215+
}
219216

220217
/**
221218
* Used by Osc and Envelope to chain signal math
222219
*/
223-
p5.prototype._mathChain = function (o, math, thisChain, nextChain, type) {
220+
function _mathChain(o, math, thisChain, nextChain, type) {
224221
// if this type of math already exists in the chain, replace it
225222
for (var i in o.mathOps) {
226223
if (o.mathOps[i] instanceof type) {
@@ -236,13 +233,13 @@ p5.prototype._mathChain = function (o, math, thisChain, nextChain, type) {
236233
math.connect(nextChain);
237234
o.mathOps[thisChain] = math;
238235
return o;
239-
};
236+
}
240237

241238
// helper methods to convert audio file as .wav format,
242239
// will use as saving .wav file and saving blob object
243240
// Thank you to Matt Diamond's RecorderJS (MIT License)
244241
// https://github.com/mattdiamond/Recorderjs
245-
export function convertToWav(audioBuffer) {
242+
function convertToWav(audioBuffer) {
246243
var leftChannel, rightChannel;
247244
leftChannel = audioBuffer.getChannelData(0);
248245

@@ -314,7 +311,7 @@ function writeUTFBytes(view, offset, string) {
314311
}
315312
}
316313

317-
export function safeBufferSize(idealBufferSize) {
314+
function safeBufferSize(idealBufferSize) {
318315
let bufferSize = idealBufferSize;
319316

320317
// if the AudioWorkletNode is actually a ScriptProcessorNode created via polyfill,
@@ -334,6 +331,7 @@ export function safeBufferSize(idealBufferSize) {
334331
return bufferSize;
335332
}
336333

334+
337335
/**
338336
* Save a p5.SoundFile as a .wav file. The browser will prompt the user
339337
* to download the file to their device.
@@ -351,9 +349,19 @@ export function saveSound(soundFile, fileName) {
351349
p5.prototype.writeFile([dataView], fileName, 'wav');
352350
}
353351

354-
// export default {
355-
// // convertToWav: convertToWav,
356-
// // midiToFreq: midiToFreq,
357-
// // noteToFreq: noteToFreq,
358-
// // safeBufferSize: safeBufferSize
359-
// };
352+
353+
export {
354+
sampleRate,
355+
freqToMidi,
356+
midiToFreq,
357+
noteToFreq,
358+
soundFormats,
359+
disposeSound,
360+
_checkFileFormats,
361+
_mathChain,
362+
convertToWav,
363+
interleave,
364+
writeUTFBytes,
365+
safeBufferSize,
366+
saveSound
367+
};

0 commit comments

Comments
 (0)