audio-player: corrected indentation and comments

This commit is contained in:
Trek H 2019-09-24 17:15:58 +09:30
parent 1a8493853e
commit 1de5438565
4 changed files with 142 additions and 140 deletions

View File

@ -23,7 +23,7 @@
</div> </div>
<div class="container-fluid"> <div class="container-fluid">
<div class="form-group"> <div class="form-group">
<input class="form-control-file" type="file" id="fileinput" onchange="processData();"> <input class="form-control-file" type="file" id="fileinput" onchange="playFile();">
</div> </div>
</div> </div>
<div class="container-fluid"> <div class="container-fluid">

View File

@ -23,20 +23,21 @@ LICENSE
If not, see [GNU licenses](http://www.gnu.org/licenses). If not, see [GNU licenses](http://www.gnu.org/licenses).
*/ */
function processData() { // playFile will process and play the chosen target file.
function playFile() {
const input = event.target.files[0] const input = event.target.files[0]
const reader = new FileReader() const reader = new FileReader()
reader.onload = event => { reader.onload = event => {
bytes = new Uint8Array(event.target.result) bytes = new Uint8Array(event.target.result)
// decode adpcm to pcm // Decode adpcm to pcm.
var decoded = decode(Array.from(bytes)) var decoded = decode(Array.from(bytes))
// convert raw pcm to wav TODO(Trek): make these configurable. // Convert raw pcm to wav TODO(Trek): make these configurable.
var wav = pcmToWav(decoded, 48000, 1, 16); var wav = pcmToWav(decoded, 48000, 1, 16);
// play wav data in player // Play wav data in player.
const blob = new Blob([Uint8Array.from(wav)], { const blob = new Blob([Uint8Array.from(wav)], {
type: 'audio/wav' type: 'audio/wav'
}); });
@ -65,6 +66,7 @@ function getQuery() {
} }
} }
// load gets the file from the given url and displays a link for download.
function load() { function load() {
var url = document.getElementById('url').value; var url = document.getElementById('url').value;
if (url == "") { if (url == "") {

View File

@ -24,24 +24,24 @@ LICENSE
// pcmToWav takes raw pcm data along with the sample rate, number of channels and bit-depth, // pcmToWav takes raw pcm data along with the sample rate, number of channels and bit-depth,
// and adds a WAV header to it so that it can be read and played by common players. // and adds a WAV header to it so that it can be read and played by common players.
// input and output data bytes are represented as arrays of 8 bit integers. // Input and output data bytes are represented as arrays of 8 bit integers.
// WAV spec.: http://soundfile.sapp.org/doc/WaveFormat/ // WAV spec.: http://soundfile.sapp.org/doc/WaveFormat/
function pcmToWav(data, rate, channels, bitdepth) { function pcmToWav(data, rate, channels, bitdepth) {
subChunk2ID = [100, 97, 116, 97]; // "data" subChunk2ID = [100, 97, 116, 97]; // "data".
subChunk2Size = int32ToBytes(data.length); subChunk2Size = int32ToBytes(data.length);
subChunk1ID = [102, 109, 116, 32]; // "fmt " subChunk1ID = [102, 109, 116, 32]; // "fmt ".
subChunk1Size = int32ToBytes(16); subChunk1Size = int32ToBytes(16);
audioFmt = int16ToBytes(1); // 1 = PCM audioFmt = int16ToBytes(1); // 1 = PCM.
numChannels = int16ToBytes(channels); numChannels = int16ToBytes(channels);
sampleRate = int32ToBytes(rate); sampleRate = int32ToBytes(rate);
byteRate = int32ToBytes(rate * channels * bitdepth / 8); byteRate = int32ToBytes(rate * channels * bitdepth / 8);
blockAlign = int16ToBytes(channels * bitdepth / 8); blockAlign = int16ToBytes(channels * bitdepth / 8);
bitsPerSample = int16ToBytes(bitdepth) bitsPerSample = int16ToBytes(bitdepth)
chunkID = [82, 73, 70, 70]; // "RIFF" chunkID = [82, 73, 70, 70]; // "RIFF".
chunkSize = int32ToBytes(36 + data.length); chunkSize = int32ToBytes(36 + data.length);
format = [87, 65, 86, 69]; // "WAVE" format = [87, 65, 86, 69]; // "WAVE".
result = chunkID; result = chunkID;
result.push(...chunkSize, ...format, ...subChunk1ID, ...subChunk1Size, ...audioFmt, ...numChannels, ...sampleRate, ...byteRate, ...blockAlign, ...bitsPerSample, ...subChunk2ID, ...subChunk2Size); result.push(...chunkSize, ...format, ...subChunk1ID, ...subChunk1Size, ...audioFmt, ...numChannels, ...sampleRate, ...byteRate, ...blockAlign, ...bitsPerSample, ...subChunk2ID, ...subChunk2Size);