Skip to content

Commit 45da809

Browse files
committed
enabled synchronizing of timing stream to streamed audio start
1 parent 837889d commit 45da809

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### v0.29.1
4+
* Added setStartTime() method to TimingStream to facilitate syncing wit when files loaded via URL actually begin playing
5+
* Updated recognizeFile() to automatically sync timing stream to playback
6+
37
### v0.29.0
48
* BREAKING: recognizeFile()'s data option renamed to file
59
* File option may be a string URL. This enables streaming transcription/playback and mobile Safari support.

dist/watson-speech.js

Lines changed: 46 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

speech-to-text/recognize-file.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,21 @@ module.exports = function recognizeFile(options) { // eslint-disable-line comple
134134
}
135135

136136
if (options.play) {
137-
// todo: if realtime is set, update the realtime stream's start time to match
138137
// when file playback actually begins
139138
// (mostly important for downloaded files)
140139
FilePlayer.playFile(options.file).then(function(player) {
141140
recognizeStream.on('stop', player.stop.bind(player));
142141
recognizeStream.on('error', player.stop.bind(player));
142+
143+
// for files loaded via URL, restet the start time of the timing stream to when it begins playing
144+
if (timingStream && typeof options.file === 'string') {
145+
// eslint-disable-next-line func-style
146+
var fn = function() {
147+
timingStream.setStartTime(); // defaults to Date.now()
148+
player.audio.removeEventListener('playing', fn);
149+
};
150+
player.audio.addEventListener('playing', fn);
151+
}
143152
}).catch(function(err) {
144153

145154
// Node.js automatically unpipes any source stream(s) when an error is emitted (on the assumption that the previous stream's output caused the error.)

speech-to-text/timing-stream.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ TimingStream.prototype._transform = function(msg, encoding, next) {
6868
}
6969
};
7070

71+
72+
7173
/**
7274
* Grabs the appropriate timestamp from the given message, depending on options.emitAt and the type of message
7375
*
@@ -107,6 +109,17 @@ TimingStream.prototype.getDelayMs = function(msg) {
107109
return Math.max(0, delayMs); // never return a negative number
108110
};
109111

112+
/**
113+
* Overrides the start time, adjusting the delay applied to all pending results.
114+
*
115+
* Stream may emit up to 1 more result based on the older time after this is called.
116+
*
117+
* @param {Number} [time=Date.now()] Start time in Miliseconds since epoch
118+
*/
119+
TimingStream.prototype.setStartTime = function(time) {
120+
this.startTime = time || Date.now();
121+
};
122+
110123
TimingStream.prototype.promise = require('./to-promise');
111124

112125
// when stop is called, immediately stop emitting results

0 commit comments

Comments
 (0)