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

[Bug]: The sound stimulus does not play. #6943

Open
JeongWoo7780 opened this issue Oct 25, 2024 · 6 comments
Open

[Bug]: The sound stimulus does not play. #6943

JeongWoo7780 opened this issue Oct 25, 2024 · 6 comments
Labels
🐞 bug Issue describes a bug (crash or error) or undefined behavior.

Comments

@JeongWoo7780
Copy link

PsychoPy Version

2024.2.1

What OS are your PsychoPy running on?

Windows 10

Bug Description

My python version: 3.10.11

I'm using a VENV virtual environment. I recently changed the version of python (3.9 > current) and found that my experimental code doesn't work.

Previously, my code was working fine, and the sound was outputting correctly. I have attached a video of it.

But now, when I run the same code, it does not output any error message.

The text and image stimulus presentation works fine as expected, but the sound stimulus is not outputting.

I have checked the sampling rate of the wav file, sound settings, etc. In the past, it has worked fine to display a message that an error occurred due to a sampling rate error.

However, I have no idea what the current problem is.

test.mp4

I left the code in the additional part

Expected Behaviour

The correct sound stimulus should be output.

Currently, the text and image stimuli are working properly.

Steps to Reproduce

  1. I ran the experimental paradigm using psychopy on a Python 3.10.11 venv virtual machine.

...

Additional context

from psychopy import visual, core, event
from psychopy.sound import Sound
import time
from psychopy import prefs

prefs.hardware['audioLib'] = ['ptb']
#prefs.hardware["audioDevice"] == "default"

win = visual.Window(size=(2560, 1440), fullscr=True, color="white", winType='pyglet', allowGUI=True, title='CBT Cognitive')

def create_text_stim(text, height, wrap_width=1.7):
    return visual.TextStim(win, text=text, color="black", height=height, wrapWidth=wrap_width)

# image stim

def create_image_stim(image_path):
    return visual.ImageStim(win, image=image_path)

# sound stim
def create_sound_stim(sound_path, sample_rate=44100):
    return Sound(sound_path, sampleRate=sample_rate)

# countdown

def countdown(seconds=3):
    for i in range(seconds, 0, -1):
        count = visual.TextStim(win, text=str(i), color="black", height=0.3)
        count.draw()
        win.flip()
        core.wait(1)

# speaker image & text

def show_speaker_with_text(text, duration):
    speaker = create_image_stim("exp_images/speaker.png")
    text_stim = create_text_stim(text, height=0.1)
    speaker.pos = (0, -0.3)
    text_stim.pos = (0, 0.2)
    speaker.draw()
    text_stim.draw()
    win.flip()
    core.wait(duration)

# text stim1
def present_text_stimulus_no_countdown_with_sound(text, duration, height, sound_path):
    stimulus = create_text_stim(text, height, wrap_width=1.7)
    stimulus.draw()
    win.flip()
    sound = create_sound_stim(sound_path, sample_rate=44100)
    sound.play()
    core.wait(duration)

# text stim2

def present_text_stimulus_with_countdown_with_sound(text, duration, height, speaker_duration=10, sound_path=None):
    stimulus = create_text_stim(text, height, wrap_width=1.7)
    stimulus.draw()
    win.flip()
    if sound_path:
        sound = create_sound_stim(sound_path, sample_rate=44100)
        sound.play()
    core.wait(duration)
    countdown()
    show_speaker_with_text("Speak", speaker_duration)

# text stim
def present_text_stimulus_no_countdown(text, duration, height):
    stimulus = create_text_stim(text, height, wrap_width=1.7) 
    stimulus.draw()
    win.flip()
    core.wait(duration)

# two images stim

def present_two_images(image_path1, image_path2, duration, speaker_duration=10, sound_path=None):
    img1 = create_image_stim(image_path1)
    img2 = create_image_stim(image_path2)
    img1.pos = (-0.5, 0)
    img2.pos = (0.5, 0)
    img1.draw()
    img2.draw()
    win.flip()
    if sound_path:
        sound = create_sound_stim(sound_path, sample_rate=44100)
        sound.play()
    core.wait(duration)
    countdown()
    show_speaker_with_text("Speak", speaker_duration)

# image & text
def present_image_and_text_above(image_path, text, duration, text_height, speaker_duration=10, sound_path=None):
    img = create_image_stim(image_path)
    txt = create_text_stim(text, text_height, wrap_width=1.8)
    img.pos = (0, -0.3)
    txt.pos = (0, 0.6)
    txt.draw()
    img.draw()
    win.flip()
    if sound_path:
        sound = create_sound_stim(sound_path, sample_rate=44100)
        sound.play()
    core.wait(duration)
    countdown()
    show_speaker_with_text("Speak", speaker_duration)

