sound latency issues under Windows 7

358 views
Skip to first unread message

piot

unread,
May 20, 2016, 10:53:52 AM5/20/16
to psychopy-users
Hello,

We get sound latency of 200 ms on the standalonve version of PsychoPy (1.83.04) on Windows 7 in our lab:

    microphone latency  0.2000 s    Warning: "Microsoft Sound Mapper - Input" latency > 10ms

We have measured it also by connecting the sound output to the sound input of another computer. The startle wav file is consistently played 200 ms after the parrallel port trigger.
When using Neurobs Presentation, the trigger is set perfectly at the same time as the sound. We have installed the latest sound drivers for our dedicated sound card, but it didn't change anything.

I'm using the following code:

startle_sound = sound.Sound("startle50ms.wav")

if is_pport:
    port = parallel.setPortAddress(address = pport_address)

def sendTrigger(code):
    '''sending trigger to parallel port'''
    port.setData(code)
    core.wait(0.01) # duration of trigger: 100ms
    parallel.setData(0) # disable trigger

startle_sound.play()
if is_pport: sendTrigger(code=st_port_code)

Could you give us some ideas, how to correct the latency issues? Is there a way to test sound latency under Windows, instead of running the benchmarks, which take quite some time...?

I have the same issue on my laptop (under Win 7), although the benchmarks don't show any latency issues under linux.

btw. I use setPortAddress, because I get a message to use it, when trying to set port = parallel.ParallelPort(addresse = pport_address); I had a look at the parallel.py code but didn't really figure out the reason, why ParallelPort doesn't work. Should I try to install other drivers?

Best wishes
Piotr

piot

unread,
May 20, 2016, 10:59:06 AM5/20/16
to psychopy-users
ah, I copied the wrong line, it should say:
 speakers latency    0.2000 s    Warning: "Microsoft Sound Mapper - Output" latency > 10ms

jonathan.peirce from smtp.nottingham

unread,
May 20, 2016, 12:13:17 PM5/20/16
to psychop...@googlegroups.com
And which driver are you using in the psychopy preferences?
--
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/4ea05a44-5b3e-4902-9106-fa2890a15ace%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


This message and any attachment are intended solely for the addressee
and may contain confidential information. If you have received this
message in error, please send it back to me, and immediately delete it. 

Please do not use, copy or disclose the information contained in this
message or in any attachment.  Any views or opinions expressed by the
author of this email do not necessarily reflect the views of the
University of Nottingham.

This message has been checked for viruses but the contents of an
attachment may still contain software viruses which could damage your
computer system, you are advised to perform your own checks. Email
communications with the University of Nottingham may be monitored as
permitted by UK legislation.

piot

unread,
May 20, 2016, 12:46:36 PM5/20/16
to psychopy-users
Hi Jon,

I think the  'pyo' driver was set, but I'm not 100% sure. We didn't have much time, because the next experiment was scheduled afterwards.

Since the hardware measurement showed the same latency as psychopy's benchmarks, I'm trying to play around with the sound settings on my laptop.

Do the chosen sound drivers in the psychopy preferences affect the benchmark wizard? I have deleted all drivers and the benchmarks were still performed and showed me a latency of ms = 200, whereas my experiment obviously wouldn't start, because of missing drivers...


best wishes

piotr

piot

unread,
May 20, 2016, 1:02:23 PM5/20/16
to psychopy-users
btw, is there some other script for testing only sound latency, as with the benchmarking wizard?

when running testSoundLatency.py I get:

##### Running: C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy-1.83.04-py2.7.egg\psychopy\demos\coder\hardware\testSoundLatency.py #####
pyo version 0.6.6 (uses single precision)
<class 'labjack.LabJackPython.LabJackException'>: Could not load labjackud driver. Ethernet connectivity availability only.

    The error was: [Error 126] The specified module could not be found
Using pyo(with Primary Sound Driver) for sounds
Traceback (most recent call last):
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy-1.83.04-py2.7.egg\psychopy\demos\coder\hardware\testSoundLatency.py", line 27, in <module>
    ports = u3.U3()
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\labjack\u3.py", line 81, in __init__
    self.open(**kargs)
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\labjack\u3.py", line 116, in open
    Device.open(self, 3, firstFound = firstFound, serial = serial, localId = localId, devNumber = devNumber, handleOnly = handleOnly, LJSocket = LJSocket )
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\labjack\LabJackPython.py", line 574, in open
    d = openLabJack(devType, ct, firstFound = True, handleOnly = handleOnly, LJSocket = LJSocket)
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\labjack\LabJackPython.py", line 1420, in openLabJack
    return _makeDeviceFromHandle(handle, deviceType)
  File "C:\Program Files (x86)\PsychoPy2\lib\site-packages\labjack\LabJackPython.py", line 1478, in _makeDeviceFromHandle
    raise e
