-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmrec.pas
215 lines (182 loc) · 5.4 KB
/
dmrec.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
{
Headesetcheck
Copyright (C) 2020 Marco Caselli <marcocas@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}
unit dmrec;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LazDyn_PortAudio, process, MouseAndKeyInput, LCLType, ctypes, Forms, ExtCtrls, Menus, LCLIntf;
type
{ Tdm }
Tdm = class(TDataModule)
N1: TMenuItem;
mnuCommand: TMenuItem;
mnuKey: TMenuItem;
mnuQuit: TMenuItem;
PopupMenu1: TPopupMenu;
TrayIcon1: TTrayIcon;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
procedure mnuKeyClick(Sender: TObject);
procedure mnuQuitClick(Sender: TObject);
private
PAParam: PaStreamParameters;
padevice: integer;
Handlest: PPaStream;
public
procedure Async(Data: PtrInt);
end;
var
dm: Tdm;
implementation
uses Dialogs;
const
// Find the rigth shared library for current platform
{$IFDEF LINUX}
PORTAUDIOLIB = 'libportaudio.so.2';
{$ENDIF LINUX}
{$IFDEF WINDOWS}
{$ifdef CPU64}
PORTAUDIOLIB = 'libportaudio-64.dll';
{$endif}
{$ifdef CPU32}
PORTAUDIOLIB = 'libportaudio-32.dll';
{$endif}
{$ENDIF WINDOWS}
{$IFDEF DARWIN}
PORTAUDIOLIB = 'LibPortaudio.dylib';
{$ENDIF DARWIN}
const
SAMPLE_RATE = 1000; // # Sample rate for our input stream
BLOCK_SIZE = 200; //# Number of samples before we trigger a processing callback
PRESS_SECONDS = 0.2; //# Number of seconds button should be held to register press
PRESS_SAMPLE_THRESHOLD = 60000; //# Signal amplitude to register as a button click
BLOCKS_TO_PRESS = (SAMPLE_RATE / BLOCK_SIZE) * PRESS_SECONDS;
/// some useful key code for multimedia control
// VK_VOLUME_MUTE = 173;
// VK_VOLUME_DOWN = 174;
// VK_VOLUME_UP = 175;
VK_MEDIA_NEXT_TRACK = 176;
VK_MEDIA_PREV_TRACK = 177;
// VK_MEDIA_STOP = 178;
VK_MEDIA_PLAY_PAUSE = 179;
var
is_held: boolean;
time_pressed: integer;
{$R *.lfm}
{ Tdm }
function StreamCallback(input: Pointer; output: Pointer; frameCount: CULong; timeInfo: PPaStreamCallbackTimeInfo; statusFlags: PaStreamCallbackFlags; userData: Pointer): CInt32; cdecl;
var
Mean: integer;
Buf: pcuint16;
Acc: int64;
i, j: integer;
begin
acc := 0;
buf := input;
for i := 0 to (frameCount) - 1 do
begin
Acc := acc + Buf^;
Inc(buf);
end;
Mean := Acc div (frameCount);
if Mean < PRESS_SAMPLE_THRESHOLD then
begin
Inc(time_pressed);
if (time_pressed > BLOCKS_TO_PRESS) and not is_held then
begin
is_held := True;
if GetKeyState(VK_SHIFT) < 0 then // is shift down
Application.QueueAsyncCall(@dm.Async, VK_MEDIA_NEXT_TRACK)
else
if GetKeyState(VK_CONTROL) < 0 then // is ctrl-down
Application.QueueAsyncCall(@dm.Async, VK_MEDIA_PREV_TRACK)
else
Application.QueueAsyncCall(@dm.Async, VK_MEDIA_PLAY_PAUSE);
end;
end
else
begin
is_held := False;
time_pressed := 0;
end;
Result := 0;
end;
procedure Tdm.DataModuleCreate(Sender: TObject);
var
err: PaError;
begin
Handlest := nil;
if not Pa_Load(PORTAUDIOLIB) then
begin
MessageDlg('Cannot load Portaudio library', mtError, [mbAbort], 0);
Halt(1);
end;
Err := Pa_Initialize();
if Err <> paError(paNoError) then
begin
MessageDlg('Portaudio error: ' + Pa_GetErrorText(err), mtError, [mbAbort], 0);
Halt(2);
end;
padevice := 0;
PAParam.HostApiSpecificStreamInfo := nil;
PAParam.device := padevice;
PAParam.SuggestedLatency := 0.0;
PAParam.SampleFormat := paInt16;
PAParam.channelCount := 1;
err := Pa_OpenStream(@HandleSt, @PAParam, nil, SAMPLE_RATE, BLOCK_SIZE, paClipOff, PPaStreamCallback(@StreamCallback), nil);
if Err <> paError(paNoError) then
begin
MessageDlg('Portaudio error: ' + Pa_GetErrorText(err), mtError, [mbAbort], 0);
Halt(3);
end;
is_held := True;
time_pressed := 0;
Err := Pa_StartStream(Handlest);
if Err <> paError(paNoError) then
begin
MessageDlg('Portaudio error: ' + Pa_GetErrorText(err), mtError, [mbAbort], 0);
Halt(4);
end;
end;
procedure Tdm.DataModuleDestroy(Sender: TObject);
begin
if Assigned(Handlest) then
Pa_StopStream(Handlest);
Pa_Unload();
end;
procedure Tdm.mnuKeyClick(Sender: TObject);
begin
(Sender as TMenuItem).Checked := True;
end;
procedure Tdm.mnuQuitClick(Sender: TObject);
begin
Application.Terminate;
end;
procedure Tdm.Async(Data: PtrInt);
var
dummy: string;
begin
// send virtual key press to O.S.
if mnuKey.Checked then
KeyInput.Press(Data);
// or you can easily run an external application
if mnuCommand.Checked then
case Data of
VK_MEDIA_NEXT_TRACK: RunCommand('C:\source\ovoplayer\bin\win32\ovoplayerctrl.exe', ['--next'], dummy);
VK_MEDIA_PREV_TRACK: RunCommand('C:\source\ovoplayer\bin\win32\ovoplayerctrl.exe', ['--previous'], dummy);
VK_MEDIA_PLAY_PAUSE: RunCommand('C:\source\ovoplayer\bin\win32\ovoplayerctrl.exe', ['--playpause'], dummy);
end;
end;
end.