source: trunk/Packages/uos/uos_soundtouch.pas

Last change on this file was 664, checked in by chronos, 3 days ago
  • Added: Ability to play music in background in start screen and in-game. Used uos as audio library.
  • Property svn:executable set to *
File size: 7.3 KB
Line 
1{This unit is part of United Openlibraries of Sound (uos)}
2
3{This is the Dynamic loading + Unix compatible version of SoundTouch Pascal Wrapper
4 from Sandro Cumerlato <sandro.cumerlato@gmail.com>.
5 of the original C version of Olli Parviainen <oparviai@iki.fi>.
6
7 Added BPMdetect method too.
8 Load library with St_load() and release with St_unload().
9 License : modified LGPL.
10 Fred van Stappen / fiens@hotmail.com}
11
12unit uos_soundtouch;
13
14{$mode objfpc}{$H+}
15
16{$PACKRECORDS C}
17
18interface
19
20uses
21 ctypes, DynLibs;
22
23const
24libst=
25
26{$IFDEF darwin}
27 'libSoundTouchDLL.dylib';
28 {$ELSE}
29 {$IFDEF unix}
30 'libSoundTouch.so.1';
31 {$ELSE}
32 {$if defined(cpu64)}
33 'SoundTouch_x64.dll';
34 {$else}
35 'SoundTouch.dll';
36 {$endif}
37 {$ENDIF}
38 {$ENDIF}
39
40{$IF not DEFINED(windows)}
41type
42 THandle = pointer;
43{$endif}
44
45type
46 Tt_bs2bdp = ^Tt_bs2bd;
47 Tt_bs2bd = packed record
48 level : CInt32;
49 srate : CInt32;
50 a0_lo : CDouble;
51 b1_lo : CDouble;
52 a0_hi : CDouble;
53 a1_hi : CDouble;
54 b1_hi : CDouble;
55 gain : CDouble;
56 lfs : packed record
57 asis : array[0..1] of cdouble;
58 lo : array[0..1] of cdouble;
59 hi : array[0..1] of cdouble;
60 end;
61 end;
62
63var
64 bpm_createInstance: function(chan: CInt32; sampleRate : CInt32): THandle; cdecl;
65 bpm_destroyInstance: procedure(h: THandle); cdecl;
66 bpm_getBpm: function(h: THandle): cfloat; cdecl;
67 bpm_putSamples: procedure(h: THandle; const samples: pcfloat;
68 numSamples: cardinal); cdecl;
69
70 soundtouch_clear: procedure(h: THandle); cdecl;
71 soundtouch_createInstance: function(): THandle; cdecl;
72 soundtouch_flush: procedure(h: THandle); cdecl;
73 soundtouch_getSetting: function(h: THandle; settingId: integer): integer; cdecl;
74 soundtouch_getVersionId: function(): cardinal; cdecl;
75 soundtouch_getVersionString2: procedure(VersionString: PAnsiChar;
76 bufferSize: integer); cdecl;
77 soundtouch_getVersionString: function(): PAnsiChar; cdecl;
78 soundtouch_isEmpty: function(h: THandle): integer; cdecl;
79 soundtouch_numSamples: function(h: THandle): cardinal; cdecl;
80 soundtouch_numUnprocessedSamples: function(h: THandle): cardinal; cdecl;
81 soundtouch_putSamples: procedure(h: THandle; const samples: pcfloat;
82 numSamples: cardinal); cdecl;
83 soundtouch_receiveSamples: function(h: THandle; outBuffer: pcfloat;
84 maxSamples: cardinal): cardinal; cdecl;
85 soundtouch_setChannels: procedure(h: THandle; numChannels: cardinal); cdecl;
86 soundtouch_setPitch: procedure(h: THandle; newPitch: single); cdecl;
87 soundtouch_setPitchOctaves: procedure(h: THandle; newPitch: single); cdecl;
88 soundtouch_setPitchSemiTones: procedure(h: THandle; newPitch: single); cdecl;
89 soundtouch_setRate: procedure(h: THandle; newRate: single); cdecl;
90 soundtouch_setRateChange: procedure(h: THandle; newRate: single); cdecl;
91 soundtouch_setSampleRate: procedure(h: THandle; srate: cardinal); cdecl;
92 soundtouch_destroyInstance: procedure(h: THandle); cdecl;
93 soundtouch_setSetting: function(h: THandle; settingId: integer; Value: integer): boolean; cdecl;
94 soundtouch_setTempo: procedure(h: THandle; newTempo: single); cdecl;
95 soundtouch_setTempoChange: procedure(h: THandle; newTempo: single); cdecl;
96
97 LibHandle:TLibHandle=dynlibs.NilHandle; // this will hold our handle for the lib
98 ReferenceCounter : cardinal = 0; // Reference counter
99
100function ST_IsLoaded : boolean; inline;
101function ST_Load(const libfilename: string): boolean; // load the lib
102procedure ST_Unload(); // unload and frees the lib from memory : do not forget to call it before close application.
103
104implementation
105
106function ST_IsLoaded: boolean;
107begin
108 Result := (LibHandle <> dynlibs.NilHandle);
109end;
110
111function ST_Load(const libfilename: string): boolean;
112var
113thelib: string;
114begin
115 Result := False;
116 if LibHandle<>0 then
117begin
118 Inc(ReferenceCounter);
119result:=true
120end else begin
121 if Length(libfilename) = 0 then thelib := libst else thelib := libfilename;
122 LibHandle:=DynLibs.SafeLoadLibrary(thelib); // obtain the handle we want.
123 if LibHandle <> DynLibs.NilHandle then
124 begin
125 try
126 Pointer(soundtouch_createInstance) :=
127 GetProcAddress(LibHandle, 'soundtouch_createInstance');
128 if Pointer(soundtouch_createInstance) = nil then // not the SoundTouchDLL library.
129 begin
130 ST_Unload;
131 result := false end
132 else
133 begin
134 Pointer(soundtouch_clear) :=
135 GetProcAddress(LibHandle, 'soundtouch_clear');
136 Pointer(soundtouch_destroyInstance) :=
137 GetProcAddress(LibHandle, 'soundtouch_destroyInstance');
138 Pointer(soundtouch_flush) :=
139 GetProcAddress(LibHandle, 'soundtouch_flush');
140 Pointer(soundtouch_getSetting) :=
141 GetProcAddress(LibHandle, 'soundtouch_getSetting');
142 Pointer(soundtouch_getVersionId) :=
143 GetProcAddress(LibHandle, 'soundtouch_getVersionId');
144 Pointer(soundtouch_getVersionString2) :=
145 GetProcAddress(LibHandle, 'soundtouch_getVersionString2');
146 Pointer(soundtouch_getVersionString) :=
147 GetProcAddress(LibHandle, 'soundtouch_getVersionString');
148 Pointer(soundtouch_isEmpty) :=
149 GetProcAddress(LibHandle, 'soundtouch_isEmpty');
150 Pointer(soundtouch_numSamples) :=
151 GetProcAddress(LibHandle, 'soundtouch_numSamples');
152 Pointer(soundtouch_numUnprocessedSamples) :=
153 GetProcAddress(LibHandle, 'soundtouch_numUnprocessedSamples');
154 Pointer(soundtouch_putSamples) :=
155 GetProcAddress(LibHandle, 'soundtouch_putSamples');
156 Pointer(soundtouch_receiveSamples) :=
157 GetProcAddress(LibHandle, 'soundtouch_receiveSamples');
158 Pointer(soundtouch_setChannels) :=
159 GetProcAddress(LibHandle, 'soundtouch_setChannels');
160 Pointer(soundtouch_setPitch) :=
161 GetProcAddress(LibHandle, 'soundtouch_setPitch');
162 Pointer(soundtouch_setPitchOctaves) :=
163 GetProcAddress(LibHandle, 'soundtouch_setPitchOctaves');
164 Pointer(soundtouch_setPitchSemiTones) :=
165 GetProcAddress(LibHandle, 'soundtouch_setPitchSemiTones');
166 Pointer(soundtouch_setRate) :=
167 GetProcAddress(LibHandle, 'soundtouch_setRate');
168 Pointer(soundtouch_setRateChange) :=
169 GetProcAddress(LibHandle, 'soundtouch_setRateChange');
170 Pointer(soundtouch_setSampleRate) :=
171 GetProcAddress(LibHandle, 'soundtouch_setSampleRate');
172 Pointer(soundtouch_setSetting) :=
173 GetProcAddress(LibHandle, 'soundtouch_setSetting');
174 Pointer(soundtouch_setTempo) :=
175 GetProcAddress(LibHandle, 'soundtouch_setTempo');
176 Pointer(soundtouch_setTempoChange) :=
177 GetProcAddress(LibHandle, 'soundtouch_setTempoChange');
178
179 Pointer(bpm_createInstance) :=
180 GetProcAddress(LibHandle, 'bpm_createInstance');
181 Pointer(bpm_destroyInstance) :=
182 GetProcAddress(LibHandle, 'bpm_destroyInstance');
183 Pointer(bpm_getBpm) :=
184 GetProcAddress(LibHandle, 'bpm_getBpm');
185 Pointer(bpm_putSamples) :=
186 GetProcAddress(LibHandle, 'bpm_putSamples');
187
188 Result := St_IsLoaded;
189 ReferenceCounter:=1;
190
191 end;
192
193 except
194 ST_Unload;
195 end;
196 end;
197end;
198end;
199
200procedure ST_Unload;
201begin
202// < Reference counting
203 if ReferenceCounter > 0 then
204 dec(ReferenceCounter);
205 if ReferenceCounter > 0 then
206 exit;
207 // >
208
209 if LibHandle <> DynLibs.NilHandle then
210 begin
211 DynLibs.UnloadLibrary(LibHandle);
212 LibHandle := DynLibs.NilHandle;
213 end;
214end;
215
216end.
Note: See TracBrowser for help on using the repository browser.