Re: continuous sound output with psychtoolbox

Hi,

i'm forwarding this to the psychtoolbox forum in case it is of
general interest.

yes that's doable and works on all operating systems. The
'FillBuffer' call has an optional 'streamingrefill' flag. If you set
it to 1, it allows to "append" sound to the buffer. Technically it
doesn't really append, but it refills the parts of the buffer that
are already played out, so if you'd refill 1 second of sound, the
function would either return immediately if 1 sec of space is free in
the buffer, or it would wait until space becomes available and then
append.

BasicSoundFeedbackDemo shows this. It implements a sound feedback
loop with controllable latency to create an echo effect if you have a
microphone attached.

Looping is also simple. Just set the 'repetition' parameter in the
'Start' call to zero -- then it repeats until you 'Stop' it. The
lastest driver also has the concept of setting 'stop' times to
preprogram the time of sound offset.

The latest ptb beta also has the commands...

PsychPortAudio('RefillBuffer', pahandle [, bufferhandle=0],
bufferdata [, startIndex=0]);
PsychPortAudio('SetLoop', pahandle[, startSample=0][, endSample=max]
[, UnitIsSeconds=0]);

...to refill arbitrary parts of the buffer during playout, or define
a playback loop in some subpart of the buffer, e.g., allocate a large
buffer, load it with multiple different chunks of sound and switch
between them by changing the "soundloop" that is actually used during
playback

Also new is support for "Schedules", basically like playlists on a
media player. You can define a whole sequence of sound-loops that are
played one after each other to create complex sound on the fly from
predefined sound chunks:

PsychPortAudio('UseSchedule', pahandle, enableSchedule [, maxSize =
128]);
[success, freeslots] = PsychPortAudio('AddToSchedule', pahandle [,
bufferHandle=0][, repetitions=1][, startSample=0][, endSample=max][,
UnitIsSeconds=0]);

I also intend to add support for multiple buffers in a future beta
(like textures or offscreen windows) to make handling of such things
more natural.

There aren't any demos for this new stuff yet, but the online help
should get you started.

One thing that might make sense for most serious playback is to
select the 'reqlatencieclass' setting in PsychPortaudio('Open') as 1,
instead of the default of zero. This selects low-latency mode, but as
a side-effect it also optimized for highest timing precision, which
is more important than low latency most of the time. Oh and it
disables sound-dithering as well: Normally the least significant bit
of a sound signal is modulated with random dithering to reduce
quantization artifacts due to the limited bit-depths of the audio
DAC's. This is good for sound quality, but for very low-level studies
of sound perception you may want to have the sound signal as
unaltered or unaffected as possible.

On MS-Windows it is important to use a professional sound card with
ASIO driver support and the special plugin one can download from the
Wiki for that purpose. Without ASIO support, the standard Windows
sound system only provides very high latencies, miserable timing for
sound onset and offset etc.

On Feb 4, 2009, at 1:42 PM, Vinzenz Schoenfelder wrote:

> hi mario,
> i'm using a fireface firewire-soundcard (with coreaudio output in
> mac os x) to output sound data onto multiple channels. so far, i
> use the standard PsychPortAudio('FillBuffer','Start','Stop')-
> routines to output small chunks of audio.
> is there any way to generate a continuous sound stream, that is fed
> with new data during playing, or that loops without interruption,
> e.g. a continuous sine wave? does it depend on the system matlab
> runs on (linux, mac, windows)?
> kind wishes from berlin
> vinzenz
> ps. i'm writing in english, since a friend (see Cc) of ours in
> london, is also interested.
As an addition, the BasicSoundFeedbackDemo was written over a year ago and ok for its time, but due to a few new functions added to PsychPortAudio, one could rewrite that demo to be more robust and deterministic in its timing for feedback latency. One can do better than that, with more effort in coding. I'll probably have a look at it for some future beta.

The other thing is that latencies of 2 msecs for feedback are very difficult to achieve. Such low latencies would likely require very carefully selected sound- and computer hardware and a carefully selected and tuned operating system and very careful coding.

E.g. roundtrip latencies from microphone to speaker on a MacBookPro with its built-in soundchip of much less than 20 msecs are very hard to achieve, mostly because the onboard soundchip is just not designed for much lower latencies. You'd need rather high-end soundcards to reliably achieve below 10 msecs of roundtrip latency.

-mario

