Skip to content

Commit

Permalink
continued audio batch queuing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidMockler committed Aug 9, 2024
1 parent 558db9d commit b082d2e
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 29 deletions.
17 changes: 0 additions & 17 deletions api/src/endpoint/drStory/queueTest.ts

This file was deleted.

57 changes: 57 additions & 0 deletions api/src/endpoint/drStory/sentenceAudio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const DigitalReaderSentenceAudio = require('../../models/drSentenceAudio');
const {API500Error} = require('../../utils/APIError');
const axios = require('axios');


const http = axios.create({
baseURL: "https://abair.ie/api2",
headers: {
"Content-Type": "application/json",
//"Access-Control-Allow-Origin": "*"
}
});

/**
* Creates a new story document on the DB.
* @param {Object} req body: Story object
* @return {Promise}
*/

//function test (req:any) {
async function synthesiseAndStoreSent (req, res) {

function no() {
res.json(undefined);
}
function yes() {
res.json(sentAudioObj);
}

// make sure all necessary body params are present
if (req.body===undefined) return no();

if (req.body.drStoryId===undefined) return no();
if (req.body.voiceCode===undefined) return no();
if (req.body.sentenceId===undefined) return no();

/*const sentAudioObj = DigitalReaderSentenceAudio.find({
drStoryId: req.body.drStoryId,
sentenceId: req.body.sentenceId,
voice: req.body.voiceCode
})*/
const sentAudioObj = await DigitalReaderSentenceAudio.find(req.body);
if (!sentAudioObj) return no();

return yes();

/*return new Promise(
(resolve, reject) => {
setTimeout( () =>
resolve('test resolution!'), 1000
)
// above should be changed to an await of the a call to the synth api
})*/
}

export = synthesiseAndStoreSent;
148 changes: 148 additions & 0 deletions api/src/endpoint/drStory/synthesiseAndStoreSent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
const DigitalReaderSentenceAudio = require('../../models/drSentenceAudio');
const {API500Error} = require('../../utils/APIError');
const axios = require('axios');


const http = axios.create({
baseURL: "https://abair.ie/api2",
headers: {
"Content-Type": "application/json",
//"Access-Control-Allow-Origin": "*"
}
});

/**
* Creates a new story document on the DB.
* @param {Object} req body: Story object
* @return {Promise}
*/

//function test (req:any) {
async function synthesiseAndStoreSent (req:any) {

function yes() {
return new Promise(
async (resolve, reject) => {

// if the sentence audio is not already stored in the db
const story = await DigitalReaderSentenceAudio.find({
drStoryId: req.body.drStoryId,
sentenceId: req.body.sentenceId,
voice: req.body.voiceCode
});
console.log(story)
if (!(Array.isArray(story) && story.length!==0)) {
console.log('storing!')
const audioObj = await http.post('/synthesise',
reqBody
);
console.log(audioObj);
const storedSent = await DigitalReaderSentenceAudio.create({
drStoryId: req.body.drStoryId,
sentenceId: req.body.sentenceId,
voice: req.body.voiceCode,
// currently only supports mp3 files
audioUrl: `data:audio/mp3;base64,` + audioObj.data.audioContent, // audioContent goes to audioUrl for compatibility with synthesis service
timing: audioObj.data.timing
});
}
resolve('Response: ' + req.body.textInput)
//console.log(audioObj)


//resolve(0);
})
}

function no() {
return Promise.resolve(1);
}

// make sure all necessary body params are present
if (req.body===undefined) return no();

if (req.body.textInput===undefined) return no();
if (req.body.voiceCode===undefined) return no();
if (req.body.audioEncoding===undefined) return no();
if (req.body.speed===undefined) return no();

if (req.body.drStoryId===undefined) return no();
if (req.body.sentenceId===undefined) return no();

let reqBody: any;

// check that the sent does not already exist in the db
/*if (!DigitalReaderSentenceAudio.find({
drStoryId: req.body.drStoryId,
sentenceId: req.body.sentenceId,
voice: req.body.voiceCode
})) {*/

// make a call to the synthesis API
reqBody = {
synthinput: {
text: req.body.textInput,
normalised: true,
},
voiceparams: {
languageCode: "ga-IE",
name: req.body.voiceCode,
},
audioconfig: {
audioEncoding: req.body.audioEncoding,
speakingRate: req.body.speed,
pitch: 1,
},
timing: "WORD"
};

//const tmp = axios.post('/synthesise', reqBody)
//console.log(tmp)

//setTimeout(()=>console.log('event over!'), 500);
/*const audioObj = await http.post('/synthesise',
reqBody,
{headers: {
"Content-Type": "application/json",
}}
);
console.log(audioObj)*/

/*{headers: {
"Content-Type": "application/json",
}}*/



//console.log(audioObj)

// TODO : store the result in the DB.
/*if (audioObj) {
const storedSent = await DigitalReaderSentenceAudio.create({
drStoryId: req.body.drStoryId,
sentenceId: req.body.sentId,
voice: req.body.voice.code,
audioUrl: audioObj.audioUrl,
timing: audioObj.timing
});
if (!storedSent) {
return no();
}
}*/
//}

return yes();

/*return new Promise(
(resolve, reject) => {
setTimeout( () =>
resolve('test resolution!'), 1000
)
// above should be changed to an await of the a call to the synth api
})*/
}

