Recent

Author Topic: Output buffer to audio.  (Read 988 times)

Dzandaa

  • Sr. Member
  • ****
  • Posts: 447
  • From C# to Lazarus
Output buffer to audio.
« on: June 10, 2025, 04:03:45 pm »
Hi,

I have an asynchronous buffer that contains audio data.
What is the best and simplest unit/library to send this data to the audio system?
Portable in Linux and Windows.

Thank you.

B->
Regards,
Dzandaa

cdbc

  • Hero Member
  • *****
  • Posts: 2207
    • http://www.cdbc.dk
Re: Output buffer to audio.
« Reply #1 on: June 10, 2025, 04:11:18 pm »
Hi
UOS or BASS...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Dzandaa

  • Sr. Member
  • ****
  • Posts: 447
  • From C# to Lazarus
Re: Output buffer to audio.
« Reply #2 on: June 12, 2025, 04:58:40 pm »
Hi,

UOS seems great, but complicated (Lots of files to include).

I just have a float buffer and a sampling rate.

B->
Regards,
Dzandaa

Fred vS

  • Hero Member
  • *****
  • Posts: 3622
    • StrumPract is the musicians best friend
Re: Output buffer to audio.
« Reply #3 on: June 12, 2025, 06:00:36 pm »
Hi,

UOS seems great, but complicated (Lots of files to include).

I just have a float buffer and a sampling rate.

B->

It all depends on your needs and the audio format of the buffer.
If you only want to play this buffer, only the PortAudio and SndFile library dependencies are required.
You can dynamically load these libraries from the uos package or use the ones installed on your system.

You may take a look at demo: https://github.com/fredvs/uos/blob/main/examples/consoleplaymemorybuffer.pas
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Dzandaa

  • Sr. Member
  • ****
  • Posts: 447
  • From C# to Lazarus
Re: Output buffer to audio.
« Reply #4 on: June 13, 2025, 10:39:06 am »
Hi
@Fred:

I saw this example, my problem is that I don't have any "bufferinfos", just an array of doubles without compression.

Thanks anyway.

B->
Regards,
Dzandaa

Fred vS

  • Hero Member
  • *****
  • Posts: 3622
    • StrumPract is the musicians best friend
Re: Output buffer to audio.
« Reply #5 on: June 13, 2025, 10:57:36 am »
Hi
@Fred:

I saw this example, my problem is that I don't have any "bufferinfos", just an array of doubles without compression.

Thanks anyway.

B->

Hello.

You may do a custom bufferinfos.
Here how to do if you have already a filled buffer (thebuffer), but look at the consoleplaymemorybuffer.pas for all the varables needed.


Code: Pascal  [Select][+][-]
  1.  
  2.  var
  3.   ...
  4.   thebufferinfos: TuosF_BufferInfos;    
  5.  
  6. // Load the libraries
  7.    res := uos_LoadLib(PChar(PA_FileName), PChar(SF_FileName), nil, nil, nil, nil, nil);
  8.  
  9.     if res = 0 then
  10.     begin  
  11.      
  12.       PlayerIndex1 := 0;
  13.       uos_CreatePlayer(PlayerIndex1);  
  14.  
  15.       // assign bufferinfos acording to your buffer
  16.       thebufferinfos.Channels:=2;
  17.       thebufferinfos.SampleRate:=44100;
  18.       thebufferinfos.LibOpen:=1; //sndfile
  19.       thebufferinfos.Ratio:=1;
  20.  
  21.        input1 := uos_AddFromMemoryBuffer(PlayerIndex1, thebuffer, thebufferinfos, -1, 1024);
  22.    
  23.        uos_AddIntoDevOut(PlayerIndex1, -1, -1, uos_inputgetSampleRate(PlayerIndex1, input1),
  24.         uos_inputgetChannels(PlayerIndex1, input1), 0, 1024, -1);
  25.  
  26.  
  27.        uos_Play(PlayerIndex1);
  28.  
  29.        sleep(2000);    // needed in console app
  30.  
  31.  end;
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Dzandaa

  • Sr. Member
  • ****
  • Posts: 447
  • From C# to Lazarus