labjack.LabJackPython.LabJackException: The device handle is None.

piot

unread,
May 20, 2016, 2:22:49 PM5/20/16
to psychopy-users
sorry Jon, I didn't read the description of the script, while being in a hurry;

I've found wizard.py and can play around with it to test different preferences

Jeremy Gray

unread,
May 20, 2016, 2:29:34 PM5/20/16
to psychop...@googlegroups.com
That is the right script, but unfortunately the latency reported there is not actually a measurement (just a value reported by one of the libraries, probably port audio), and we now suspect its not reliable or even informative. In fact, we've removing it from the wizard. So I would not trust those numbers.

--Jeremy

piot

unread,
May 20, 2016, 3:01:41 PM5/20/16
to psychopy-users
ouch...

so there are several possibilities, from what I've read like trying out pyo with 'ASIO' or using winsound or trying that pyaudiosound script...

Unfortunately our collaborators have no idea about using psychopy, so I'm not sure what do about it... In the worst case, I would probably have to go to their lab and test everything...

any other tips / ideas, Jeremy?

Jon Peirce

unread,
May 21, 2016, 2:56:56 PM5/21/16
to psychop...@googlegroups.com
So the 200ms you were reporting wasn't /measured/ ? I thought you said were comparing against a parallel port, but no?

As Jeremy says, ignore what software (or software manuals) reports for sound latencies. The only way to know the lag is to measure it with hardware (e.g. a scope and a microphone)

The errors you were reporting about labjack are because that script is designed to send pulses using a labjack U3 (which I then recorded and compared against a microphone on an oscilloscope).

Yes, the choice of audio backend (e.g. pyo) and the particular driver that the backend uses (e.g. ASIO or windows built-in drivers) are all likely to affect the lag.

Jon

For more options, visit https://groups.google.com/d/optout.

-- 
Jon Peirce
http://www.peirce.org.uk

piot

unread,
May 21, 2016, 6:45:22 PM5/21/16
to psychopy-users
We measured the latency with two computers. The software benchmarking showed the same latency (200ms), so I thought that the software measurement is accurate and that I could rely on it...

We will do some more hardware measurements with our lab manager on monday. I have prepared and tested scripts for different audio drivers under pyo (including asio4all), as well as winsound and pyaudio, so I hope that some of it will work with low latency..

I will post our results on Monday...

Is there a reasonable limit for setting the buffersize with pyo and/or asio4all?

piot

unread,
May 23, 2016, 12:52:18 PM5/23/16
to psychopy-users
Hello,

we have tested several libraries and drivers with the Creative Sound Blaster X-Fi soundcard on Windows 7 today. The results are, from my point of view, quite disappointing, especially when using pyo.

Here are the results (drivers 1 to 8 use pyo):

                   snd_drivers  mean         sd
                        (fctr) (dbl)      (dbl)
1       Microsoft_Sound_Mapper 520.2 24.6432321
2     Speaker_Creative_Sb_x-fi 523.8 29.3969008
3   SPDIF-OUT_Creative_Sb_x_fi    NA        NaN
4         Primary_Sound_Driver 514.3 29.1892294
5    Speaker_Creative_Sb_x-fi2 516.0 30.5395918
6  SPDIF-OUT_Creative_Sb_x_fi2    NA        NaN
7                     ASIO4ALL  56.5 10.2442396
8                Creative_Asio 221.6 45.6294252
9                     Winsound  24.0  4.6666667
10                     PyAudio 134.8  0.6324555
11                      Pygame 169.5 30.4055916

There is a latency of 220 ms when using Creative_Asio drivers. Only the ASIO4ALL drivers delived not too bad results (mean latency = 56.5 ms) for pyo, although a sd of 10 is quite bad.

I will check out different ASIO4ALL settings and do some more checks on winsound. PyAudio looks quite promising, because the sd is very low, so one could add a delay for setting the marker of 130 ms. Maybe it would be the most robust solution...

