From acb0394b7853e91c1c9807fb60a4eacfa222adcf Mon Sep 17 00:00:00 2001 From: TEParsons Date: Mon, 30 Sep 2024 11:01:53 +0100 Subject: [PATCH] BF: Handle when voicekey component isn't found (i.e. pre-2025 PsychoPy) --- psychopy_cedrus/components/riponda.py | 118 +++++++++++++------------- 1 file changed, 61 insertions(+), 57 deletions(-) diff --git a/psychopy_cedrus/components/riponda.py b/psychopy_cedrus/components/riponda.py index 7befa58..b30fbf5 100644 --- a/psychopy_cedrus/components/riponda.py +++ b/psychopy_cedrus/components/riponda.py @@ -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 @@ -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)