Re: ASIO-enabled version of psychportaudio dll.

Hi Simon,

i'm cc'ing this to the forum in case others want to learn about it,
comments interspersed.

On May 6, 2008, at 6:25 AM, Simon Overduin wrote:
>

> I'm using MATLAB R2007A on a new Lenovo Thinkpad T61 running
> Windows XP.
> I'm using a MOTU 828mkII ASIO soundcard. For one of my applications
> i need
> to have duplex audio input and output. The following code allows me
> to do
> so, using the MOTU channels appropriate for my configuration:
>
> InitializePsychSound(1);
> pahandle = PsychPortAudio('Open', [], 4+2+1, 3, 96000, [1 1], [],
> [], [0; 6]);
>

As a sidenote:

The 4 in the 4+2+1 mode parameter requests internal feedback mode.
Latency
will be minimized by bypassing Matlab, however you won't have control
over
latency, ie., if your experiment would include feeding back some
sound with
a controlled delay, you couldn't control for it, as the '4' just
requests the
shortest round-trip between input and output. For all "greater than
minimum"
delays you'd need to use the 2+1 setting and then control latency in the
way demonstrated in the BasicSoundFeedbackDemo.

You request a latencyclass of '3', which is "agressive low latency",
but this
just selects some presets for unspecified parameters. Windows+ASIO users
have finer control over latency by specifying the optional
'suggestesLatency'
parameter, which asks for a specific latency in seconds. Setting '3'
corresponds
to a suggestedLatency of 1 msec, which is quite aggressive and not
feasible on
lower end systems, although the ASIO driver is supposed to clamp the
request
to some safe value for the given sound hardware. A latencyclass of
'2' would
select a less aggressive suggestedLatency of 5 msecs which is more
feasible
on upper class consumer hardware....

> PsychPortAudio('Start', pahandle, 0, 0, 1);
> % stuff
> PsychPortAudio('Stop', pahandle);
> PsychPortAudio('Close');
>
> In case you have a chance to look at it, i have posted the PTB
> feedback
> i receive at:
>
> http://overduin.mit.edu/ptb/messages.txt
>
> The input and output latencies are reported to be ~11-12 ms, which
> agrees
> well with the total loop delay of ~23 that i consistently observe on a
> scope monitoring the input and output. I had expected i could
> achieve a
> smaller delay given my hardware and what i have read about PTB's
> abilities. Even a moderately smaller delay (15-20 ms) would be fine
> for my
> application.
>

All this doesn't apply to your MOTU card, because this line in the
output
you posted:

...snip...
PTB-DEBUG: PortAudio says: Revalidated buffer size constraints are
min 1024, max 1024, preferred 1024, granularity 0.
...snip...

means that for whatever weird reason that only the developers at Motu
could tell you, the driver of your MOTU card doesn't allow any
software application to programmatically control latency of the card
at runtime. Latency is internally controlled by varying/selecting the
size of a samplebuffer within some range. On your card the
constraints are minimum=1024 samples and maximum=1024 --> The
buffersize and thereby latency is fixed to roughly (1024 / 96000)
seconds, which corresponds to the observed 11-12 msecs for each data
stream.

However, according to this <http://www.motu.com/techsupport/technotes/
what-is-latency> technote at Motus website, you can somehow open some
device settings dialog on your machine, probably somewhere in the
system preference section of Windows, and there must be somewhere
some "Samples per buffer" slider or control that allows you to
manually set the buffersize for your card. Pretty inconvenient and
inflexible for a product that looks like its in the upper class
range, given that most 100$ cards can do this easily, but anyway...

So you can try to reduce the samplebuffer size via that control panel
and experiment, lowering the values until sound becomes unreliable
(audible artifacts, dropouts, stuttering etc.), how low you can go
depends on the quality of the rest of your hardware and operating
system, but they claim that values as low as 4 msecs are possible.
That would end you up with 4 ms input + 4ms output = 8 msecs total
latency for sound feedback.

USB connections are relative high latency by design, Firewire
connected external cards should allow lower latency and the best
choice is something connected via PCI/PCIExpress bus (CardBus or
PCMCIA or ExpressCard connector on Laptops) for lowest latency.