# Exp roop
def run_experiment():
    welcome_text = "Example"
    present_text_stimulus_no_countdown_with_sound(welcome_text, 13, height=0.07, sound_path="audio_44100/welcome.wav")

    instruction_text = "Example"
    present_text_stimulus_no_countdown_with_sound(instruction_text, 11, height=0.07, sound_path="audio_44100/instruction1.wav")

    sentence = "Example"
    present_text_stimulus_with_countdown_with_sound(sentence, 8, height=0.09, sound_path="audio_44100/sentence.wav", speaker_duration=9)

    instruction_text2 = "Example"
    present_text_stimulus_no_countdown_with_sound(instruction_text2, 12, height=0.07, sound_path="audio_44100/instruction2.wav")

    present_text_stimulus_with_countdown_with_sound(sentence, 8, height=0.09, sound_path="audio_44100/sentence.wav", speaker_duration=9)



    instruction_text3 = "Example"
    present_text_stimulus_no_countdown_with_sound(instruction_text3, 8, height=0.08, sound_path="audio_44100/instruction3.wav")

    questions = [
        "Example",
        "Example",
        "Example",
        "Example",
        "Example",
        "Example"
    ]

     for i, question in enumerate(questions, 1):
        present_text_stimulus_with_countdown_with_sound(question, 5, height=0.1, sound_path=f"audio_44100/question_{i}.wav", speaker_duration=7)

    
    instruction_text4 = "Example"
    present_text_stimulus_no_countdown_with_sound(instruction_text4, 9, height=0.07, sound_path="audio_44100/instruction4.wav")
    
    num_sentence_1 = "Example"
    present_text_stimulus_with_countdown_with_sound(num_sentence_1, 5, height=0.25, sound_path="audio_44100/num_sentence_1.wav", speaker_duration=7)

    num_sentence_2 = "Example"
    present_text_stimulus_with_countdown_with_sound(num_sentence_2, 6, height=0.25, sound_path="audio_44100/num_sentence_2.wav", speaker_duration=8)


    reverse_text = "Example"
    present_text_stimulus_no_countdown_with_sound(reverse_text, 8, height=0.09, sound_path="audio_44100/reverse_text.wav")

    reverse_word_1 = "Example"
    present_text_stimulus_with_countdown_with_sound(reverse_word_1, 5, height=0.25, sound_path="audio_44100/reverse_word_90.wav", speaker_duration=8)


    instruction_text6 = "Example"
    present_image_and_text_above("exp_images/cat_dog.png", instruction_text6, 8, text_height=0.09, sound_path="audio_44100/instruction6.wav", speaker_duration=14)



    figure_sequential_instruction = "Example"
    present_image_and_text_above("exp_images/sequential.png", figure_sequential_instruction, 22, text_height=0.08, sound_path="audio_44100/sequential.wav", speaker_duration=10)




    rotation_instruction = "Example"
    present_image_and_text_above("exp_images/rotation_1.png", rotation_instruction, 17, text_height=0.09, sound_path="audio_44100/rotation.wav", speaker_duration=10)




    image_instruction = "Example"
    present_image_and_text_above("exp_images/toothpaste.png", image_instruction, 9, text_height=0.08, sound_path="audio_44100/image_instruction.wav", speaker_duration=8)

    image_instruction2 = "Example"
    present_image_and_text_above("exp_images/eraser.png", image_instruction2, 9, text_height=0.08, sound_path="audio_44100/image_instruction.wav", speaker_duration=8)



    end_text = "Example"
    present_text_stimulus_no_countdown(end_text, 5, height=0.1)
win.close()

run_experiment()
@JeongWoo7780 JeongWoo7780 added the 🐞 bug Issue describes a bug (crash or error) or undefined behavior. label Oct 25, 2024
@zeyus
Copy link
Contributor

zeyus commented Oct 25, 2024

Does the sound work normally in the virtual machine?

Do you have the standalone psychopy installed as well, or only the venv version?

@TEParsons
Copy link
Contributor

Without specifying a speaker to play the sound on, it'll be using whatever psychtoolbox considers the default. Maybe the default speaker in your setup is one with no actual sound, like a virtual speaker?

Try doing this:

from psychopy.hardware import speaker
for profile in speaker.SpeakerDevice.getAvailableDevices():
    print(profile)

and then specifying speaker=<index of the speaker you want> when creating the Sound object

@JeongWoo7780
Copy link
Author

Does the sound work normally in the virtual machine?

Do you have the standalone psychopy installed as well, or only the venv version?

No, there is no sound in the virtual environment with the current Python version (3.10.11).
I also have a standalone Psychopy installed, but I have not encountered sound issues before (example video).

@JeongWoo7780
Copy link
Author

Without specifying a speaker to play the sound on, it'll be using whatever psychtoolbox considers the default. Maybe the default speaker in your setup is one with no actual sound, like a virtual speaker?

Try doing this:

from psychopy.hardware import speaker
for profile in speaker.SpeakerDevice.getAvailableDevices():
    print(profile)

and then specifying speaker=<index of the speaker you want> when creating the Sound object

I received the sounddevice library via pip.

Then I used the following code to get a list of sound devices.

import sounddevice as sd
print(sd.query_devices())

I set up the output devices I use, but it's still a problem.

I'm not sure what the problem is, as I didn't set any sound devices before.

@TEParsons
Copy link
Contributor

sounddevice and psychtoolbox aren't necessarily going to use the same indices for each device - so if you get the device index from sounddevice and then you're using prefs.hardware['audioLib'] = ['ptb'] then I would expect there to be a potential mismatch. Try doing:

import psychtoolbox.audio as ptb
print(ptb.get_devices())

and using the device index from there.

@zeyus
Copy link
Contributor

zeyus commented Oct 31, 2024

I also have a standalone Psychopy installed, but I have not encountered sound

Then you might also want to try adding the following at the top of the script (before importing psychology)

import os
os.environ["PSYCHOPYNOPACKAGES"] = "1"

It might not help, but I did encounter a case where no matter what I did, some incorrect setting seemed to be loading from the standalone psychopy (it was audio related too), I think the PTB version in my standalone install was older than the one I was using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Issue describes a bug (crash or error) or undefined behavior.
Projects
None yet
Development

No branches or pull requests

3 participants