forked from pilwon/node-neospeech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
89 lines (68 loc) · 2.17 KB
/
Copy pathserver.js
File metadata and controls
89 lines (68 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var stream = require('stream'),
util = require('util'),
http = require('http'),
url = require('url'),
fs = require('fs'),
exec = require('child_process').exec,
speech = new (require('./'))({
host: '127.0.0.1',
port: 7000
});
// turn source buffer intro a readable stream
function BufferStream( source ) {
if ( ! Buffer.isBuffer( source ) ) {
throw( new Error( "Source must be a buffer." ) );
}
stream.Readable.call( this );
this._source = source;
this._offset = 0;
this._length = source.length;
this.on( "end", this._destroy );
}
util.inherits( BufferStream, stream.Readable );
// attemp to avoid memory leaks (not sure if this will do)
BufferStream.prototype._destroy = function() {
this._source = null;
this._offset = null;
this._length = null;
};
// read chunks from source buffer to underlying stream buffer
BufferStream.prototype._read = function( size ) {
// push next chunk onto the internal buffer
if ( this._offset < this._length ) {
this.push( this._source.slice( this._offset, ( this._offset + size ) ) );
this._offset += size;
}
// close readable stream when consumed
if ( this._offset >= this._length ) {
this.push( null );
}
};
// bind HTTP server to port 9000 to directly receive wav file
// e.g. http://127.0.0.1:9000/?speaker=JAMES&text=Text%20to%20be%20spoken
var server = http.createServer(
function handleHttpRequest( request, response ) {
var params = url.parse(request.url, true).query;
speech.requestBuffer({
text: params.text,
speakerId: params.speaker,
voiceFormat: 'WAV',
all: true
}, function (err, result) {
if (err) { return console.error(err); }
console.log('GET for ' + request.connection.remoteAddress + ' at ' + (new Date()).toString());
console.log(params);
response.writeHead(
200,
"OK",
{
"Content-Type": "audio/x-wav",
"Contente-Length": result.buffer.length
}
);
// pipe buffer to HTTP body, it will be closed automatically when finished
new BufferStream(result.buffer).pipe(response);
});
}
);
server.listen( 9000 );