Move structs to IntPtr

This commit is contained in:
Carlos Fonseca 2019-10-05 03:58:40 +00:00
parent 9ac8cebb3b
commit acabb26378
5 changed files with 14 additions and 60 deletions

View File

@ -1,10 +1,8 @@
using DeepSpeechClient.Interfaces; using DeepSpeechClient.Interfaces;
using DeepSpeechClient.Structs;
using DeepSpeechClient.Extensions; using DeepSpeechClient.Extensions;
using System; using System;
using System.IO; using System.IO;
using System.Runtime.InteropServices;
using DeepSpeechClient.Enums; using DeepSpeechClient.Enums;
namespace DeepSpeechClient namespace DeepSpeechClient
@ -14,9 +12,8 @@ namespace DeepSpeechClient
/// </summary> /// </summary>
public class DeepSpeech : IDeepSpeech public class DeepSpeech : IDeepSpeech
{ {
private unsafe ModelState** _modelStatePP; private unsafe IntPtr** _modelStatePP;
private unsafe ModelState* _modelStateP; private unsafe IntPtr** _streamingStatePP;
private unsafe StreamingState** _streamingStatePP;
@ -65,7 +62,6 @@ namespace DeepSpeechClient
aBeamWidth, aBeamWidth,
ref _modelStatePP); ref _modelStatePP);
EvaluateResultCode(resultCode); EvaluateResultCode(resultCode);
_modelStateP = *_modelStatePP;
} }
/// <summary> /// <summary>

View File

@ -52,8 +52,6 @@
<Compile Include="Models\MetadataItem.cs" /> <Compile Include="Models\MetadataItem.cs" />
<Compile Include="NativeImp.cs" /> <Compile Include="NativeImp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Structs\ModelState.cs" />
<Compile Include="Structs\StreamingState.cs" />
<Compile Include="Structs\Metadata.cs" /> <Compile Include="Structs\Metadata.cs" />
<Compile Include="Structs\MetadataItem.cs" /> <Compile Include="Structs\MetadataItem.cs" />
</ItemGroup> </ItemGroup>

View File

@ -1,5 +1,4 @@
using DeepSpeechClient.Enums; using DeepSpeechClient.Enums;
using DeepSpeechClient.Structs;
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -19,10 +18,10 @@ namespace DeepSpeechClient
internal unsafe static extern ErrorCodes DS_CreateModel(string aModelPath, internal unsafe static extern ErrorCodes DS_CreateModel(string aModelPath,
string aAlphabetConfigPath, string aAlphabetConfigPath,
uint aBeamWidth, uint aBeamWidth,
ref ModelState** pint); ref IntPtr** pint);
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)] [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)]
internal static unsafe extern ErrorCodes DS_EnableDecoderWithLM(ModelState** aCtx, internal static unsafe extern ErrorCodes DS_EnableDecoderWithLM(IntPtr** aCtx,
string aLMPath, string aLMPath,
string aTriePath, string aTriePath,
float aLMAlpha, float aLMAlpha,
@ -30,26 +29,26 @@ namespace DeepSpeechClient
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl, [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Ansi, SetLastError = true)] CharSet = CharSet.Ansi, SetLastError = true)]
internal static unsafe extern IntPtr DS_SpeechToText(ModelState** aCtx, internal static unsafe extern IntPtr DS_SpeechToText(IntPtr** aCtx,
short[] aBuffer, short[] aBuffer,
uint aBufferSize, uint aBufferSize,
uint aSampleRate); uint aSampleRate);
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static unsafe extern IntPtr DS_SpeechToTextWithMetadata(ModelState** aCtx, internal static unsafe extern IntPtr DS_SpeechToTextWithMetadata(IntPtr** aCtx,
short[] aBuffer, short[] aBuffer,
uint aBufferSize, uint aBufferSize,
uint aSampleRate); uint aSampleRate);
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)] [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)]
internal static unsafe extern void DS_FreeModel(ModelState** aCtx); internal static unsafe extern void DS_FreeModel(IntPtr** aCtx);
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)] [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)]
internal static unsafe extern ErrorCodes DS_CreateStream(ModelState** aCtx, internal static unsafe extern ErrorCodes DS_CreateStream(IntPtr** aCtx,
uint aSampleRate, ref StreamingState** retval); uint aSampleRate, ref IntPtr** retval);
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)] [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)]
internal static unsafe extern void DS_FreeStream(ref StreamingState** aSctx); internal static unsafe extern void DS_FreeStream(ref IntPtr** aSctx);
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)] [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)]
internal static unsafe extern void DS_FreeMetadata(IntPtr metadata); internal static unsafe extern void DS_FreeMetadata(IntPtr metadata);
@ -59,19 +58,19 @@ namespace DeepSpeechClient
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl, [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Ansi, SetLastError = true)] CharSet = CharSet.Ansi, SetLastError = true)]
internal static unsafe extern void DS_FeedAudioContent(StreamingState** aSctx, internal static unsafe extern void DS_FeedAudioContent(IntPtr** aSctx,
short[] aBuffer, short[] aBuffer,
uint aBufferSize); uint aBufferSize);
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)] [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)]
internal static unsafe extern string DS_IntermediateDecode(StreamingState** aSctx); internal static unsafe extern string DS_IntermediateDecode(IntPtr** aSctx);
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl, [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Ansi, SetLastError = true)] CharSet = CharSet.Ansi, SetLastError = true)]
internal static unsafe extern IntPtr DS_FinishStream( StreamingState** aSctx); internal static unsafe extern IntPtr DS_FinishStream(IntPtr** aSctx);
[DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)] [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl)]
internal static unsafe extern IntPtr DS_FinishStreamWithMetadata(StreamingState** aSctx); internal static unsafe extern IntPtr DS_FinishStreamWithMetadata(IntPtr** aSctx);
#endregion #endregion
} }
} }

View File

@ -1,27 +0,0 @@
using System.Runtime.InteropServices;
using Alphabet = System.IntPtr;
using Scorer = System.IntPtr;
using Session = System.IntPtr;
using MemmappedEnv = System.IntPtr;
using GraphDef = System.IntPtr;
namespace DeepSpeechClient.Structs
{
//FIXME: ModelState is an opaque pointer to the API, why is this code reverse
// engineering its contents?
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public unsafe struct ModelState
{
public MemmappedEnv mmap_env;
public Session session;
public GraphDef graph_def;
public uint ncep;
public uint ncontext;
public Alphabet alphabet;
public Scorer scorer;
public uint beam_width;
public uint n_steps;
public uint mfcc_feats_per_timestep;
public uint n_context;
}
}

View File

@ -1,12 +0,0 @@
using System.Runtime.InteropServices;
namespace DeepSpeechClient.Structs
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal unsafe struct StreamingState
{
public float last_sample; // used for preemphasis
public bool skip_next_mfcc;
public ModelState* model;
}
}