Add Pascal API for reading wave files (#1243)

This commit is contained in:
Fangjun Kuang
2024-08-11 22:43:42 +08:00
committed by GitHub
parent 968623a477
commit 65f1c0fab2
8 changed files with 261 additions and 3 deletions

View 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.

View 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.