11import extend = require( 'extend' ) ;
22import { OutgoingHttpHeaders } from 'http' ;
33import { UserOptions } from 'ibm-cloud-sdk-core' ;
4+ import isStream = require( 'isstream' ) ;
5+ import { Readable } from 'stream' ;
46import { getSdkHeaders } from '../lib/common' ;
57import SynthesizeStream = require( '../lib/synthesize-stream' ) ;
68import GeneratedTextToSpeechV1 = require( './v1-generated' ) ;
@@ -10,11 +12,43 @@ class TextToSpeechV1 extends GeneratedTextToSpeechV1 {
1012 super ( options ) ;
1113 }
1214
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+
1347 /**
1448 * Repair the WAV header of an audio/wav file.
1549 *
1650 * @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
1852 */
1953 repairWavHeader = ( wavFileData : Buffer ) : Buffer => {
2054 const totalBytes = wavFileData . length ;
0 commit comments