Skip to content

Commit

Permalink
Bug fixes and updates to Sound Spinner experiment
Browse files Browse the repository at this point in the history
iOS tapper fixes, etc
  • Loading branch information
bretmorris committed Feb 22, 2018
1 parent d09f254 commit 3243605
Show file tree
Hide file tree
Showing 15 changed files with 356 additions and 170 deletions.
29 changes: 10 additions & 19 deletions soundspinner/app/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ require(["domready", "interface/UserInterface", "main.scss", "mic/Waveform", "mi
function(domReady, UserInterface, mainStyle, Waveform, Recorder, Player, Loader, StartAudioContext, Tone, Microphone){

domReady(function(){

var recordTime = 2;
var recordTime = 3;

var buttonTimeout = -1;
var currentRotation = 0;
var rotationSpeed = 0;
var isRecording = false;
var isDragging = false;
var dragSpeed = 0;
var computedSpeed = 0;

//INTERFACE////////////////////////////////////////////////

Expand Down Expand Up @@ -56,12 +55,8 @@ require(["domready", "interface/UserInterface", "main.scss", "mic/Waveform", "mi
player.position = 0;
recorder.open(function(){
recorder.start();
isRecording = true;
waveform.isRecording = isRecording;
buttonTimeout = setTimeout(function(){
interface.stopRecording();
isRecording = false;
waveform.isRecording = isRecording;
player.setBuffer(recorder.audioBuffer);
}, recordTime * 1000);
}, function(e){
Expand All @@ -71,7 +66,6 @@ require(["domready", "interface/UserInterface", "main.scss", "mic/Waveform", "mi
});

interface.on("StopRecord", function(drag){
isRecording = false;
recorder.stop();
player.setBuffer(recorder.audioBuffer);
clearTimeout(buttonTimeout);
Expand All @@ -94,7 +88,7 @@ require(["domready", "interface/UserInterface", "main.scss", "mic/Waveform", "mi

var player = new Player(recordTime);

var waveform = new Waveform(interface.waveDisplay, recorder.audioBuffer);
var waveform = new Waveform(interface.waveDisplay, recorder);

function animateIn(){
//bring everything in
Expand All @@ -118,15 +112,9 @@ require(["domready", "interface/UserInterface", "main.scss", "mic/Waveform", "mi
//make a full screen element and put it in front
var iOSTapper = document.createElement("div");
iOSTapper.id = "iOSTap";
iOSTapper.addEventListener("touchstart", function(e){
e.preventDefault();
});
document.body.appendChild(iOSTapper);
StartAudioContext.setContext(Tone.context);
StartAudioContext.on(iOSTapper);
StartAudioContext.onStarted(function(){
new StartAudioContext(Tone.context, iOSTapper).then(function() {
iOSTapper.remove();
animateIn();
window.parent.postMessage("ready","*");
});
} else {
Expand All @@ -142,14 +130,17 @@ require(["domready", "interface/UserInterface", "main.scss", "mic/Waveform", "mi

function loop(){
requestAnimationFrame(loop);
var alpha = 0.15;
var speed = rotationSpeed;
if (isDragging){
speed = dragSpeed;
}
player.speed = alpha * speed + (1 - alpha) * player.speed;
var alpha = 0.05;

computedSpeed = alpha * speed + (1 - alpha) * computedSpeed;

player.speed = computedSpeed;

if (!isRecording){
if (!recorder.isRecording){
waveform.setRotation(player.position * Math.PI * 2);
} else {
player.speed = 0;
Expand Down
44 changes: 44 additions & 0 deletions soundspinner/app/interface/Orientation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

define(function () {

var OrientationListener = function(callback){

window.addEventListener("orientationchange", this._changed.bind(this));
if (window.screen && window.screen.orientation){
window.screen.orientation.addEventListener("change", this._screenChange.bind(this));
}

this._callback = callback;
};

OrientationListener.prototype._changed = function(){
//check if it's landscape
if (Math.abs(window.orientation) === 90){
this._callback();
}
};

OrientationListener.prototype._screenChange = function(){
//check if it's landscape
if (Math.abs(window.screen.orientation.angle) === 90){
this._callback();
}
};

return OrientationListener;
});
142 changes: 142 additions & 0 deletions soundspinner/app/interface/Slider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

define(["slider.scss"], function (mainStyle) {

var Slider = function(container, min, max, initialValue){

/**
* The minimum slider value
* @type {Number}
* @private
*/
this._min = min || 0;

/**
* The maximum slider value
* @type {Number}
* @private
*/
this._max = max || 100;

/**
* The slider container
* @type {Element}
* @private
*/
this._container = document.createElement("div");
this._container.classList.add("Slider");
this._container.id = "SpeedSlider";
container.appendChild(this._container);

/**
* The slider which captures the inputs
* @type {Element}
* @private
*/
this._range = document.createElement("input");
this._range.type = "range";
this._range.id = "Range";
this._range.min = min;
this._range.max = max;
this._range.step = 0.01;
this._container.appendChild(this._range);
this._range.addEventListener("input", this._change.bind(this));

/**
* The railing behind the handle
* @type {Element}
* @private
*/
this._rail = document.createElement("div");
this._rail.id = "Rail";
this._container.appendChild(this._rail);

/**
* The railing behind the handle
* @type {Element}
* @private
*/
this._dot = document.createElement("div");
this._dot.id = "Dot";
this._rail.appendChild(this._dot);

/**
* The handle of the slider
* @type {Element}
* @private
*/
this._handle = document.createElement("div");
this._handle.id = "Handle";
this._container.appendChild(this._handle);

/**
* Internal number holder
* @type {Number}
* @private
*/
this._value = 0;

/**
* Onchange handler
* @type {Function}
*/
this.onchange = function(){};

//set the position initially
this.setValue(initialValue || 0);

//add a resize handler
window.addEventListener("resize", this._update.bind(this));
};

Slider.prototype._change = function(){
this._update();
this._value = parseFloat(this._range.value);
this.onchange(this._range.value);
};

Slider.prototype._update = function(){
var percent = (this._range.value - this._min) / (this._max - this._min);

if (Math.abs(this._range.value) < 0.15){
this._range.value = 0;
percent = 0.5;
}

var handleOffset = this._handle.offsetWidth * percent;
var halfHandle = this._handle.offsetWidth / 2;
var percentPixels = percent * this._container.offsetWidth;
//computer the width in pixels
this._handle.style.left = (percentPixels - handleOffset).toString() + "px";
};

Slider.prototype.animateIn = function(){
this._container.classList.add("Visible");
};

Slider.prototype.setValue = function(val){
this._value = val;
this._range.value = val;
this._update();
};

Slider.prototype.getValue = function(){
return this._value;
};

return Slider;
});
39 changes: 23 additions & 16 deletions soundspinner/app/interface/UserInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
var MathUtils = require('util/MathUtils');
var $ = require('jquery');
var controlsStyle = require('controls.scss');
var Slider = require('interface/Slider');
var OrientationChange = require('interface/Orientation');

module.exports = function(maxRecordTime, container) {

Expand All @@ -52,12 +54,18 @@ module.exports = function(maxRecordTime, container) {
_self.recordButton = $('<div>').prop('id', 'RecordButton').appendTo(_self.waveDisplay);
_self.recordButton.addClass("icon-svg_record");

_self.speedSlider = $('<input>').prop('id', 'SpeedSlider').appendTo(container)
.prop("type", "range").prop("min", "-2").prop("max", 2).prop("step", "0.01").prop("value", 0);
// _self.speedSliderContainer = $("<div>").prop("id", "SliderContainer").appendTo(container);

// _self.playButton = $('<div>').prop('id', 'playButton').appendTo(container);
// _self.pauseButton = $('<div>').prop('id', 'pauseButton').appendTo(container);
// _self.footer = $('<div>').prop('id', 'footer').appendTo(container);
// _self.speedSlider = $('<input>').prop('id', 'SpeedSlider').appendTo(_self.speedSliderContainer)
// .prop("type", "range").prop("min", "-2").prop("max", 2).prop("step", "0.01").prop("value", 0);

_self.speedSlider = new Slider(container, -2, 2, 0);

_self.oreitnationListener = new OrientationChange(function(){
_self.speedSlider.setValue(0);
_self.doEvent('SpeedControllUpdate',0);
_self.doEvent('EndWaveDrag');
});
},

_self.disableRecording = function(callback) {
Expand All @@ -72,8 +80,7 @@ module.exports = function(maxRecordTime, container) {

_self.animateIn = function(callback) {
_self.recordButton.addClass("Visible");

_self.speedSlider.addClass("Visible");
_self.speedSlider.animateIn();
},

_self.stopRecording = function(){
Expand All @@ -82,15 +89,9 @@ module.exports = function(maxRecordTime, container) {


_self.attachEvents = function() {
_self.speedSlider.on('input',function(e){
var val = Number(_self.speedSlider.val());
if(val < _self.slideZeroLock && val > -_self.slideZeroLock) {
val = 0;
_self.speedSlider[0].value = val;
}
_self.speedSlider.onchange = function(val){
_self.doEvent('SpeedControllUpdate',val);

});
};

_self.recordButton.on("click", function(e){
e.preventDefault();
Expand Down Expand Up @@ -128,14 +129,19 @@ module.exports = function(maxRecordTime, container) {
_self.waveDisplay.on("touchmove",_self.setMousePosition);
$(window).on("mouseup",_self.mouseUp);
$(window).on("touchend",_self.mouseUp);

$(window).on("blur", function() {
_self.speedSlider.setValue(0);
_self.doEvent('SpeedControllUpdate', 0);
});

$(window).resize(function(e){
_self.doEvent('ReizeWindow',[$(window).width(),$(window).height()]);
});
},

_self.setSliderValue = function(val) {
_self.speedSlider[0].value = val;
// _self.speedSlider[0].value = val;
},

_self.mouseDown = function(e){
Expand All @@ -153,6 +159,7 @@ module.exports = function(maxRecordTime, container) {
_self.waveDisplayDrag = false;
_self.needsUpdate = false;
_self.waveDisplay.off('mousemove');
_self.smoothedRotation = 0;
_self.doEvent('EndWaveDrag',e);
},

Expand Down
Loading

0 comments on commit 3243605

Please sign in to comment.