Page 1 of 1

Playing sounds in BCPy2000

Posted: 01 Feb 2023, 05:42
by abertomeu
Hi BCI2000 Community, I am here again :P .

I wanted to play a sound when specific cue appears on the screen. However, I don't known why my code did not work.
Here is my simple code:

Code: Select all

from BCPy2000.WavTools import PyAudioInterface

class BciApplication(BciGenericApplication):
	....
	
	def Preflight(self, sigprops):
		self.playerOnset = PyAudioInterface.player('./feedback-drums.WAV')
	
	def Transition(self, phase):
		if (pahse == 'cue'):
			self.playerOnset.play()
	...
I don't know if I am duing something wrong, can anyone help me?
Thanks in advance!

Re: Playing sounds in BCPy2000

Posted: 01 Feb 2023, 13:17
by nluczak
Hi again!
You are using the legacy method of playing audio which relies on pygame to play sounds. And, as we no longer use pygame in favor of psychopy, you should be using Psychopy.audio. Thank you for posting this as I now realize the change was not documented, I will make sure to update our tutorials on the wiki.
Here is an example of how you may want to do it:

Code: Select all

class BciApplication(BciGenericApplication):
	....
	
	def Initialize(self, indim, outdim):
		from psychopy import sound
		self.mySound = sound.Sound('A')                      # this will play an A note
		# self.mySound = sound.Sound(900)                 # this will play an 900Hz noise
		# self.mySound = sound.Sound('/path/to/file') # this will play a specified sound file
	
	def Transition(self, phase):
		if phase == 'cue':
			self.mySound.play()
	...
I would also look at the PsychoPy documentation for more details on how they implemented their audio engine.

Re: Playing sounds in BCPy2000

Posted: 02 Feb 2023, 04:04
by abertomeu
Thanks again for your kick response. It works :D