Skip to content

Commit

Permalink
Implementation of ttsChunks audio playback
Browse files Browse the repository at this point in the history
Added AudioPlayer class for playing audio chunks
sequence from request. Updated TTS popup window
logic.
  • Loading branch information
AKalinich-Luxoft committed May 14, 2018
1 parent 5571530 commit 469d89a
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 15 deletions.
11 changes: 8 additions & 3 deletions app/model/sdl/Abstract/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1092,12 +1092,17 @@ SDL.SDLModel = Em.Object.extend({
*/
onPrompt: function(ttsChunks, appID) {

var message = '';
var message = '', files = '';
if (ttsChunks) {
for (var i = 0; i < ttsChunks.length; i++) {
message += ttsChunks[i].text + '\n';
if ('TEXT' == ttsChunks[i].type) {
message += ttsChunks[i].text + '\n';
}
if ('FILE' == ttsChunks[i].type) {
files += ttsChunks[i].text + '\n';
}
}
SDL.TTSPopUp.ActivateTTS(message, appID);
SDL.TTSPopUp.ActivateTTS(message, files, appID);
}
},

Expand Down
101 changes: 101 additions & 0 deletions app/util/AudioPlayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2013, Ford Motor Company All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: ·
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer. · Redistributions in binary
* form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided
* with the distribution. · Neither the name of the Ford Motor Company nor the
* names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @name SDL.AudioPlayer
* @desc HTML5 Audio player
* @category utils
* @filesource app/util/AudioPlayer.js
* @version 1.0
*/

SDL.AudioPlayer = Em.Object.extend({

/**
* Array of audio files to play
*/
playlist: [],

/**
* Audio player component
*/
audio: new Audio(),

/**
* Add audio file to the playlist
*/
addFile: function(path) {
if (path != '') {
this.playlist.push(path);
}
},

/**
* Clear audio files playlist
*/
clearFiles: function() {
this.set('playlist', []);
},

/**
* Start playlist playing
*/
playFiles: function() {
if (this.playlist.length == 0) {
Em.Logger.log('Playing has finished');
this.stopPlaying();
return;
}
if (!this.audio.paused) {
Em.Logger.log('Audio is already playing');
return;
}

var path = this.playlist[0];
this.playlist.shift();

Em.Logger.log('Playing: ' + path);
var self = this;
this.audio.onended = function() {
self.audio.src = '';
self.playFiles();
}
this.audio.src = path;
this.audio.play();
},

/**
* Stop playlist playing
*/
stopPlaying: function() {
this.audio.onended = null;
if (!this.audio.paused) {
Em.Logger.log('Playing has stopped');
this.audio.pause();
this.audio.src = '';
}
}

});
28 changes: 17 additions & 11 deletions app/view/home/controlButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,15 @@ SDL.ControlButtons = Em.ContainerView.create({
var str = '';
if (SDL.SDLController.model &&
SDL.SDLController.model.globalProperties.helpPrompt) {
var i = 0;

for (i = 0; i <
SDL.SDLController.model.globalProperties.helpPrompt.length; i++) {
str += SDL.SDLController.model.globalProperties.helpPrompt[i].text +
' ';
var items = SDL.SDLController.model.globalProperties.helpPrompt;
for (var i = 0; i < items.length; ++i) {
var item = items[i];
if ('FILE' == item.type) {
str += '[Audio File] ';
} else {
str += item.text + ' ';
}
}
}
return str;
Expand All @@ -366,12 +369,15 @@ SDL.ControlButtons = Em.ContainerView.create({
var str = '';
if (SDL.SDLController.model &&
SDL.SDLController.model.globalProperties.timeoutPrompt) {
var i = 0;
for (i = 0; i <
SDL.SDLController.model.globalProperties.timeoutPrompt.length; i++) {
str +=
SDL.SDLController.model.globalProperties.timeoutPrompt[i].text +
' ';

var items = SDL.SDLController.model.globalProperties.timeoutPrompt;
for (var i = 0; i < items.length; ++i) {
var item = items[i];
if ('FILE' == item.type) {
str += '[Audio File] ';
} else {
str += item.text + ' ';
}
}
}

Expand Down
15 changes: 14 additions & 1 deletion app/view/sdl/TTSPopUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ SDL.TTSPopUp = Em.ContainerView.create(
timer: null,
appID: null,
timerSeconds: 5,
player: SDL.AudioPlayer.create(),
popUp: Ember.TextArea.extend(
{
elementId: 'popUp',
Expand Down Expand Up @@ -101,20 +102,30 @@ SDL.TTSPopUp = Em.ContainerView.create(
this.set('timerSeconds', 10);
FFW.TTS.OnResetTimeout(this.appID, 'TTS.Speak');
},
ActivateTTS: function(msg, appID) {
ActivateTTS: function(msg, files, appID) {
if (this.timer || this.active) {
this.DeactivateTTS();
}
var self = this;
this.set('appID', appID);
this.set('content', msg);

if (files != '') {
var files_to_play = files.split('\n');
for (var i = 0; i < files_to_play.length; ++i) {
this.player.addFile(files_to_play[i]);
}
this.player.playFiles();
}

this.set('active', true);
clearInterval(this.timer);
this.timer = setInterval(
function() {
self.set('timerSeconds', self.timerSeconds - 1);
}, 1000
); // timeout for TTS popUp timer interval in milliseconds

FFW.TTS.Started();
},
timerHandler: function() {
Expand All @@ -125,6 +136,8 @@ SDL.TTSPopUp = Em.ContainerView.create(
DeactivateTTS: function() {
clearInterval(this.timer);
this.timer = null;
this.player.stopPlaying();
this.player.clearFiles();
this.set('active', false);
this.appID = null;
this.set('timerSeconds', 5);
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@

<!-- utils -->
<script type="text/javascript" src="app/util/Audio.js"></script>
<script type="text/javascript" src="app/util/AudioPlayer.js"></script>
<script type="text/javascript" src="app/util/StreamAudio.js"></script>

<!-- Models -->
Expand Down

0 comments on commit 469d89a

Please # to comment.