17.01.2013 Views

musicdsp.org source code archive - WSInf

musicdsp.org source code archive - WSInf

musicdsp.org source code archive - WSInf

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Fast LFO in Delphi... (click this to go back to the index)<br />

References : Posted by Dambrin Didier ( gol [AT] e-officedirect [DOT] com )<br />

Linked file : LFOGenerator.zip<br />

Notes :<br />

[from Didier's mail...]<br />

[see attached zip file too!]<br />

I was working on a flanger, & needed an LFO for it. I first used a Sin(), but it was too slow, then tried a big wavetable, but it wasn't accurate<br />

enough.<br />

I then checked the alternate sine generators from your web site, & while they're good, they all can drift, so you're also wasting too much CPU in<br />

branching for the drift checks.<br />

So I made a quick & easy linear LFO, then a sine-like version of it. Can be useful for LFO's, not to output as sound.<br />

If has no branching & is rather simple. 2 Abs() but apparently they're fast. In all cases faster than a Sin()<br />

It's in delphi, but if you understand it you can translate it if you want.<br />

It uses a 32bit integer counter that overflows, & a power for the sine output.<br />

If you don't know delphi, $ is for hex (h at the end in c++?), Single is 32bit float, integer is 32bit integer (signed, normally).<br />

Code :<br />

unit Unit1;<br />

interface<br />

uses<br />

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br />

StdCtrls, ExtCtrls, ComCtrls;<br />

type<br />

TForm1 = class(TForm)<br />

PaintBox1: TPaintBox;<br />

Bevel1: TBevel;<br />

procedure PaintBox1Paint(Sender: TObject);<br />

private<br />

{ Private declarations }<br />

public<br />

{ Public declarations }<br />

end;<br />

var<br />

Form1: TForm1;<br />

implementation<br />

{$R *.DFM}<br />

procedure TForm1.PaintBox1Paint(Sender: TObject);<br />

var n,Pos,Speed:Integer;<br />

Output,Scale,HalfScale,PosMul:Single;<br />

OurSpeed,OurScale:Single;<br />

begin<br />

OurSpeed:=100; // 100 samples per cycle<br />

OurScale:=100; // output in -100..100<br />

Pos:=0; // position in our linear LFO<br />

Speed:=Round($100000000/OurSpeed);<br />

// --- triangle LFO ---<br />

Scale:=OurScale*2;<br />

PosMul:=Scale/$80000000;<br />

// loop<br />

for n:=0 to 299 do<br />

Begin<br />

// inc our 32bit integer LFO pos & let it overflow. It will be seen as signed when read by the math unit<br />

Pos:=Pos+Speed;<br />

Output:=Abs(Pos*PosMul)-OurScale;<br />

// visual<br />

Paintbox1.Canvas.Pixels[n,Round(100+Output)]:=clRed;<br />

End;<br />

// --- sine-like LFO ---<br />

Scale:=Sqrt(OurScale*4);<br />

PosMul:=Scale/$80000000;<br />

HalfScale:=Scale/2;<br />

// loop<br />

for n:=0 to 299 do<br />

Begin

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!