1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2019 Rafael da Silva Rocha. |
3
|
|
|
* Copyright (c) 2017 Brett Zamir, 2012 Niklas von Hertzen |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
6
|
|
|
* a copy of this software and associated documentation files (the |
7
|
|
|
* "Software"), to deal in the Software without restriction, including |
8
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
9
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
10
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
11
|
|
|
* the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be |
14
|
|
|
* included in all copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
18
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
19
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
20
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
21
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
22
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Encode a byte buffer as a base64 string. |
30
|
|
|
* @param {!Uint8Array} bytes The buffer. |
31
|
|
|
* @return {string} A .wav file as a DataURI. |
32
|
|
|
*/ |
33
|
|
|
export function encode(bytes) { |
34
|
|
|
let base64 = ''; |
35
|
|
|
for (let i = 0; i < bytes.length; i += 3) { |
36
|
|
|
base64 += chars[bytes[i] >> 2]; |
37
|
|
|
base64 += chars[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4]; |
38
|
|
|
base64 += chars[(bytes[i + 1] & 15) << 2 | bytes[i + 2] >> 6]; |
39
|
|
|
base64 += chars[bytes[i + 2] & 63]; |
40
|
|
|
} |
41
|
|
|
if (bytes.length % 3 === 2) { |
42
|
|
|
base64 = base64.substring(0, base64.length - 1) + '='; |
43
|
|
|
} else if (bytes.length % 3 === 1) { |
44
|
|
|
base64 = base64.substring(0, base64.length - 2) + '=='; |
45
|
|
|
} |
46
|
|
|
return base64; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Decode a base64 string as a byte as buffer. |
51
|
|
|
* @param {string} base64 A .wav file as a DataURI. |
52
|
|
|
* @return {ArrayBuffer} A .wav file as a DataURI. |
53
|
|
|
*/ |
54
|
|
|
export function decode(base64) { |
55
|
|
|
let lookup = new Uint8Array(256); |
56
|
|
|
for (let i = 0; i < chars.length; i++) { |
57
|
|
|
lookup[chars.charCodeAt(i)] = i; |
58
|
|
|
} |
59
|
|
|
let bufferLength = base64.length * 0.75; |
60
|
|
|
if (base64[base64.length - 1] === '=') { |
61
|
|
|
bufferLength--; |
62
|
|
|
if (base64[base64.length - 2] === '=') { |
63
|
|
|
bufferLength--; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
let buffer = new ArrayBuffer(bufferLength); |
67
|
|
|
let bytes = new Uint8Array(buffer); |
68
|
|
|
for (let i = 0, j = 0; i < base64.length; i += 4) { |
69
|
|
|
let encoded1 = lookup[base64.charCodeAt(i)]; |
70
|
|
|
let encoded2 = lookup[base64.charCodeAt(i + 1)]; |
71
|
|
|
let encoded3 = lookup[base64.charCodeAt(i + 2)]; |
72
|
|
|
let encoded4 = lookup[base64.charCodeAt(i + 3)]; |
73
|
|
|
bytes[j++] = encoded1 << 2 | encoded2 >> 4; |
74
|
|
|
bytes[j++] = (encoded2 & 15) << 4 | encoded3 >> 2; |
75
|
|
|
bytes[j++] = (encoded3 & 3) << 6 | encoded4 & 63; |
76
|
|
|
} |
77
|
|
|
return buffer; |
78
|
|
|
} |
79
|
|
|
|