Browse Source

Clean up and comment out functions to help explain

main
Luke Murphy 5 years ago
parent
commit
11f9d7aa57
No known key found for this signature in database GPG Key ID: 5E2EF5A63E3718CC
  1. 131
      vocoder/static/vocoder.js

131
vocoder/static/vocoder.js

@ -5,45 +5,48 @@
// https://developer.mozilla.org/en-US/docs/web/javascript/reference // https://developer.mozilla.org/en-US/docs/web/javascript/reference
// //
// TODO: Read this from the environment so it can run propoerly var serverUrl = "http://localhost:5000"; // TODO: read from environment
// depending on whether it is in development or production mode
var serverUrl = "http://localhost:5000";
var archiveUrl = serverUrl + "/add-to-archive/"; var archiveUrl = serverUrl + "/add-to-archive/";
var microphone;
var mic, recorder, soundFile; var recorder;
var recording;
var recordingTimeout = 30000; // 30 seconds (in milliseconds) var recordingTimeout = 30000; // 30 seconds (in milliseconds)
var shouldGenerateNewShape = false; var newSoundJustRecorded = false;
var shapes = [];
function setupRecording() {
mic = new p5.AudioIn(); function doRecording() {
mic.start(); /**
* Starting recording.
recorder = new p5.SoundRecorder(); **/
recorder.setInput(mic); if (microphone.enabled) {
setTimeout(recorder.record(recording), recordingTimeout);
soundFile = new p5.SoundFile();
function doRecording() {
if (mic.enabled) {
setTimeout(recorder.record(soundFile), recordingTimeout);
}
} }
}
function doStopping() { function doStopping() {
/**
* Stop recording a new recording.
**/
if (recorder.recording) { if (recorder.recording) {
recorder.stop(); recorder.stop();
shouldGenerateNewShape = true; newSoundJustRecorded = true;
}
} }
}
function doPlaying() { function doPlaying() {
if (soundFile.isLoaded()) { /**
soundFile.play(); * Play the recording.
} **/
if (recording.isLoaded()) {
recording.play();
} }
}
function doArchiving() { function doArchiving() {
var soundBlob = soundFile.getBlob(); /**
* Send the recording to the back-end.
**/
var soundBlob = recording.getBlob();
var httpRequestOptions = { var httpRequestOptions = {
method: "POST", method: "POST",
@ -51,13 +54,22 @@ function setupRecording() {
headers: new Headers({ enctype: "multipart/form-data" }) headers: new Headers({ enctype: "multipart/form-data" })
}; };
// TODO: do error handling when things fail httpDo(archiveUrl, httpRequestOptions); // TODO: add error handling and fix
// TODO: this doesn't actually work right now }
httpDo(archiveUrl, httpRequestOptions);
}
// TODO: we'll probably want something more glam than function setupRecording() {
// some default buttons but that's fine for experimenting /**
* Setup logic for recording.
**/
microphone = new p5.AudioIn();
microphone.start();
recorder = new p5.SoundRecorder();
recorder.setInput(microphone);
recording = new p5.SoundFile();
// TODO: buttons are just for experimenting ...
recordButton = createButton("record"); recordButton = createButton("record");
recordButton.position(10, 90); recordButton.position(10, 90);
recordButton.mousePressed(doRecording); recordButton.mousePressed(doRecording);
@ -75,36 +87,41 @@ function setupRecording() {
playButton.mousePressed(doArchiving); playButton.mousePressed(doArchiving);
} }
function collectSoundParamters() { function getSoundInfo() {
// TODO: collect all the sound parameters /**
// [x] amplitude * Retrieve sound information like pitch, amplitude, duration, etc.
// [x] length of sound **/
// [ ] pitch amplitude = recording.getPeaks();
// [ ] nuance duration = recording.duration();
// btw, there is no "nuance" function for p5js. My current guess is to // pitch?
// perhaps measure it from the "getPeaks". For all wave peaks measured, // nuance?
// ??? (need to think this through, not really sure ...)
amplitude = soundFile.getPeaks();
duration = soundFile.duration();
} }
function generateShape() { function generateNewShape() {
// TODO: take in sound parameters and generate a shape /**
ellipse(100, 100, 25, 25); * Create a new p5.js shape.
**/
ellipse(100, 100, 25, 25); // TODO: experimenting for now ...
} }
function setup() { function setup() {
// TODO: Replace this with the leaflet/mappa setup just /**
// using this for now to draw something to experiment on * The p5.js initial setup function.
createCanvas(600, 600); **/
createCanvas(600, 600); // TODO: experimenting for now ...
setupRecording(); setupRecording();
} }
function draw() { function draw() {
if (shouldGenerateNewShape) { /**
collectSoundParamters(); * The p5.js draw loop.
generateShape(); **/
shouldGenerateNewShape = false; if (newSoundJustRecorded === true) {
shapes.push(generateNewShape());
newSoundJustRecorded = false;
}
for (var shape of shapes) {
shape.display();
} }
} }

Loading…
Cancel
Save