multichannel audio output (coder)

1,877 views
Skip to first unread message

Christopher Berger

unread,
Mar 17, 2014, 1:21:34 PM3/17/14
to psychop...@googlegroups.com
I am curious if there is any way to get psychopy to play mono .wav files independently from multiple audio channels (i.e., more than your usual left-right outputs). This is for an auditory perception experiment in which I would like to play a sound from one speaker at a time from up to 8 speakers located in different locations.  I'm using an external recording interface (PreSonus-AudioBox 1818) that has 8 (mono) output channels connected via usb to my iMac (OS x 10.9) for the output. 




Jeremy Gray

unread,
Mar 17, 2014, 1:39:20 PM3/17/14
to psychop...@googlegroups.com
In theory this is very possible, but you'll have to do some coding. The regular PsychoPy sound does not give you control over the speaker, but the underlying sound library the PsychoPy uses can give you that control. So you'd have to explore class psychopy.sound.SoundPyo, and see what needs to be changed. You want to be able to tell pyo to use a particular output channel. You'll have to have some familiarity with pyo, in particular pyo.SndTable() when reading from a file, or pyo.DataTable() when reading data from an array. . 

--Jeremy


--Jeremy


On Mon, Mar 17, 2014 at 1:21 PM, Christopher Berger <christophe...@gmail.com> wrote:
I am curious if there is any way to get psychopy to play mono .wav files independently from multiple audio channels (i.e., more than your usual left-right outputs). This is for an auditory perception experiment in which I would like to play a sound from one speaker at a time from up to 8 speakers located in different locations.  I'm using an external recording interface (PreSonus-AudioBox 1818) that has 8 (mono) output channels connected via usb to my iMac (OS x 10.9) for the output. 




--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/0bc1db74-0971-4630-b83b-d66f4325d1ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeremy Gray

unread,
Mar 17, 2014, 1:42:14 PM3/17/14
to psychop...@googlegroups.com
ps. Here's the pyo documentation: http://www.iact.umontreal.ca/pyo/manual/

--Jeremy

Christopher Berger

unread,
Mar 18, 2014, 7:07:32 AM3/18/14
to psychop...@googlegroups.com
Thank you very much for your reply Jeremy, 

