Skip to content

Commit

Permalink
Merge pull request psychopy#6941 from TEParsons/dev-enh-launch-speed
Browse files Browse the repository at this point in the history
ENH: Improvements to app launch speed
  • Loading branch information
TEParsons authored Oct 28, 2024
2 parents 05c493f + fa9b973 commit 1843d55
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 41 deletions.
27 changes: 9 additions & 18 deletions psychopy/experiment/components/camera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,9 @@
from psychopy.experiment.components import (
BaseComponent, BaseDeviceComponent, Param, _translate, getInitVals
)
from psychopy.sound.audiodevice import sampleRateQualityLevels
from psychopy.tools import stringtools as st, systemtools as syst, audiotools as at


_hasPTB = True
try:
import psychtoolbox.audio as audio
except (ImportError, ModuleNotFoundError):
logging.warning(
"The 'psychtoolbox' library cannot be loaded but is required for audio "
"capture (use `pip install psychtoolbox` to get it). Microphone "
"recording will be unavailable this session. Note that opening a "
"microphone stream will raise an error.")
_hasPTB = False
# Get list of sample rates
micSampleRates = {r[1]: r[0] for r in sampleRateQualityLevels.values()}


class CameraComponent(BaseDeviceComponent):
"""
This component provides a way to use the webcam to record participants during an experiment.
Expand Down Expand Up @@ -370,11 +355,16 @@ def getMicDeviceNames():
hint=msg,
label=_translate("Channels"))

def getSampleRates():
return [r[0] for r in at.sampleRateQualityLevels.values()]
def getSampleRateLabels():
return [r[1] for r in at.sampleRateQualityLevels.values()]
msg = _translate(
"How many samples per second (Hz) to record at")
self.params['micSampleRate'] = Param(
sampleRate, valType='num', inputType="choice", categ='Audio',
allowedVals=list(micSampleRates),
allowedVals=getSampleRates,
allowedLabels=getSampleRateLabels,
hint=msg, direct=False,
label=_translate("Sample rate (hz)"))

Expand Down Expand Up @@ -417,8 +407,9 @@ def writeDeviceCode(self, buff):
inits = getInitVals(self.params)
self.setupMicNameInInits(inits)
# --- setup mic ---
# substitute sample rate value for numeric equivalent
inits['micSampleRate'] = micSampleRates[inits['micSampleRate'].val]
# make sure mic sample rate is numeric
if inits['micSampleRate'].val in at.sampleRateLabels:
inits['micSampleRate'].val = at.sampleRateLabels[inits['micSampleRate'].val]
# substitute channel value for numeric equivalent
inits['micChannels'] = {'mono': 1, 'stereo': 2, 'auto': None}[self.params['micChannels'].val]
# initialise mic device
Expand Down
6 changes: 5 additions & 1 deletion psychopy/experiment/components/eyetracker_record/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def __init__(self, exp, parentName, name='etRecord',
self.type = 'EyetrackerRecord'
self.url = "https://www.psychopy.org/builder/components/eyetracker.html"
self.exp.requirePsychopyLibs(['iohub', 'hardware'])
self.exp.requireImport(
importName="EyetrackerControl",
importFrom="psychopy.hardware.eyetracker"
)

self.params['actionType'] = Param(
actionType,
Expand Down Expand Up @@ -113,7 +117,7 @@ def writeInitCode(self, buff):
inits = self.params
# Make a controller object
code = (
"%(name)s = hardware.eyetracker.EyetrackerControl(\n"
"%(name)s = EyetrackerControl(\n"
)
buff.writeIndentedLines(code % inits)
buff.setIndentLevel(1, relative=True)
Expand Down
32 changes: 13 additions & 19 deletions psychopy/experiment/components/microphone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@
from psychopy.experiment.components import (
BaseComponent, BaseDeviceComponent, Param, getInitVals, _translate
)
from psychopy.tools.audiotools import sampleRateQualityLevels

_hasPTB = True
try:
import psychtoolbox.audio as audio
except (ImportError, ModuleNotFoundError):
logging.warning(
"The 'psychtoolbox' library cannot be loaded but is required for audio "
"capture (use `pip install psychtoolbox` to get it). Microphone "
"recording will be unavailable this session. Note that opening a "
"microphone stream will raise an error.")
_hasPTB = False

# Get list of sample rates
sampleRates = {r[1]: r[0] for r in sampleRateQualityLevels.values()}


class MicrophoneComponent(BaseDeviceComponent):
Expand Down Expand Up @@ -136,9 +121,15 @@ def getDeviceNames():
"many channels as the selected device allows."
)
)

def getSampleRates():
return [r[0] for r in at.sampleRateQualityLevels.values()]
def getSampleRateLabels():
return [r[1] for r in at.sampleRateQualityLevels.values()]
self.params['sampleRate'] = Param(
sampleRate, valType='num', inputType="choice", categ='Device',
allowedVals=list(sampleRates),
allowedVals=getSampleRates,
allowedLabels=getSampleRateLabels,
label=_translate("Sample rate (hz)"),
hint=_translate(
"How many samples per second (Hz) to record at"
Expand Down Expand Up @@ -323,8 +314,9 @@ def writeDeviceCode(self, buff):
inits = getInitVals(self.params)

# --- setup mic ---
# Substitute sample rate value for numeric equivalent
inits['sampleRate'] = sampleRates[inits['sampleRate'].val]
# make sure sample rate is numeric
if inits['sampleRate'].val in at.sampleRateLabels:
inits['sampleRate'].val = at.sampleRateLabels[inits['sampleRate'].val]
# Substitute channel value for numeric equivalent
inits['channels'] = {'mono': 1, 'stereo': 2, 'auto': None}[self.params['channels'].val]
# initialise mic device
Expand Down Expand Up @@ -417,7 +409,9 @@ def writeInitCode(self, buff):

def writeInitCodeJS(self, buff):
inits = getInitVals(self.params)
inits['sampleRate'] = sampleRates[inits['sampleRate'].val]
# make sure sample rate is numeric
if inits['sampleRate'].val in at.sampleRateLabels:
inits['sampleRate'].val = at.sampleRateLabels[inits['sampleRate'].val]
# Alert user if non-default value is selected for device
if inits['device'].val != 'default':
alert(5055, strFields={'name': inits['name'].val})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def __init__(self, exp, name='calibration',
self.url = "https://psychopy.org/builder/components/eyetracker_calibration.html"

self.exp.requirePsychopyLibs(['iohub', 'hardware'])
self.exp.requireImport(
importName="EyetrackerControl",
importFrom="psychopy.hardware.eyetracker"
)

# Basic params
self.order += [
Expand Down Expand Up @@ -252,7 +256,7 @@ def writeMainCode(self, buff):
# Make config object
code = (
"# define parameters for %(name)s\n"
"%(name)s = hardware.eyetracker.EyetrackerCalibration(win, \n"
"%(name)s = EyetrackerCalibration(win, \n"
)
buff.writeIndentedLines(code % inits)
buff.setIndentLevel(1, relative=True)
Expand Down
1 change: 0 additions & 1 deletion psychopy/hardware/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import glob
from itertools import chain
from psychopy import logging
from . import eyetracker, listener
from .manager import DeviceManager, deviceManager
from .base import BaseDevice, BaseResponse, BaseResponseDevice

Expand Down
3 changes: 2 additions & 1 deletion psychopy/monitors/calibTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Distributed under the terms of the GNU General Public License (GPL).

from .calibData import wavelength_5nm, juddVosXYZ1976_5nm, cones_SmithPokorny
from psychopy import __version__, logging, hardware
from psychopy import __version__, logging

try:
import serial
Expand Down Expand Up @@ -1116,6 +1116,7 @@ def getRGBspectra(stimSize=0.3, winSize=(800, 600), photometer='COM1'):
photom = photometer
else:
# setup photom
from psychopy import hardware
photom = hardware.Photometer(photometer)
if photom != None:
havephotom = 1
Expand Down
3 changes: 3 additions & 0 deletions psychopy/tools/audiotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
4: (SAMPLE_RATE_96kHz, 'High-Def (96kHz)'),
5: (SAMPLE_RATE_192kHz, 'Ultra High-Def (192kHz)')
}
sampleRateLabels = {
r[1]: r[0] for r in sampleRateQualityLevels.values()
}

# supported formats for loading and saving audio samples to file
try:
Expand Down

0 comments on commit 1843d55

Please sign in to comment.