export = synthesiseAndStoreSent;
51 changes: 44 additions & 7 deletions api/src/routes/drStory.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ let storyRoutes;
// POST
const create = require("../endpoint/drStory/create");
const storeSynthAudio = require("../endpoint/drStory/storeSynthAudio");
const sentenceAudio = require("../endpoint/drStory/sentenceAudio");
/*const viewFeedback = require("../endpoint/story/viewFeedback");
const updateStoryAndCheckGrammar = require("../endpoint/story/updateStoryAndCheckGrammar");
const averageWordCount = require("../endpoint/story/averageWordCount");
Expand All @@ -64,6 +65,7 @@ let storyRoutes;
"/create": create,
"/getMatchingWords": getMatchingWords,
"/storeSynthAudio": storeSynthAudio,
"/sentenceAudio": sentenceAudio,
//"/viewFeedback/:id": viewFeedback,
//"/updateStoryAndCheckGrammar": updateStoryAndCheckGrammar,
/*"/averageWordCount/:studentId": averageWordCount,
Expand All @@ -72,26 +74,61 @@ let storyRoutes;
});
})();

const test = require("../endpoint/drStory/queueTest");

function queue(testFn:Function):Function {
//let testQueue:Promise<any> = Promise.resolve(0);

/*function nextQueue(someFn, req) {
testQueue = someFn(req);
}*/

const synthesiseAndStoreSent:Function = require("../endpoint/drStory/synthesiseAndStoreSent");

// reference: https://stackoverflow.com/questions/36083121/expressjs-backend-put-requests-into-a-queue
// solution to queuing system found in above post
// the order does not always match up but order is not particularly important
function queue(testFn:Function) {
let queue:Promise<any> = Promise.resolve();

return function(...args) {
return function(req) {
queue = queue.then(() => {
return test(...args);
return testFn(req);
});
return queue;
}
}

const queuedFn = queue(test);
const queuedFn = queue(synthesiseAndStoreSent);

queuedFn('{textInput}').then((data) => {console.log(data)})

/*testQueue.then(() => {
console.log('something!')
testQueue = testFn('req');
});*/

storyRoutes
.route('/testQueue')
.post(
.post( /*(req, res) => {
testQueue.then((data) => {
// function logic e.g storing to db etc.
//console.log(req.body.text, data)
console.log(req.body.textInput, data)
//console.log(data)
testQueue = synthesiseAndStoreSent(req); // currently does not work properly
res.json(200);
//nextQueue(testFn, req);
});
}*/
(req, res) => {
queuedFn(req.body).then( (data) => {console.log(data)} )
console.log(req.body.textInput)
queuedFn(req).then( (data) => {
console.log(data)
//console.log(data)
//res.json(200);
})
//res.status(102).json(); // dummy response sent to client to free up clientside "space" for other API calls
res.json(102); // should return a status of 102
//res.status(102).json(1);
}
)

Expand Down
33 changes: 33 additions & 0 deletions ngapp/src/app/core/services/dr-story.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import config from 'abairconfig';
import { firstValueFrom, tap, catchError } from 'rxjs';

import { DigitalReaderStory } from 'app/core/models/drStory';
import { AudioEncoding } from './synthesis.service';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -275,6 +276,38 @@ export class DigitalReaderStoryService {
return this.http.post<any[]>(this.baseUrl + 'drStory/getMatchingWords/', {lemma:lemma, tags:tags});
}

getSentenceAudio(drStoryId:string, sentId:number, voiceCode:string) {

const reqBody = {
drStoryId: drStoryId,
sentId: sentId,
voice: voiceCode
}

return this.http.post<any[]>(this.baseUrl + 'drStory/sentenceAudio', reqBody);
}

runTestQueue(
textInput: string,
voiceCode:string,
audioEncoding:AudioEncoding,
speed:number,
drStoryId:string,
sentenceId:number) {
//runTestQueue(input: string) {

return this.http.post<Array<string>>(this.baseUrl + 'drStory/testQueue', /*{text:input}*/
{
textInput: textInput,
voiceCode: voiceCode,
audioEncoding: audioEncoding,
speed: speed,
drStoryId: drStoryId,
sentenceId: sentenceId
}
);
}

/*
createDRStory(title: string, date: Date, dialects: Array<string>, htmlText: string, author: string) {
const drstoryObj = {
Expand Down
Loading

0 comments on commit b082d2e

Please sign in to comment.