Skip to content

Commit

Permalink
version 2.0.4 fixs SSML MISTAKES
Browse files Browse the repository at this point in the history
  • Loading branch information
mastashake08 committed Jan 25, 2023
1 parent 264236d commit 2ecbafe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
71 changes: 37 additions & 34 deletions SpeechKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default class SpeechKit extends EventTarget {
this.recognition.lang = language
this.grammarList = new SpeechGrammarList()
this.synth = window.speechSynthesis || window.webkitspeechSynthesis
this.recognition.continuous = continuous;
this.recognition.interimResults = interimResults;
this.recognition.continuous = continuous
this.recognition.interimResults = interimResults
this.utterance = new SpeechSynthesisUtterance()
this.pitch = pitch
this.rate = rate
Expand All @@ -32,43 +32,41 @@ export default class SpeechKit extends EventTarget {
const evt = new CustomEvent('speechkitsoundend', { detail: {
event: event
}})
this.dispatchEvent(evt))
this.dispatchEvent(evt)
})

this.recognition.addEventListener('onspeechend', function(event) {
const evt = new CustomEvent('speechkitspeechend', { detail: {
event: event
}})
this.dispatchEvent(evt))
this.dispatchEvent(evt)
})

this.recognition.addEventListener('onstart', function() {
const event = new Event('speechkitstart');
const event = new Event('speechkitstart')
this.dispatchEvent(event)
})

this.recognition.addEventListener('onresult', function(event) {
if(event.results[0].isFinal) {
this.resultList = event.results
const evt = new CustomEvent('speechkitresult', { detail: {
results: this.resultList
}
});
const evt = new CustomEvent('speechkitresult', { detail: { results: this.resultList }})
this.dispatchEvent(evt)
})
}
}
})


this.recognition.addEventLisenter('onerror', function(event) {
const evt = new CustomEvent('speechkiterror', {
detail: {
event: event
}
});
})
this.dispatchEvent(evt)
})

this.recognition.addEventLisenter('onspeechend', function() {
const event = new Event('speechkitend');
const event = new Event('speechkitend')
this.dispatchEvent(event)
})

Expand All @@ -83,7 +81,7 @@ export default class SpeechKit extends EventTarget {
})
this.dispatchEvent(event)
})
}
}

this.utterance.addEventLisenter('onstart', function (event) {
const evt = new CustomEvent('speechkitutterancestart', {
Expand Down Expand Up @@ -131,7 +129,7 @@ export default class SpeechKit extends EventTarget {
this.dispatchEvent(evt)
})

this.utterance.addEventListener('onmark', (event) {
this.utterance.addEventListener('onmark', function(event) {
const evt = new CustomEvent('speechkitutterancemarked', {
detail: {
event:event
Expand All @@ -145,13 +143,18 @@ export default class SpeechKit extends EventTarget {
detail: {
event:event
}
})
this.dispatchEvent(evt)
})
} catch (e){
const evt = new CustomEvent('speechkitlistenererror', {
detail: {
error: e
}
})
this.dispatchEvent(evt)
})
} catch {
continue
}
}
}
}

/**
* Start listening for speech recognition.
Expand Down Expand Up @@ -340,9 +343,9 @@ export default class SpeechKit extends EventTarget {
const text = this.getText()
try {
if (!navigator.canShare) {
const type = "text/plain";
const blob = new Blob([text], { type });
const data = [new ClipboardItem({ [type]: blob })];
const type = "text/plain"
const blob = new Blob([text], { type })
const data = [new ClipboardItem({ [type]: blob })]
await navigator.clipboard.write(data)
alert ('Text copied to clipboard')
} else {
Expand Down Expand Up @@ -387,8 +390,8 @@ export default class SpeechKit extends EventTarget {

parseSentenceSSML (text) {
let xmlString = ""
const segmenter = new Intl.Segmenter(navigator.language, { granularity: 'sentence' });
const iterator1 = segmenter.segment(text)[Symbol.iterator]();
const segmenter = new Intl.Segmenter(navigator.language, { granularity: 'sentence' })
const iterator1 = segmenter.segment(text)[Symbol.iterator]()
([...segmenter.segment(text)]).forEach((seg, index) => {
xmlString += `\n <s id="sentence-${index}"> ${seg.segment} </s>`
})
Expand All @@ -406,7 +409,7 @@ export default class SpeechKit extends EventTarget {
try {
const parser = new DOMParser()
const xmlDoc = parser.parseFromString(xmlString, "application/xml")
const errorNode = xmlDoc.querySelector('parsererror');
const errorNode = xmlDoc.querySelector('parsererror')
const errorNode2 = xmlDoc.querySelector('speak')
const errorNode3 = xmlDoc.querySelector('break')
if (errorNode) {
Expand All @@ -431,7 +434,7 @@ export default class SpeechKit extends EventTarget {
}

addBreakSSML (xmlString, sntc, offset, time = 200) {
const segmenter = new Intl.Segmenter(navigator.language, { granularity: 'sentence' });
const segmenter = new Intl.Segmenter(navigator.language, { granularity: 'sentence' })
const xmlDoc = this.parseSSML(xmlString)
const segments = segmenter.segment(xmlString)
const segs = Array.from(segments, (seg, index) => {
Expand All @@ -444,30 +447,30 @@ export default class SpeechKit extends EventTarget {
}]
})
const idx = `#sentence-${index}`
let sp1 = document.createElement("break");
let sp1 = document.createElement("break")
sp1.setAttribute('time', `${encodeURIComponent(time+"ms")}`)
let sp2 = xmlDoc.querySelector(idx);
sp2.insertBefore(sp1, sp2.childNodes[0].nextSibling);
let sp2 = xmlDoc.querySelector(idx)
sp2.insertBefore(sp1, sp2.childNodes[0].nextSibling)
}
})
return new XMLSerializer().serializeToString(xmlDoc)
}

addEmphasisSSML (xmlString, sntc, offset, time = 200) {
const segmenter = new Intl.Segmenter(navigator.language, { granularity: 'sentence' });
const segmenter = new Intl.Segmenter(navigator.language, { granularity: 'sentence' })
const xmlDoc = this.parseSSML(xmlString)
const segments = segmenter.segment(xmlString)
const segs = Array.from(segments, (seg, index) => {
if(seg.segment == sntc) {
const idx = `#sentence-${index}`
let sp1 = document.createElement("emphasis");
let sp1 = document.createElement("emphasis")
sp1.setAttribute('level', 'strong')
const txt = document.createTextNode(seg.segment)
sp1.appendChild(txt)
let sp2 = xmlDoc.querySelector(idx);
let sp2 = xmlDoc.querySelector(idx)
try {
sp2.removeChild(sp2.textNode)
sp2.addChild(sp1);
sp2.addChild(sp1)
} catch {
sp2.replaceChild(sp1, sp2.firstChild)
}
Expand Down
3 changes: 3 additions & 0 deletions local-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
npm version $1
npm pack --pack-destination="$2"
ECHO "$2/mastashake08-speech-kit-$1.tgz"
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@mastashake08/speech-kit",
"version": "1.9.7",
"version": "2.0.4",
"description": "Package for simplifying the Speech Recognition and Speech Utterence process.",
"main": "SpeechKit.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "sh local-build.sh ~/Desktop"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 2ecbafe

Please sign in to comment.