sherpa_onnx.pas
1.7 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
{ Copyright (c) 2024 Xiaomi Corporation }
unit sherpa_onnx;
{$mode objfpc}
interface
type
TSherpaOnnxWave = record
Samples: array of Single; { normalized to the range [-1, 1] }
SampleRate: Integer;
end;
{ It supports reading a single channel wave with 16-bit encoded samples.
Samples are normalized to the range [-1, 1].
}
function SherpaOnnxReadWave(Filename: string): TSherpaOnnxWave;
implementation
uses
ctypes;
const
{See https://www.freepascal.org/docs-html/prog/progap7.html}
{$IFDEF WINDOWS}
SherpaOnnxLibName = 'sherpa-onnx-c-api.dll';
{$ENDIF}
{$IFDEF DARWIN}
SherpaOnnxLibName = 'sherpa-onnx-c-api';
{$linklib sherpa-onnx-c-api}
{$ENDIF}
{$IFDEF LINUX}
SherpaOnnxLibName = 'libsherpa-onnx-c-api.so';
{$ENDIF}
type
SherpaOnnxWave = record
Samples: pcfloat;
SampleRate: cint32;
NumSamples: cint32;
end;
PSherpaOnnxWave = ^SherpaOnnxWave;
function SherpaOnnxReadWaveWrapper(Filename: PAnsiChar): PSherpaOnnxWave; cdecl;
external SherpaOnnxLibName name 'SherpaOnnxReadWave';
procedure SherpaOnnxFreeWaveWrapper(P: PSherpaOnnxWave); cdecl;
external SherpaOnnxLibName name 'SherpaOnnxFreeWave';
function SherpaOnnxReadWave(Filename: string): TSherpaOnnxWave;
var
AnsiFilename: AnsiString;
PFilename: PAnsiChar;
PWave: PSherpaOnnxWave;
I: Integer;
begin
AnsiFilename := Filename;
PFilename := PAnsiChar(AnsiFilename);
PWave := SherpaOnnxReadWaveWrapper(PFilename);
SetLength(Result.Samples, PWave^.NumSamples);
Result.SampleRate := PWave^.SampleRate;
for I := Low(Result.Samples) to High(Result.Samples) do
Result.Samples[i] := PWave^.Samples[i];
SherpaOnnxFreeWaveWrapper(PWave);
end;
end.