1
1
import extend = require( 'extend' ) ;
2
2
import { OutgoingHttpHeaders } from 'http' ;
3
3
import { UserOptions } from 'ibm-cloud-sdk-core' ;
4
+ import isStream = require( 'isstream' ) ;
5
+ import { Readable } from 'stream' ;
4
6
import { getSdkHeaders } from '../lib/common' ;
5
7
import SynthesizeStream = require( '../lib/synthesize-stream' ) ;
6
8
import GeneratedTextToSpeechV1 = require( './v1-generated' ) ;
@@ -10,11 +12,43 @@ class TextToSpeechV1 extends GeneratedTextToSpeechV1 {
10
12
super ( options ) ;
11
13
}
12
14
15
+ /**
16
+ * Repair the WAV header of an audio/wav file in Stream format.
17
+ * The Stream is read into memory, then the data is repaired and returned as a Buffer.
18
+ *
19
+ * @param {Buffer } wavFileAsStream - wave audio as a stream
20
+ * @return {Buffer } wavFileData - a Buffer with the correct header
21
+ */
22
+ repairWavHeaderStream = ( wavFileAsStream : Readable ) : Promise < Buffer > => {
23
+ // in case of accidentally calling the wrong method
24
+ if ( ! isStream ( wavFileAsStream ) ) {
25
+ return Buffer . isBuffer ( wavFileAsStream )
26
+ ? Promise . resolve ( this . repairWavHeader ( wavFileAsStream ) )
27
+ : Promise . reject ( 'Expected input data to be a Stream.' ) ;
28
+ }
29
+
30
+ const buffers : Buffer [ ] = [ ] ;
31
+ return new Promise ( ( resolve , reject ) => {
32
+ // stream info to the buffer
33
+ wavFileAsStream . on ( 'data' , data => {
34
+ buffers . push ( data ) ;
35
+ } ) ;
36
+
37
+ wavFileAsStream . on ( 'end' , ( ) => {
38
+ resolve ( this . repairWavHeader ( Buffer . concat ( buffers ) ) ) ;
39
+ } ) ;
40
+
41
+ wavFileAsStream . on ( 'error' , err => {
42
+ reject ( err ) ;
43
+ } ) ;
44
+ } )
45
+ } ;
46
+
13
47
/**
14
48
* Repair the WAV header of an audio/wav file.
15
49
*
16
50
* @param {Buffer } wavFileData - Wave audio - will be edited in place and returned
17
- * @return {Buffer } wavFileData - the original Buffer, with a correct header
51
+ * @return {Buffer } the original Buffer, with the correct header
18
52
*/
19
53
repairWavHeader = ( wavFileData : Buffer ) : Buffer => {
20
54
const totalBytes = wavFileData . length ;
0 commit comments