Skip to content

Commit

Permalink
Fixed issue with inconsistent click sound (now using psychopy instead…
Browse files Browse the repository at this point in the history
… of playsound). Updated PySide to version 6.6.2.
  • Loading branch information
Philipp committed Mar 6, 2024
1 parent 69237c6 commit aaaed61
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The app is started via `python start.py`.
You can compile an executable/application for windows/mac via `pyinstaller start_win.spec` or `pyinstaller start_mac.spec`.

# License
Copyright 2023 Dr.-Ing. Philipp Bulling
Copyright 2024 Dr.-Ing. Philipp Bulling

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
Binary file removed raw/clickhi.wav
Binary file not shown.
Binary file removed raw/clicklo.wav
Binary file not shown.
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Adafruit-PlatformDetect==3.38.0
Adafruit-PureIO==1.1.9
hidapi==0.12.0.post2
numpy
playsound==1.2.2
psychopy
PyObjC; sys_platform == 'darwin'
platformdirs
pyinstaller
pyqtgraph
PySide6==6.4.2
PySide6==6.6.2
4 changes: 2 additions & 2 deletions ui/About.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def __init__(self):
linkLabel.setOpenExternalLinks(True)

licenseLabel = QLabel("The graphical user interface was built with PySide6 and PyQtGraph. The Circuit Pyhton driver \"cedargrove_nau7802\" is used to interface the NAU7802 ADC.\n\n"
"If you encounter any issues, please reach out to [email protected].")
"If you encounter any issues, please reach out to [email protected].")
licenseLabel.setWordWrap(True)

nameLabel = QLabel("Copyright 2023 Dr.-Ing. Philipp Bulling")
nameLabel = QLabel("Copyright 2024 Dr.-Ing. Philipp Bulling")
versionLabel = QLabel("Software Version: " + Params.version.value)

layout.addWidget(aboutLabel)
Expand Down
59 changes: 25 additions & 34 deletions ui/MeasurementCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
from PySide6.QtWidgets import QWidget
from PySide6.QtGui import QAction
import pyqtgraph as pg
from playsound import playsound

from psychopy import prefs
prefs.hardware['audioLib'] = ['ptb']
from psychopy import sound
from psychopy import logging
logging.console.setLevel(logging.ERROR)

import numpy as np
import time, datetime

from threading import Thread
from threading import Event

from ui.MeasurementGui import Ui_Form
from util.repeatedTimer import RepeatedTimer
from util.params import Params
Expand Down Expand Up @@ -117,34 +120,16 @@ def __init__(self, weightSensor):
#========================================
# Audio handling
#========================================
self.stopAudioThreadEvent = Event()
#######################################################################
# TODO: Do it like this, as soon as PySide6.5.0 is available:
#self.soundHi = QSoundEffect(parent) # parent = MainWindow
#self.soundHi.setSource(QUrl.fromLocalFile(Params.fileClickHi.value))
#self.soundHi.setVolume(1.0)
#######################################################################
self.playSndHiEvent = Event()
self.playSndLoEvent = Event()
self.clickHi = sound.Sound(value='E', secs=0.1, octave=5)
self.clickLo = sound.Sound(value='C', secs=0.1, octave=5)

# Start a timer that is only used for the tare display
self.tareTimer = RepeatedTimer(1/self.fsMeas, self.onTareVisualization)

#========================================
# Callback functions
#========================================
def onAudioPlayback(self, stopEvent, playHi, playLo):
while(True):
if stopEvent.is_set():
stopEvent.clear()
break
if playHi.is_set():
playHi.clear()
playsound(Params.fileClickHi.value)
if playLo.is_set():
playLo.clear()
playsound(Params.fileClickLo.value)
time.sleep(0.001) # Necessary to reduce priority of this thread?!
##################################################
# Only for debugging resons.
self.OLD_TIME = time.time()
##################################################

def onStartMeasurement(self):
if not self.running:
Expand All @@ -156,16 +141,13 @@ def onStartMeasurement(self):
self.weightSpinBox.setEnabled(False)
self.tareButton.setEnabled(False)

self.audioThread = Thread(target=self.onAudioPlayback, args=(self.stopAudioThreadEvent, self.playSndHiEvent, self.playSndLoEvent))
self.audioThread.start()
self.measurementTimer = RepeatedTimer(1/self.fsMeas, self.onMeasurementCallback)

self.running = True

def onStopMeasurement(self):
if self.running:
self.measurementTimer.stop()
self.stopAudioThreadEvent.set()
self.audioThread.join()
self.running = False

self.measCnt = 0
Expand Down Expand Up @@ -212,10 +194,19 @@ def onMeasurementCallback(self):

# Set the events for the click-playback
if secCnt > 0:
self.clickHi.stop()
self.clickLo.stop()
if (self.lookupTable[secCnt] > self.lookupTable[secCnt-1]):
self.playSndHiEvent.set()
self.clickHi.play()
elif (self.lookupTable[secCnt] < self.lookupTable[secCnt-1]) or (self.countdown[secCnt] < 4 and self.lookupTable[secCnt] == 0):
self.playSndLoEvent.set()
self.clickLo.play()

##################################################
# Only for debugging reasons: Print elapsed time.
NEW_TIME = time.time()
print("Elapsed time: " + str(round(NEW_TIME - self.OLD_TIME, 2)) + " s")
self.OLD_TIME = NEW_TIME
##################################################

# Increase the repetition- and set counter
if self.countdown[secCnt] == 1 and self.lookupTable[secCnt] == 0:
Expand Down
4 changes: 1 addition & 3 deletions util/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ class Params(Enum):
logFile = os.path.join(datadir, 'debug.log')

workoutCfgFile = os.path.join(basedir, 'settings/workouts.csv')
fileClickHi = os.path.join(basedir, 'raw/clickhi.wav')
fileClickLo = os.path.join(basedir, 'raw/clicklo.wav')
appIcon = os.path.join(basedir, 'raw/icon.ico')
appName = APPNAME
appAuthor = APPAUTHOR
version = '1.2.1'
version = '1.3.0'

0 comments on commit aaaed61

Please sign in to comment.