Skip to content
This repository has been archived by the owner on Feb 23, 2021. It is now read-only.

Commit

Permalink
Merge f1081f3 into 370a926
Browse files Browse the repository at this point in the history
  • Loading branch information
tamarahills committed Dec 20, 2018
2 parents 370a926 + f1081f3 commit b4a7cd3
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 105 deletions.
17 changes: 12 additions & 5 deletions command/CommandController.js
Expand Up @@ -215,7 +215,8 @@ router.post('/articleservice', VerifyToken, async function(req, res) {
let audioMetadata = await buildPocketAudio(
introFile,
articleFile,
req.body.article_id
req.body.article_id,
req.body.locale
);
// Add the correct voice:
audioMetadata['voice'] = voice.main;
Expand Down Expand Up @@ -902,16 +903,22 @@ async function buildSummaryAudioFromUrl(url, item_id) {
async function buildAudioFromText(
textString,
voiceType = process.env.POLLY_VOICE || 'Salli',
article_id
article_id,
locale
) {
const cleanText = texttools.cleanText(textString);
const chunkText = texttools.chunkText(cleanText);
logger.debug('chunkText is: ', chunkText.length, chunkText);
return polly_tts.getSpeechSynthUrl(chunkText, voiceType, article_id);
return polly_tts.getSpeechSynthUrl(chunkText, voiceType, article_id, locale);
}

async function buildPocketAudio(introFile, articleFile, article_id) {
return polly_tts.processPocketAudio(introFile, articleFile, article_id);
async function buildPocketAudio(introFile, articleFile, article_id, locale) {
return polly_tts.processPocketAudio(
introFile,
articleFile,
article_id,
locale
);
}

async function buildPocketResponseFromMetadata(mmd, version) {
Expand Down
186 changes: 93 additions & 93 deletions command/polly_tts.js
Expand Up @@ -12,16 +12,16 @@ const utils = new Utils();

var polly_tts = {
/* Sends a chunk of text to be synthesized by Polly.
* text: Text to be synthesized (with ssml tags)
* filenameIndex: an index denoting this chunk of
* text's array index (for later stitching)
* audio_file: the name of the root of the file to
* attach the index to.
* voiceType:
*
* resolves: The name of the new synthesized local file.
* reject: error from Polly.
*/
* text: Text to be synthesized (with ssml tags)
* filenameIndex: an index denoting this chunk of
* text's array index (for later stitching)
* audio_file: the name of the root of the file to
* attach the index to.
* voiceType:
*
* resolves: The name of the new synthesized local file.
* reject: error from Polly.
*/
getPollyChunk: function(text, filenameIndex, audio_file, voiceType) {
return new Promise(function(resolve, reject) {
let rate = process.env.PROSODY_RATE || 'medium';
Expand Down Expand Up @@ -73,11 +73,11 @@ var polly_tts = {
},

/* Stitches together an array of local audio
* files using ffmpeg.
*
* resolves: The name of the new stitches file.
* reject: error from ffmpeg
*/
* files using ffmpeg.
*
* resolves: The name of the new stitches file.
* reject: error from ffmpeg
*/
concatAudio: function(parts, audio_file) {
return new Promise((resolve, reject) => {
let filename = './' + audio_file + '.mp3';
Expand All @@ -101,11 +101,11 @@ var polly_tts = {
},

/* This is special handling for the Pocket audio file.
* Synthesizes a speech file for an array of text
* chunks.
*
* resolves: The name of the new local audio file
*/
* Synthesizes a speech file for an array of text
* chunks.
*
* resolves: The name of the new local audio file
*/
synthesizeSpeechFile(parts, voiceType) {
return new Promise(resolve => {
let audio_file = uuidgen.generate();
Expand All @@ -126,17 +126,17 @@ var polly_tts = {
},

/* This is special handling for the Pocket audio file.
* It stitches together the intro and outro for the clients.
*
* concat intro + body
* upload stitched file
* resolve stitched file
* ... then the rest can be done after the promise resolves
* fire xcode request to sqs
* handle db writes
* upload intro & body separately for Alexa.
*/
processPocketAudio(introFile, articleFile, article_id) {
* It stitches together the intro and outro for the clients.
*
* concat intro + body
* upload stitched file
* resolve stitched file
* ... then the rest can be done after the promise resolves
* fire xcode request to sqs
* handle db writes
* upload intro & body separately for Alexa.
*/
processPocketAudio(introFile, articleFile, article_id, locale) {
return new Promise(resolve => {
polly_tts
.concatAudio([introFile, articleFile], uuidgen.generate())
Expand All @@ -158,19 +158,19 @@ var polly_tts = {
}
});
// Send the stitched file off for transcoding.
xcodeQueue.add(audio_file, article_id);
xcodeQueue.add(audio_file, article_id, locale);
});
});
},

/*
* This uploads a synthesized file to the
* configured S3 bucket in the environment
* variable POLLY_S3_BUCKET.
*
* resolves: URL of the file
* reject: error
*/
/*
* This uploads a synthesized file to the
* configured S3 bucket in the environment
* variable POLLY_S3_BUCKET.
*
* resolves: URL of the file
* reject: error
*/
uploadFile: function(newAudioFile) {
return new Promise((resolve, reject) => {
var s3 = new AWS.S3({
Expand Down Expand Up @@ -206,21 +206,21 @@ var polly_tts = {
});
},

/*
* This synthesizes the chunked up file
* and returns a URL of the mp3. Clients
* of this function are the Scout skill,
* mobile app. Not used by the pocket app.
*
* It also queues the final product for
* transcoding to opus format in the S3
* bucket at a later date. All temp files
* used to synthesize the file are deleted
*
* resolves: URL of the file
* reject: error
*/
getSpeechSynthUrl: function(parts, voiceType, item_id) {
/*
* This synthesizes the chunked up file
* and returns a URL of the mp3. Clients
* of this function are the Scout skill,
* mobile app. Not used by the pocket app.
*
* It also queues the final product for
* transcoding to opus format in the S3
* bucket at a later date. All temp files
* used to synthesize the file are deleted
*
* resolves: URL of the file
* reject: error
*/
getSpeechSynthUrl: function(parts, voiceType) {
return new Promise((resolve, reject) => {
let audio_file = uuidgen.generate();
let promArray = [];
Expand Down Expand Up @@ -272,26 +272,26 @@ var polly_tts = {
}
});
//Put the file in queue for transcoding.
xcodeQueue.add(audio_file + '.mp3', item_id);
// xcodeQueue.add(audio_file + '.mp3', item_id, 'none');
}
});
});
});
},

/*
* Takes a local audio file and:
* 1. Uploads to the S3 bucket
* 2. Queues it for transcoding to opus
* 3. Deletes the local file.
* Currently used by the Pocket app as a
* special handling for the case of stitching
* the intro/main article instead of returning
* separate parts.
*
* resolves: URL of the audio file in S3 Bucket.
* reject: error
*/
/*
* Takes a local audio file and:
* 1. Uploads to the S3 bucket
* 2. Queues it for transcoding to opus
* 3. Deletes the local file.
* Currently used by the Pocket app as a
* special handling for the case of stitching
* the intro/main article instead of returning
* separate parts.
*
* resolves: URL of the audio file in S3 Bucket.
* reject: error
*/
postProcessPart: function(audio_file) {
return new Promise(resolve => {
polly_tts.uploadFile(audio_file).then(function(audio_url) {
Expand All @@ -309,15 +309,15 @@ var polly_tts = {
});
},

/*
* Takes a local mp3 file:
* 1. Changes file.mp3 to file*.*
* 2. Searches locally for file*.* files
* 3. Iterates through those files and
* deletes them
* Should only be called after everything has
* been uploaded.
*/
/*
* Takes a local mp3 file:
* 1. Changes file.mp3 to file*.*
* 2. Searches locally for file*.* files
* 3. Iterates through those files and
* deletes them
* Should only be called after everything has
* been uploaded.
*/
deleteLocalFiles: function(rootFile, callback) {
logger.debug('Entering deleteLocalFiles: ' + rootFile);
let files = glob.sync(rootFile.replace('.mp3', '*.*'));
Expand All @@ -338,18 +338,18 @@ var polly_tts = {
});
},

/*
* Takes an audio_url in S3:
* Gets the size, sample rate, duration, format, voice
* for the given file
*
* 1. Convert URL to local filename
* 2. Get the filesize.
* 3. Get the audio attributes using ffmpeg
*
* resolve: metadata object
* reject: err
*/
/*
* Takes an audio_url in S3:
* Gets the size, sample rate, duration, format, voice
* for the given file
*
* 1. Convert URL to local filename
* 2. Get the filesize.
* 3. Get the audio attributes using ffmpeg
*
* resolve: metadata object
* reject: err
*/
getFileMetadata: function(audio_url) {
let audio_file = './' + utils.urlToFile(audio_url);
return new Promise((resolve, reject) => {
Expand All @@ -376,9 +376,9 @@ var polly_tts = {
},

/*
* Given an audio_url in an S3 bucket, returns the
* size of the file.
*/
* Given an audio_url in an S3 bucket, returns the
* size of the file.
*/
getFileSizeFromUrl: async function(audio_url) {
return new Promise(resolve => {
logger.debug('getFileSizeFromUrl');
Expand Down
13 changes: 8 additions & 5 deletions command/xcodeQueue.js
Expand Up @@ -22,33 +22,36 @@ const xcodeQueue = {
return !!process.env.SQS_QUEUE;
},

add: function(file, item_id) {
add: function(file, item_id, locale) {
if (this.useXcode()) {
logger.debug('XCODE: filename: ' + file);
this.addTranscode(
file,
item_id,
constants.strings.CODEC_OPUS_CAF,
constants.strings.CONTAINER_CAF
constants.strings.CONTAINER_CAF,
locale
);
this.addTranscode(
file,
item_id,
constants.strings.CODEC_OPUS_MKV,
constants.strings.CONTAINER_MKV
constants.strings.CONTAINER_MKV,
locale
);
} else {
logger.debug('No SQS queue defined, skipping XCode message.');
}
},

addTranscode: function(file, item_id, codec, container) {
addTranscode: function(file, item_id, codec, container, locale) {
var jsonBody = {
filename: file,
targetCodec: codec,
bitrate: '24k',
container: container,
item_id: item_id
item_id: item_id,
locale: locale
};

var params = {
Expand Down
4 changes: 2 additions & 2 deletions data/database.js
Expand Up @@ -266,7 +266,7 @@ class Database {
additionalOpusInfo,
{
uuid: uuidgen.generate(),
url: mp3FileUrl.replace('.mp3', '.opus-caf')
url: mp3FileUrl.replace('.mp3', '.caf')
}
);
const opusAudioFile = new AudioFiles(opusCafFileInfo);
Expand All @@ -280,7 +280,7 @@ class Database {
additionalOpusInfo,
{
uuid: uuidgen.generate(),
url: mp3FileUrl.replace('.mp3', '.opus-mkv')
url: mp3FileUrl.replace('.mp3', '.mkv')
}
);
const opusMkvAudioFile = new AudioFiles(opusMkvFileInfo);
Expand Down

0 comments on commit b4a7cd3

Please sign in to comment.