Skip to content

Commit 42b0913

Browse files
authored
feat(text-to-speech): add method to repair wav header for a stream (#981)
* the method takes a stream of wav data, reads it into memory, repairs the header, then returns a Promise that resolves with the repaired data as a buffer
1 parent d8fc302 commit 42b0913

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,15 @@ const params = {
722722

723723
// Synthesize speech, correct the wav header, then save to disk
724724
// (wav header requires a file length, but this is unknown until after the header is already generated and sent)
725+
// note that `repairWavHeaderStream` will read the whole stream into memory in order to process it.
726+
// the method returns a Promise that resolves with the repaired buffer
725727
textToSpeech
726728
.synthesize(params)
727729
.then(response => {
728730
const audio = response.result;
729-
textToSpeech.repairWavHeader(audio);
731+
return textToSpeech.repairWavHeaderStream(audio);
732+
})
733+
.then(repairedFile => {
730734
fs.writeFileSync('audio.wav', audio);
731735
console.log('audio.wav written with a corrected wav header');
732736
})

text-to-speech/v1.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import extend = require('extend');
22
import { OutgoingHttpHeaders } from 'http';
33
import { UserOptions } from 'ibm-cloud-sdk-core';
4+
import isStream = require('isstream');
5+
import { Readable } from 'stream';
46
import { getSdkHeaders } from '../lib/common';
57
import SynthesizeStream = require('../lib/synthesize-stream');
68
import 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

Comments
 (0)