"use strict";
// tslint:disable:no-bitwise
/*
 * RIFFWAVE.js v0.02 - Audio encoder for HTML5 <audio> elements.
 * Copyright (C) 2011 Pedro Ladaria <pedro.ladaria at Gmail dot com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 * The full license is available at http://www.gnu.org/licenses/gpl.html
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 *
 * Changelog:
 *
 * 0.01 - First release
 * 0.02 - New faster base64 encoding
 *
 */
Object.defineProperty(exports, "__esModule", { value: true });
const FastBase64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const FastBase64EncLookup = [];
function FastBase64_Init() {
    for (let i = 0; i < 4096; i++) {
        FastBase64EncLookup[i] = FastBase64Chars[i >> 6] + FastBase64Chars[i & 0x3F];
    }
}
function FastBase64_Encode(src) {
    let len = src.length;
    let dst = '';
    let i = 0;
    let n;
    while (len > 2) {
        n = (src[i] << 16) | (src[i + 1] << 8) | src[i + 2];
        dst += FastBase64EncLookup[n >> 12] + FastBase64EncLookup[n & 0xFFF];
        len -= 3;
        i += 3;
    }
    if (len > 0) {
        const n1 = (src[i] & 0xFC) >> 2;
        let n2 = (src[i] & 0x03) << 4;
        if (len > 1)
            n2 |= (src[++i] & 0xF0) >> 4;
        dst += FastBase64Chars[n1];
        dst += FastBase64Chars[n2];
        if (len === 2) {
            let n3 = (src[i++] & 0x0F) << 2;
            n3 |= (src[i] & 0xC0) >> 6;
            dst += FastBase64Chars[n3];
        }
        if (len === 1)
            dst += '=';
        dst += '=';
    }
    return dst;
} // end Encode
exports.FastBase64_Encode = FastBase64_Encode;
FastBase64_Init();
function u32ToArray(i) {
    return [i & 0xFF, (i >> 8) & 0xFF, (i >> 16) & 0xFF, (i >> 24) & 0xFF];
}
function u16ToArray(i) {
    return [i & 0xFF, (i >> 8) & 0xFF];
}
function MakeRiff(sampleRate, bitsPerSample, data) {
    let wav = [];
    const header = {
        audioFormat: 1,
        bitsPerSample,
        blockAlign: 0,
        byteRate: 0,
        chunkId: [0x52, 0x49, 0x46, 0x46],
        chunkSize: 0,
        format: [0x57, 0x41, 0x56, 0x45],
        numChannels: 1,
        sampleRate,
        subChunk1Id: [0x66, 0x6d, 0x74, 0x20],
        subChunk1Size: 16,
        subChunk2Id: [0x64, 0x61, 0x74, 0x61],
        subChunk2Size: 0 // 40   4    data size = NumSamples*NumChannels*BitsPerSample/8
    };
    header.byteRate = (header.sampleRate * header.numChannels * header.bitsPerSample) >> 3;
    header.blockAlign = (header.numChannels * header.bitsPerSample) >> 3;
    header.subChunk2Size = data.length;
    header.chunkSize = 36 + header.subChunk2Size;
    wav = header.chunkId.concat(u32ToArray(header.chunkSize), header.format, header.subChunk1Id, u32ToArray(header.subChunk1Size), u16ToArray(header.audioFormat), u16ToArray(header.numChannels), u32ToArray(header.sampleRate), u32ToArray(header.byteRate), u16ToArray(header.blockAlign), u16ToArray(header.bitsPerSample), header.subChunk2Id, u32ToArray(header.subChunk2Size), data);
    const dataURI = 'data:audio/wav;base64,' + FastBase64_Encode(wav);
    const result = {
        dat: [],
        dataURI,
        header,
        wav
    };
    return result;
}
exports.MakeRiff = MakeRiff;
//# sourceMappingURL=riffwave.js.map