Skip to content

Commit a4995f9

Browse files
committed
chore: remove buffer-based client
1 parent 2005587 commit a4995f9

File tree

6 files changed

+38
-104
lines changed

6 files changed

+38
-104
lines changed

README.md

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ npm install --save bingspeech-api-client
1717

1818
See example below on how to require and use for Speech to text (STT) and text to speech (TTS).
1919

20-
2120
## Examples
2221

2322
Following example code is assuming you are using [typescript](https://www.typescriptlang.org/). If you are, skip this section and go straight to the examples. But if you are using node ES6 and want to use the example code read on.
@@ -26,17 +25,13 @@ At present node does not support `import`. As mentioned on [MDN](https://develop
2625

2726
>Note: This feature[`import`] is only beginning to be implemented in browsers natively at this time. It is implemented in many transpilers, such as the Traceur Compiler, Babel, Rollup or Webpack.
2827
29-
3028
To get the example code working change the first line to:
3129

3230
```js
3331
const { BingSpeechClient, VoiceRecognitionResponse } = require('bingspeech-api-client');
3432
```
3533

36-
37-
### Usage with streams
38-
39-
#### STT usage example (recognize)
34+
### STT usage example (recognize)
4035

4136
```javascript
4237
import { BingSpeechClient, VoiceRecognitionResponse } from 'bingspeech-api-client';
@@ -50,7 +45,7 @@ let client = new BingSpeechClient(subscriptionKey);
5045
client.recognizeStream(audioStream).then(response => console.log(response.results[0].name));
5146
```
5247

53-
#### TTS usage example (synthesize)
48+
### TTS usage example (synthesize)
5449

5550
```javascript
5651
import { BingSpeechClient, VoiceVoiceSynthesisResponse } from 'bingspeech-api-client';
@@ -61,33 +56,3 @@ let subscriptionKey = 'your_private_subscription_key';
6156
let client = new BingSpeechClient(subscriptionKey);
6257
client.synthesizeStream('I have a dream').then(audioStream => /* ... */);
6358
```
64-
65-
### Usage with buffers (deprecated, will be removed in 2.x)
66-
67-
#### STT usage example (recognize)
68-
69-
```javascript
70-
import { BingSpeechClient, VoiceRecognitionResponse } from 'bingspeech-api-client';
71-
72-
// audio input in a Buffer
73-
let wav = fs.readFileSync('myaudiofile.wav');
74-
75-
// Bing Speech Key (https://www.microsoft.com/cognitive-services/en-us/subscriptions)
76-
let subscriptionKey = 'your_private_subscription_key';
77-
78-
let client = new BingSpeechClient(subscriptionKey);
79-
client.recognize(wav).then(response => console.log(response.results[0].name));
80-
```
81-
82-
#### TTS usage example (synthesize)
83-
84-
```javascript
85-
import { BingSpeechClient, VoiceVoiceSynthesisResponse } from 'bingspeech-api-client';
86-
87-
// Bing Speech Key (https://www.microsoft.com/cognitive-services/en-us/subscriptions)
88-
let subscriptionKey = 'your_private_subscription_key';
89-
90-
let client = new BingSpeechClient(subscriptionKey);
91-
client.synthesize('I have a dream').then(response => { /* audio is a Buffer in response.wave */ });
92-
```
93-

package-lock.json

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client.spec.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { expect } from 'chai';
22

3-
import { BingSpeechClient, VoiceRecognitionResponse, VoiceSynthesisResponse } from './';
3+
import { BingSpeechClient, VoiceRecognitionResponse } from './';
44

55
import * as nock from 'nock';
66

77
describe('Bing Speech API client', () => {
8+
beforeEach(() => {
9+
nock('https://api.cognitive.microsoft.com')
10+
.post('/sts/v1.0/issueToken')
11+
.reply(200, 'FAKETOKEN');
12+
});
13+
814
it('should recognize a voice', () => {
915
const mockResponse: VoiceRecognitionResponse = {
1016
version: '3.0',
@@ -31,40 +37,32 @@ describe('Bing Speech API client', () => {
3137
]
3238
};
3339

34-
nock('https://api.cognitive.microsoft.com')
35-
.post('/sts/v1.0/issueToken')
36-
.reply(200, 'FAKETOKEN');
37-
3840
nock('https://speech.platform.bing.com')
3941
.post('/recognize')
4042
.query(true)
4143
.reply(200, mockResponse);
4244

43-
let client = new BingSpeechClient('fakeSubscriptionId');
45+
const client = new BingSpeechClient('fakeSubscriptionId');
4446

45-
let wave = new Buffer('');
46-
return client.recognize(wave)
47+
const stream: NodeJS.ReadWriteStream = null;
48+
return client.recognizeStream(stream)
4749
.then((response: VoiceRecognitionResponse) => {
4850
expect(response).to.eql(mockResponse);
4951
});
5052
});
5153

5254
it('should synthesize a voice', () => {
53-
const mockResponse = 'this is a wav';
54-
55-
nock('https://api.cognitive.microsoft.com')
56-
.post('/sts/v1.0/issueToken')
57-
.reply(200, 'FAKETOKEN');
55+
const client = new BingSpeechClient('fakeSubscriptionId');
5856

5957
nock('https://speech.platform.bing.com')
6058
.post('/synthesize')
61-
.reply(200, mockResponse);
62-
63-
let client = new BingSpeechClient('fakeSubscriptionId');
59+
.reply(200, '');
6460

65-
return client.synthesize('This is a fake test')
66-
.then((response: VoiceSynthesisResponse) => {
67-
expect(response.wave.toString()).to.eq(mockResponse);
61+
// TODO fix & improve this test.
62+
// It passes but it is weak and, what is worse, it shows an ECONNRESET as if some connection wasn't properly intercepted by nock
63+
return client.synthesizeStream('This is a fake test')
64+
.then((stream: NodeJS.ReadableStream) => {
65+
expect(stream).to.be.an('object');
6866
});
6967
});
7068
});

src/client.ts

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import * as fs from 'fs';
2-
import * as util from 'util';
31
import * as uuid from 'uuid';
42
import * as needle from 'needle';
5-
import * as stream from 'stream';
63
import * as querystring from 'querystring';
74

8-
import { VoiceRecognitionResponse, VoiceSynthesisResponse } from './models';
5+
import { VoiceRecognitionResponse } from './models';
96

107
const debug = require('debug')('bingspeechclient');
118

@@ -74,19 +71,9 @@ export class BingSpeechClient {
7471
this.subscriptionKey = subscriptionKey;
7572
}
7673

77-
/**
78-
* @deprecated Use the recognizeStream function instead. Will be removed in 2.x
79-
*/
80-
recognize(wave: Buffer, locale: string = 'en-us'): Promise<VoiceRecognitionResponse> {
81-
var bufferStream = new stream.PassThrough();
82-
bufferStream.end(wave);
83-
84-
return this.recognizeStream(bufferStream, locale);
85-
}
86-
8774
recognizeStream(input: NodeJS.ReadWriteStream, locale: string = 'en-us'): Promise<VoiceRecognitionResponse> {
8875
// see also https://nowayshecodes.com/2016/02/12/speech-to-text-with-project-oxford-using-node-js/
89-
// TODO make locale and content-type configurable
76+
// TODO make content-type configurable
9077
return this.issueToken()
9178
.then((token) => {
9279
// Access token expires every 10 minutes. Renew it every 9 minutes only.
@@ -133,30 +120,6 @@ export class BingSpeechClient {
133120
});
134121
}
135122

136-
/**
137-
* * @deprecated Use the synthesizeStream function instead. Will be removed in 2.x
138-
*/
139-
synthesize(text: string, locale: string = 'en-us', gender: string = 'female'): Promise<VoiceSynthesisResponse> {
140-
return this.synthesizeStream(text, locale, gender)
141-
.then(waveStream => {
142-
return new Promise<VoiceSynthesisResponse>((resolve, reject) => {
143-
let buffers: any[] = [];
144-
waveStream.on('data', (buffer: any) => buffers.push(buffer));
145-
waveStream.on('end', () => {
146-
let wave = Buffer.concat(buffers);
147-
let response: VoiceSynthesisResponse = {
148-
wave: wave
149-
};
150-
resolve(response);
151-
});
152-
waveStream.on('error', (err: Error) => reject(err));
153-
});
154-
})
155-
.catch((err: Error) => {
156-
throw new Error(`Voice synthesis failed miserably: ${err.message}`);
157-
});
158-
}
159-
160123
synthesizeStream(text: string, locale: string = 'en-us', gender: string = 'female', outputFormat:string = this.DEFAULT_AUDIO_OUTPUT_FORMAT): Promise<NodeJS.ReadableStream> {
161124
// see also https://github.com/Microsoft/Cognitive-Speech-TTS/blob/master/Samples-Http/NodeJS/TTSService.js
162125
return this.issueToken()

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { BingSpeechClient } from './client';
2-
export { VoiceRecognitionResponse, VoiceSynthesisResponse } from './models';
2+
export { VoiceRecognitionResponse } from './models';
33

src/models.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,4 @@ export interface VoiceRecognitionResponse {
2727
LOWCONF?: string;
2828
}
2929
}[];
30-
};
31-
32-
/**
33-
* @deprecated Use streaming mode instead. Will be removed in 2.x
34-
*/
35-
export interface VoiceSynthesisResponse {
36-
wave: Buffer;
3730
}

0 commit comments

Comments
 (0)