Compared to Presentation, the latency is very high :(. We get almost no latency, when using presentation with this sound card.
see also https://www.neurobs.com/menu_presentation/menu_hardware/sound_cards/test_results

We used the pyo version, which is installed with psychopy, but I might try it out with pyo 0.8

I would be very glad, if you could give me some advice, what we could do to reduce the sound latency...

We've used a similar script to the one, which is attached.

Best wishes
Piotr



Code hier eingeben...
test_latency.py

Richard Höchenberger

unread,
May 23, 2016, 3:56:31 PM5/23/16
to psychopy-users
Hi Piotr,

On Mon, May 23, 2016 at 6:52 PM, piot <piotr....@gmail.com> wrote:
> Here are the results (drivers 1 to 8 use pyo):

very nice work indeed!

I've said this several times on this list before, and one day somebody
is going to punch me for repeating it over an over again :) but I
would always recommend using data acquisition boards with analog
outputs instead of regular sound cards when it comes to psychophysics.
These boards are sometimes even cheaper than high-end sound cards, and
often work with latencies << 1 ms. We'd been using NI DAQ boards for
years to present very brief (9 ms) auditory and visual stimuli (using
a LED), and this always worked without the slightest issues. I later
ran a study employing longer auditory stimuli (McGurk paradigm) and
never had any latency issues (verified using a scope) either. Another
great thing about these DAQ boards is that, once you have one in the
lab, you can also use them to send triggers to pretty much any other
device, and finally ditch the parallel port ;)

But this is just my 2 ct :)

All the best,

Richard

Jon Peirce

unread,
May 23, 2016, 5:48:55 PM5/23/16
to psychop...@googlegroups.com
Thanks Piot,

I'm sorry (and amazed) to see that pyo is so slow on windows! I mostly work on Mac and linux and it isn't nearly as bad there. One user (I forget who) even wrote to say he was getting <ms audio timing on ubuntu realtime kernel. Possibly the portaudio isn't well optimised for windows and that tends to be what's used as a cross-platform option.

I'm afraid we haven't written any of this and rely on the providers of the packages (my real expertise is vision not audio). Maybe Olivier Bélanger, who wrote pyo, would know how to optimise it better on windows (although I think he also focuses on mac mostly).

Sorry it isn't "just working" for you. Actually, your results so far suggest maybe we should switch psychopy on win32 to use winsound instead!

best wishes
Jon

For more options, visit https://groups.google.com/d/optout.

Richard Höchenberger

unread,
May 23, 2016, 6:45:29 PM5/23/16
to psychopy-users
On Mon, May 23, 2016 at 11:48 PM, Jon Peirce <jon.p...@gmail.com> wrote:
> Actually, your results so far suggest maybe we should switch psychopy on
> win32 to use winsound instead!

The winsound performance is even more amazing considering that the
sound file wasn't pre-loaded, but instead 'played directly from disk',
if I read the code correctly! Very interesting.

Best,

Richard

piot

unread,
May 24, 2016, 9:49:00 AM5/24/16
to psychopy-users
Jon,

I'm not really a fan of Windows, but we have currently only windows machines in our psychophysiology lab. I've thought for quite some time now about using an audio oriented linux distribution or configuring it according to this tutorial: https://wiki.archlinux.org/index.php/Professional_audio. The low latency on real time kernels, which you have mentioned sounds interesting...

Do you have experience in running psychopy from RAM using a linux distro booting from an usb stick? Our lab is very busy right now, so this may be a good thing to try first...

The sds of Winsound are actually better, then I've reported. There was an outlier in the data and I think that it was based on an incorrect identification of sound onset in BrainVisionAnalyzer. Maybe we have overlooked it, when inspecting the data visually. I didn't look at it again, because I was planning to do some additional tests yesterday night (some of them with pyo 0.8). I will post the results, when I find some time to look at the new data. The data of asia4all (with buffersize reduced to a minimum of 64 and some other options) looked very nice, while recording. I couldn't really see a difference between sound onset and marker, while looking at it, so I'm quite curious about looking at the data more closely.

Thanks for the tip about data acquisition boards, Richard!

All the best
Piotr

piot

unread,
Jun 3, 2016, 7:45:53 PM6/3/16
to psychopy-users
Hello,

Olivier was so kind to compile pyo with Windows WDM-KS. I've tried it out under a standard python installation on Windows. Unfortunately it doesn't work with the standalone version of PsychoPy 1.83.4, when I copy the files to the site-packages folder. Instead of the Windows WDM-KS host api I get:

 index: 3, id: 0, name: skeleton implementation, num devices: 0, default in: -1, default out: -1

If I install pyo into the psychopy folder I have the same problem...

I get this behavior only when using the standalone version of psychopy. I don't get it, when using the Enthought distribution, but my experiment doesn't work at all, when I use Enthought Canopy, so I have to stick to the standalone version.

Any ideas?

Piotr

piot

unread,
Jun 3, 2016, 7:58:45 PM6/3/16
to psychopy-users
oh, and sound just stopped working on my laptop under psychopy. I've uninstalled all other versions of python, removed all python entries from the registry and reinstalled psychopy standalone (1.83.4) several times and there is still no sound. I've no idea what else I could do... I use another pc meanwhile... 

piot

unread,
Jun 6, 2016, 7:28:20 PM6/6/16
to psychopy-users
Ok, so I've tried out pyo with wdmks. It's promising, the latency was around 30ms, so a lot better than with all the other drivers, which I've tried before. I've used a buffer size of 32 with the following code:

