Skip to content

Commit

Permalink
jshint
Browse files Browse the repository at this point in the history
  • Loading branch information
cesine committed Dec 19, 2014
1 parent a9b75a7 commit 5820465
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions js/recordmp3.js
@@ -1,12 +1,12 @@
(function(window){
(function(global){

var WORKER_PATH = 'js/recorderWorker.js';
var encoderWorker = new Worker('js/mp3Worker.js');
var audio_context, source;

var __log = function(e, data) {
log.innerHTML += "\n" + e + " " + (data || '');
}
};

var Recorder = function(cfg){
var config = cfg || {};
Expand Down Expand Up @@ -65,15 +65,15 @@

var average = values / length;
self.vumeter.style.width = Math.min(parseInt(average*2),100)+'%';
}
};

this.configure = function(cfg){
for (var prop in cfg){
if (cfg.hasOwnProperty(prop)){
config[prop] = cfg[prop];
}
}
}
};

this.toggleRecording = function() {
if (recording) {
Expand All @@ -85,11 +85,11 @@
config.element.className += ' recording';
self.audio = null;
__log('Recording...');
}
};

this.record = function(){
recording = true;
}
};

this.stop = function() {
if (self.playing) {
Expand All @@ -111,11 +111,11 @@
btnStop.disabled = true;
btnRecord.disabled = false;
btnSave.disabled = false;
}
};

this.stopRecording = function(){
recording = false;
}
};

this.play = function() {
if (self.playing) {
Expand All @@ -142,7 +142,7 @@
btnPlay.innerHTML = '<span class="icon-pause"></span>';
}
}
}
};

this.save = function(){
btnPlay.disabled = true;
Expand All @@ -154,35 +154,35 @@
self.convertToMP3();
} else {
// Assume WAV.
window[self.callback](self, self.audioData);
global[self.callback](self, self.audioData);
}
}
};

this.clear = function(){
worker.postMessage({ command: 'clear' });
initButtons();
removeClass(config.element, 'recording');
removeClass(config.element, 'processing');
}
};

this.getBuffer = function(cb) {
currCallback = cb || config.callback;
worker.postMessage({ command: 'getBuffer' })
}
worker.postMessage({ command: 'getBuffer' });
};

this.exportWAV = function(type){
type = type || config.type || 'audio/wav';
worker.postMessage({
command: 'exportWAV',
type: type
});
}
};

worker.onmessage = function(e){
var blob = e.data;
self.audioData = blob;
btnPlay.disabled = false;
}
};

this.convertToMP3 = function() {
var arrayBuffer;
Expand Down Expand Up @@ -210,29 +210,29 @@
__log("Done converting to Mp3");

var mp3Blob = new Blob([new Uint8Array(e.data.buf)], {type: 'audio/mp3'});
window[self.callback](self, mp3Blob);
global[self.callback](self, mp3Blob);

}
};
};

fileReader.readAsArrayBuffer(this.audioData);
}
};


function encode64(buffer) {
var encode64 = function(buffer) {
var binary = '',
bytes = new Uint8Array( buffer ),
len = bytes.byteLength;

for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}
return global.btoa( binary );
};

function parseWav(wav) {
function readInt(i, bytes) {
var parseWav = function(wav) {
var readInt= function(i, bytes) {
var ret = 0,
shft = 0;

Expand All @@ -243,31 +243,31 @@
bytes--;
}
return ret;
}
};
if (readInt(20, 2) != 1) throw 'Invalid compression code, not PCM';
if (readInt(22, 2) != 1) throw 'Invalid number of channels, not 1';
return {
sampleRate: readInt(24, 4),
bitsPerSample: readInt(34, 2),
samples: wav.subarray(44)
};
}
};

function Uint8ArrayToFloat32Array(u8a){
var Uint8ArrayToFloat32Array= function(u8a){
var f32Buffer = new Float32Array(u8a.length);
for (var i = 0; i < u8a.length; i++) {
var value = u8a[i<<1] + (u8a[(i<<1)+1]<<8);
if (value >= 0x8000) value |= ~0x7FFF;
f32Buffer[i] = value / 0x8000;
}
return f32Buffer;
}
};

function removeClass(el, name) {
var removeClass= function(el, name) {
el.className = el.className.replace(' '+name, '');
}
};

function buildInterface() {
var buildInterface = function() {
__log('Building interface...');
initButtons();
config.element.appendChild(btnPlay);
Expand All @@ -276,10 +276,10 @@
config.element.appendChild(btnSave);
self.vumeter = config.element.querySelector('.btn-record .vumeter');
__log('Interface built.');
}
function initButtons() {
};
var initButtons =function() {
btnRecord.onclick = self.toggleRecording;
btnRecord.className = 'btn-record'
btnRecord.className = 'btn-record';
btnRecord.innerHTML = '<span class="vumeter"></span><span class="icon-record"></span>';
btnRecord.disabled = false;
btnStop.onclick = self.stop;
Expand All @@ -294,7 +294,7 @@
btnSave.className = 'btn-save';
btnSave.innerHTML = '<span class="icon-upload"></span>';
btnSave.disabled = true;
}
};

source.connect(this.analyser);
this.analyser.connect(this.node);
Expand All @@ -303,22 +303,22 @@
buildInterface();

return this;
__log('Recorder initialised.');
// __log('Recorder initialised.');
};

window.Recorder = Recorder;
global.Recorder = Recorder;

var initRecorder = function() {
try {
// webkit shim
window.AudioContext = window.AudioContext || window.webkitAudioContext;
global.AudioContext = global.AudioContext || global.webkitAudioContext;
navigator.getUserMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
window.URL = window.URL || window.webkitURL;
global.URL = global.URL || global.webkitURL;

audio_context = new AudioContext;
audio_context = new AudioContext();
__log('Audio context set up.');
__log('navigator.getUserMedia ' + (navigator.getUserMedia ? 'available.' : 'not present!'));
} catch (e) {
Expand All @@ -328,7 +328,7 @@
navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
__log('No live audio input: ' + e);
});
}
};

var startUserMedia = function(stream) {
var recorders = document.querySelectorAll('.RecordMP3js-recorder');
Expand All @@ -339,12 +339,12 @@
for(var i=0; i<recorders.length; i++) {
recorders[i].recorder = new Recorder({element: recorders[i]});
}
}
};

if (window.addEventListener) {
window.addEventListener('load', initRecorder, false);
} else if (window.attachEvent) {
window.attachEvent('onload', initRecorder);
if (global.addEventListener) {
global.addEventListener('load', initRecorder, false);
} else if (global.attachEvent) {
global.attachEvent('onload', initRecorder);
}

})(window);
})(exports || window);

0 comments on commit 5820465

Please sign in to comment.