psychopy.org | Reference | Downloads | Github

How to present an image on the background while playing audio files?

hi,
I want to present and image in the backround while playing several audio files.
I wrote 2 simple functions, the first returns an imageStim object for the background, the second builds a player object using the package Pyglet and play a sequence of audio files.
the problem is that the background image is flickering.
what should I change?

thanks a lot

def backround_stp(win):
    str_cross = "./input/graphics/instructions/Slide3.JPG"
    cross = visual.ImageStim(win=win,image=str_cross, units='pix')
    return cross

def play_stp(win,neuORneg):
    sound_path = "input/Audio"
    dir_audio = os.listdir(sound_path)
    player = pg.media.Player()
    half_stp = int(len(dir_audio)/2)
    if neuORneg=='neu':
        s,f=0,half_stp+1
    else: #neg
        s,f=half_stp+1,len(dir_audio)+1
    for index_str_audio in range(s,f):
        music = pg.resource.media(sound_path + '/' + dir_audio[index_str_audio], streaming=False)
        player.queue(music)
    cross =backround_stp(win)
    time = player.time
    **cross.autoDraw = True**
    win.flip()  
    player.play()
    pg.app.run()
    **cross.autoDraw = False**
    win.close()

win = window()
play_stp(win,'neu')
Summary

This text will be blurred

First, is this a builder experiment and you’re showing us a code component, or is this something you built from the ground up in coder? I’m guessing the latter, if you’re using Pyglet directly.

Second, you shouldn’t need to use Pyglet directly at all if you’re using PsychoPy. The PsychoPy window should have everything you need and then some. My guess is that the fundamental issue is the pyglet app and psychopy window interfering with each other somehow.

Third, can you please re-post your code surrounded by ` marks? If you put three above and three below your code it should preserve the indentation. Right now I can’t tell what’s going with what.

code block

Hi,
thanks for your answer!
I built to code from the ground.
I used Pyglet becasue I tried to play 20 audio files (mp3) in a sequence, and didn’t managed to do it by psycopy.Sound.
do you know how can I do it?

thanks a lot!

this is my code -

def window():
    user32 = ctypes.windll.user32
    screen_size = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
    win = visual.window.Window(units='pix', fullscr=True, size=screen_size,color='black', monitor=0) 
    return win

def background_stp(win):
    str_cross = "./input/graphics/instructions/Slide3.JPG"
    cross = visual.ImageStim(win=win,image=str_cross, units='pix')
    return cross


def play_stp(win,neuORneg):
    sound_path = "input/Audio"
    dir_audio = os.listdir(sound_path)
    player = pg.media.Player()
    half_stp = int(len(dir_audio)/2)
    if neuORneg=='neu':
        s,f=0,half_stp+1
    else: #neg
        s,f=half_stp+1,len(dir_audio)+1
    for index_str_audio in range(s,f):
        music = pg.resource.media(sound_path + '/' + dir_audio[index_str_audio], streaming=False)
        player.queue(music)
    cross = background_stp(win)
    cross.autoDraw = True
    win.flip()
    player.play()
    pg.app.run()
    cross.autoDraw = False
    win.close()

win = window()
play_stp(win,'neu')

That’s perfectly doable with psychopy.sound, especially if you’re doing everything in the coder. (Note, by the way, that you won’t be able to do this study online at all, you’d need to make it in the builder for that.)

The basic approach is going to be to create a list of psychopy.Sound objects, and then just loop through the array. It won’t be in one playlist object like you have it here, but you can fairly easily check the status of the current sound and move to the next sound as needed. Here’s a skectch of the code you would add (with a lot of the rest of the code snipped out)

from psychopy.constants import PLAYING
#...
playlist = []
for index_str_audio in range(s,f):
        music = sound.Sound(sound_path + '/' + dir_audio[index_str_audio])
        playlist.append(music)
#...
# main play loop
for i in range(0, len(playlist)):
    playlist[i].play()
    while playlist[i].status == PLAYING:
       # you can draw whatever you want to draw here
       win.flip()

This should go through the playlist and play each item in it in turn, and once each item has finished, start the next one.

Thank you! :slight_smile:
I can’t import psychopy.sound,
it raise this error -

    from psychopy import vsound
  File "C:\Users\psylab6027\Anaconda3\envs\bodymap\lib\site-packages\psychopy\sound\__init__.py", line 96, in <module>
    raise exceptions.DependencyError(
psychopy.exceptions.DependencyError: No sound libs could be loaded. Tried: ['sounddevice', 'pyo', 'pygame']
Check whether the necessary sound libs are installed

I use psychopy 3.2.4 instaled as conda enviorment.
should I downgrade psychopy version?

thanks again

If anything I would upgrade to 2020.1.3, but the basic error here is that conda didn’t install the sound libraries psychopy needs for some reason. This is very fixable: https://anaconda.org/poppy-project/sounddevice

You could also try using the PsychoPy standalone, which comes with all of these libraries installed.

hi,
thanks again!
I tried your solution, but it raised this errors.
do you understand why?

File "C:/Users/psylab6027/Documents/GitHub/BodyMap/BodyMap/play_stp.py", line 53, in play_stp music = sound.Sound(sound_path + '/' + dir_audio[index_str_audio]) File "C:\Users\psylab6027\Anaconda3\envs\bodymap\lib\site-packages\psychopy\sound\backend_sounddevice.py", line 317, in __init__ self.setSound(value, secs=self.secs, octave=self.octave, File "C:\Users\psylab6027\Anaconda3\envs\bodymap\lib\site-packages\psychopy\sound\backend_sounddevice.py", line 361, in setSound _SoundBase.setSound(self, value, secs, octave, hamming, log) File "C:\Users\psylab6027\Anaconda3\envs\bodymap\lib\site-packages\psychopy\sound\_base.py", line 195, in setSound self._setSndFromFile(p) File "C:\Users\psylab6027\Anaconda3\envs\bodymap\lib\site-packages\psychopy\sound\backend_sounddevice.py", line 395, in _setSndFromFile self.sndFile = f = sf.SoundFile(filename) File "C:\Users\psylab6027\Anaconda3\envs\bodymap\lib\site-packages\soundfile.py", line 629, in __init__ self._file = self._open(file, mode_int, closefd) File "C:\Users\psylab6027\Anaconda3\envs\bodymap\lib\site-packages\soundfile.py", line 1183, in _open _error_check(_snd.sf_error(file_ptr), File "C:\Users\psylab6027\Anaconda3\envs\bodymap\lib\site-packages\soundfile.py", line 1357, in _error_check raise RuntimeError(prefix + _ffi.string(err_str).decode('utf-8', 'replace')) RuntimeError: Error opening 'input/Audio/recordNum1.mp3': File contains data in an unknown format. 1.

Oh, MP3s don’t work in PsychoPy. See here: What sound file types are supported in PsychoPy - unable to open an mp3