1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2017-2019 Rafael da Silva Rocha. |
3
|
|
|
* |
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
5
|
|
|
* a copy of this software and associated documentation files (the |
6
|
|
|
* "Software"), to deal in the Software without restriction, including |
7
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
8
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
9
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
10
|
|
|
* the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be |
13
|
|
|
* included in all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
18
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
19
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
20
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
21
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @fileoverview The WaveFile class. |
27
|
|
|
* @see https://github.com/rochars/wavefile |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** @module wavefile */ |
31
|
|
|
|
32
|
|
|
import {encode, decode} from 'base64-arraybuffer-es6'; |
33
|
|
|
import WaveFileConverter from './lib/wavefile-converter'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* A class to manipulate wav files. |
37
|
|
|
* @extends WaveFileConverter |
38
|
|
|
*/ |
39
|
|
|
export default class WaveFile extends WaveFileConverter { |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param {?Uint8Array=} wavBuffer A wave file buffer. |
43
|
|
|
* @throws {Error} If container is not RIFF, RIFX or RF64. |
44
|
|
|
* @throws {Error} If format is not WAVE. |
45
|
|
|
* @throws {Error} If no 'fmt ' chunk is found. |
46
|
|
|
* @throws {Error} If no 'data' chunk is found. |
47
|
|
|
*/ |
48
|
|
|
constructor(wavBuffer=null) { |
49
|
|
|
super(); |
50
|
|
|
if (wavBuffer) { |
51
|
|
|
this.fromBuffer(wavBuffer); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Use a .wav file encoded as a base64 string to load the WaveFile object. |
57
|
|
|
* @param {string} base64String A .wav file as a base64 string. |
58
|
|
|
* @throws {Error} If any property of the object appears invalid. |
59
|
|
|
*/ |
60
|
|
|
fromBase64(base64String) { |
61
|
|
|
this.fromBuffer(new Uint8Array(decode(base64String))); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return a base64 string representig the WaveFile object as a .wav file. |
66
|
|
|
* @return {string} A .wav file as a base64 string. |
67
|
|
|
* @throws {Error} If any property of the object appears invalid. |
68
|
|
|
*/ |
69
|
|
|
toBase64() { |
70
|
|
|
/** @type {!Uint8Array} */ |
71
|
|
|
let buffer = this.toBuffer(); |
72
|
|
|
return encode(buffer, 0, buffer.length); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return a DataURI string representig the WaveFile object as a .wav file. |
77
|
|
|
* The return of this method can be used to load the audio in browsers. |
78
|
|
|
* @return {string} A .wav file as a DataURI. |
79
|
|
|
* @throws {Error} If any property of the object appears invalid. |
80
|
|
|
*/ |
81
|
|
|
toDataURI() { |
82
|
|
|
return 'data:audio/wav;base64,' + this.toBase64(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Use a .wav file encoded as a DataURI to load the WaveFile object. |
87
|
|
|
* @param {string} dataURI A .wav file as DataURI. |
88
|
|
|
* @throws {Error} If any property of the object appears invalid. |
89
|
|
|
*/ |
90
|
|
|
fromDataURI(dataURI) { |
91
|
|
|
this.fromBase64(dataURI.replace('data:audio/wav;base64,', '')); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|