Thanks again for your help! Here is my update to the previous post.
I realized the experiment wasnāt working online because I didnāt define the sound properly in JS:
Sound component:
Python Code (begin experiment tab):
beep = sound.Sound('A', secs = 25) # just create this once for the whole experiment
JS code:
beep = new sound.Sound({
win: psychoJS.window,
value: 'A',
secs: 10,
});
beep.setVolume(0.3);
I also took your suggestion and used the code you wrote for creating a timer.
Timer
Begin experiment tab:
Python
play_timer = core.CountdownTimer(start = 0.25)
stop_timer = core.CountdownTimer(start = 0.25)
JS
play_timer = new util.CountdownTimer({
startTime: 0.25
});
stop_timer = new util.CountdownTimer({
startTime: 0.25
});
Begin routine tab:
Python
play_timer_started = False
stop_timer_started = False
JS
play_timer_started = false;
stop_timer_started = false;
Finally, the code which maps the sound to the Polygon:
Click event
Python (each frame):
if mouse.isPressedIn(polygon) and beep.status != STARTED:
if not play_timer_started:
play_timer.reset() # start counting down from 0.25 s
play_timer_started = True
if play_timer_started and play_timer.getTime() < 0.0: # >= 250 ms elapsed
beep.play()
play_timer_started = False
# and similarly for stopping
elif mouse.isPressedIn(polygon) and beep.status == STARTED:
if not play_timer_started:
play_timer.reset() # start counting down from 0.25 s
play_timer_started = True
if play_timer_started and play_timer.getTime() < 0.0: # >= 250 ms elapsed
beep.pause()
play_timer_started = False
JS:
if ((mouse.isPressedIn(polygon) && (beep.status !== PsychoJS.Status.STARTED))) {
if ((! play_timer_started)) {
play_timer.reset();
play_timer_started = true;
}
if ((play_timer_started && (play_timer.getTime() < 0.0))) {
beep.play();
play_timer_started = false;
}
} else {
if ((mouse.isPressedIn(polygon) && (beep.status === PsychoJS.Status.STARTED))) {
if ((! play_timer_started)) {
play_timer.reset();
play_timer_started = true;
}
if ((play_timer_started && (play_timer.getTime() < 0.0))) {
beep.pause();
play_timer_started = false;
}
}
}
This solution works excellently in Builder but does not work in PsychoJS (possibly due to a translation error on my end).
The following code does work in PsychoJS but has the same hiccuping issue. This involves defining the sound and click event, but without any timer to add a delay to when the tone onset occurs.
Click event
Python (each frame):
if mouse.isPressedIn(polygon) and beep.status != STARTED:
beep.play()
elif mouse.isPressedIn(polygon) and beep.status == STARTED:
beep.stop()
JS:
if ((mouse.isPressedIn(polygon) && (beep.status !== PsychoJS.Status.STARTED))) {
beep.play();
} else {
if ((mouse.isPressedIn(polygon) && (beep.status === PsychoJS.Status.STARTED))) {
beep.stop();
}
}
As such, I think the issue may have to do with how I defined the timer. I used the following documentation as a reference: JSDoc: Class: CountdownTimer
I will keep trying over the next week and will report any updates.