Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: text to speech with button response and keboard response #143

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feature: add variable to allow pause between words time_between_words
  • Loading branch information
CMonnin committed Nov 27, 2024
commit c19c1d1d38c9753001102dbbfafff9f754404041
26 changes: 22 additions & 4 deletions packages/plugin-text-to-speech-button-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ const info = <const>{
type: ParameterType.INT,
default: 0,
},
/** A pause between words in milliseconds */
time_between_words: {
type: ParameterType.INT,
default: 0,
},
},
data: {
/** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. */
Expand Down Expand Up @@ -162,14 +167,27 @@ class TextToSpeechButtonPluginResponse implements JsPsychPlugin<Info> {
}

// Set up SpeechSytnthesis
const utterance = new SpeechSynthesisUtterance(trial.stimulus);
utterance.lang = trial.lang;
const words = trial.stimulus.split(" ");
let currentIndex = 0;

// start time
const start_time = performance.now();

// start speechSynthesis
speechSynthesis.speak(utterance);
function speakNextWord() {
if (currentIndex < words.length) {
const utterance = new SpeechSynthesisUtterance(words[currentIndex]);
utterance.lang = trial.lang;

utterance.onend = () => {
setTimeout(() => {
currentIndex++;
speakNextWord();
}, trial.time_between_words);
};
speechSynthesis.speak(utterance);
}
}
speakNextWord();

// store response
const response = {
Expand Down
29 changes: 24 additions & 5 deletions packages/plugin-text-to-speech-keyboard-response/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ const info = <const>{
type: ParameterType.BOOL,
default: true,
},
/** A pause between words in milliseconds */
time_between_words: {
type: ParameterType.INT,
default: 0,
},
},
data: {
/** Indicates which key the participant pressed. */
Expand Down Expand Up @@ -117,13 +122,27 @@ class TextToSpeechKeyboardResponsePlugin implements JsPsychPlugin<Info> {
if (trial.prompt !== null) {
display_element.insertAdjacentHTML("beforeend", trial.prompt);
}

// Set up SpeechSytnthesis
const utterance = new SpeechSynthesisUtterance(trial.stimulus);
utterance.lang = trial.lang;
const words = trial.stimulus.split(" ");
let currentIndex = 0;

// start time

// start speechSynthesis
speechSynthesis.speak(utterance);
function speakNextWord() {
if (currentIndex < words.length) {
const utterance = new SpeechSynthesisUtterance(words[currentIndex]);
utterance.lang = trial.lang;

utterance.onend = () => {
setTimeout(() => {
currentIndex++;
speakNextWord();
}, trial.time_between_words);
};
speechSynthesis.speak(utterance);
}
}
speakNextWord();

// store response
var response = {
Expand Down