-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
ChatGPT.SoundRecorder.pas
88 lines (74 loc) · 2.39 KB
/
ChatGPT.SoundRecorder.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
unit ChatGPT.SoundRecorder;
interface
uses
System.Classes, System.SysUtils, FMX.Media;
type
TAudioRecord = class(TComponent)
private
FFileName: string;
FMicrophone: TAudioCaptureDevice;
FOnStartRecord: TNotifyEvent;
procedure MicrophonePermissionRequest(Sender: TObject; const&Message: string; const AccessGranted: Boolean);
procedure SetOnStartRecord(const Value: TNotifyEvent);
function GetIsAvailableDevice: Boolean;
public
function IsMicrophoneRecording: Boolean;
constructor Create(AOwner: TComponent); override;
procedure StartRecord(const FileName: string);
procedure StopRecord;
property Microphone: TAudioCaptureDevice read FMicrophone write FMicrophone;
property OnStartRecord: TNotifyEvent read FOnStartRecord write SetOnStartRecord;
property IsAvailableDevice: Boolean read GetIsAvailableDevice;
end;
implementation
{ TAudioRecord }
constructor TAudioRecord.Create(AOwner: TComponent);
begin
inherited;
FMicrophone := nil;// TCaptureDeviceManager.Current.DefaultAudioCaptureDevice;
if Assigned(FMicrophone) then
FMicrophone.OnPermissionRequest := MicrophonePermissionRequest;
end;
function TAudioRecord.GetIsAvailableDevice: Boolean;
begin
Result := Assigned(FMicrophone);
end;
function TAudioRecord.IsMicrophoneRecording: Boolean;
begin
if not Assigned(FMicrophone) then
Exit(False);
Result := FMicrophone.State = TCaptureDeviceState.Capturing;
end;
procedure TAudioRecord.MicrophonePermissionRequest(Sender: TObject; const Message: string; const AccessGranted: Boolean);
begin
if not Assigned(FMicrophone) then
Exit;
if AccessGranted then
begin
FMicrophone.StartCapture;
if Assigned(FOnStartRecord) then
FOnStartRecord(Self);
end
else
raise Exception.Create('Cannot record audio without the relevant permission being granted: '#13#10 + Message);
end;
procedure TAudioRecord.SetOnStartRecord(const Value: TNotifyEvent);
begin
FOnStartRecord := Value;
end;
procedure TAudioRecord.StartRecord(const FileName: string);
begin
if not Assigned(FMicrophone) then
raise Exception.Create('No suitable device');
FFileName := FileName;
FMicrophone.FileName := FFileName;
FMicrophone.RequestPermission;
end;
procedure TAudioRecord.StopRecord;
begin
if not Assigned(FMicrophone) then
raise Exception.Create('No suitable device');
if IsMicrophoneRecording then
FMicrophone.StopCapture;
end;
end.