Re: Output buffer to audio.
« Reply #6 on: June 13, 2025, 12:16:36 pm »
Hi Fred,

Thank you again,

Here is my code (Just have to add library in the same directory of the executable);

Don't know what is wrong.

Perhaps my data or the timer I use.

Thanks.

B->
 P.S. are you from Belgium?
Regards,
Dzandaa

Fred vS

  • Hero Member
  • *****
  • Posts: 3622
    • StrumPract is the musicians best friend
Re: Output buffer to audio.
« Reply #7 on: June 13, 2025, 12:52:10 pm »
Hello Dzandaa.

Hum, I am a few lost in your code, I dont get what is the goal of the program.

If I understood ok, you want to play the sine-wave from the charts. (is it so)?

But I dont understand what you want to do with the timer.

Would it not be simpler to create a buffer from the uos-synthesizer?

You may take a look at /uos/examples/consolesynth.pas and when you change something in the chart, reflects it to the wave-synth ?

Quote
P.S. are you from Belgium?
Yes, I was born in Bruxelles.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Dzandaa

  • Sr. Member
  • ****
  • Posts: 447
  • From C# to Lazarus
Re: Output buffer to audio.
« Reply #8 on: June 13, 2025, 01:46:54 pm »
Hi
@Fred:

I'd like to hear the sound corresponding to the SinData Buffer looping when you press "Play"
The buffer is updated in Updatechart (line 228) each time you change the number of sine waves.

I don't know if a timer is useful.

Just an exercise.

B->

P.S. We are neighbors, within a few dozen kilometers.
Regards,
Dzandaa

Fred vS

  • Hero Member
  • *****
  • Posts: 3622
    • StrumPract is the musicians best friend
Re: Output buffer to audio.
« Reply #9 on: June 13, 2025, 01:57:51 pm »
Re-hello Dzandaa.

In the case of you want to create a sine-wav buffer that can have up to 10 different sine-wave playing together and if you are ok to use the uos-synthesizer to create the buffer, here some tips how to do (using consolesynth.pas as template)

Code: Pascal  [Select][+][-]
  1. // add the different sine-wave, here for 3 sine-wave.
  2.  inindex1 :=
  3.           uos_AddFromSynth(PlayerIndex1, -1, -1, -1, 420, 420, -1, -1, -1, -1, -1, 0, -1, -1, -1);
  4.  
  5.  inindex2 :=
  6.           uos_AddFromSynth(PlayerIndex1, -1, -1, -1, 520, 520, -1, -1, -1, -1, -1, 0, -1, -1, -1);
  7.  
  8.  inindex3 :=
  9.           uos_AddFromSynth(PlayerIndex1, -1, -1, -1, 620, 620, -1, -1, -1, -1, -1, 0, -1, -1, -1);

And when you want to enable/disable or change the sine of each AddFromSynth():
Code: Pascal  [Select][+][-]
  1.     uos_InputSetSynth(PlayerIndex1, inindex1, -1, -1, 420, 420, -1, -1, -1, 0, -1, True);  // only change first sine-wave
  2.     uos_InputSetSynth(PlayerIndex1, inindex2, -1, -1, 520, 520, -1, -1, -1, 0, -1, false);    // disable second  sine-wave.
  3.     uos_InputSetSynth(PlayerIndex1, inindex3, -1, -1, 620, 620, -1, -1, -1, 0, -1, false);    // disable 3th  sine-wave.

Here for example if you want sine-wave 1 + sine-wave 2:
Code: Pascal  [Select][+][-]
  1.     uos_InputSetSynth(PlayerIndex1, inindex1, -1, -1, 420, 420, -1, -1, -1, 0, -1, True);  // only change first sine-wave
  2.     uos_InputSetSynth(PlayerIndex1, inindex2, -1, -1, 520, 520, -1, -1, -1, 0, -1, true);    // enable second  sine-wave.
  3.     uos_InputSetSynth(PlayerIndex1, inindex3, -1, -1, 620, 620, -1, -1, -1, 0, -1, false);    // disable 3th  sine-wave.


