Lazarus

Programming => Graphics and Multimedia => Audio and Video => Topic started by: Hobbit1972 on February 06, 2023, 01:00:43 pm

Title: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 06, 2023, 01:00:43 pm
Hi!

I'm using UOS (with PortAudio lib) to route MicIn to SpkOut:
Code: Pascal  [Select][+][-]
  1.   uos_CreatePlayer(0);
  2.   uos_AddIntoDevOut(0);
  3.   uos_AddFromDevIn(0);
  4.   uos_Play(0);
  5.  

This gives me quite audible latency between in and out. I Tried to fiddle with the paramters of AddFromDevIn like
Code: Pascal  [Select][+][-]
  1.  uos_AddFromFrvIn(0,-1,4,-1,-1,-1,128,-1);
but this doesn't change much.

Is there some way to reduce latency here?

Tx!
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Fred vS on February 06, 2023, 02:39:53 pm
Hello.

Please take a look at the examples in /uos/examples/.
There is also a uos-forum: http://uos-forum.108.s1.nabble.com/

Thanks.

Fre;D
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: metis on February 06, 2023, 02:51:43 pm
@Hobbit1972

Which OS, which SoundCard, which AudioDriver do You use ?

Did You try ASIO (https://en.wikipedia.org/wiki/Audio_Stream_Input/Output) or JACK (https://en.wikipedia.org/wiki/JACK_Audio_Connection_Kit) (JACK@GitHub (https://github.com/jackaudio/jack2)) ?

On Windows, I use PortAudio together with ASIO4ALL (https://www.asio4all.org/) to obtain incredible, crystal-clear low-latency Sound
with Audios and Videos(!) with my FFmpeg-based Audio/Video-RenderingEngine FFPlay4Laz (https://forum.lazarus.freepascal.org/index.php/topic,26666.msg464784.html#msg464784).
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 06, 2023, 03:00:09 pm
@Fred vS: any particular example touching latency in mind?
The latency value is seconds? milliseconds? foo units?  :)

@metis:
Win11 64bit. Out-of-the-box card&driver of my mainboard. As the program should run on different platforms, general ideas would be appreciated.

Tx

Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Fred vS on February 06, 2023, 03:13:14 pm
@Fred vS: any particular example touching latency in mind?
The latency value is seconds? milliseconds? foo units?  :)

Please try SimplePlayer demo.
About latency: http://www.portaudio.com/docs/latency.html

Fre;D
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Fred vS on February 06, 2023, 03:22:09 pm
Hi!

I'm using UOS (with PortAudio lib) to route MicIn to SpkOut:

Re-hello.

Did you try the simplerecorder.lpi example?
Is the sound ok?
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 06, 2023, 03:36:20 pm
@Fred vS:
Yes, SimplePlayer runs and is okay. Except "play revers" where it gets jumpy.

Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Fred vS on February 06, 2023, 03:55:34 pm
@Fred vS:
Yes, SimplePlayer runs and is okay. Except "play revers" where it gets jumpy.

Ha, ok, ( hum, jumpy with what kind of audio files, mp3, wav, ogg, ... ?)

And what about SimpleRecorder, is the mic-input to output sound ok ?
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 06, 2023, 04:16:05 pm
@Fred vS:
Yes, SimplePlayer runs and is okay. Except "play revers" where it gets jumpy.

Ha, ok, ( hum, jumpy with what kind of audio files, mp3, wav, ogg, ... ?)

And what about SimpleRecorder, is the mic-input to output sound ok ?

ogg. plays backwards till it reaches 0:00:00.743, jumps to 0:00:01.114, plays backwards... and so on...

Recorder: let me check that out...
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 06, 2023, 04:18:41 pm
And what about SimpleRecorder, is the mic-input to output sound ok ?

SimpleRecorder: I'd say latency is ~1/4s, just like in my program.
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 06, 2023, 04:23:13 pm
@Fred vS:
FYI: as the compiler threw an error not knowing which overloed function to use, I made this change in uos.pas:
Code: Pascal  [Select][+][-]
  1.           if StreamIn[x].AACI.lwDataLen > (StreamIn[x].AACI.BitsPerSample div 8) then
  2.             StreamIn[x].Data.outframes := trunc(comp(StreamIn[x].AACI.lwDataLen) Div (StreamIn[x].AACI.
  3.                                           BitsPerSample Div 8))
  4.  
I added comp() to get it to work.
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Fred vS on February 06, 2023, 04:26:58 pm
ogg. plays backwards till it reaches 0:00:00.743, jumps to 0:00:01.114, plays backwards... and so on...

Ok, I will check this asap with ogg files, write you later.

[EDIT] Ha, ok, I see, it is wanted  ;).
If reverse-song reach to begin, there is a loop with first chunck.
This reverse effect is to show how to use the uos-dsp feature, feel free to adapt the code like you want.