While I was waiting for a reply I did find a python-based solution using pyaudio (http://people.csail.mit.edu/hubert/pyaudio/). I can get play a .wav file to all 8 channels by easily manipulating the channel_map in this library. Attached is a script and a .wav file that will play from channel 1 (but with arguments given to all other 7 channels to be quiet) if you copy both files to the desktop, open terminal, cd to the desktop, and type "python test2.py _1.wav" (then press enter). The problem with this solution is that PsychoPy doesn't recognize pyaudio--I get the error:"ImportError: No module named pyaudio" when I try to run this script in PsychoPy. 

Because this is a pretty straight forward and flexible way way of manipulating the output arguments for audio, I was curious if there is any way to implement this pyaudio module in PsychoPy?

In the meantime, I will look into the  psychopy.sound.SoundPyo class and see if I can get the same solution-->Thank you very much for this!

Best,
Chris
test2.py
_1.wav

Christopher Berger

unread,
Mar 18, 2014, 11:51:33 AM3/18/14
to psychop...@googlegroups.com
So, I actually figured this out! 

I was able to use the the function sys.path.append() at the beginning of the script (attached to the previous message) to get PsychoPy to recognize the pyaudio module.  So it looked like this for me: sys.path.append('/Library/python/2.7/site-packages/')
I also had to do the same for the wave module which looked like this for me: sys.path.append('/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7')

I thought I'd mention it in case it might also help future PsychoPy users looking to use send audio output to multiple channels. 

Best, 
Chris

Andrew Collegio

unread,
Oct 13, 2015, 3:50:05 PM10/13/15
to psychopy-users
Hi Chris, 

Thanks for posting this, and sorry to revive an old post. I too am trying to send audio output to multiple channels, and I've been trying to use your tips to do so with PyAudio. I don't have much coding background, however. Do you have any other tips, or perhaps other sample code in which you've used multiple channels for audio output with PsychoPy? Is the solution as simple as manipulating channel maps within PsychoPy once I have downloaded PyAudio, or will I need to make any changes within PyAudio itself? In the experiment I am programming, I have two sounds, each of which will be played randomly through one of 4 different speakers, each of which will be at a different location. Only one sound will be played at a time, though there will be many trials. I'll also be using an external multichannel sound card. 

Thanks, 

Andrew

Christopher Berger

unread,
Oct 14, 2015, 9:56:13 AM10/14/15
to psychop...@googlegroups.com
Hi Andrew, 

Yes, it’s basically as simple as manipulating the channel map. I don’t have a good working script I can send you at the moment but I  copy and pasted the important bits from a much larger experiment script I am using that you’ll need in your code to execute this successfully. For the example below the audio will play a file called 'sound1.wav' from the first (of your 4) channel and be silent in the rest: 

###

import sys
sys.path.append('/Library/python/2.7/site-packages/')
sys.path.append('/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7’) #this three lines are critical to get Psychopy to recognize pyaudio
import pyaudio #import pyAudio
import wave #to play  .wav files
from psychopy import core, sound #might be useful

PyAudio = pyaudio.PyAudio
cm=(0, -1, -1, -1) #the channel map for the audio output. You probably want to set this to some variable you call from an excel file in your experiment loop. 
wf = wave.open(sound1.wav) #sets the sound you want to play from file. You probably want to set this to some variable you call from an excel file in your experiment loop too so you can select different sounds on a trial-by-tiral basis. 
p = PyAudio() #just re-initializes pyAudio  

stream_info = pyaudio.PaMacCoreStreamInfo(
                    flags=pyaudio.PaMacCoreStreamInfo.paMacCorePlayNice, 
                    channel_map=(cm)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), 
                    channels=wf.getnchannels(),
                    rate=wf.getframerate(),
                    output=True,
                    output_host_api_specific_stream_info=stream_info,
                    stream_callback=callback)

#then you can call the sound during your experiment to start playing by using:

stream.start_stream()

#and to stop using: 

stream.stop_stream()

#and incase you want to close it out so it doesn’t clog other events: 

stream.close()

###

Hope this helps. 

Oh, also for a working example just to get a feel for how this works, you can add these three lines to the beginning the script I posted earlier in this thread and it should work if you follow my instructions in that thread. 

import sys
sys.path.append('/Library/python/2.7/site-packages/')
sys.path.append('/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7’) #this three lines are critical to get Psychopy to recognize pyaudio

Sorry for the stream of consciousness response. Let me know if anything is unclear and maybe I can elaborate a bit when I have a bit more time. 

Best, 
Chris
--

Christopher C. Berger, M.A.
Doctoral Student 
Department of Neuroscience 
Karolinska Institutet
Retzius väg 8, 171 77 Stockholm, Sweden
 
email: christophe...@ki.se
office phone: (+46) 08 524 87 989 
website: http://130.237.111.254/ehrsson/Berger_Website/index.html
Brain, Body & Self Laboratory: http://www.ehrssonlab.se/index.php





You received this message because you are subscribed to a topic in the Google Groups "psychopy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psychopy-users/6iao-XbWxag/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psychopy-user...@googlegroups.com.

To post to this group, send email to psychop...@googlegroups.com.

Andrew Collegio

unread,
Oct 21, 2015, 4:08:56 PM10/21/15
to psychopy-users
Hi Chris,

Thanks for the response. Still attempting to get this to work. I've been reading some more of the documentation on the PyAudio website. Is the ability to specify a channel map a Mac-specific function? I'm using a PC, and it seems the only way (at least in their documentation) to call the 'cm' variable is using the host-specific class 'PaMacCoreStreamInfo()'. Psychopy is recognizing PyAudio, but the only trouble I'm having is the channel map selection. Perhaps I will have to make the switch to a Mac, though that is not preferred. 

Thanks, 

Andrew
Reply all
Reply to author
Forward
0 new messages