-
Notifications
You must be signed in to change notification settings - Fork 1
/
portaudioheader.pas
294 lines (256 loc) · 8.71 KB
/
portaudioheader.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
unit PortAudioHeader;
// Copyright 2022-2024 Zoran Vučenović
// SPDX-License-Identifier: Apache-2.0
{
Some declarations from file portaudio.h, translated from C to Free Pascal.
http://www.portaudio.com/
http://files.portaudio.com/docs/v19-doxydocs/portaudio_8h_source.html
Not all declarations from the C file are included; the purpose of this
translation is to be used by Swan ZX Spectrum emulator for producing sound
and declarations were added only when needed there.
}
{$mode ObjFPC}{$H+}
{$J-}
{$PackRecords C}
interface
uses
SysUtils, dynlibs, ctypes;
const
paContinue = 0;
paComplete = 1;
paAbort = 2;
paFramesPerBufferUnspecified = 0;
paNoError = 0;
paMinimalErrorCode = -10000;
paNotInitialized = paMinimalErrorCode;
paUnanticipatedHostError = paMinimalErrorCode + 1;
paInvalidChannelCount = paMinimalErrorCode + 2;
paInvalidSampleRate = paMinimalErrorCode + 3;
paInvalidDevice = paMinimalErrorCode + 4;
paInvalidFlag = paMinimalErrorCode + 5;
paSampleFormatNotSupported = paMinimalErrorCode + 6;
paBadIODeviceCombination = paMinimalErrorCode + 7;
paInsufficientMemory = paMinimalErrorCode + 8;
paBufferTooBig = paMinimalErrorCode + 9;
paBufferTooSmall = paMinimalErrorCode + 10;
paNullCallback = paMinimalErrorCode + 11;
paBadStreamPtr = paMinimalErrorCode + 12;
paTimedOut = paMinimalErrorCode + 13;
paInternalError = paMinimalErrorCode + 14;
paDeviceUnavailable = paMinimalErrorCode + 15;
paIncompatibleHostApiSpecificStreamInfo = paMinimalErrorCode + 16;
paStreamIsStopped = paMinimalErrorCode + 17;
paStreamIsNotStopped = paMinimalErrorCode + 18;
paInputOverflowed = paMinimalErrorCode + 19;
paOutputUnderflowed = paMinimalErrorCode + 20;
paHostApiNotFound = paMinimalErrorCode + 21;
paInvalidHostApi = paMinimalErrorCode + 22;
paCanNotReadFromACallbackStream = paMinimalErrorCode + 23;
paCanNotWriteToACallbackStream = paMinimalErrorCode + 24;
paCanNotReadFromAnOutputOnlyStream = paMinimalErrorCode + 25;
paCanNotWriteToAnInputOnlyStream = paMinimalErrorCode + 26;
paIncompatibleStreamHostApi = paMinimalErrorCode + 27;
paBadBufferPtr = paMinimalErrorCode + 28;
type
TPaTime = cdouble;
TPaStreamCallbackTimeInfo = record
InputBufferAdcTime: TPaTime;
CurrentTime: TPaTime;
OutputBufferDacTime: TPaTime;
end;
PPaStreamCallbackTimeInfo = ^TPaStreamCallbackTimeInfo;
TPaStreamCallbackFlags = culong;
TPaStreamCallback =
function(Input: Pointer; Output: Pointer;
FrameCount: culong; TimeInfo: PPaStreamCallbackTimeInfo;
StatusFlags: TPaStreamCallbackFlags; UserData: Pointer): cint; cdecl;
TPaError = cint;
TPaStream = Pointer;
PPaStream = ^TPaStream;
// PPPaStream = ^PPaStream;
TPaSampleFormat = culong;
const
paFloat32 = TPaSampleFormat($00000001);
paInt32 = TPaSampleFormat($00000002);
paInt24 = TPaSampleFormat($00000004);
paInt16 = TPaSampleFormat($00000008);
paInt8 = TPaSampleFormat($00000010);
paUInt8 = TPaSampleFormat($00000020);
paCustomFormat = TPaSampleFormat($00010000);
paNonInterleaved = TPaSampleFormat($80000000);
var
Pa_Initialize: function(): TPaError; cdecl;
Pa_Terminate: function(): TPaError; cdecl;
Pa_OpenDefaultStream: function(Stream: PPaStream; NumInputChannels: cint;
NumOutputChannels: cint; SampleFormat: TPaSampleFormat; SampleRate: cdouble;
FramesPerBuffer: culong; StreamCallback: TPaStreamCallback; UserData: Pointer): TPaError; cdecl;
Pa_StartStream: function(Stream: TPaStream): TPaError; cdecl;
Pa_StopStream: function(Stream: TPaStream): TPaError; cdecl;
Pa_AbortStream: function(Stream: TPaStream): TPaError; cdecl;
Pa_CloseStream: function(Stream: TPaStream): TPaError; cdecl;
Pa_IsStreamStopped: function(Stream: TPaStream): TPaError; cdecl;
Pa_IsStreamActive: function(Stream: TPaStream): TPaError; cdecl;
Pa_GetErrorText: function(ErrorCode: TPaError): PAnsiChar; cdecl;
Pa_Sleep: procedure(MSec: clong); cdecl;
function TryLoadPortAudioLib(LibraryPath: String): Boolean;
procedure TryUnloadPortAudioLib;
function IsLoaded: Boolean;
implementation
var
LoadCount: Int64;
LibHandle: TLibHandle;
function EmptyInitializeTerminateFun(): TPaError; cdecl;
begin
Result := 0;
end;
{$push}
{$warn 5024 off : Parameter "$1" not used}
function EmptyOpenDefaultStreamFun(
Stream: PPaStream; NumInputChannels: cint;
NumOutputChannels: cint; SampleFormat: TPaSampleFormat; SampleRate: cdouble;
FramesPerBuffer: culong; StreamCallback: TPaStreamCallback; UserData: Pointer
): TPaError; cdecl;
begin
Result := 0;
end;
function EmptyStartStopAbortClose(Stream: TPaStream): TPaError; cdecl;
begin
Result := 0;
end;
var
EmptyStr: AnsiString;
function EmptyGetErrorText(ErrorCode: TPaError): PAnsiChar; cdecl;
begin
Result := PAnsiChar(EmptyStr);
end;
procedure EmptySleepProc(MSec: clong); cdecl;
begin
//
end;
{$pop}
procedure SetToEmptyProcedures;
begin
Pa_Initialize := @EmptyInitializeTerminateFun;
Pa_Terminate := @EmptyInitializeTerminateFun;
Pa_OpenDefaultStream := @EmptyOpenDefaultStreamFun;
Pa_StartStream := @EmptyStartStopAbortClose;
Pa_StopStream := @EmptyStartStopAbortClose;
Pa_AbortStream := @EmptyStartStopAbortClose;
Pa_CloseStream := @EmptyStartStopAbortClose;
Pa_IsStreamStopped := @EmptyStartStopAbortClose;
Pa_IsStreamActive := @EmptyStartStopAbortClose;
Pa_GetErrorText := @EmptyGetErrorText;
Pa_Sleep := @EmptySleepProc;
end;
function TryLoadPortAudioLib(LibraryPath: String): Boolean;
const
DefaultLibraryName =
{$if defined(mswindows)}
'portaudio.dll';
{$elseif defined(darwin)}
'libportaudio.dylib';
{$elseif defined(unix)}
'libportaudio.so.2';
{$else}
{$fatal OS not supported!}
{$endif}
{$ifdef mswindows}
var
Ext: String;
LibAux: String;
procedure TryWithBitness(S: String);
begin
S := S + IntToStr(SizeOf(Pointer) * 8);
LibHandle := SafeLoadLibrary(S + Ext);
if LibHandle = NilHandle then begin
S := 'lib' + S;
LibHandle := SafeLoadLibrary(S + Ext);
if LibHandle = NilHandle then
LibHandle := SafeLoadLibrary(S + 'bit' + Ext);
end;
end;
{$endif}
begin
if LoadCount = 0 then begin
Result := False;
if Trim(LibraryPath) = '' then begin
LibHandle := SafeLoadLibrary(DefaultLibraryName);
{$ifdef mswindows}
if LibHandle = NilHandle then begin
LibAux := ExtractFileName(DefaultLibraryName);
Ext := ExtractFileExt(LibAux);
LibAux := Copy(LibAux, 1, Length(LibAux) - Length(Ext));
TryWithBitness(LibAux);
if LibHandle = NilHandle then begin
TryWithBitness(LibAux + '-');
if LibHandle = NilHandle then
TryWithBitness(LibAux + '_');
end;
end;
{$endif}
end else
LibHandle := SafeLoadLibrary(LibraryPath);
if LibHandle = NilHandle then begin
SetToEmptyProcedures;
Exit;
end;
Pointer(Pa_Initialize) := GetProcAddress(LibHandle, 'Pa_Initialize');
Pointer(Pa_Terminate) := GetProcAddress(LibHandle, 'Pa_Terminate');
Pointer(Pa_OpenDefaultStream) := GetProcAddress(LibHandle, 'Pa_OpenDefaultStream');
Pointer(Pa_StartStream) := GetProcAddress(LibHandle, 'Pa_StartStream');
Pointer(Pa_StopStream) := GetProcAddress(LibHandle, 'Pa_StopStream');
Pointer(Pa_AbortStream) := GetProcAddress(LibHandle, 'Pa_AbortStream');
Pointer(Pa_CloseStream) := GetProcAddress(LibHandle, 'Pa_CloseStream');
Pointer(Pa_IsStreamStopped) := GetProcAddress(LibHandle, 'Pa_IsStreamStopped');
Pointer(Pa_IsStreamActive) := GetProcAddress(LibHandle, 'Pa_IsStreamActive');
Pointer(Pa_GetErrorText) := GetProcAddress(LibHandle, 'Pa_GetErrorText');
Pointer(Pa_Sleep) := GetProcAddress(LibHandle, 'Pa_Sleep');
if not (Assigned(Pa_Initialize) and Assigned(Pa_Terminate) and Assigned(Pa_OpenDefaultStream)
and Assigned(Pa_StartStream) and Assigned(Pa_StopStream) and Assigned(Pa_AbortStream)
and Assigned(Pa_CloseStream) and Assigned(Pa_IsStreamStopped) and Assigned(Pa_IsStreamActive)
and Assigned(Pa_GetErrorText) and Assigned(Pa_Sleep)
)
then begin
LoadCount := 1;
TryUnloadPortAudioLib;
Exit;
end;
end;
LoadCount := LoadCount + 1;
Result := True;
end;
procedure TryUnloadPortAudioLib;
begin
if LoadCount > 0 then begin
if LoadCount = 1 then begin
SetToEmptyProcedures;
if not UnloadLibrary(LibHandle) then
Exit;
LibHandle := NilHandle;
end;
LoadCount := LoadCount - 1;
end;
end;
function IsLoaded: Boolean;
begin
Result := LibHandle <> NilHandle;
end;
procedure Init;
begin
EmptyStr := '';
LibHandle := NilHandle;
LoadCount := 0;
SetToEmptyProcedures;
end;
procedure Final;
begin
if LoadCount > 0 then begin
LoadCount := 1;
TryUnloadPortAudioLib;
end;
end;
initialization
Init;
finalization
Final;
end.