Skip to content

Commit

Permalink
BF: Handle when voicekey component isn't found (i.e. pre-2025 PsychoPy)
Browse files Browse the repository at this point in the history
  • Loading branch information
TEParsons committed Sep 30, 2024
1 parent 85a05b0 commit acb0394
Showing 1 changed file with 61 additions and 57 deletions.
118 changes: 61 additions & 57 deletions psychopy_cedrus/components/riponda.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from packaging.version import Version
from psychopy import __version__ as ppyVersion
from psychopy.experiment.components.buttonBox import ButtonBoxComponent
from psychopy.experiment.components.voicekey import VoiceKeyComponent
from psychopy.experiment.routines.photodiodeValidator import PhotodiodeValidatorRoutine
from psychopy.experiment import getInitVals, Param
from psychopy.experiment.plugins import DeviceBackend
Expand Down Expand Up @@ -131,67 +132,70 @@ def writeDeviceCode(self, buff):
)
buff.writeOnceIndentedLines(code % inits)

if Version(ppyVersion) >= "2025.1.0":
# base voicekey support was only added in 2025.1, so this is necessary to handle older versions
from psychopy.experiment.components.voicekey import VoiceKeyComponent

class RipondaVoiceKeyBackend(DeviceBackend):
"""
Implements Riponda for the VoiceKey Component
"""

key = "riponda"
label = _translate("Cedrus Riponda")
component = VoiceKeyComponent
deviceClasses = ['psychopy.hardware.voicekey.MicrophoneVoiceKeyEmulator']
class RipondaVoiceKeyBackend(DeviceBackend):
"""
Implements Riponda for the VoiceKey Component
"""

def getParams(self: VoiceKeyComponent):
# define order
order = [
"ripondaIndex",
"ripondaChannels",
"ripondaThreshold",
]
# define params
params = {}
params['ripondaIndex'] = Param(
0, valType='int', inputType="single", categ='Device',
label=_translate("Device number"),
hint=_translate(
"Device number, if you have multiple devices which one do you want (0, 1, 2...)"
key = "riponda"
label = _translate("Cedrus Riponda")
component = VoiceKeyComponent
deviceClasses = ['psychopy.hardware.voicekey.MicrophoneVoiceKeyEmulator']

def getParams(self: VoiceKeyComponent):
# define order
order = [
"ripondaIndex",
"ripondaChannels",
"ripondaThreshold",
]
# define params
params = {}
params['ripondaIndex'] = Param(
0, valType='int', inputType="single", categ='Device',
label=_translate("Device number"),
hint=_translate(
"Device number, if you have multiple devices which one do you want (0, 1, 2...)"
)
)
)
params['ripondaChannels'] = Param(
7, valType="code", inputType="single", categ="Device",
label=_translate("Num. channels"),
hint=_translate(
"How many microphones are plugged into this Riponda?"
params['ripondaChannels'] = Param(
7, valType="code", inputType="single", categ="Device",
label=_translate("Num. channels"),
hint=_translate(
"How many microphones are plugged into this Riponda?"
)
)
)
params['ripondaThreshold'] = Param(
0, valType='code', inputType="single", categ='Device',
label=_translate("Threshold"),
hint=_translate(
"Threshold volume (0 for min, 255 for max) above which to register a voicekey "
"response"
params['ripondaThreshold'] = Param(
0, valType='code', inputType="single", categ='Device',
label=_translate("Threshold"),
hint=_translate(
"Threshold volume (0 for min, 255 for max) above which to register a voicekey "
"response"
)
)
)

return params, order
return params, order

def addRequirements(self):
self.exp.requireImport(
importName="riponda", importFrom="psychopy_cedrus"
)
def addRequirements(self):
self.exp.requireImport(
importName="riponda", importFrom="psychopy_cedrus"
)

def writeDeviceCode(self: VoiceKeyComponent, buff):
# get inits
inits = getInitVals(self.params)
# make ButtonGroup object
code = (
"deviceManager.addDevice(\n"
" deviceClass='psychopy_cedrus.riponda.RipondaVoiceKeyGroup',\n"
" deviceName=%(deviceLabel)s,\n"
" pad=%(ripondaIndex)s,\n"
" channels=%(ripondaChannels)s\n"
" threshold=%(ripondaThreshold)s,\n"
")\n"
)
buff.writeOnceIndentedLines(code % inits)
def writeDeviceCode(self: VoiceKeyComponent, buff):
# get inits
inits = getInitVals(self.params)
# make ButtonGroup object
code = (
"deviceManager.addDevice(\n"
" deviceClass='psychopy_cedrus.riponda.RipondaVoiceKeyGroup',\n"
" deviceName=%(deviceLabel)s,\n"
" pad=%(ripondaIndex)s,\n"
" channels=%(ripondaChannels)s\n"
" threshold=%(ripondaThreshold)s,\n"
")\n"
)
buff.writeOnceIndentedLines(code % inits)

0 comments on commit acb0394

Please sign in to comment.