> I have been unable to confirm if my inability to achieve smaller
> delays is
> related to the subsequent critical warnings in which PTB informs me
> that
> high-precision timing is not available. My Lenovo laptop is indeed
> dual-core (Intel Mobile Core 2 Duo T7250), so it is possible, as
> the error
> messages suggest, that its TSC timecounter is not properly
> synchronized.
> While the PTB FAQ suggests that this brand of processor should not
> have
> TSC problems, after installing the recommended CPUz application i
> did find
> that the "Clocks (Core #0) Core Speed" fluctuated, e.g. between
> 1965 and
> 2000 (x10.0 Multplier) MHz, and occasionally above 2100.
>

The TSC's of Core2Duo should be synchronized at the hardware level
and invariant to cpu clock frequency changes -- they are used, e.g.,
in Intel based Macs, which is one reason why Apple hardware doesn't
suffer any of the clock problems that typical PC hardware does. Apart
from that, PTB locks all timing related code on Windows to one single
core to avoid such problems - another of many work-arounds that are
not needed on well designed operating systems like OS/X or Linux...
But depending on how recent your PTB 'beta' is, the reported clock
problems could be false positives which should not happen anymore
after an update to the latest beta. There was a flaw in the timing
tests that can cause false positives, but only on Windows, and only
when using PsychPortAudio under high load -- your application might
meet this criteria. That problem should be fixed since the 18th april
update of 'beta'. Of course it could also be that there is another
unrelated bug in the reference timer of your machine, which has
nothing to do with the tscs.

let us hear if any of these suggestions work out for you,
-mario
Hi Mario,

Thanks for investigating. MOTU does indeed allow Samples Per Buffer to
be set for each device (under the "General" tab). I was able to reduce
it to 128 from the default 1024 for my 828mkII, and thereby reduce the
input and output delays to 2.2 and 2.7 ms - without any perceptible
interruptions to the audio stream. (I was already using a Firewire
connection, and the April 18 2008 beta PTB update.) Note that these
delays should be even shorter at my 96kHz sampling rate, but i've
experienced a near-doubling in the latency since i temporarily
installed - and even after i uninstalled - another MOTU device (the
UltraLite). Nevertheless the total latency i can now achieve is more
than acceptable.

Thanks again,
Simon


--- In psychtoolbox@yahoogroups.com, Mario Kleiner <mario.kleiner@...>
wrote:
>
> Hi Simon,
>
> i'm cc'ing this to the forum in case others want to learn about it,
> comments interspersed.
>
> On May 6, 2008, at 6:25 AM, Simon Overduin wrote:
> >
>
> > I'm using MATLAB R2007A on a new Lenovo Thinkpad T61 running
> > Windows XP.
> > I'm using a MOTU 828mkII ASIO soundcard. For one of my applications
> > i need
> > to have duplex audio input and output. The following code allows me
> > to do
> > so, using the MOTU channels appropriate for my configuration:
> >
> > InitializePsychSound(1);
> > pahandle = PsychPortAudio('Open', [], 4+2+1, 3, 96000, [1 1], [],
> > [], [0; 6]);
> >
>
> As a sidenote:
>
> The 4 in the 4+2+1 mode parameter requests internal feedback mode.
> Latency
> will be minimized by bypassing Matlab, however you won't have control
> over
> latency, ie., if your experiment would include feeding back some
> sound with
> a controlled delay, you couldn't control for it, as the '4' just
> requests the
> shortest round-trip between input and output. For all "greater than
> minimum"
> delays you'd need to use the 2+1 setting and then control latency in the
> way demonstrated in the BasicSoundFeedbackDemo.
>
> You request a latencyclass of '3', which is "agressive low latency",
> but this
> just selects some presets for unspecified parameters. Windows+ASIO users
> have finer control over latency by specifying the optional
> 'suggestesLatency'
> parameter, which asks for a specific latency in seconds. Setting '3'
> corresponds
> to a suggestedLatency of 1 msec, which is quite aggressive and not
> feasible on
> lower end systems, although the ASIO driver is supposed to clamp the
> request
> to some safe value for the given sound hardware. A latencyclass of
> '2' would
> select a less aggressive suggestedLatency of 5 msecs which is more
> feasible
> on upper class consumer hardware....
>
> > PsychPortAudio('Start', pahandle, 0, 0, 1);
> > % stuff
> > PsychPortAudio('Stop', pahandle);
> > PsychPortAudio('Close');
> >
> > In case you have a chance to look at it, i have posted the PTB
> > feedback
> > i receive at:
> >
> > http://overduin.mit.edu/ptb/messages.txt
> >
> > The input and output latencies are reported to be ~11-12 ms, which
> > agrees
> > well with the total loop delay of ~23 that i consistently observe on a
> > scope monitoring the input and output. I had expected i could
> > achieve a
> > smaller delay given my hardware and what i have read about PTB's
> > abilities. Even a moderately smaller delay (15-20 ms) would be fine
> > for my
> > application.
> >
>
> All this doesn't apply to your MOTU card, because this line in the
> output
> you posted:
>
> ...snip...
> PTB-DEBUG: PortAudio says: Revalidated buffer size constraints are
> min 1024, max 1024, preferred 1024, granularity 0.
> ...snip...
>
> means that for whatever weird reason that only the developers at Motu
> could tell you, the driver of your MOTU card doesn't allow any
> software application to programmatically control latency of the card
> at runtime. Latency is internally controlled by varying/selecting the
> size of a samplebuffer within some range. On your card the
> constraints are minimum=1024 samples and maximum=1024 --> The
> buffersize and thereby latency is fixed to roughly (1024 / 96000)
> seconds, which corresponds to the observed 11-12 msecs for each data
> stream.
>
> However, according to this <http://www.motu.com/techsupport/technotes/
> what-is-latency> technote at Motus website, you can somehow open some
> device settings dialog on your machine, probably somewhere in the
> system preference section of Windows, and there must be somewhere
> some "Samples per buffer" slider or control that allows you to
> manually set the buffersize for your card. Pretty inconvenient and
> inflexible for a product that looks like its in the upper class
> range, given that most 100$ cards can do this easily, but anyway...
>
> So you can try to reduce the samplebuffer size via that control panel
> and experiment, lowering the values until sound becomes unreliable
> (audible artifacts, dropouts, stuttering etc.), how low you can go
> depends on the quality of the rest of your hardware and operating
> system, but they claim that values as low as 4 msecs are possible.
> That would end you up with 4 ms input + 4ms output = 8 msecs total
> latency for sound feedback.
>
> USB connections are relative high latency by design, Firewire
> connected external cards should allow lower latency and the best
> choice is something connected via PCI/PCIExpress bus (CardBus or
> PCMCIA or ExpressCard connector on Laptops) for lowest latency.
>
>
> > I have been unable to confirm if my inability to achieve smaller
> > delays is
> > related to the subsequent critical warnings in which PTB informs me
> > that
> > high-precision timing is not available. My Lenovo laptop is indeed
> > dual-core (Intel Mobile Core 2 Duo T7250), so it is possible, as
> > the error
> > messages suggest, that its TSC timecounter is not properly
> > synchronized.
> > While the PTB FAQ suggests that this brand of processor should not
> > have
> > TSC problems, after installing the recommended CPUz application i
> > did find
> > that the "Clocks (Core #0) Core Speed" fluctuated, e.g. between
> > 1965 and
> > 2000 (x10.0 Multplier) MHz, and occasionally above 2100.
> >
>
> The TSC's of Core2Duo should be synchronized at the hardware level
> and invariant to cpu clock frequency changes -- they are used, e.g.,
> in Intel based Macs, which is one reason why Apple hardware doesn't
> suffer any of the clock problems that typical PC hardware does. Apart
> from that, PTB locks all timing related code on Windows to one single
> core to avoid such problems - another of many work-arounds that are
> not needed on well designed operating systems like OS/X or Linux...
> But depending on how recent your PTB 'beta' is, the reported clock
> problems could be false positives which should not happen anymore
> after an update to the latest beta. There was a flaw in the timing
> tests that can cause false positives, but only on Windows, and only
> when using PsychPortAudio under high load -- your application might
> meet this criteria. That problem should be fixed since the 18th april
> update of 'beta'. Of course it could also be that there is another
> unrelated bug in the reference timer of your machine, which has
> nothing to do with the tscs.
>
> let us hear if any of these suggestions work out for you,
> -mario
>