--- In psychtoolbox@yahoogroups.com, "Mario Kleiner" <mario.kleiner@...> wrote:
>
> Don't get it. What is a "similar question"? The original post only refers to streaming sound output, ie., continuously. You're talking about feedback from a microphone etc. to speakers/headphones? Or also about output?
>
> What is the operating system, and sound-card used? If we talk about Windows, is it a soundcard with native ASIO support and drivers and our special ASIO plugin from the Wiki installed? What is the exact purpose and task? How does your script differ from the BasicSoundFeedbackDemo?
>
> Code based on and used with latest PTB beta?
>
> -mario
>
> --- In psychtoolbox@yahoogroups.com, "Sylvia" <krycek36@> wrote:
> >
> > Hello,
> >
> >
> >
> > I have a similar question regarding sound quality. I have implemented a script similar to the BasicSoundFeedbakDemo to introduce a latency to the sound and I have been finding difference in the quality depending to the amount of latency. The greater the latency the less prominent the "static." I would want to have something with less then a 2ms delay though that seems to have the most noise in the sound played through the headphones. This noise is not in the wave file that is recorded. Any comments or input is greatly appreciated. Thank you.
> >
> > --Sylvia
> >
> > - In psychtoolbox@yahoogroups.com, Mario Kleiner <mario.kleiner@> wrote:
> > >
> > > Hi,
> > >
> > > i'm forwarding this to the psychtoolbox forum in case it is of
> > > general interest.
> > >
> > > yes that's doable and works on all operating systems. The
> > > 'FillBuffer' call has an optional 'streamingrefill' flag. If you set
> > > it to 1, it allows to "append" sound to the buffer. Technically it
> > > doesn't really append, but it refills the parts of the buffer that
> > > are already played out, so if you'd refill 1 second of sound, the
> > > function would either return immediately if 1 sec of space is free in
> > > the buffer, or it would wait until space becomes available and then
> > > append.
> > >
> > > BasicSoundFeedbackDemo shows this. It implements a sound feedback
> > > loop with controllable latency to create an echo effect if you have a
> > > microphone attached.
> > >
> > > Looping is also simple. Just set the 'repetition' parameter in the
> > > 'Start' call to zero -- then it repeats until you 'Stop' it. The
> > > lastest driver also has the concept of setting 'stop' times to
> > > preprogram the time of sound offset.
> > >
> > > The latest ptb beta also has the commands...
> > >
> > > PsychPortAudio('RefillBuffer', pahandle [, bufferhandle=0],
> > > bufferdata [, startIndex=0]);
> > > PsychPortAudio('SetLoop', pahandle[, startSample=0][, endSample=max]
> > > [, UnitIsSeconds=0]);
> > >
> > > ...to refill arbitrary parts of the buffer during playout, or define
> > > a playback loop in some subpart of the buffer, e.g., allocate a large
> > > buffer, load it with multiple different chunks of sound and switch
> > > between them by changing the "soundloop" that is actually used during
> > > playback
> > >
> > > Also new is support for "Schedules", basically like playlists on a
> > > media player. You can define a whole sequence of sound-loops that are
> > > played one after each other to create complex sound on the fly from
> > > predefined sound chunks:
> > >
> > > PsychPortAudio('UseSchedule', pahandle, enableSchedule [, maxSize =
> > > 128]);
> > > [success, freeslots] = PsychPortAudio('AddToSchedule', pahandle [,
> > > bufferHandle=0][, repetitions=1][, startSample=0][, endSample=max][,
> > > UnitIsSeconds=0]);
> > >
> > > I also intend to add support for multiple buffers in a future beta
> > > (like textures or offscreen windows) to make handling of such things
> > > more natural.
> > >
> > > There aren't any demos for this new stuff yet, but the online help
> > > should get you started.
> > >
> > > One thing that might make sense for most serious playback is to
> > > select the 'reqlatencieclass' setting in PsychPortaudio('Open') as 1,
> > > instead of the default of zero. This selects low-latency mode, but as
> > > a side-effect it also optimized for highest timing precision, which
> > > is more important than low latency most of the time. Oh and it
> > > disables sound-dithering as well: Normally the least significant bit
> > > of a sound signal is modulated with random dithering to reduce
> > > quantization artifacts due to the limited bit-depths of the audio
> > > DAC's. This is good for sound quality, but for very low-level studies
> > > of sound perception you may want to have the sound signal as
> > > unaltered or unaffected as possible.
> > >
> > > On MS-Windows it is important to use a professional sound card with
> > > ASIO driver support and the special plugin one can download from the
> > > Wiki for that purpose. Without ASIO support, the standard Windows
> > > sound system only provides very high latencies, miserable timing for
> > > sound onset and offset etc.
> > >
> > > On Feb 4, 2009, at 1:42 PM, Vinzenz Schoenfelder wrote:
> > >
> > > > hi mario,
> > > > i'm using a fireface firewire-soundcard (with coreaudio output in
> > > > mac os x) to output sound data onto multiple channels. so far, i
> > > > use the standard PsychPortAudio('FillBuffer','Start','Stop')-
> > > > routines to output small chunks of audio.
> > > > is there any way to generate a continuous sound stream, that is fed
> > > > with new data during playing, or that loops without interruption,
> > > > e.g. a continuous sine wave? does it depend on the system matlab
> > > > runs on (linux, mac, windows)?
> > > > kind wishes from berlin
> > > > vinzenz
> > > > ps. i'm writing in english, since a friend (see Cc) of ours in
> > > > london, is also interested.
> > >
> >
>
Ok,

