简体   繁体   中英

Python: using pygame to play sounds

So I'm trying to run a for loop in a way that a sound will be played together with an image for as long as the sound lasts. I can see the image displaying for a very short time and also I hear the sound playing. But then it all stops, I'm not able to give input in the window (I should give in 'j' or 'f') in 0.85 seconds. After this a new trial should begin, but it doesn't. Don't know if that's because of this error that I'm getting:

sound1 = sound.SoundPygame(value=stimulussound)

AttributeError: 'SoundPygame' object has no attribute 'SoundPygame'

I don't understand why I get that error since sound1 is being played the first trial. But after the sound stops, the image goes away, the fixationcross is showed on screen, but the fixationcross doesn't go away after 0.85 seconds... And also if I push J of F, it DOES save it in a variable! And it also saves a reactiontime! Anyways, here's my code, why isn't a second trial starting after the first one running great?

#showing instructions
instructions = visual.TextStim(window, text=actualinstructions)
instructions.draw()
window.flip()

#waiting for j-key before rest of experiment runs
if event.waitKeys("j"):
    window.flip()    
    start = True 

#whileloop to start experiment
while start == True:
#forloop
    for i in range (0,192):
    #saving the two needed pathways in a variable
        stimulusimage = conditiesalles[int(i)][int(2)]
        stimulussound = conditiesalles[int(i)][int(3)] #f.e. C:\Users\Ineke\Documents\Python Scripts\Project\stimuli\Sounds\Negative\6.wav



    #lengthofsound: using function 
        sound1 = sound.SoundPygame(value=stimulussound)
        lengthofsound = sound1.getDuration()

    #resetting timer and making a variable that knows the time 
        timerClock.reset()
        currentTime = timerClock.getTime()
        if (currentTime <= lengthofsound):
            #imagestim
            showingimage = visual.ImageStim(window, image=stimulusimage)
            showingimage.draw()
            window.flip()
            #soundPygame
            sound = sound.SoundPygame(value=stimulussound)
            sound.play()

        if (currentTime > lengthofsound):
            timerClock.reset()
            window.flip()

        if (currentTime <= timefixationcross):
            #fixatiekruis
            Fixationcross.fixationscross(window)

            #getKeys j of f = inputuser
            if event.getKeys('j'):
                inputuser = 'j'
                reactiontime = currentTime
            elif event.getKeys('f'):
                inputuser = 'f'
                reactiontime = currentTime
            else: 
                inputuser = "geen input of foute knop gebruikt"
                reactiontime = "geen reactie"
        inputsuser.append(inputuser)
        reactiontimesuser.append(reactiontime)

    #closing the window
    start == False    
    window.close()

This error occurs because you overwrite the variable sound in this line:

sound = sound.SoundPygame(value=stimulussound)

... so the variable "sound" it no longer points to the psychopy.sound module once this line has been run. The error message tells you that there is no such attribute as psychopy.sound.SoundPygame.SoundPygame . The solution is simply to rename the variable in the above line to something different than "sound".

As a side note, you seem to create identical sound objects on each trial. sound1 and what you currently call sound are both sound.SoundPygame(value=stimulussound) . Why not just have one?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM