Created
February 13, 2014 06:57
-
-
Save dirkk0/8970979 to your computer and use it in GitHub Desktop.
timbre.js drag&drop mp3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Decode</title> | |
</head> | |
<body> | |
drag & drop an audio file. | |
<ul style="list-style:none"> | |
<li><input type="radio" name="method" id="createBuffer" value="createBuffer" checked><label for="createBuffer">createBuffer</label></li> | |
<li><input type="radio" name="method" id="decodeAudioData" value="decodeAudioData"><label for="decodeAudioData">decodeAudioData</label></li> | |
</ul> | |
<tt><pre id="result"></pre></tt> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> | |
<script> | |
$(function() { | |
"use strict"; | |
var begin = 0; | |
$(window).on("dragover", function() { | |
return false; | |
}); | |
$(window).on("drop", function(e) { | |
begin = Date.now(); | |
$("#result").text("decoding..."); | |
var reader = new FileReader(); | |
reader.onload = function(e) { | |
main(e.target.result); | |
}; | |
reader.readAsArrayBuffer(e.originalEvent.dataTransfer.files[0]); | |
return false; | |
}); | |
var AudioContext = window.webkitAudioContext || window.AudioContext; | |
var ctx = new AudioContext(); | |
var method = { | |
createBuffer: function(binary, callback) { | |
var buffer, data; | |
try { | |
buffer = ctx.createBuffer(binary, false); | |
data = { | |
sampleRate: ctx.sampleRate, | |
channels: buffer.numberOfChannels, | |
duration: buffer.getChannelData(0).length / ctx.sampleRate, | |
"processing time": Date.now() - begin | |
}; | |
} catch (e) { | |
data = { error: e.toString() }; | |
} | |
callback(data); | |
}, | |
decodeAudioData: function(binary, callback) { | |
ctx.decodeAudioData(binary, function(buffer) { | |
var data = { | |
sampleRate: ctx.sampleRate, | |
channels: buffer.numberOfChannels, | |
duration: buffer.getChannelData(0).length / ctx.sampleRate, | |
"processing time": Date.now() - begin | |
}; | |
callback(data); | |
}, function(e) { | |
callback({ error: "invalid audio data in ArrayBuffer???" }); | |
}); | |
} | |
}; | |
function main(file) { | |
method[$("input[name='method']:checked").val()](file, function(result) { | |
$("#result").text(JSON.stringify(result, null, 2)); | |
}); | |
} | |
}); | |
</script> | |
</body> | |
</head> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment