Skip to content

Instantly share code, notes, and snippets.

@LewisGet
Created August 28, 2016 04:11
Show Gist options
  • Save LewisGet/db61bcb16268c19ab9615e3a050fcb4a to your computer and use it in GitHub Desktop.
Save LewisGet/db61bcb16268c19ab9615e3a050fcb4a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<!-- saved from url=(0039)http://mdn.github.io/decode-audio-data/ -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width">
<title>decodeAudioData example</title>
</head>
<body>
<input id="fileName" type="text" value="" />
<button onclick="window.getSoundBuffer(window.fileName.value);">excute</button>
<button onclick="window.remake(window.source.buffer);">remake</button>
<script type="text/javascript">
window.getSoundBuffer = function (fileName) {
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var source = audioCtx.createBufferSource();
request = new XMLHttpRequest();
request.open('GET', './' + fileName, true);
request.responseType = 'arraybuffer';
request.onload = function() {
var audioData = request.response;
audioCtx.decodeAudioData(audioData, function(buffer) {
source.buffer = buffer;
window.source = source;
console.log(buffer);
});
};
request.send();
};
window.remake = function (buffer) {
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var newBuffer = audioCtx.createBuffer(1, buffer.length, buffer.sampleRate);
bufferChannel = buffer.getChannelData(0);
newBufferChannel = newBuffer.getChannelData(0);
for (var i = 0; i < bufferChannel.length; i++)
{
newBufferChannel[i] = bufferChannel[i];
}
var source = audioCtx.createBufferSource();
source.buffer = newBuffer;
source.connect(audioCtx.destination);
source.start();
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment