-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmain_sd.pas
More file actions
241 lines (197 loc) · 6.61 KB
/
main_sd.pas
File metadata and controls
241 lines (197 loc) · 6.61 KB
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
unit main_sd;
{$mode objfpc}{$H+}
interface
uses
SysUtils,
Forms,
Classes,
StdCtrls,
ExtCtrls,
uos_flat;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure Timer1Timer(Sender: TObject);
private
{ private declarations }
sound: array[0..2] of string;
ms: array[0..2] of Tmemorystream;
posi: integer;
drum_beats: array[0..2] of string;
public
{ public declarations }
end;
var
Form1: TForm1;
stopit: Boolean = False;
allok: Boolean = False;
x: integer = 0;
channels: cardinal = 2; // stereo output
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Timer1Timer(Sender: TObject);
var
i: integer;
ge: Boolean = False;
begin
Timer1.Enabled := False;
if stopit = False then
begin
for i := 0 to 2 do
if (Copy(drum_beats[i], posi, 1) = 'x') then
uos_PlaynofreePaused(i)// uos_InputSetDSPVolume(i,0,0.5,0.5,true);
;
application.ProcessMessages; // yes or no ?
// if uos_SetGlobalEvent(true) was executed --> This set events (like pause/replay threads) to global.
// One event (for example uos_replay) will have impact on all players.
for i := 0 to 2 do
if (ge = False) and (Copy(drum_beats[i], posi, 1) = 'x') then
begin
uos_RePlay(i);
ge := True; // A uos_replay() of each player will have impact on all players.
end;
Inc(posi);
if (posi > 16) then
posi := 1;
Timer1.Enabled := True;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.interval := StrToInt(edit1.Text);
stopit := False;
posi := 1;
Timer1.Enabled := True;
edit1.Enabled:=false;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
stopit := True;
edit1.Enabled:=true;
end;
procedure TForm1.FormActivate(Sender: TObject);
var
ordir: string;
lib1, lib2: string;
i: integer;
begin
ordir := Application.Location;
{$IFDEF Windows}
{$if defined(cpu64)}
lib1 := ordir + 'lib\Windows\64bit\LibPortaudio-64.dll';
lib2 := ordir + 'lib\Windows\64bit\LibSndFile-64.dll';
{$else}
lib1 := ordir + 'lib\Windows\32bit\LibPortaudio-32.dll';
lib2 := ordir + 'lib\Windows\32bit\LibSndFile-32.dll';
{$endif}
{$ENDIF}
{$if defined(CPUAMD64) and defined(linux) }
lib1 := ordir + 'lib/Linux/64bit/LibPortaudio-64.so';
lib2 := ordir + 'lib/Linux/64bit/LibSndFile-64.so';
{$ENDIF}
{$if defined(cpu86) and defined(linux)}
lib1 := ordir + 'lib/Linux/32bit/LibPortaudio-32.so';
lib2 := ordir + 'lib/Linux/32bit/LibSndFile-32.so';
{$ENDIF}
{$if defined(linux) and defined(cpuarm)}
lib1 := ordir + 'lib/Linux/arm_raspberrypi/libportaudio-arm.so';
lib2 := ordir + 'lib/Linux/arm_raspberrypi/libsndfile-arm.so';
{$ENDIF}
{$if defined(linux) and defined(cpuaarch64)}
lib1 := ordir + 'lib/Linux/aarch64_raspberrypi/libportaudio_aarch64.so';
lib2 := ordir + 'lib/Linux/aarch64_raspberrypi/libsndfile_aarch64.so';
{$ENDIF}
{$if defined(CPUAMD64) and defined(openbsd) }
lib1 := ordir + 'lib/OpenBSD/64bit/LibPortaudio-64.so';
lib2 := ordir + 'lib/OpenBSD/64bit/LibSndFile-64.so';
{$ENDIF}
{$IFDEF freebsd}
{$if defined(cpu64)}
lib1 := ordir + 'lib/FreeBSD/64bit/libportaudio-64.so' ;
lib2 := ordir + 'lib/FreeBSD/64bit/libportaudio-64.so' ;
{$else}
lib1 := ordir + 'lib/FreeBSD/32bit/libportaudio-32.so';
lib2 := ordir + 'lib/FreeBSD/32bit/libportaudio-32.so' ;
{$endif}
{$ENDIF}
{$IFDEF Darwin}
{$IFDEF CPU32}
ordir := copy(ordir, 1, Pos('/UOS', ordir) - 1);
lib1 := ordir + '/lib/Mac/32bit/LibPortaudio-32.dylib';
lib2 := ordir + '/lib/Mac/32bit/LibSndFile-32.dylib';
{$ENDIF}
{$IFDEF CPU64}
ordir := copy(ordir, 1, Pos('/UOS', ordir) - 1);
lib1 := opath + '/lib/Mac/64bit/LibPortaudio-64.dylib';
lib2 := opath + '/lib/Mac/64bit/LibSndFile-64.dylib';
{$ENDIF}
{$ENDIF}
{$if defined(CPUAMD64) and defined(linux) }
// For Linux amd64, check libsndfile.so
if lib2 <> 'system' then
if uos_TestLoadLibrary(PChar(lib2)) = false then
lib2 := lib2 + '.2';
if lib1 <> 'system' then
if uos_TestLoadLibrary(PChar(lib1)) = false then
lib1 := lib1 + '.2';
{$endif}
uos_LoadLib(PChar(lib1), PChar(lib2), nil, nil, nil, nil, nil);
sound[0] := Application.Location + 'sound' + directoryseparator + 'drums' + directoryseparator + 'HH.wav';
sound[1] := Application.Location + 'sound' + directoryseparator + 'drums' + directoryseparator + 'SD.wav';
sound[2] := Application.Location + 'sound' + directoryseparator + 'drums' + directoryseparator + 'BD.wav';
// { // using memorystream
ms[0] := TMemoryStream.Create;
ms[0].LoadFromFile(PChar(sound[0]));
ms[0].Position := 0;
ms[1] := TMemoryStream.Create;
ms[1].LoadFromFile(PChar(sound[1]));
ms[1].Position := 0;
ms[2] := TMemoryStream.Create;
ms[2].LoadFromFile(PChar(sound[2]));
ms[2].Position := 0;
// }
drum_beats[0] := 'x0x0x0x0x0x0x0x0'; // hat
drum_beats[1] := '0000x0000000x000'; // snare
drum_beats[2] := 'x0000000x0x00000'; // kick
posi := 1;
for i := 0 to 2 do
if uos_CreatePlayer(i) then
if uos_SetGlobalEvent(i, True) then
// This set events (like pause/replay thread) to global.
//One event (for example replay) will have impact on all players.
// using memorystream
if uos_AddFromMemoryStream(i, ms[i], 0, -1, 0, 256) > -1 then
// using file
// if uos_AddFromfile(i,pchar(sound[i]),-1,0,256) > -1 then
// if uos_InputAddDSPVolume(i,0,1,1) > -1 then
if uos_AddFromEndlessMuted(i, channels, 256) > -1 then
// this for a dummy endless input, must be last input
{$if defined(cpuarm) or defined(cpuaarch64)} // need a lower latency
if uos_AddIntoDevOut(i, -1, 0.08, -1, -1, 0, 256, -1) > -1 then // stereo output
{$else}
if uos_AddIntoDevOut(i, -1, 0.03, -1, -1, 0, 256, -1) > -1 then // stereo output
{$endif}
begin
uos_PlayNoFree(i);
sleep(250);
allok := True;
end
else
allok := False;
// if allok = false then application.terminate;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
uos_free();
end;
end.