Browse Source

Clean up and comment out functions to help explain

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

145
vocoder/static/vocoder.js

@ -5,59 +5,71 @@
// 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() {
if (recorder.recording) { /**
recorder.stop(); * Stop recording a new recording.
shouldGenerateNewShape = true; **/
} if (recorder.recording) {
recorder.stop();
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",
body: new FormData().append("soundBlob", soundBlob), body: new FormData().append("soundBlob", soundBlob),
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);
} function setupRecording() {
/**
* Setup logic for recording.
**/
microphone = new p5.AudioIn();
microphone.start();
recorder = new p5.SoundRecorder();
recorder.setInput(microphone);
recording = new p5.SoundFile();
// TODO: we'll probably want something more glam than // TODO: buttons are just for experimenting ...
// some default buttons but that's fine 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