[EDITED] Fixed code.

« Last Edit: June 13, 2025, 02:34:39 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3622
    • StrumPract is the musicians best friend
Re: Output buffer to audio.
« Reply #10 on: June 13, 2025, 02:08:48 pm »
Hi
I'd like to hear the sound corresponding to the SinData Buffer looping when you press "Play"
The buffer is updated in Updatechart (line 228) each time you change the number of sine waves.
Ok, but, imho, using the build-in uos-synthesizer, all could be simpler (see previous post) [EDIT: previous post was updated].
Also you will need only one library: PortAudio.


Quote
P.S. We are neighbors, within a few dozen kilometers.
Nice !  ;D (But those last decades I am your neighbor a few of dozen thousands kilometers away. :-X )
« Last Edit: June 13, 2025, 02:39:53 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Dzandaa

  • Sr. Member
  • ****
  • Posts: 447
  • From C# to Lazarus
Re: Output buffer to audio.
« Reply #11 on: June 13, 2025, 03:15:09 pm »
Hi,

@Fred:

I'll try that suggestion for the fun, but this program is just a test of outputting to audio.

In the "real" program, I receive complex data in a buffer (not a sum of sines as in my example), I do an FFT on the signal, remove some frequencies, do an inverse FFT and receive a buffer of double with the resulting signal. And I want to heard that signal :)

B->

Regards,
Dzandaa

Fred vS

  • Hero Member
  • *****
  • Posts: 3622
    • StrumPract is the musicians best friend
Re: Output buffer to audio.
« Reply #12 on: June 13, 2025, 03:54:49 pm »
Hi,

@Fred:

I'll try that suggestion for the fun, but this program is just a test of outputting to audio.

In the "real" program, I receive complex data in a buffer (not a sum of sines as in my example), I do an FFT on the signal, remove some frequencies, do an inverse FFT and receive a buffer of double with the resulting signal. And I want to heard that signal :)

B->

OK, I see, I will try to give you something (but asap, sorry I am totally full).
But for this a Loopproc should be used instead of a timer.
And for updating the buffer using uos_InputAddDSP() could be ok.
You may see how it work in Simple Player demo.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Dzandaa

  • Sr. Member
  • ****
  • Posts: 447
  • From C# to Lazarus
Re: Output buffer to audio.
« Reply #13 on: June 13, 2025, 04:11:15 pm »
Hi,
@Fred:

Thank you for your patience.
I tried using the synthesizer, but I'm not getting any sound output.
It must have been an error on my part, either in the data or in my understanding of UOS.
It's not a big deal, so don't worry about it.
Here's my synthesizer version.
Have a nice day.

B->
Regards,
Dzandaa

Fred vS

  • Hero Member
  • *****
  • Posts: 3622
    • StrumPract is the musicians best friend
Re: Output buffer to audio.
« Reply #14 on: June 13, 2025, 04:36:02 pm »
Hi,
@Fred:

Thank you for your patience.
I tried using the synthesizer, but I'm not getting any sound output.
It must have been an error on my part, either in the data or in my understanding of UOS.
It's not a big deal, so don't worry about it.
Here's my synthesizer version.
Have a nice day.

B->

Hello Dzandaa.

I did not check yet you last demo, will do tonight.

But in your previous demo, you forgot to add a Device Out (device for the sound card, so it is normal to have no sound).
Code: Pascal  [Select][+][-]
  1. InputIndex := uos_AddFromMemoryBuffer(PlayerIndex, SinData, Bufferinfos, -1, 4096);
  2.  
  3. uos_AddIntoDevOut(PlayerIndex, -1, -1, SamplingFrequency, 2, 0, 1024, -1); // add this to get sound

Doing this you will get some kind of sound in your previous demo (but not ok, a loop proc must be used vs timer).

Tonight I hope to find some time to check all this.
             
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018