UpdatePsychtoolbox your beta. I've made a small extension to the PsychPortAudio driver and added a new demo. The driver update requires Matlab R2007a or later on Windows. Let me know if you use an older Matlab version. I'll update the driver for all other OSes with the next regular beta update.

The new demo DelayedSoundFeedbackDemo makes use of the extended driver to achieve lower and more exact sound delays. If you look at the code you'll see that it is substantially more complex than the old BasicSoundFeedbackDemo. You'll need to modify it for your purpose (e.g., strip the unneeded debug output and plots, hard-code parameters once you figured out the optimal settings for your setup) and you must use external measurement equipment to verify if the real latencies are what the demo claims. In theory this should be pretty exact, but in practice there is always the potential of coding errors in this new script, or bugs in your soundcards driver that can't be detected from within the software. E.g., you could feed the microphone input and the sound output into the two channels of either an oszillograph, or into the two input channels of the Line-In of a second computer. Then you record the signal from the mic and the delayed signal and compare the recorded sounds in the two channels to measure the feedback latency and compare it to expected values.

So this demo should allow for slightly lower and more precise latencies than the old one. It has multiple tweakable parameters and these need to be adapted to your specific system setup and sound hardware for optimal reliability and latency. Read the "help DelayedSoundFeedbackDemo" for instructions. For the Audigy 2 ZS, you'll likely need something like...

DelayedSoundFeedbackDemo( x , 1, 48000, minLat );

... as a starting point. The Audigy can record & playback sound at a maximum frequency of 48000 Hz without introducing sample rate conversion delays. Macs can go as high as 96000 Hz with their onboard sound chips. As an ASIO card, it must be operated in full-duplex mode (therefore the '1' flag). You can see if ASIO support works, because the demo will output some messages that tell you if ASIO is used, otherwise it will output some info about lack of ASIO support on your setup. It should say something like:

"PTB-INFO: For 2 channels Playback: Audio subsystem is ASIO, Audio device name is blah blah...."

The most important tweakable parameters in the demo are 'x' and 'minLat'. With 'x' you specify the desired feedback latency in milliseconds, e.g., x = 15 for 15 msecs from microphone to headphones. The special setting of x = 0 will switch the driver into a special mode which provides you with the lowest possible feedback latency on your system -- the expected minimum latency in this mode will be printed out to the Matlab window. If you set x > 0, the script will either stay silent if the requested latency can be achieved, or it will print tons of diagnostic warning messages if it doesn't work - if you are asking for too much. You shouldn't hear any artifacts, static, crackling or whatever, otherwise the system is overloaded and your feedback will not only sound bad, but the delay will likely be wrong as well. The driver can recognize many, but not all possible problems caused by overload. In any case you'll need to measure with external equipment once to make sure everything is fine.

Ok, what about the 'minLat' parameter? That one directly controls the tradeoff between quality/reliability and minimum achievable latency. Lower values (in msecs) --> lower latencies, more possible trouble. You'll need to tweak this until you find something that works well enough for your setup. A workable setting may be anywhere betwee 15 msecs if you are really unlucky with your setup and maybe 2 msecs if you are extraordinarily lucky.

Delays between 20 and 50 msecs should be no problem at all, with the new script i can even get my MacBookPro down to about 12 msecs under optimal conditions.

A minimum delay around 5-8 msecs is borderline, but you'll see -- Not impossible but likely at the limits of your system. Creatives product sheets claim latencies as low as 2 msecs, but that probably means one-way, so the feedback latency will be more like 5-6 msecs under optimal conditions.

In any case you'll need to tune/optimze your setup for best performance:

1. Close all applications except Matlab, make sure that no other sound apps, not software update, indexing service or virus scanners / firewalls etc. are running.

2. Disconnect from the network and from any unneeded peripheral devices.

3. You may try running with an additional Priority(MaxPriority(0)); command, maybe logged in as administrator user -- it may help or hurt. Administrator users can try Priority(2); to squeeze out a bit more realtime priority.

4. Running Matlab in -nojvm mode without GUI may help.

5. Sometimes the first run of the script or the first run after a 'clear all' command is more unreliable for low latencies.

6. What else? The configuration panel for Creative soundcards lets you often choose between different modes of operation, like game, pro audio quality. One of them is optimal for lowest latency, but not the others.

7. Make sure you have the latest drivers from Creative installed for your card.

There are a few more tweakable, more specialized parameters but check these first. Of course there are also more expensive soundcards for high demands on low latency.

Regarding your oddball task: Some ASIO soundcards also support a special "Zero latency direct input monitoring" mode. In this mode the signal from the input is directly fed back to the amplifiers and output, bypassing most of the cards processing and all of the computers processing. This gives the lowest possible latency, on some pro cards literally zero latency, on others at least reliably below 5 msecs. See...

