From acabb26378d788c9108c9bf416bbef222b5a48e7 Mon Sep 17 00:00:00 2001 From: Carlos Fonseca <32177100+carlfm01@users.noreply.github.com> Date: Sat, 5 Oct 2019 03:58:40 +0000 Subject: [PATCH] Move structs to IntPtr --- .../dotnet/DeepSpeechClient/DeepSpeech.cs | 8 ++---- .../DeepSpeechClient/DeepSpeechClient.csproj | 2 -- .../dotnet/DeepSpeechClient/NativeImp.cs | 25 +++++++++-------- .../DeepSpeechClient/Structs/ModelState.cs | 27 ------------------- .../Structs/StreamingState.cs | 12 --------- 5 files changed, 14 insertions(+), 60 deletions(-) delete mode 100644 native_client/dotnet/DeepSpeechClient/Structs/ModelState.cs delete mode 100644 native_client/dotnet/DeepSpeechClient/Structs/StreamingState.cs diff --git a/native_client/dotnet/DeepSpeechClient/DeepSpeech.cs b/native_client/dotnet/DeepSpeechClient/DeepSpeech.cs index 21b3dc06..19247507 100644 --- a/native_client/dotnet/DeepSpeechClient/DeepSpeech.cs +++ b/native_client/dotnet/DeepSpeechClient/DeepSpeech.cs @@ -1,10 +1,8 @@ using DeepSpeechClient.Interfaces; -using DeepSpeechClient.Structs; using DeepSpeechClient.Extensions; using System; using System.IO; -using System.Runtime.InteropServices; using DeepSpeechClient.Enums; namespace DeepSpeechClient @@ -14,9 +12,8 @@ namespace DeepSpeechClient /// public class DeepSpeech : IDeepSpeech { - private unsafe ModelState** _modelStatePP; - private unsafe ModelState* _modelStateP; - private unsafe StreamingState** _streamingStatePP; + private unsafe IntPtr** _modelStatePP; + private unsafe IntPtr** _streamingStatePP; @@ -65,7 +62,6 @@ namespace DeepSpeechClient aBeamWidth, ref _modelStatePP); EvaluateResultCode(resultCode); - _modelStateP = *_modelStatePP; } /// diff --git a/native_client/dotnet/DeepSpeechClient/DeepSpeechClient.csproj b/native_client/dotnet/DeepSpeechClient/DeepSpeechClient.csproj index bd5a5a13..320ecde5 100644 --- a/native_client/dotnet/DeepSpeechClient/DeepSpeechClient.csproj +++ b/native_client/dotnet/DeepSpeechClient/DeepSpeechClient.csproj @@ -52,8 +52,6 @@ - - diff --git a/native_client/dotnet/DeepSpeechClient/NativeImp.cs b/native_client/dotnet/DeepSpeechClient/NativeImp.cs index f57b973d..3f126acc 100644 --- a/native_client/dotnet/DeepSpeechClient/NativeImp.cs +++ b/native_client/dotnet/DeepSpeechClient/NativeImp.cs @@ -1,5 +1,4 @@ using DeepSpeechClient.Enums; -using DeepSpeechClient.Structs; using System; using System.Runtime.InteropServices; @@ -19,10 +18,10 @@ namespace DeepSpeechClient internal unsafe static extern ErrorCodes DS_CreateModel(string aModelPath, string aAlphabetConfigPath, uint aBeamWidth, - ref ModelState** pint); + ref IntPtr** pint); [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 aTriePath, float aLMAlpha, @@ -30,26 +29,26 @@ namespace DeepSpeechClient [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl, 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, uint aBufferSize, uint aSampleRate); [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, uint aBufferSize, uint aSampleRate); [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)] - internal static unsafe extern ErrorCodes DS_CreateStream(ModelState** aCtx, - uint aSampleRate, ref StreamingState** retval); + internal static unsafe extern ErrorCodes DS_CreateStream(IntPtr** aCtx, + uint aSampleRate, ref IntPtr** retval); [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)] internal static unsafe extern void DS_FreeMetadata(IntPtr metadata); @@ -59,19 +58,19 @@ namespace DeepSpeechClient [DllImport("libdeepspeech.so", CallingConvention = CallingConvention.Cdecl, 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, uint aBufferSize); [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, 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)] - internal static unsafe extern IntPtr DS_FinishStreamWithMetadata(StreamingState** aSctx); + internal static unsafe extern IntPtr DS_FinishStreamWithMetadata(IntPtr** aSctx); #endregion } } diff --git a/native_client/dotnet/DeepSpeechClient/Structs/ModelState.cs b/native_client/dotnet/DeepSpeechClient/Structs/ModelState.cs deleted file mode 100644 index abb53758..00000000 --- a/native_client/dotnet/DeepSpeechClient/Structs/ModelState.cs +++ /dev/null @@ -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; - } -} diff --git a/native_client/dotnet/DeepSpeechClient/Structs/StreamingState.cs b/native_client/dotnet/DeepSpeechClient/Structs/StreamingState.cs deleted file mode 100644 index 820bded7..00000000 --- a/native_client/dotnet/DeepSpeechClient/Structs/StreamingState.cs +++ /dev/null @@ -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; - } -}