Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for picking audio files and playing them back! #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions css/master.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ body {
margin: 0;
overflow: hidden;
}

#filePicker {
position: absolute;
bottom: 0;
right: 0;
z-index: 1;
}
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</style>
</head>
<body>
<input id="filePicker" type="file">
<!-- <audio id="alert" src="sound/alert.ogg"></audio> -->
<div id="div"></div>
<textarea id="alertPitch" name="minPitch" rows="8" cols="80" title="alert for min pitch">0</textarea>
Expand Down
30 changes: 24 additions & 6 deletions js/fft/FFTdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ class _spectrogram {
this.analyser = false;
}
init(stream) {
this.audioCtx = new AudioContext();
this.analyser = this.audioCtx.createAnalyser();
this.analyser.fftSize = 2048*4;
this.tmpStream = this.audioCtx.createMediaStreamSource(stream);
this.audioCtx.createMediaStreamSource(stream).connect(this.analyser);
this.analyser.smoothingTimeConstant = 0;
this.resetAnalyser(stream);

this.data = new Uint8Array(this.analyser.frequencyBinCount);

this.analyserFrequencyBinCount = this.analyser.frequencyBinCount;
this.analyserSampleRate = this.audioCtx.sampleRate;
}
resetAnalyser(stream) {
if (this.audioCtx) this.audioCtx.close();
this.audioCtx = new AudioContext();
this.resetContext();
this.audioCtx.createMediaStreamSource(stream).connect(this.analyser);
this.tmpStream = this.audioCtx.createMediaStreamSource(stream);
}
resetContext() {
this.analyser = this.audioCtx.createAnalyser();
this.analyser.fftSize = 2048*4;
this.analyser.smoothingTimeConstant = 0;
}
update() {
this.analyser.getByteFrequencyData(this.data);
//
Expand All @@ -27,4 +34,15 @@ class _spectrogram {
// transform(this.data, this.empty);
// console.log(this.empty);
}
changeStream(stream) {
this.audioCtx.close();
this.audioCtx = null;
this.resetAnalyser(stream);
}
swapContext(ctx) {
let ret = this.audioCtx;
this.audioCtx = ctx;
this.resetContext();
return ret;
}
}
58 changes: 57 additions & 1 deletion js/interface/mouseControl.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@


let _audio = null;

let mClick = {
x : 0,
Expand Down Expand Up @@ -38,6 +39,9 @@ class _buttonControl {
if (child) {return child.spectrogramPauseToggle()}
this.spectrogram.setPause(this.spectrogram.paused ? false : true);
this.fft.setPause(this.fft.paused ? false : true);
if (_audio) {
this.spectrogram.paused ? _audio.pause() : _audio.play();
}
if (this.spectrogram.paused) {buttonList.buttons[5].setText(`‖`);}
else {buttonList.buttons[5].setText(`▶`);}

Expand Down Expand Up @@ -85,6 +89,43 @@ class _buttonControl {
rolloffSave.push({ ...smoothPeaks[i] });
}
}
changeUserDevice(child=false) {
if (child) {return child.changeUserDevice()}
navigator.mediaDevices.getUserMedia({ audio: true }).then(a => {
console.log('changing to ' + a.toString());
fft.changeStream(a);
}).catch(console.error);
}
playSoundFile(child=false) {
if (child) {return child.playSoundFile()}
let fs = document.querySelector("#filePicker").files;
if (fs.length < 1) {
alert("Please pick a file first!");
return;
}
let f = fs[0];
let u = URL.createObjectURL(f);
let a = new Audio(u);
let o;

_audio = a;

a.addEventListener('ended', ev => {
_audio = null;
URL.revokeObjectURL(u);
this.changeUserDevice();
});

let c = new AudioContext();
let source = c.createMediaElementSource(a);
source.connect(c.destination);

o = fft.swapContext(c);

source.connect(fft.analyser);

a.play();
}
}

let buttonControl = new _buttonControl();
Expand Down Expand Up @@ -166,7 +207,22 @@ function buttonsInit() {
tmpButton.setPos(20,140);
tmpButton.setText(`save rolloff`);
buttonList.add(tmpButton);

// changeUserDevice
tmpButton = new _button();
tmpButton.childBind(buttonControl);
tmpButton.setFunction(buttonControl.changeUserDevice)
tmpButton.setSize(140, 20);
tmpButton.setPos(20,170);
tmpButton.setText(`select user device`);
buttonList.add(tmpButton);
// playSoundFile
tmpButton = new _button();
tmpButton.childBind(buttonControl);
tmpButton.setFunction(buttonControl.playSoundFile)
tmpButton.setSize(140, 20);
tmpButton.setPos(20,200);
tmpButton.setText(`play sound`);
buttonList.add(tmpButton);
//
// tmpButton = new _button();
// tmpButton.childBind(buttonControl);
Expand Down
2 changes: 1 addition & 1 deletion js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function spectrum(stream) {
}

// ============================== controls ==============================
if ((m.x < 300 && m.y < 200)) {
if ((m.x < 300 && m.y < 240)) {
showControls = true;
alertPitchInput.style.visibility = "visible";
}
Expand Down