<http://www.rme-audio.com/english/techinfo/lola_lomo.htm>

... for some explanation from RME.

Our driver doesn't support this feature yet. The code extension should be straight-forward and rather simple to implement and it sound a valuable addition, but it requires changes to our low-level portaudio_x86.dll plugin, which in turn will require all PTB users of ASIO on Windows to upgrade their portaudio_x86 plugin from the Wiki. As this is a bit of a hazzle i'll need to carefully test this extension, iow. it will take a bit of time to add this feature and it needs to be coordinated with the beta update schedule. Another problem for an oddball paradigm with this zero-latency monitoring could be that the audio processing path in this mode would be different from the > 0 latency mode, so the typical electronic background "noise floor" might be slightly different in both conditions and give away an extra cue to the subject unless everything is properly adjusted - or you use a really bad microphone where any smallish hint drowns in the general noise of bad equipment ;-)

Try how far you can go with the current code in PTB beta after a quick UpdatePsychtoolbox and tell us your results. I only tested this new stuff on OS/X. The machine i originally used to develop and test the ASIO output on Windows died a few months ago, so i can't easily test on a real ASIO card myself at the moment.

best,
-mario


--- In psychtoolbox@yahoogroups.com, "Sylvia" <krycek36@...> wrote:
>
> Hi Mario,
>
> Thank you for your quick response, I appreciate it. To clarify, my question is of a similar flavor or quality. I am also working to create a continuous sound stream that receives new audio data as the sound is played back. From the previous response to the thread there was mention about sound quality and low latency. I want to achieve both high sound quality and low latency. The "static" might be caused by some microphone artifact, I'm unsure.
>
> I am currently using Windows XP with psychtoolbox version 3.0.8 beta and have a SB Audigy 2 zs soundcard installed which I believe has ASIO and the driver portaudio_x86.dll is installed.
>
> The purpose of the task is to have a person produce a sound such as "ah" and have it played back at various latencies, i.e. 20-50ms. The person talks through a microphone and receives the audio feedback through the head phones. Also trying to achieve something along the lines of an oddball paradigm where there is a standard latency (I would like it less then 5ms but I'm not sure that's possible and thank you for the update and elaborating on that point) and then every so often have a longer latency inserted and have the participant respond via a response box when they can detect the longer latency. Any information that you can provide would be greatly appreciated. Thank you.
>
>
>
>
>
>
> >
> > As an addition, the BasicSoundFeedbackDemo was written over a year ago and ok for its time, but due to a few new functions added to PsychPortAudio, one could rewrite that demo to be more robust and deterministic in its timing for feedback latency. One can do better than that, with more effort in coding. I'll probably have a look at it for some future beta.
> >
> > The other thing is that latencies of 2 msecs for feedback are very difficult to achieve. Such low latencies would likely require very carefully selected sound- and computer hardware and a carefully selected and tuned operating system and very careful coding.
> >
> > E.g. roundtrip latencies from microphone to speaker on a MacBookPro with its built-in soundchip of much less than 20 msecs are very hard to achieve, mostly because the onboard soundchip is just not designed for much lower latencies. You'd need rather high-end soundcards to reliably achieve below 10 msecs of roundtrip latency.
> >
> > -mario
> >
> > --- In psychtoolbox@yahoogroups.com, "Mario Kleiner" <mario.kleiner@> wrote:
> > >
> > > Don't get it. What is a "similar question"? The original post only refers to streaming sound output, ie., continuously. You're talking about feedback from a microphone etc. to speakers/headphones? Or also about output?
> > >
> > > What is the operating system, and sound-card used? If we talk about Windows, is it a soundcard with native ASIO support and drivers and our special ASIO plugin from the Wiki installed? What is the exact purpose and task? How does your script differ from the BasicSoundFeedbackDemo?
> > >
> > > Code based on and used with latest PTB beta?
> > >
> > > -mario
> > >
> > > --- In psychtoolbox@yahoogroups.com, "Sylvia" <krycek36@> wrote:
> > > >
> > > > Hello,
> > > >
> > > >
> > > >
> > > > I have a similar question regarding sound quality. I have implemented a script similar to the BasicSoundFeedbakDemo to introduce a latency to the sound and I have been finding difference in the quality depending to the amount of latency. The greater the latency the less prominent the "static." I would want to have something with less then a 2ms delay though that seems to have the most noise in the sound played through the headphones. This noise is not in the wave file that is recorded. Any comments or input is greatly appreciated. Thank you.
> > > >
> > > > --Sylvia
> > > >
> > > > - In psychtoolbox@yahoogroups.com, Mario Kleiner <mario.kleiner@> wrote:
> > > > >
> > > > > Hi,
> > > > >
> > > > > i'm forwarding this to the psychtoolbox forum in case it is of
> > > > > general interest.
> > > > >
> > > > > yes that's doable and works on all operating systems. The
> > > > > 'FillBuffer' call has an optional 'streamingrefill' flag. If you set
> > > > > it to 1, it allows to "append" sound to the buffer. Technically it
> > > > > doesn't really append, but it refills the parts of the buffer that
> > > > > are already played out, so if you'd refill 1 second of sound, the
> > > > > function would either return immediately if 1 sec of space is free in
> > > > > the buffer, or it would wait until space becomes available and then
> > > > > append.
> > > > >
> > > > > BasicSoundFeedbackDemo shows this. It implements a sound feedback
> > > > > loop with controllable latency to create an echo effect if you have a
> > > > > microphone attached.
> > > > >
> > > > > Looping is also simple. Just set the 'repetition' parameter in the
> > > > > 'Start' call to zero -- then it repeats until you 'Stop' it. The
> > > > > lastest driver also has the concept of setting 'stop' times to
> > > > > preprogram the time of sound offset.
> > > > >
> > > > > The latest ptb beta also has the commands...
> > > > >
> > > > > PsychPortAudio('RefillBuffer', pahandle [, bufferhandle=0],
> > > > > bufferdata [, startIndex=0]);
> > > > > PsychPortAudio('SetLoop', pahandle[, startSample=0][, endSample=max]
> > > > > [, UnitIsSeconds=0]);
> > > > >
> > > > > ...to refill arbitrary parts of the buffer during playout, or define
> > > > > a playback loop in some subpart of the buffer, e.g., allocate a large
> > > > > buffer, load it with multiple different chunks of sound and switch
> > > > > between them by changing the "soundloop" that is actually used during
> > > > > playback
> > > > >
> > > > > Also new is support for "Schedules", basically like playlists on a
> > > > > media player. You can define a whole sequence of sound-loops that are
> > > > > played one after each other to create complex sound on the fly from
> > > > > predefined sound chunks:
> > > > >
> > > > > PsychPortAudio('UseSchedule', pahandle, enableSchedule [, maxSize =
> > > > > 128]);
> > > > > [success, freeslots] = PsychPortAudio('AddToSchedule', pahandle [,
> > > > > bufferHandle=0][, repetitions=1][, startSample=0][, endSample=max][,
> > > > > UnitIsSeconds=0]);
> > > > >
> > > > > I also intend to add support for multiple buffers in a future beta
> > > > > (like textures or offscreen windows) to make handling of such things
> > > > > more natural.
> > > > >
> > > > > There aren't any demos for this new stuff yet, but the online help
> > > > > should get you started.
> > > > >
> > > > > One thing that might make sense for most serious playback is to
> > > > > select the 'reqlatencieclass' setting in PsychPortaudio('Open') as 1,
> > > > > instead of the default of zero. This selects low-latency mode, but as
> > > > > a side-effect it also optimized for highest timing precision, which
> > > > > is more important than low latency most of the time. Oh and it
> > > > > disables sound-dithering as well: Normally the least significant bit
> > > > > of a sound signal is modulated with random dithering to reduce
> > > > > quantization artifacts due to the limited bit-depths of the audio
> > > > > DAC's. This is good for sound quality, but for very low-level studies
> > > > > of sound perception you may want to have the sound signal as
> > > > > unaltered or unaffected as possible.
> > > > >
> > > > > On MS-Windows it is important to use a professional sound card with
> > > > > ASIO driver support and the special plugin one can download from the
> > > > > Wiki for that purpose. Without ASIO support, the standard Windows
> > > > > sound system only provides very high latencies, miserable timing for
> > > > > sound onset and offset etc.
> > > > >
> > > > > On Feb 4, 2009, at 1:42 PM, Vinzenz Schoenfelder wrote:
> > > > >
> > > > > > hi mario,
> > > > > > i'm using a fireface firewire-soundcard (with coreaudio output in
> > > > > > mac os x) to output sound data onto multiple channels. so far, i
> > > > > > use the standard PsychPortAudio('FillBuffer','Start','Stop')-
> > > > > > routines to output small chunks of audio.
> > > > > > is there any way to generate a continuous sound stream, that is fed
> > > > > > with new data during playing, or that loops without interruption,
> > > > > > e.g. a continuous sine wave? does it depend on the system matlab
> > > > > > runs on (linux, mac, windows)?
> > > > > > kind wishes from berlin
> > > > > > vinzenz
> > > > > > ps. i'm writing in english, since a friend (see Cc) of ours in
> > > > > > london, is also interested.
> > > > >
> > > >
> > >
> >
>
Hi Sylvia,

