Browse Source

tts client: fix some bugs, add default sound on error, join sound

master
vampirefrog 1 year ago
parent
commit
cfcadfd7e2
  1. 169
      static/tts.js

169
static/tts.js

@ -34,6 +34,59 @@ function getOffsetTop(elem) { @@ -34,6 +34,59 @@ function getOffsetTop(elem) {
return offsetTop;
}
function highlightDiv(id) {
var div = document.getElementById(id);
if(!div) return;
div.className += ' speaking';
}
function unhighlightDiv(id) {
var div = document.getElementById(id);
if(!div) return;
div.className = div.className.replace(/\s*speaking\s*/g, '');
setTimeout(function() {
div.className = div.className.replace('zoomIn', '') + ' zoomOut';
setTimeout(function() {
try {
chatLog.removeChild(div);
} catch(e) {
// ignore
}
}, 500);
}, 8000);
}
function removeDiv(id) {
var div = document.getElementById(id);
if(!div) return;
div.parentNode.removeChild(div);
}
function playSound(s, next) {
console.log('playSound', s);
s.on('ready', function(event) {
s.play();
});
s.on('ended', function(event) {
if(next) next.call(this);
});
s.on('error', function(event) {
console.log('error playing sound', event);
if(next) next.call(this);
});
if(s.readyState == 'ready') {
s.play();
} else if(s.readyState == 'error' || s.readyState == 'ended') {
if(next) next.call(this);
}
}
function endSound(item) {
unhighlightDiv(item.id);
queue.remove(item.id);
}
function appendChat(chat) {
var div = document.createElement('div');
var className = 'chatMessage animated zoomIn';
@ -103,50 +156,8 @@ function appendChat(chat) { @@ -103,50 +156,8 @@ function appendChat(chat) {
.toLowerCase()
;
var highlightDiv = function(id) {
var div = document.getElementById(id);
if(!div) return;
div.className += ' speaking';
};
var unhighlightDiv = function(id) {
var div = document.getElementById(chat.id);
if(!div) return;
div.className = div.className.replace(/\s*speaking\s*/g, '');
setTimeout(function() {
div.className = div.className.replace('zoomIn', '') + ' zoomOut';
setTimeout(function() {
try {
chatLog.removeChild(div);
} catch(e) {
// ignore
}
}, 500);
}, 8000);
};
var playSound = function(s, next) {
s.on('ready', function(event) {
s.play();
});
s.on('ended', function(event) {
if(next) next.call(this);
});
s.on('error', function(event) {
if(next) next.call(this);
});
if(s.readyState == 'ready') {
s.play();
} else if(s.readyState == 'error' || s.readyState == 'ended') {
if(next) next.call(this);
}
};
var endSound = function(item) {
unhighlightDiv(item.id);
queue.remove(item);
};
var defaultSound = new PcmSound(soundManager);
defaultSound.load(config.sounds.default);
if(speak) {
var speechSound = new PcmSound(soundManager);
@ -163,9 +174,15 @@ function appendChat(chat) { @@ -163,9 +174,15 @@ function appendChat(chat) {
speechSound: speechSound,
sound: sound
},
function(item) {
(item) => {
highlightDiv(item.id);
playSound(speechSound, function() { playSound(sound, function() { endSound(item); }); });
playSound(item.data.speechSound, () => {
playSound(item.data.sound, () => {
playSound(defaultSound, () => {
endSound(item);
});
});
});
}
);
} else {
@ -174,24 +191,43 @@ function appendChat(chat) { @@ -174,24 +191,43 @@ function appendChat(chat) {
{
speechSound: speechSound
},
function(item) {
(item) => {
highlightDiv(item.id);
playSound(speechSound, function() { endSound(item); });
playSound(item.data.speechSound, () => {
playSound(defaultSound, () => {
endSound(item);
});
});
}
);
}
} else {
} else if(chat.sound) {
var sound = new PcmSound(soundManager);
sound.load(chat.sound || config.sounds.default);
sound.load(chat.sound);
var item = queue.append(
chat.id,
{
speechSound: speechSound
sound: sound
},
function(item) {
(item) => {
highlightDiv(item.id);
playSound(item.data.sound, () => {
playSound(defaultSound, () => {
endSound(item);
});
});
}
);
} else {
var item = queue.append(
chat.id,
{},
(item) => {
highlightDiv(item.id);
playSound(sound, function() { endSound(item); });
playSound(defaultSound, () => {
endSound(item);
});
}
);
}
@ -296,7 +332,7 @@ function handleMessage(data) { @@ -296,7 +332,7 @@ function handleMessage(data) {
break;
case 'gift':
if(config.donations && config.donations[data.platform] && config.donations[data.platform][data.giftType]) {
var giftConfig = config.donations[data.platform][data.giftType];
var giftConfig = config.donations[data.platform][data.giftType] || config.donations[data.platform].default;
var string = giftConfig.string;
if(data.amount > 1) {
@ -327,7 +363,7 @@ function handleMessage(data) { @@ -327,7 +363,7 @@ function handleMessage(data) {
avatar: data.avatar,
displayName: data.displayName,
group: group,
sound: randomItem(giftConfig.sound),
sound: randomItem(giftConfig.sound || config.sounds.donation || config.sounds.default),
platform: data.platform,
username: data.username,
speak: data.message,
@ -362,7 +398,7 @@ function handleMessage(data) { @@ -362,7 +398,7 @@ function handleMessage(data) {
group: group,
platform: data.platform,
username: data.username,
sound: config.sounds.default,
sound: config.sounds.donation || config.sounds.default,
speak: data.message,
image: images,
content: content
@ -371,7 +407,15 @@ function handleMessage(data) { @@ -371,7 +407,15 @@ function handleMessage(data) {
break;
case 'delete':
for(var i in data.ids) {
queue.remove(data.ids[i]);
let item = queue.getItemById(data.ids[i]);
if(item && item.data) {
if(item.data.sound)
item.data.sound.stop();
if(item.data.speechSound)
item.data.speechSound.stop();
queue.remove(data.ids[i]);
}
removeDiv(data.ids[i]);
}
console.log('%cDelete%c '+data.ids.join(', '), 'color: red; font-weight: bold', 'color: inherit; font-weight: normal');
break;
@ -417,6 +461,17 @@ function handleMessage(data) { @@ -417,6 +461,17 @@ function handleMessage(data) {
]
});
break;
case 'join':
appendChat({
id: data.id,
avatar: data.avatar,
displayName: data.displayName,
sound: config.sounds.join,
content: [
'joined'
]
});
break;
case 'moderator':
appendChat({
id: data.id,

Loading…
Cancel
Save