Add Pascal API for reading wave files (#1243)
This commit is contained in:
7
sherpa-onnx/pascal-api/README.md
Normal file
7
sherpa-onnx/pascal-api/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# Introduction
|
||||
|
||||
This directory contains APIs for [Object Pascal](https://en.wikipedia.org/wiki/Object_Pascal).
|
||||
|
||||
Please see
|
||||
https://github.com/k2-fsa/sherpa-onnx/tree/master/pascal-api-examples
|
||||
for usages.
|
||||
77
sherpa-onnx/pascal-api/sherpa_onnx.pas
Normal file
77
sherpa-onnx/pascal-api/sherpa_onnx.pas
Normal file
@@ -0,0 +1,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.
|
||||
Reference in New Issue
Block a user