> Thank you for getting back to me in such a timely matter. All of your comments and suggestions have been extremely helpful. I was able to download the ASIODirectInputMonitoring zip file and install it in the correct folders, however when I ran the appropriate scripts I would receive a fail message. I am not sure if this is an error on my end or if the sound card isn't supported.

PTB beta has been updated yesterday and all this code is now included + some improvements. So you should delete all files in that zip file except for the portaudio_x86.dll, then UpdatePsychtoolbox, then copy the dll into the Psychtoolbox main folder. Then restart matlab.

Run DirectInputMonitoringTest agai, make sure to read about the meaning of the control keys in its help. If it still fails, show us the output of the script with the exact error message etc. The driver should say something like "using PortAudio V19-devel with DIM" at some point - the "with DIM" being proof that you installed the dll correctly on your system.

Then following error messages, if any, will be diagnostic of potential problems.

I know that the code should probably work because it ran without error on a M-Audio Delta card i found, but i don't know for real because i don't have cables to actually attach a microphone.

I didn't ever achieve 2 ms latencies. My best on a MacBookPro was 12 msecs with onboard sound and the DelayedSoundFeedbackDemo, if i remember correctly. In its special "zero latency" mode, the demo should achieve lower latency but is unable to actually determine how low. No tests on ASIO hardware were done due to lack of access to it. I only said that some product docu for the Audigy claims "as low as 2 msecs" without the docu mentioning if this is one-way or two-way, but i'm almost 100% certain it meant 2 msecs for either output or input under very optimal conditions. A more realistic result for that card would be around 6-10 msecs if everything is well.

