Recent

Author Topic: Cross Platform Audio Recommendation anyone?  (Read 11600 times)

derek.john.evans

  • Guest
Re: Cross Platform Audio Recommendation anyone?
« Reply #15 on: September 16, 2015, 12:23:53 am »
I have tried to find samples of using LADSPA in Lazarus; but I cannot find any, probably me just googling for the wrong keywords.

I had a look at the LADSPA for FreePascal unit. Finally got it working, but its a complex solution. First up, there are 6 callback functions which need to be made stdcall. Otherwise it wont work.

To create a TLADSPAPlugin class, you use:
Code: Pascal  [Select][+][-]
  1.   uses LADSPA, dynlibs;
  2.   TLibHandle FLib := LoadLibrary(ProgramDirectory + 'plugin.dll');
  3.   FPlugin := TLADSPAPlugin.Create(FLib, 0);
  4.   FPlugin.Instantiate(44100, 1);    
  5.  

At that point, you should be able to get some information about the plugin. eg: PortCount, Port Names, Author, Copyright, etc.

But, thats where is starts getting hairy. LADSPA uses ports for both input/output samples, and for effect parameters. Its a neat trick, but can be hard to visualize. You setup a port array, which contains samples and parameters for the effect. (VST has separate parameters and sample processing buffers)
Code: Pascal  [Select][+][-]
  1.  FPorts: array of array of TLADSPA_Data;      
  2.  

The array should be initilized to default values, and then the plugin needs to be activated:
Code: Pascal  [Select][+][-]
  1.   SetLength(FPorts, FPlugin.PortCount, 1);
  2.   for LIndex := 0 to FPlugin.PortCount - 1 do begin
  3.     FPorts[LIndex][0] := 0.5;
  4.   end;
  5.   FPorts[6][0] := 1.2;
  6.   FPlugin.Activate;    
  7.  
The effect im testing is the 12 band EQ I posted, converted to LADSPA using Geep Jeez. The port description is this:
Code: [Select]
0 INPUT
1 INPUT
2 OUTPUT
3 OUTPUT
4 HPF
5 Low Shelf
6 80 Hz
7 150 Hz
8 250 Hz
9 400 Hz
10 630 Hz
11 800 Hz
12 1.6 kHz
13 3 kHz
14 5 kHz
15 7 kHz
16 10 kHz
17 12 kHz
18 LPF
19 Output Gain

Here is the hairy part. With Bass, you need to ensure you create streams with the flag BASS_SAMPLE_FLOAT. So, all the samples will be singles. If you setup a DSP callback, you will receive a buffer, and byte length. The samples are interleaved. ie: LRLRLRLRLR
Problem is, LADSPA wants all the left channel as port 0, and all the right channel as port 1. And, depending on the byte length given by BASS, you need to resize the input/output ports, and reconnect them before calling the LADSPA plugin.

Anyway, long story short, this is what you would be looking at having todo.
Code: Pascal  [Select][+][-]
  1. procedure TMyEffect.Process(const ABuffer: Pointer; const ALength: DWORD);
  2. var
  3.   LPtr: PSingle;
  4.   LP0, LP1: PSingle;
  5.   LIndex, LSampleCount, LPortSize: Integer;
  6. begin
  7.   LSampleCount := (ALength div SizeOf(Single)) div 2;
  8.   for LIndex := 0 to FPlugin.PortCount - 1 do begin
  9.     if FPlugin.IsInput(LIndex) or FPlugin.IsOutput(LIndex) then begin
  10.       LPortSize := LSampleCount;
  11.     end else begin
  12.       LPortSize := 1;
  13.     end;
  14.     if Length(FPorts[LIndex]) <> LPortSize then begin
  15.       SetLength(FPorts[LIndex], LPortSize);
  16.       FPlugin.ConnectPort(0, LIndex, @FPorts[LIndex][0]);
  17.     end;
  18.   end;
  19.   LP0 := @FPorts[0][0];
  20.   LP1 := @FPorts[1][0];
  21.   LPtr := ABuffer;
  22.   for LIndex := 0 to LSampleCount - 1 do begin
  23.     LP0[LIndex] := LPtr[0];
  24.     LP1[LIndex] := LPtr[1];
  25.     Inc(LPtr, 2);
  26.   end;
  27.   FPlugin.Run(LSampleCount);
  28.   LP0 := @FPorts[2][0];
  29.   LP1 := @FPorts[3][0];
  30.   LPtr := ABuffer;
  31.   for LIndex := 0 to LSampleCount - 1 do begin
  32.     LPtr[0] := LP0[LIndex];
  33.     LPtr[1] := LP1[LIndex];
  34.     Inc(LPtr, 2);
  35.   end;
  36. end;  
  37.  

That code isn't releasable. It assumes the DSP callback will be stereo and it assumes the effect is stereo and input ports are 0 & 1, and output ports are 2 & 3.

But, by using FPorts[6][0] := 1.2 I was able to increase the bass (80hz) of a MP3. ie: 6 is the port number for 80hz.

I guess, see what you think. Its a lot of fun getting it working, but, I can see a lot of work to be done to make it a solid solution. ie: support for any LADSPA effect, and autogenerating TForm's with sliders.

Hope that helps.
« Last Edit: October 02, 2015, 03:09:08 am by Geepster »

 

TinyPortal © 2005-2018