SimpleRecorder: I'd say latency is ~1/4s, just like in my program.

Sorry but I did not understand.  :-[
Is the sound ok or not?
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Fred vS on February 06, 2023, 04:28:38 pm
@Fred vS:
FYI: as the compiler threw an error not knowing which overloed function to use, I made this change in uos.pas:
Code: Pascal  [Select][+][-]
  1.           if StreamIn[x].AACI.lwDataLen > (StreamIn[x].AACI.BitsPerSample div 8) then
  2.             StreamIn[x].Data.outframes := trunc(comp(StreamIn[x].AACI.lwDataLen) Div (StreamIn[x].AACI.
  3.                                           BitsPerSample Div 8))
  4.  
I added comp() to get it to work.

Please give the error you get from the compiler.
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 06, 2023, 04:30:36 pm
SimpleRecorder: I'd say latency is ~1/4s, just like in my program.

Sorry but I did not understand.  :-[
Is the sound ok or not?

Sound is (and was alaways) okay, but processing time (=latency) is too long for my purposes. Between me speaking in the mic and hearing my voice in the speaker is ~250ms  :o

ofc Win was and is a pain in the a** concerning these kind of things.  :(
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 06, 2023, 04:32:07 pm
@Fred vS:
FYI: as the compiler threw an error not knowing which overloed function to use, I made this change in uos.pas:
Code: Pascal  [Select][+][-]
  1.           if StreamIn[x].AACI.lwDataLen > (StreamIn[x].AACI.BitsPerSample div 8) then
  2.             StreamIn[x].Data.outframes := trunc(comp(StreamIn[x].AACI.lwDataLen) Div (StreamIn[x].AACI.
  3.                                           BitsPerSample Div 8))
  4.  
I added comp() to get it to work.

Please give the error you get from the compiler.

Here you go:
Code: Pascal  [Select][+][-]
  1. uos.pas(10381,43) Error: Can't determine which overloaded function to call
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Fred vS on February 06, 2023, 04:43:24 pm
Here you go:
Code: Pascal  [Select][+][-]
  1. uos.pas(10381,43) Error: Can't determine which overloaded function to call

Hum, here no problems.
What version of uos are you using?

The last is this:  https://github.com/fredvs/uos/archive/refs/heads/main.zip

What example are you compiling to get that error?

Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 06, 2023, 04:51:57 pm
Here you go:
Code: Pascal  [Select][+][-]
  1. uos.pas(10381,43) Error: Can't determine which overloaded function to call

Hum, here no problems.
What version of uos are you using?

The last is this:  https://github.com/fredvs/uos/archive/refs/heads/main.zip

What example are you compiling to get that error?

I'm using the latest  ;)
Doesn't matter what example, as long as I use the unmodified uos.pas the compilers throws this error.

Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Fred vS on February 06, 2023, 04:54:04 pm
SimpleRecorder: I'd say latency is ~1/4s, just like in my program.

Sorry but I did not understand.  :-[
Is the sound ok or not?

Sound is (and was alaways) okay, but processing time (=latency) is too long for my purposes. Between me speaking in the mic and hearing my voice in the speaker is ~250ms  :o

ofc Win was and is a pain in the a** concerning these kind of things.  :(

If you want a shorter delay, you have to play with the FramesCount parameter of input and output.

     
Code: Pascal  [Select][+][-]
  1. uos_AddFromDevIn(0, -1, -1, -1, -1, -1, -1, -1);  
  2.       /// add Input from mic/aux into IN device with custom parameters
  3.       //////////// PlayerIndex : Index of a existing Player
  4.       //////////// Device ( -1 is default Input device )
  5.       //////////// Latency  ( -1 is latency suggested ) )
  6.       //////////// SampleRate : delault : -1 (44100)
  7.       //////////// OutputIndex : OutputIndex of existing Output // -1 : all output, -2: no output, other integer : existing output)
  8.       //////////// SampleFormat : -1 default : Int16 : (0: Float32, 1:Int32, 2:Int16)
  9.       //////////// FramesCount : -1 default : 4096   ( > = safer, < =  better latency )
  10.  
  11.   uos_AddIntoDevOut(0, -1, -1, -1, -1, 1,-1, -1);
  12.       //// add a Output into device with custom parameters
  13.       //////////// PlayerIndex : Index of a existing Player
  14.       //////////// Device ( -1 is default Output device )
  15.       //////////// Latency  ( -1 is latency suggested ) )
  16.       //////////// SampleRate : delault : -1 (44100)
  17.       //////////// Channels : delault : -1 (2:stereo) (0: no channels, 1:mono, 2:stereo, ...)
  18.       //////////// SampleFormat : -1 default : Int16 : (0: Float32, 1:Int32, 2:Int16)
  19.       //////////// FramesCount : -1 default : 65536
  20.       // ChunkCount : default : -1 (= 512)
  21.       // ChunkCount : default : -1 (= 512)
  22.  

How little is the FramesCount how short is the delay.
And play also with the Latency parameter.

I agree it is not easy to find the right combination for each sound card (plus Windows is not the King of Latency).
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Fred vS on February 06, 2023, 05:01:50 pm
I'm using the latest  ;)
Doesn't matter what example, as long as I use the unmodified uos.pas the compilers throws this error.

Bizarre...
I will be very happy to find out how you did to get that error.

Are you using Lazarus IDE and the *.lpi uos-examples or do you compile the examples a other way?
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 06, 2023, 05:16:18 pm

If you want a shorter delay, you have to play with the FramesCount parameter of input and output.

     
Code: Pascal  [Select][+][-]
  1. ....

How little is the FramesCount how short is the delay.
And play also with the Latency parameter.

I agree it is not easy to find the right combination for each sound card (plus Windows is not the King of Latency).

Values I tried so far only increase latency  :(
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 06, 2023, 05:18:47 pm

If you want a shorter delay, you have to play with the FramesCount parameter of input and output.

     
Code: Pascal  [Select][+][-]
  1. ....

How little is the FramesCount how short is the delay.
And play also with the Latency parameter.

I agree it is not easy to find the right combination for each sound card (plus Windows is not the King of Latency).

Values I tried so far only increase latency  :(

Ah, cutting down chunk size was needed, too.
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Fred vS on February 06, 2023, 05:21:17 pm

If you want a shorter delay, you have to play with the FramesCount parameter of input and output.

     
Code: Pascal  [Select][+][-]
  1. ....

How little is the FramesCount how short is the delay.
And play also with the Latency parameter.

I agree it is not easy to find the right combination for each sound card (plus Windows is not the King of Latency).

Values I tried so far only increase latency  :(

Even with a little FramesCount for input and output?
Did you try with a other Windows audio app that can do "direct-wire" (input directly to output) ?
Do you get better result?
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 07, 2023, 06:39:58 am
Frame size together with chunk size improves latency.  :)
I'll have to add a pair of sliders to allow fine tuning on different machines.
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Fred vS on February 07, 2023, 02:33:20 pm
Hello.

I dont use Windows any more but, like metis noted: https://forum.lazarus.freepascal.org/index.php/topic,62191.msg470140.html#msg470140
there is ASIO4ALL that gives better latency.
PortAudio can deal with it, just install it and choose it as device.
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: metis on February 07, 2023, 04:21:53 pm
As mentioned in the OP, "to route MicIn to SpkOut" on Win11 64bit, see the
JACKROUTER setup guide for windows 10 64bit (https://www.youtube.com/watch?v=zzd7naKzRgw) (YouTube).

With PortAudio, You have to select the 'JackRouter' as shown in the attached Pic.
(This is, how I use PortAudio+JACK on my WinXP, but it should work the same Way on Win11.)
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: Hobbit1972 on February 08, 2023, 07:57:23 am
I'll have a look at the jackrouter/asio4all. Tx.
Title: Re: [UOS/PortAudio] Minimizing Audio Latency?
Post by: metis on February 09, 2023, 05:27:33 pm
Here's another YouTube-Video that should suit your Case: Ableton Tutorial: Using JACK for Windows with Ableton Live (https://m.youtube.com/watch?v=1-OXTmpygCk).
See @ 06:13 for how to set up the JACK-Router for 'PortAudio', and
see @ 09:02 for how to set up JACK-Connections.

It's shown on Win7, but Things shouldn't have changed too much since then.
TinyPortal © 2005-2018