If the direct input monitoring works out for your Audigy card, then you'd get almost zero or minimal latency if you request zero latency from the DelayedSoundFeedbackDemo, and then the next higher latency would be around 6-10 msecs if you're lucky. You basically have a gap between maybe 1 msec and 6-10 msec in which you can't choose the delay, because input monitoring only works at fixed almost zero latency and the other mode only starts at around 6-10 msec.

On OS/X the driver doesn't support direct input monitoring yet, that's on the todo list. Also the onboard sound chips of Apple computers don't support this feature at all. Only some more expensive dedicated discrete soundcards for Macs do that, just as in Windowsland. The feature needs special support from higher-end hardware.

But the Audigy on Windows should support it at least to some degree if we believe Creative's product FAQ's.

-mario

>
> In an attempt to lower the delay, I have found that I am able to achieve lower delays on a mac than on a windows operating system. I am curious about the system you have been using to achieve delays as low as 2 ms I have yet to break the 10ms barrier on my machine.
>
> Thank you again for all of your help. It is greatly appreciated.
>
> -Sylvia
>
>
> --- In psychtoolbox@yahoogroups.com, "Mario Kleiner" <mario.kleiner@> wrote:
> >
> > I've implemented a first prototype of support for "Zero latency direct input monitoring" on soundcards that support this feature in hardware. This currently only works on MS-Windows with ASIO 2.0 capable soundcards that support that feature - not all do.
> >
> > Most of RME's cards and some of Creative Labs cards should support this feature, e.g., the Audigy 2ZS should have some level of support.
> >
> > How to test on Windows:
> >
> > 1. Download and unzip the zip file ASIODirectInputMonitoringTest.zip from the Files section on the forum.
> >
> > 2. Copy the various stuff inside into the proper Psychtoolbox folders, overwriting the corresponding files in your PTB beta installation.
> >
> > 3. Restart Matlab.
> >
> > Run DirectInputMonitoringTest.m for a basic test and exercise of your hardware.
> >
> > DelayedSoundFeedbackDemo has been extended to use this feature if it is supported on your hardware to provide the lowest possible feedback latency on your sound hardware, if called like this:
> >
> > DelayedSoundFeedbackDemo(0,1);
> >
> > On unsupported hardware, it will fall back to our own low-latency mechanism in PsychPortAudio to do its best, as explained in earlier posts.
> >
> > I'd like to get feedback if and how well this works. It is entirely untested because i don't have hardware with support for this feature.
> >
> > best,
> > -mario
> >
> > For reference, the following explanation of what this feature is supposed to do:
> >
> > > Regarding your oddball task: Some ASIO soundcards also support a special "Zero latency direct input monitoring" mode. In this mode the signal from the input is directly fed back to the amplifiers and output, bypassing most of the cards processing and all of the computers processing. This gives the lowest possible latency, on some pro cards literally zero latency, on others at least reliably below 5 msecs. See...
> > >
> > > <http://www.rme-audio.com/english/techinfo/lola_lomo.htm>
> > >
> > > ... for some explanation from RME.
> > >
> > > Our driver doesn't support this feature yet. The code extension should be straight-forward and rather simple to implement and it sound a valuable addition, but it requires changes to our low-level portaudio_x86.dll plugin, which in turn will require all PTB users of ASIO on Windows to upgrade their portaudio_x86 plugin from the Wiki. As this is a bit of a hazzle i'll need to carefully test this extension, iow. it will take a bit of time to add this feature and it needs to be coordinated with the beta update schedule. Another problem for an oddball paradigm with this zero-latency monitoring could be that the audio processing path in this mode would be different from the > 0 latency mode, so the typical electronic background "noise floor" might be slightly different in both conditions and give away an extra cue to the subject unless everything is properly adjusted - or you use a really bad microphone where any smallish hint drowns in the general noise of bad equipment ;-)
> > >
> > > Try how far you can go with the current code in PTB beta after a quick UpdatePsychtoolbox and tell us your results. I only tested this new stuff on OS/X. The machine i originally used to develop and test the ASIO output on Windows died a few months ago, so i can't easily test on a real ASIO card myself at the moment.
> > >
> > > best,
> > > -mario
> > >
> > >
> > > --- In psychtoolbox@yahoogroups.com, "Sylvia" <krycek36@> wrote:
> > > >
> > > > Hi Mario,
> > > >
> > > > Thank you for your quick response, I appreciate it. To clarify, my question is of a similar flavor or quality. I am also working to create a continuous sound stream that receives new audio data as the sound is played back. From the previous response to the thread there was mention about sound quality and low latency. I want to achieve both high sound quality and low latency. The "static" might be caused by some microphone artifact, I'm unsure.
> > > >
> > > > I am currently using Windows XP with psychtoolbox version 3.0.8 beta and have a SB Audigy 2 zs soundcard installed which I believe has ASIO and the driver portaudio_x86.dll is installed.
> > > >
> > > > The purpose of the task is to have a person produce a sound such as "ah" and have it played back at various latencies, i.e. 20-50ms. The person talks through a microphone and receives the audio feedback through the head phones. Also trying to achieve something along the lines of an oddball paradigm where there is a standard latency (I would like it less then 5ms but I'm not sure that's possible and thank you for the update and elaborating on that point) and then every so often have a longer latency inserted and have the participant respond via a response box when they can detect the longer latency. Any information that you can provide would be greatly appreciated. Thank you.
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > >
> > > > > As an addition, the BasicSoundFeedbackDemo was written over a year ago and ok for its time, but due to a few new functions added to PsychPortAudio, one could rewrite that demo to be more robust and deterministic in its timing for feedback latency. One can do better than that, with more effort in coding. I'll probably have a look at it for some future beta.
> > > > >
> > > > > The other thing is that latencies of 2 msecs for feedback are very difficult to achieve. Such low latencies would likely require very carefully selected sound- and computer hardware and a carefully selected and tuned operating system and very careful coding.
> > > > >
> > > > > E.g. roundtrip latencies from microphone to speaker on a MacBookPro with its built-in soundchip of much less than 20 msecs are very hard to achieve, mostly because the onboard soundchip is just not designed for much lower latencies. You'd need rather high-end soundcards to reliably achieve below 10 msecs of roundtrip latency.
> > > > >
> > > > > -mario
> > > > >
> > > > > --- In psychtoolbox@yahoogroups.com, "Mario Kleiner" <mario.kleiner@> wrote:
> > > > > >
> > > > > > Don't get it. What is a "similar question"? The original post only refers to streaming sound output, ie., continuously. You're talking about feedback from a microphone etc. to speakers/headphones? Or also about output?
> > > > > >
> > > > > > What is the operating system, and sound-card used? If we talk about Windows, is it a soundcard with native ASIO support and drivers and our special ASIO plugin from the Wiki installed? What is the exact purpose and task? How does your script differ from the BasicSoundFeedbackDemo?
> > > > > >
> > > > > > Code based on and used with latest PTB beta?
> > > > > >
> > > > > > -mario
> > > > > >
> > > > > > --- In psychtoolbox@yahoogroups.com, "Sylvia" <krycek36@> wrote:
> > > > > > >
> > > > > > > Hello,
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > I have a similar question regarding sound quality. I have implemented a script similar to the BasicSoundFeedbakDemo to introduce a latency to the sound and I have been finding difference in the quality depending to the amount of latency. The greater the latency the less prominent the "static." I would want to have something with less then a 2ms delay though that seems to have the most noise in the sound played through the headphones. This noise is not in the wave file that is recorded. Any comments or input is greatly appreciated. Thank you.
> > > > > > >
> > > > > > > --Sylvia
> > > > > > >
> > > > > > > - In psychtoolbox@yahoogroups.com, Mario Kleiner <mario.kleiner@> wrote:
> > > > > > > >
> > > > > > > > Hi,
> > > > > > > >
> > > > > > > > i'm forwarding this to the psychtoolbox forum in case it is of
> > > > > > > > general interest.
> > > > > > > >
> > > > > > > > yes that's doable and works on all operating systems. The
> > > > > > > > 'FillBuffer' call has an optional 'streamingrefill' flag. If you set
> > > > > > > > it to 1, it allows to "append" sound to the buffer. Technically it
> > > > > > > > doesn't really append, but it refills the parts of the buffer that
> > > > > > > > are already played out, so if you'd refill 1 second of sound, the
> > > > > > > > function would either return immediately if 1 sec of space is free in
> > > > > > > > the buffer, or it would wait until space becomes available and then
> > > > > > > > append.
> > > > > > > >
> > > > > > > > BasicSoundFeedbackDemo shows this. It implements a sound feedback
> > > > > > > > loop with controllable latency to create an echo effect if you have a
> > > > > > > > microphone attached.
> > > > > > > >
> > > > > > > > Looping is also simple. Just set the 'repetition' parameter in the
> > > > > > > > 'Start' call to zero -- then it repeats until you 'Stop' it. The
> > > > > > > > lastest driver also has the concept of setting 'stop' times to
> > > > > > > > preprogram the time of sound offset.
> > > > > > > >
> > > > > > > > The latest ptb beta also has the commands...
> > > > > > > >
> > > > > > > > PsychPortAudio('RefillBuffer', pahandle [, bufferhandle=0],
> > > > > > > > bufferdata [, startIndex=0]);
> > > > > > > > PsychPortAudio('SetLoop', pahandle[, startSample=0][, endSample=max]
> > > > > > > > [, UnitIsSeconds=0]);
> > > > > > > >
> > > > > > > > ...to refill arbitrary parts of the buffer during playout, or define
> > > > > > > > a playback loop in some subpart of the buffer, e.g., allocate a large
> > > > > > > > buffer, load it with multiple different chunks of sound and switch
> > > > > > > > between them by changing the "soundloop" that is actually used during
> > > > > > > > playback
> > > > > > > >
> > > > > > > > Also new is support for "Schedules", basically like playlists on a
> > > > > > > > media player. You can define a whole sequence of sound-loops that are
> > > > > > > > played one after each other to create complex sound on the fly from
> > > > > > > > predefined sound chunks:
> > > > > > > >
> > > > > > > > PsychPortAudio('UseSchedule', pahandle, enableSchedule [, maxSize =
> > > > > > > > 128]);
> > > > > > > > [success, freeslots] = PsychPortAudio('AddToSchedule', pahandle [,
> > > > > > > > bufferHandle=0][, repetitions=1][, startSample=0][, endSample=max][,
> > > > > > > > UnitIsSeconds=0]);
> > > > > > > >
> > > > > > > > I also intend to add support for multiple buffers in a future beta
> > > > > > > > (like textures or offscreen windows) to make handling of such things
> > > > > > > > more natural.
> > > > > > > >
> > > > > > > > There aren't any demos for this new stuff yet, but the online help
> > > > > > > > should get you started.
> > > > > > > >
> > > > > > > > One thing that might make sense for most serious playback is to
> > > > > > > > select the 'reqlatencieclass' setting in PsychPortaudio('Open') as 1,
> > > > > > > > instead of the default of zero. This selects low-latency mode, but as
> > > > > > > > a side-effect it also optimized for highest timing precision, which
> > > > > > > > is more important than low latency most of the time. Oh and it
> > > > > > > > disables sound-dithering as well: Normally the least significant bit
> > > > > > > > of a sound signal is modulated with random dithering to reduce
> > > > > > > > quantization artifacts due to the limited bit-depths of the audio
> > > > > > > > DAC's. This is good for sound quality, but for very low-level studies
> > > > > > > > of sound perception you may want to have the sound signal as
> > > > > > > > unaltered or unaffected as possible.
> > > > > > > >
> > > > > > > > On MS-Windows it is important to use a professional sound card with
> > > > > > > > ASIO driver support and the special plugin one can download from the
> > > > > > > > Wiki for that purpose. Without ASIO support, the standard Windows
> > > > > > > > sound system only provides very high latencies, miserable timing for
> > > > > > > > sound onset and offset etc.
> > > > > > > >
> > > > > > > > On Feb 4, 2009, at 1:42 PM, Vinzenz Schoenfelder wrote:
> > > > > > > >
> > > > > > > > > hi mario,
> > > > > > > > > i'm using a fireface firewire-soundcard (with coreaudio output in
> > > > > > > > > mac os x) to output sound data onto multiple channels. so far, i
> > > > > > > > > use the standard PsychPortAudio('FillBuffer','Start','Stop')-
> > > > > > > > > routines to output small chunks of audio.
> > > > > > > > > is there any way to generate a continuous sound stream, that is fed
> > > > > > > > > with new data during playing, or that loops without interruption,
> > > > > > > > > e.g. a continuous sine wave? does it depend on the system matlab
> > > > > > > > > runs on (linux, mac, windows)?
> > > > > > > > > kind wishes from berlin
> > > > > > > > > vinzenz
> > > > > > > > > ps. i'm writing in english, since a friend (see Cc) of ours in
> > > > > > > > > london, is also interested.
> > > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>