forked from ibm-early-programs/watson-tv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
143 lines (125 loc) · 3.94 KB
/
scripts.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var tv = {
method: {
post: 'transcribe',
get: 'transcription'
},
select: function(method) {
'use strict';
if (method === 'transcribe') {
tv.method = {
post: 'transcribe',
get: 'transcription'
};
$('.tv-transcription-btn').removeClass('tv-disabled-btn');
$('.tv-translation-btn').addClass('tv-disabled-btn');
} else {
tv.method = {
post: 'translate',
get: 'translation'
};
$('.tv-transcription-btn').addClass('tv-disabled-btn');
$('.tv-translation-btn').removeClass('tv-disabled-btn');
}
},
sendUrl: function() {
'use strict';
var url = $('#tv-url').val();
var video = $('#tv-video');
var stack = [];
var processing = false;
//$.post('https://residency.mybluemix.net/' + tv.method.post, {url: url}, function() {
$.post('/' + tv.method.post, {url: url}, function() {
'use strict';
video.attr('src', url);
video[0].load();
var setText = function(rText, duration, deferred) {
var split = rText.split(' ');
var count = 0;
var delay;
if (duration > 0) {
delay = duration / split.length * 1000;
} else {
delay = 0;
}
console.log('delay: ' + delay);
var interval = setInterval(function() {
console.log(' ' + split[count]);
tv.text += ' ' + split[count];
$('#tv-translation-text').html(tv.text);
$("#tv-translation-text").animate({ scrollTop: $('#tv-translation-text')[0].scrollHeight }, 250);
if (count++ === split.length - 1) {
processing = false;
deferred.resolve();
console.log('resolve');
clearInterval(interval);
}
}, delay);
};
var doTimeout = function(r, deferred) {
var currentTime = video[0].currentTime;
var delay = currentTime - r.start * 1000;
setTimeout(function() {
setText(r.text, r.end - r.start, deferred);
}, delay);
};
function asyncEvent(r) {
console.log('async');
var deferred = $.Deferred();
var currentTime = video[0].currentTime;
if (r.start <= currentTime) {
console.log('set');
setText(r.text, r.end - currentTime - 2, deferred);
} else {
console.log('timeout');
doTimeout(r, deferred);
}
return deferred.promise();
}
var addToStack = function(r) {
console.log('add to stack');
stack.push(r);
};
var d = $.Deferred().resolve();
var nextPromise = function() {
console.log('next promise');
d = d.then(function() {
console.log('then');
var r = stack.shift();
if (r) {
processing = true;
asyncEvent(r).then(nextPromise);
}
});
};
// poll transcription API for latest responses
setInterval(function() {
//$.get('https://residency.mybluemix.net/' + tv.method.get, function(response) {
$.get('/' + tv.method.get, function(response) {
if (response && response.length > 0) {
video[0].play();
if (tv.responses !== response.length) {
for (var i = tv.responses; i < response.length; ++i) {
tv.responses++;
var r = response[i];
console.log(r);
r.end = r.end ? r.end : video[0].duration;
r.text = r.text.charAt(0).toUpperCase() + r.text.slice(1);
r.text = r.text.trim() + '.';
console.log(processing);
if (stack.length === 0 && !processing) {
console.log('start');
addToStack(r);
nextPromise();
} else {
addToStack(r);
}
}
}
}
});
}, 3000);
});
},
responses: 0,
text: ""
};