Using an .exe in feedback that is not audio or an image

Hello,

I have an .exe that dispenses a food reward when the animal answers a trial correctly. I am having trouble figuring out how to include it in my custom code component. This is what I have tried, below in the Begin Routine tab. And I have pellet = ’ ’ in the Begin Experiment tab. Everything works correctly except for the pellet.exe being activated.

if Resp==AB:
    soundfile = 'correct.wav'
    pellet = 'pellet.exe'
    Timeout = 0
    total_score += 1
    score = 1
else:
    soundfile = 'Incorrect.wav'
    Timeout = 2
    total_score -= 1
    score = 0

So what do you want to implement

What I need to happen is when they answer correctly on a trial, the sound plays and the pellet is dispensed. When they answer incorrectly different sound plays and no pellet is dispensed.
Currently when they answer correctly on a trial, the correct.wav plays, I have used a sound component in the builder for that. But I am not sure how to tell the program to play the pellet.exe, as it is not a sound, image, text, or any other component in the builder. I have the pellet.exe in the folder with the experiment.

1 Like

Thank you for that!
I ended up creating the following code for my pellet.exe:

pelletPath = 'c:/pellet.exe'

def pellet(num = 3):
    for i in range(num):
        if os.path.isfile(pelletPath):
            os.system(pelletPath)

However I get the error code below. I’m guessing I am putting the code in the wrong place. I have it in the Begin Experiment tab of my feedback routine (which is my third routine, first two are images and responses). Do you by chance know if that is the cause of my error code? Or if it is related to something else?:

You’ve put your code in the right place (i.e. in the “Begin experiment” tab).

The error points to line 213 of your script. We can’t see all of that line in the error message though (i.e. the complete call to create sound_feedback). Please copy and paste that section of code so we can see what is going on. Probably good to provide a bit of context (i.e code before and after that line).

Is the creation of this sound object more custom code, or is it a sound created from a Builder component?

Lastly, please don’t post photos of code or errors. It is easier for us to work with actual text (which we can copy and paste from ourselves when giving answers).

My apologies for the photo. The systems I am trying to install the program on do not have internet. But I should be able to copy and paste it into a txt document and then transfer it to my regular computer and then add it here.
I am unable to replicate the error message on my own laptop, everything is actually working properly (except the pellet.exe because I do not have a dispenser connected to my laptop, but it shows the correct error message saying dispenser not connected). Below is the code that the error is referring too. I have used the sound component from the Builder, and then used the variable $soundfile in the sound component. I have two .wav files in the experiment folder and custom code that tells it to play one sound if correct response, the other if incorrect. I will have access to the computers that are giving the error messages on Friday and will upload the error message then.


# Initialize components for Routine "Feedback"
FeedbackClock = core.Clock()
respond = ''
Timeout = 0
total_score = 0
avg_acc = 1
acc_hist = []
soundfile = ''
pelletPath = 'c:/pellet.exe'

def pellet(num = 1):
    """Dispense [num] pellets. Prints 'Pellet' if `pellet.exe` is not found (for
       development). Waits 500ms between pellets."""
    for i in range(num):
        if os.path.isfile(pelletPath):
            os.system(pelletPath)

 


win1 = visual.TextStim(win=win, name='win1',
    text='',
    font='Open Sans',
    pos=(0, 0), height=0.1, wrapWidth=None, ori=0.0, 
    color='white', colorSpace='rgb', opacity=None, 
    languageStyle='LTR',
    depth=-1.0);
lose1 = visual.TextStim(win=win, name='lose1',
    text='',
    font='Open Sans',
    pos=(0, 0), height=0.1, wrapWidth=None, ori=0.0, 
    color='white', colorSpace='rgb', opacity=None, 
    languageStyle='LTR',
    depth=-2.0);
sound_feedback = sound.Sound('A', secs=-1, stereo=True, hamming=True,
    name='sound_feedback')
sound_feedback.setVolume(1.0)

# Create some handy timers
globalClock = core.Clock()  # to track the time since experiment started
routineTimer = core.CountdownTimer()  # to track time remaining of each (non-slip) routine 

OK, this will be a bit hard to debug if the error is not reproducible. The code you provided that initiates the sound seems OK, and works when used in isolation.

Sometimes errors like this arise though if different sorts of sounds are being used in an experiment (e.g. one sound file might have a different sampling frequency than another, or one might be in stereo vs mono). The sound system gets initialised to work with the characteristics of one file and can then get tripped up if it needs to work with something else. So you might want to check some of your sound files (i.e. those specified in your soundfile variable in your conditions file, using software like Audacity to check the file characteristics, such as sampling rate and stereo). Audacity itself can be used to re-sample files to be consistent (e.g. set them all to be 44.1 kHz or whatever).

But perhaps most importantly, first you should probably check the sound backend that you are using in your PsychoPy settings (this might be one of the differences between your computer and the one in the lab). Make sure they agree, and perhaps ideally select ptb (for PsychToolbox). PsychToolbox is more modern and reliable and has better performance than the pyo backend you are currently using. In the toolbar, click the “experiment settings” icon and under the audio library option, select ptb. If it is currently set to be “use preferences”, that might account for differences between your two computers. Once you’ve tested that, you might also want to try toggling the “Force stereo” setting and see if that makes a difference.

1 Like

Don’t worry about that - the photo above gave all the detail needed. But just for future reference, it can be easier to work with text (it saves slow typists like me needing to re-type bits of it into an answer, for example). And if anyone else in the future comes across a similar error, they will be able to find this topic (and hopefully an answer) via a Google search, which won’t happen if it is locked inside an image.

1 Like

So I played around with the audio settings and when I changed it to PTB it worked! The pellet.exe is working as well! Thanks for the tip about text vs photos, I’ll be sure to copy and paste it to a .txt file next time!

1 Like