import pyo
#pyo.pa_list_host_apis()
#pyo.pa_list_devices()
s=pyo.Server(buffersize=32)
s.setInputDevice(12)
s.setOutputDevice(11)
s.boot()
s.start()
snd = "startle50ms.wav"
#sf = pyo.SfPlayer(snd, mul=.1).out()
t = pyo.SndTable(snd)
freq = t.getRate()
sound = pyo.TableRead(table=t, freq=freq, loop=False, mul=.2)

sound.out()

On the downside it didn't work with the sound module in psychopy, because it chose the wrong input driver. So I had to specify the sound input with pyo. Also there was an error after playing the files from time to time...

thanks to Richard's remark about winsound, I measured the sound latency, when playing the sound from RAM with winsound. The performance is remarkable, the sound onset is recorded before the port trigger according to BrainVision Recorder.

I didn't know, that it's possibe to play sound from ram with winsound, but the code is very simple:

import winsound
sound_file = "startle50ms.wav"
with open(sound_file) as f:
    snd_ = f.read()
winsound.PlaySound(snd_, winsound.SND_MEMORY)

the complete code is attached for anybody interested. I will stick to winsound for now. I'm very happy, that I've found an acceptable solution

cheers
piotr
winsound_ram.py

Álvaro Cabana

unread,
Jun 14, 2016, 6:17:19 PM6/14/16
to psychopy-users
Hi Richard, 

Could you point to some resources on how to use a NI-DAQ card to present audio stimuli? Is it straightforward using PyDAQmx? Could you show a code snippet?
Thank you very much.

Regards,
Álvaro

piot

unread,
Jun 28, 2016, 6:02:39 AM6/28/16
to psychopy-users
Dear Jon,

so, when I load the sound with winsound from memory and send a parallel port trigger right afterwards, there is a delay of 30 ms to 50 ms. When testing the whole experiment, the port trigger was arriving approx. 30-50 ms after the sound was played. So in this case, it's not a sound issue anymore. I have no idea, what to do about it. It's not a big deal for this experiment, but I would prefer to find a solution for these kinds of things, to be confident about writing other experiments, using sound and parallel port triggers in Python/Psychopy. I'm currently using inpOut32 on Windows 7.

Any ideas?

Best wishes
Piotr

Richard Höchenberger

unread,
Jun 28, 2016, 7:49:34 AM6/28/16
to psychopy-users, alma...@gmail.com
Dear Álvaro,

On Wed, Jun 15, 2016 at 12:17 AM, Álvaro Cabana <alma...@gmail.com> wrote:
Hi Richard, 

Could you point to some resources on how to use a NI-DAQ card to present audio stimuli? Is it straightforward using PyDAQmx? Could you show a code snippet?
Thank you very much.

sorry for my late reply, I completely missed your message!

I don't have any code at hand right now; but we use PyLibNIDAQmx at our lab. What you want to do is, essentially, load the audio file (I think we used scipy.io.wavfile.read for that purpose), initialize the analog output channel(s) with the exact same sampling rate as the audio file, and write the audio data to these output channels. PyLibNIDAQmx will also be included in the next standalone version of PsychoPy for Windows :) Please let me know if this already helps you, or if you need additional information or example code. I haven't worked with audio output in a while and therefore don't have any working examples readily available, but could possible put together a set of minimal code snippets to play a WAV file if required.

All the best, and good luck!

    Richard

Jonathan Peirce

unread,
Jun 28, 2016, 7:54:45 AM6/28/16
to psychop...@googlegroups.com
My guess is that winsound is waiting (blocking) until the sound has played but I don't know anything about how it works. I doubt that the parallel port command itself has a delay (you could test it against a screen onset with a photodiode to check) so I imagine the delay is in the return from winsound

Jon

For more options, visit https://groups.google.com/d/optout.

-- 
Jonathan Peirce
University of Nottingham

http://www.peirce.org.uk

piot

unread,
Jun 28, 2016, 4:05:28 PM6/28/16
to psychopy-users
thanks, Jon. the trigger comes in the middle phase of the sound most of the time, so it's probably not waiting until the sound has played. the sound has a duration of 50 ms and the trigger arrives at about 30 ms. I will check it out with a photodiode, when I have time.

piotr

piot

unread,
Jul 25, 2016, 8:01:57 AM7/25/16
to psychopy-users
Jon, do you think, that the problem might be caused by the use of setPortAddress (legacy function). I couldn't figure out, why ParallelPort didn't work on that computer..

btw the description of the ParallelPort function disappeared from the documentation...

Piotr


Am Dienstag, 28. Juni 2016 13:54:45 UTC+2 schrieb Jon:
Reply all
Reply to author
Forward
0 new messages