diff --git a/packages/text-to-speech/src/htmlToSsml.ts b/packages/text-to-speech/src/htmlToSsml.ts index 19db626f1..6d4fb8040 100644 --- a/packages/text-to-speech/src/htmlToSsml.ts +++ b/packages/text-to-speech/src/htmlToSsml.ts @@ -131,16 +131,20 @@ export type SSMLItem = { textItems: string[] } -export type VoiceOptions = { - primary: string - secondary: string +export type SSMLOptions = { + primaryVoice: string + secondaryVoice: string + rate: string + language: string } -const startSsml = (element: Element, voices: VoiceOptions): string => { +const startSsml = (element: Element, options: SSMLOptions): string => { const voice = - element.nodeName === 'BLOCKQUOTE' ? voices.secondary : voices.primary + element.nodeName === 'BLOCKQUOTE' + ? options.secondaryVoice + : options.primaryVoice return ` - + ` } @@ -152,10 +156,7 @@ export const ssmlItemText = (item: SSMLItem): string => { return [item.open, ...item.textItems, item.close].join('') } -export const htmlToSsml = ( - html: string, - voices: { primary: string; secondary: string } -): SSMLItem[] => { +export const htmlToSsml = (html: string, options: SSMLOptions): SSMLItem[] => { const dom = parseHTML(html) const body = dom.document.querySelector('#readability-page-1') if (!body) { @@ -174,7 +175,7 @@ export const htmlToSsml = ( i = emitElement(textItems, node, true) items.push({ - open: startSsml(node, voices), + open: startSsml(node, options), close: endSsml(), textItems: textItems, }) diff --git a/packages/text-to-speech/src/index.ts b/packages/text-to-speech/src/index.ts index 6d2e34736..2ff849856 100644 --- a/packages/text-to-speech/src/index.ts +++ b/packages/text-to-speech/src/index.ts @@ -217,8 +217,10 @@ const synthesizeTextToSpeech = async ( } } else { const ssmlItems = htmlToSsml(input.text, { - primary: speechConfig.speechSynthesisVoiceName, - secondary: 'en-US-GuyNeural', + primaryVoice: speechConfig.speechSynthesisVoiceName, + secondaryVoice: 'en-US-GuyNeural', + language: speechConfig.speechSynthesisLanguage, + rate: '1', }) for (const ssmlItem of Array.from(ssmlItems)) { diff --git a/packages/text-to-speech/test/htmlToSsml.test.ts b/packages/text-to-speech/test/htmlToSsml.test.ts index 424de5d9d..c751058de 100644 --- a/packages/text-to-speech/test/htmlToSsml.test.ts +++ b/packages/text-to-speech/test/htmlToSsml.test.ts @@ -1,9 +1,17 @@ import 'mocha' import { expect } from 'chai' + +import fs from 'fs' +import { glob } from 'glob' import { htmlToSsml } from '../src/htmlToSsml' describe('htmlToSsml', () => { - const TEST_VOCIES = { primary: 'test-primary', secondary: 'test-secondary' } + const TEST_OPTIONS = { + primaryVoice: 'test-primary', + secondaryVoice: 'test-secondary', + language: 'en-US', + rate: '1' + } describe('a simple html file', () => { it('should convert Html to SSML', async () => { @@ -11,7 +19,7 @@ describe('htmlToSsml', () => {

this is some text

- `, TEST_VOCIES + `, TEST_OPTIONS ) const text = ssml[0].textItems.join('').trim() expect(text).to.equal( @@ -29,7 +37,7 @@ this is in the first paragraph this is also in the first paragraph

- `, TEST_VOCIES + `, TEST_OPTIONS ) const text = ssml[0].textItems.join('').trim() expect(text).to.equal( @@ -45,7 +53,7 @@ this is also in the first paragraph
second

third

- `, TEST_VOCIES + `, TEST_OPTIONS ) const first = ssml[0].textItems.join('').trim() const second = ssml[1].textItems.join('').trim() @@ -62,14 +70,30 @@ this is also in the first paragraph ) expect(ssml[0].open.trim()).to.equal( - `` + `` ) expect(ssml[1].open.trim()).to.equal( - `` + `` ) expect(ssml[2].open.trim()).to.equal( - `` + `` ) }) }) + // For local testing: + // describe('readability test files', () => { + // it('should convert Html to SSML without throwing', async () => { + // const g = new glob.GlobSync('../readabilityjs/test/test-pages/*') + // console.log('glob: ', glob) + // for (const f of g.found) { + // const readablePath = `${f}/expected.html` + // if (!fs.existsSync(readablePath)) { + // continue + // } + // const html = fs.readFileSync(readablePath, { encoding: 'utf-8' }) + // const ssmlItems = htmlToSsml(html, TEST_OPTIONS) + // console.log('SSML ITEMS', ssmlItems) + // } + // }) + // }) })