-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix decode audio recorded by Apple's Telegram
- Loading branch information
1 parent
4644db7
commit 855f712
Showing
7 changed files
with
227 additions
and
72 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
<!DOCTYPE html> | ||
|
||
<html> | ||
|
||
<head> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>Decode Example</title> | ||
<style type="text/css"> | ||
ul { list-style: none; } | ||
li { margin: 1em; } | ||
audio { display: block; } | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<h1>Decoder Example</h1> | ||
<p>Choose either a file from disk, or a file from a server.</p> | ||
<p>File is decoded to pcm buffers and then played back as Riff</p> | ||
|
||
<h2>Options</h2> | ||
|
||
<div> | ||
<label>Decoder output sample rate</label> | ||
<input id="sampleRate" type="number" value="48000" /> | ||
</div> | ||
|
||
<div> | ||
<label>Wave file bit depth</label> | ||
<input id="bitDepth" type="number" value="16" /> | ||
</div> | ||
|
||
<h2>Commands</h2> | ||
<input type="file" id="fileInput"/> | ||
<button type="button" id="remoteButton">Load using XMLHttpRequest</button> | ||
|
||
<h2>Recordings</h2> | ||
<ul id="recordingslist"></ul> | ||
|
||
<script> | ||
function decodeOgg(arrayBuffer){ | ||
|
||
var typedArray = new Uint8Array(arrayBuffer); | ||
var decoderWorker = new Worker('../dist-unminified/decoderWorker.js'); | ||
var wavWorker = new Worker('../dist-unminified/waveWorker.js'); | ||
var desiredSampleRate = parseInt(sampleRate.value,10); | ||
|
||
decoderWorker.postMessage({ | ||
command:'init', | ||
decoderSampleRate: desiredSampleRate, | ||
outputBufferSampleRate: desiredSampleRate | ||
}); | ||
|
||
wavWorker.postMessage({ | ||
command:'init', | ||
wavBitDepth: parseInt(bitDepth.value,10), | ||
wavSampleRate: desiredSampleRate | ||
}); | ||
|
||
|
||
var use16 = true; | ||
var text = use16 ? 'INT16' : 'FLOAT32'; | ||
var int16Array = []; | ||
var float32Array = []; | ||
decoderWorker.onmessage = function(e){ | ||
var data = e.data; | ||
// null means decoder is finished | ||
if(!data || data.type == 'done') { | ||
console.log('waveform:', data && data.waveform); | ||
wavWorker.postMessage({ command: 'done' }); | ||
} else { // e.data contains decoded buffers as float32 values | ||
wavWorker.postMessage({ | ||
command: 'encode', | ||
buffers: e.data | ||
}, e.data.map(function(typedArray){ | ||
var int16 = typedArray instanceof Int16Array; | ||
for(var i = 0; i < typedArray.length; ++i) { | ||
var sample = typedArray[i]; | ||
if(!int16) { | ||
sample = Math.floor(sample * 32767.5 - 0.5); | ||
float32Array.push(sample); | ||
} else { | ||
sample = Math.floor(sample - 0.5); | ||
int16Array.push(sample); | ||
} | ||
|
||
text += sample + ','; | ||
} | ||
|
||
return typedArray.buffer; | ||
})); | ||
} | ||
}; | ||
|
||
wavWorker.onmessage = function(e){ | ||
var data = e.data; | ||
// null signifies that no more data is expected and worker is closed. | ||
if (data && data.page) { | ||
var fileName = new Date().toISOString() + ".wav"; | ||
var dataBlob = new Blob( [ data.page ], { type: "audio/wav" } ); | ||
var url = URL.createObjectURL( dataBlob ); | ||
|
||
var audio = document.createElement('audio'); | ||
audio.controls = true; | ||
audio.src = URL.createObjectURL( dataBlob ); | ||
|
||
var blob = new Blob([text], {type: "text/plain;charset=utf-8"}); | ||
url = URL.createObjectURL(blob); | ||
fileName = new Date().toISOString() + ".txt"; | ||
|
||
var link = document.createElement('a'); | ||
link.href = url; | ||
link.download = fileName; | ||
link.innerHTML = link.download; | ||
|
||
console.log(int16Array, float32Array); | ||
|
||
var li = document.createElement('li'); | ||
li.appendChild(link); | ||
li.appendChild(audio); | ||
|
||
recordingslist.appendChild(li); | ||
|
||
if(!float32Array.length) { | ||
decoderWorker.postMessage({ | ||
command:'init', | ||
decoderSampleRate: desiredSampleRate, | ||
outputBufferSampleRate: desiredSampleRate | ||
}); | ||
|
||
wavWorker.postMessage({ | ||
command:'init', | ||
wavBitDepth: parseInt(bitDepth.value,10), | ||
wavSampleRate: desiredSampleRate | ||
}); | ||
|
||
setTimeout(() => { | ||
console.log('pass typedArray', typedArray); | ||
decoderWorker.postMessage({ | ||
//command: 'decode', | ||
//command: 'waveform', | ||
//command: 'decode16', | ||
command: 'decode', | ||
pages: typedArray, | ||
waveform: true | ||
}, [typedArray.buffer] ); | ||
}, 500); | ||
} else { | ||
var diff = []; | ||
for(var i = 0; i < int16Array.length; ++i) { | ||
var first = int16Array[i]; | ||
var second = float32Array[i]; | ||
if(Math.abs(second - first) == 1) continue; | ||
else if(first != second) { | ||
diff.push({index: i, values: [first, second]}); | ||
} | ||
} | ||
console.log(diff); | ||
} | ||
} | ||
}; | ||
|
||
decoderWorker.postMessage({ | ||
command: 'decode', | ||
//command: 'waveform', | ||
//command: 'decode16', | ||
//command: use16 ? 'decode16' : 'decode', | ||
pages: typedArray, | ||
waveform: true | ||
}/* , [typedArray.buffer] */ ); | ||
}; | ||
|
||
fileInput.onchange = function(e){ | ||
var fileReader = new FileReader(); | ||
|
||
fileReader.onload = function() { | ||
decodeOgg(this.result); | ||
}; | ||
|
||
fileReader.readAsArrayBuffer( e.target.files[0] ); | ||
}; | ||
|
||
remoteButton.onclick = function(){ | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open("GET", "./mono.opus", true); | ||
xhr.responseType = "arraybuffer"; | ||
|
||
xhr.onload = function(e) { | ||
decodeOgg(this.response); | ||
}; | ||
|
||
xhr.send(); | ||
}; | ||
|
||
</script> | ||
</body> | ||
</html> |
Binary file not shown.
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