Skip to content

Commit 8c4f576

Browse files
authored
Support .Net framework 2.0 (#1062)
1 parent 598c12c commit 8c4f576

File tree

10 files changed

+90
-79
lines changed

10 files changed

+90
-79
lines changed

dotnet-examples/offline-decode-files/Program.cs

+22-23
Original file line numberDiff line numberDiff line change
@@ -17,75 +17,74 @@ class Options
1717
{
1818

1919
[Option("sample-rate", Required = false, Default = 16000, HelpText = "Sample rate of the data used to train the model")]
20-
public int SampleRate { get; set; }
20+
public int SampleRate { get; set; } = 16000;
2121

2222
[Option("feat-dim", Required = false, Default = 80, HelpText = "Dimension of the features used to train the model")]
23-
public int FeatureDim { get; set; }
23+
public int FeatureDim { get; set; } = 80;
2424

2525
[Option(Required = false, HelpText = "Path to tokens.txt")]
26-
public string Tokens { get; set; }
26+
public string Tokens { get; set; } = "";
2727

2828
[Option(Required = false, Default = "", HelpText = "Path to transducer encoder.onnx. Used only for transducer models")]
29-
public string Encoder { get; set; }
29+
public string Encoder { get; set; } = "";
3030

3131
[Option(Required = false, Default = "", HelpText = "Path to transducer decoder.onnx. Used only for transducer models")]
32-
public string Decoder { get; set; }
32+
public string Decoder { get; set; } = "";
3333

3434
[Option(Required = false, Default = "",HelpText = "Path to transducer joiner.onnx. Used only for transducer models")]
35-
public string Joiner { get; set; }
35+
public string Joiner { get; set; } = "";
3636

3737
[Option("model-type", Required = false, Default = "", HelpText = "model type")]
38-
public string ModelType { get; set; }
38+
public string ModelType { get; set; } = "";
3939

4040
[Option("whisper-encoder", Required = false, Default = "", HelpText = "Path to whisper encoder.onnx. Used only for whisper models")]
41-
public string WhisperEncoder { get; set; }
41+
public string WhisperEncoder { get; set; } = "";
4242

4343
[Option("whisper-decoder", Required = false, Default = "", HelpText = "Path to whisper decoder.onnx. Used only for whisper models")]
44-
public string WhisperDecoder { get; set; }
44+
public string WhisperDecoder { get; set; } = "";
4545

4646
[Option("whisper-language", Required = false, Default = "", HelpText = "Language of the input file. Can be empty")]
47-
public string WhisperLanguage{ get; set; }
47+
public string WhisperLanguage{ get; set; } = "";
4848

4949
[Option("whisper-task", Required = false, Default = "transcribe", HelpText = "transcribe or translate")]
50-
public string WhisperTask{ get; set; }
50+
public string WhisperTask{ get; set; } = "transcribe";
5151

5252
[Option("tdnn-model", Required = false, Default = "", HelpText = "Path to tdnn yesno model")]
53-
public string TdnnModel { get; set; }
54-
53+
public string TdnnModel { get; set; } = "";
5554

5655
[Option(Required = false, HelpText = "Path to model.onnx. Used only for paraformer models")]
57-
public string Paraformer { get; set; }
56+
public string Paraformer { get; set; } = "";
5857

5958
[Option("nemo-ctc", Required = false, HelpText = "Path to model.onnx. Used only for NeMo CTC models")]
60-
public string NeMoCtc { get; set; }
59+
public string NeMoCtc { get; set; } = "";
6160

6261
[Option("telespeech-ctc", Required = false, HelpText = "Path to model.onnx. Used only for TeleSpeech CTC models")]
63-
public string TeleSpeechCtc { get; set; }
62+
public string TeleSpeechCtc { get; set; } = "";
6463

6564
[Option("num-threads", Required = false, Default = 1, HelpText = "Number of threads for computation")]
66-
public int NumThreads { get; set; }
65+
public int NumThreads { get; set; } = 1;
6766

6867
[Option("decoding-method", Required = false, Default = "greedy_search",
6968
HelpText = "Valid decoding methods are: greedy_search, modified_beam_search")]
70-
public string DecodingMethod { get; set; }
69+
public string DecodingMethod { get; set; } = "greedy_search";
7170

7271
[Option("rule-fsts", Required = false, Default = "",
7372
HelpText = "If not empty, path to rule fst for inverse text normalization")]
74-
public string RuleFsts { get; set; }
73+
public string RuleFsts { get; set; } = "";
7574

7675
[Option("max-active-paths", Required = false, Default = 4,
7776
HelpText = @"Used only when --decoding--method is modified_beam_search.
7877
It specifies number of active paths to keep during the search")]
79-
public int MaxActivePaths { get; set; }
78+
public int MaxActivePaths { get; set; } = 4;
8079

8180
[Option("hotwords-file", Required = false, Default = "", HelpText = "Path to hotwords.txt")]
82-
public string HotwordsFile { get; set; }
81+
public string HotwordsFile { get; set; } = "";
8382

8483
[Option("hotwords-score", Required = false, Default = 1.5F, HelpText = "hotwords score")]
85-
public float HotwordsScore { get; set; }
84+
public float HotwordsScore { get; set; } = 1.5F;
8685

8786
[Option("files", Required = true, HelpText = "Audio files for decoding")]
88-
public IEnumerable<string> Files { get; set; }
87+
public IEnumerable<string> Files { get; set; } = new string[] {};
8988
}
9089

9190
static void Main(string[] args)

dotnet-examples/offline-tts/Program.cs

+16-16
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,49 @@ class Options
1818
{
1919

2020
[Option("tts-rule-fsts", Required = false, Default = "", HelpText = "path to rule.fst")]
21-
public string RuleFsts { get; set; }
21+
public string RuleFsts { get; set; } = "";
2222

2323
[Option("tts-rule-fars", Required = false, Default = "", HelpText = "path to rule.far")]
24-
public string RuleFars { get; set; }
24+
public string RuleFars { get; set; } = "";
2525

2626
[Option("vits-dict-dir", Required = false, Default = "", HelpText = "Path to the directory containing dict for jieba.")]
27-
public string DictDir { get; set; }
27+
public string DictDir { get; set; } = "";
2828

2929
[Option("vits-data-dir", Required = false, Default = "", HelpText = "Path to the directory containing dict for espeak-ng.")]
30-
public string DataDir { get; set; }
30+
public string DataDir { get; set; } = "";
3131

3232
[Option("vits-length-scale", Required = false, Default = 1, HelpText = "speech speed. Larger->Slower; Smaller->faster")]
33-
public float LengthScale { get; set; }
33+
public float LengthScale { get; set; } = 1;
3434

3535
[Option("vits-noise-scale", Required = false, Default = 0.667f, HelpText = "noise_scale for VITS models")]
36-
public float NoiseScale { get; set; }
36+
public float NoiseScale { get; set; } = 0.667F;
3737

38-
[Option("vits-noise-scale-w", Required = false, Default = 0.8f, HelpText = "noise_scale_w for VITS models")]
39-
public float NoiseScaleW { get; set; }
38+
[Option("vits-noise-scale-w", Required = false, Default = 0.8F, HelpText = "noise_scale_w for VITS models")]
39+
public float NoiseScaleW { get; set; } = 0.8F;
4040

4141
[Option("vits-lexicon", Required = false, Default = "", HelpText = "Path to lexicon.txt")]
42-
public string Lexicon { get; set; }
42+
public string Lexicon { get; set; } = "";
4343

4444
[Option("vits-tokens", Required = false, Default = "", HelpText = "Path to tokens.txt")]
45-
public string Tokens { get; set; }
45+
public string Tokens { get; set; } = "";
4646

4747
[Option("tts-max-num-sentences", Required = false, Default = 1, HelpText = "Maximum number of sentences that we process at a time.")]
48-
public int MaxNumSentences { get; set; }
48+
public int MaxNumSentences { get; set; } = 1;
4949

5050
[Option(Required = false, Default = 0, HelpText = "1 to show debug messages.")]
51-
public int Debug { get; set; }
51+
public int Debug { get; set; } = 0;
5252

5353
[Option("vits-model", Required = true, HelpText = "Path to VITS model")]
54-
public string Model { get; set; }
54+
public string Model { get; set; } = "";
5555

5656
[Option("sid", Required = false, Default = 0, HelpText = "Speaker ID")]
57-
public int SpeakerId { get; set; }
57+
public int SpeakerId { get; set; } = 0;
5858

5959
[Option("text", Required = true, HelpText = "Text to synthesize")]
60-
public string Text { get; set; }
60+
public string Text { get; set; } = "";
6161

6262
[Option("output-filename", Required = true, Default = "./generated.wav", HelpText = "Path to save the generated audio")]
63-
public string OutputFilename { get; set; }
63+
public string OutputFilename { get; set; } = "./generated.wav";
6464
}
6565

6666
static void Main(string[] args)

dotnet-examples/online-decode-files/Program.cs

+21-22
Original file line numberDiff line numberDiff line change
@@ -18,81 +18,80 @@ class OnlineDecodeFiles
1818
class Options
1919
{
2020
[Option(Required = true, HelpText = "Path to tokens.txt")]
21-
public string Tokens { get; set; }
21+
public string Tokens { get; set; } = "";
2222

2323
[Option(Required = false, Default = "cpu", HelpText = "Provider, e.g., cpu, coreml")]
24-
public string Provider { get; set; }
24+
public string Provider { get; set; } = "";
2525

2626
[Option(Required = false, HelpText = "Path to transducer encoder.onnx")]
27-
public string Encoder { get; set; }
27+
public string Encoder { get; set; } = "";
2828

2929
[Option(Required = false, HelpText = "Path to transducer decoder.onnx")]
30-
public string Decoder { get; set; }
30+
public string Decoder { get; set; } = "";
3131

3232
[Option(Required = false, HelpText = "Path to transducer joiner.onnx")]
33-
public string Joiner { get; set; }
33+
public string Joiner { get; set; } = "";
3434

3535
[Option("paraformer-encoder", Required = false, HelpText = "Path to paraformer encoder.onnx")]
36-
public string ParaformerEncoder { get; set; }
36+
public string ParaformerEncoder { get; set; } = "";
3737

3838
[Option("paraformer-decoder", Required = false, HelpText = "Path to paraformer decoder.onnx")]
39-
public string ParaformerDecoder { get; set; }
39+
public string ParaformerDecoder { get; set; } = "";
4040

4141
[Option("zipformer2-ctc", Required = false, HelpText = "Path to zipformer2 CTC onnx model")]
42-
public string Zipformer2Ctc { get; set; }
42+
public string Zipformer2Ctc { get; set; } = "";
4343

4444
[Option("num-threads", Required = false, Default = 1, HelpText = "Number of threads for computation")]
45-
public int NumThreads { get; set; }
45+
public int NumThreads { get; set; } = 1;
4646

4747
[Option("decoding-method", Required = false, Default = "greedy_search",
4848
HelpText = "Valid decoding methods are: greedy_search, modified_beam_search")]
49-
public string DecodingMethod { get; set; }
49+
public string DecodingMethod { get; set; } = "greedy_search";
5050

5151
[Option(Required = false, Default = false, HelpText = "True to show model info during loading")]
52-
public bool Debug { get; set; }
52+
public bool Debug { get; set; } = false;
5353

5454
[Option("sample-rate", Required = false, Default = 16000, HelpText = "Sample rate of the data used to train the model")]
55-
public int SampleRate { get; set; }
55+
public int SampleRate { get; set; } = 16000;
5656

5757
[Option("max-active-paths", Required = false, Default = 4,
5858
HelpText = @"Used only when --decoding--method is modified_beam_search.
5959
It specifies number of active paths to keep during the search")]
60-
public int MaxActivePaths { get; set; }
60+
public int MaxActivePaths { get; set; } = 4;
6161

6262
[Option("enable-endpoint", Required = false, Default = false,
6363
HelpText = "True to enable endpoint detection.")]
64-
public bool EnableEndpoint { get; set; }
64+
public bool EnableEndpoint { get; set; } = false;
6565

6666
[Option("rule1-min-trailing-silence", Required = false, Default = 2.4F,
6767
HelpText = @"An endpoint is detected if trailing silence in seconds is
6868
larger than this value even if nothing has been decoded. Used only when --enable-endpoint is true.")]
69-
public float Rule1MinTrailingSilence { get; set; }
69+
public float Rule1MinTrailingSilence { get; set; } = 2.4F;
7070

7171
[Option("rule2-min-trailing-silence", Required = false, Default = 1.2F,
7272
HelpText = @"An endpoint is detected if trailing silence in seconds is
7373
larger than this value after something that is not blank has been decoded. Used
7474
only when --enable-endpoint is true.")]
75-
public float Rule2MinTrailingSilence { get; set; }
75+
public float Rule2MinTrailingSilence { get; set; } = 1.2F;
7676

7777
[Option("rule3-min-utterance-length", Required = false, Default = 20.0F,
7878
HelpText = @"An endpoint is detected if the utterance in seconds is
7979
larger than this value. Used only when --enable-endpoint is true.")]
80-
public float Rule3MinUtteranceLength { get; set; }
80+
public float Rule3MinUtteranceLength { get; set; } = 20.0F;
8181

8282
[Option("hotwords-file", Required = false, Default = "", HelpText = "Path to hotwords.txt")]
83-
public string HotwordsFile { get; set; }
83+
public string HotwordsFile { get; set; } = "";
8484

8585
[Option("hotwords-score", Required = false, Default = 1.5F, HelpText = "hotwords score")]
86-
public float HotwordsScore { get; set; }
86+
public float HotwordsScore { get; set; } = 1.5F;
8787

8888
[Option("rule-fsts", Required = false, Default = "",
8989
HelpText = "If not empty, path to rule fst for inverse text normalization")]
90-
public string RuleFsts { get; set; }
90+
public string RuleFsts { get; set; } = "";
9191

9292

9393
[Option("files", Required = true, HelpText = "Audio files for decoding")]
94-
public IEnumerable<string> Files { get; set; }
95-
94+
public IEnumerable<string> Files { get; set; } = new string[] {};
9695
}
9796

9897
static void Main(string[] args)

scripts/dotnet/OfflineRecognizer.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
using System;
44
using System.Collections.Generic;
5-
using System.Linq;
65
using System.Runtime.InteropServices;
76

87
namespace SherpaOnnx
@@ -29,7 +28,14 @@ public void Decode(OfflineStream stream)
2928
// The caller should ensure all passed streams are ready for decoding.
3029
public void Decode(IEnumerable<OfflineStream> streams)
3130
{
32-
IntPtr[] ptrs = streams.Select(s => s.Handle).ToArray();
31+
// TargetFramework=net20 does not support System.Linq
32+
// IntPtr[] ptrs = streams.Select(s => s.Handle).ToArray();
33+
List<IntPtr> list = new List<IntPtr>();
34+
foreach (OfflineStream s in streams)
35+
{
36+
list.Add(s.Handle);
37+
}
38+
IntPtr[] ptrs = list.ToArray();
3339
Decode(_handle.Handle, ptrs, ptrs.Length);
3440
}
3541

scripts/dotnet/OnlineRecognizer.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/// Copyright (c) 2024.5 by 东风破
44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
76
using System.Runtime.InteropServices;
87

98
namespace SherpaOnnx
@@ -49,7 +48,15 @@ public void Decode(OnlineStream stream)
4948
// The caller should ensure all passed streams are ready for decoding.
5049
public void Decode(IEnumerable<OnlineStream> streams)
5150
{
52-
IntPtr[] ptrs = streams.Select(s => s.Handle).ToArray();
51+
// TargetFramework=net20 does not support System.Linq
52+
// IntPtr[] ptrs = streams.Select(s => s.Handle).ToArray();
53+
List<IntPtr> list = new List<IntPtr>();
54+
foreach (OnlineStream s in streams)
55+
{
56+
list.Add(s.Handle);
57+
}
58+
59+
IntPtr[] ptrs = list.ToArray();
5360
Decode(_handle.Handle, ptrs, ptrs.Length);
5461
}
5562

scripts/dotnet/OnlineRecognizerResult.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public OnlineRecognizerResult(IntPtr handle)
7777
}
7878
else
7979
{
80-
_timestamps = Array.Empty<float>();
80+
_timestamps = new float[] {};
8181
}
8282
}
8383
}

scripts/dotnet/run.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ macos_arm64_wheel=$src_dir/$macos_arm64_wheel_filename
3838
windows_x64_wheel_filename=sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-win_amd64.whl
3939
windows_x64_wheel=$src_dir/$windows_x64_wheel_filename
4040

41-
windows_x86_wheel_filename=sherpa_onnx-${SHERPA_ONNX_VERSION}-cp38-cp38-win32.whl
41+
windows_x86_wheel_filename=sherpa-onnx-${SHERPA_ONNX_VERSION}-win-x86.tar.bz2
4242
windows_x86_wheel=$src_dir/$windows_x86_wheel_filename
4343

4444
windows_arm64_wheel_filename=sherpa-onnx-${SHERPA_ONNX_VERSION}-win-arm64.tar.bz2
@@ -138,10 +138,10 @@ if [ ! -f $src_dir/windows-x86/sherpa-onnx-core.dll ]; then
138138
if [ -f $windows_x86_wheel ]; then
139139
cp -v $windows_x86_wheel .
140140
else
141-
curl -OL https://$HF_MIRROR/csukuangfj/sherpa-onnx-wheels/resolve/main/$windows_x86_wheel_filename
141+
curl -OL https://$HF_MIRROR/csukuangfj/sherpa-onnx-libs/resolve/main/windows-for-dotnet/$windows_x86_wheel_filename
142142
fi
143-
unzip $windows_x86_wheel_filename
144-
cp -v sherpa_onnx-${SHERPA_ONNX_VERSION}.data/data/bin/*.dll ../
143+
tar xvf $windows_x86_wheel_filename
144+
cp -v sherpa-onnx-${SHERPA_ONNX_VERSION}-win-x86/*dll ../
145145
cd ..
146146

147147
rm -rf wheel
@@ -159,7 +159,7 @@ if [ ! -f $src_dir/windows-arm64/sherpa-onnx-core.dll ]; then
159159
else
160160
curl -OL https://$HF_MIRROR/csukuangfj/sherpa-onnx-libs/resolve/main/windows-for-dotnet/$windows_arm64_wheel_filename
161161
fi
162-
unzip $windows_arm64_wheel_filename
162+
tar xvf $windows_arm64_wheel_filename
163163
cp -v sherpa-onnx-${SHERPA_ONNX_VERSION}-win-arm64/*dll ../
164164
cd ..
165165

scripts/dotnet/sherpa-onnx.csproj.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<PackageReadmeFile>README.md</PackageReadmeFile>
55
<OutputType>Library</OutputType>
66
<LangVersion>10.0</LangVersion>
7-
<TargetFrameworks>netstandard2.0</TargetFrameworks>
7+
<TargetFrameworks>net6.0;net45;net40;net35;net20;netstandard2.0</TargetFrameworks>
88
<RuntimeIdentifiers>linux-x64;osx-x64;osx-arm64;win-x64;win-x86;win-arm64</RuntimeIdentifiers>
99
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1010
<AssemblyName>sherpa-onnx</AssemblyName>

scripts/dotnet/sherpa-onnx.csproj.runtime.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
44
<PackageReadmeFile>README.md</PackageReadmeFile>
55
<OutputType>Library</OutputType>
6-
<TargetFrameworks>netstandard2.0</TargetFrameworks>
6+
<TargetFrameworks>net6.0;net45;net40;net35;net20;netstandard2.0</TargetFrameworks>
77
<RuntimeIdentifier>{{ dotnet_rid }}</RuntimeIdentifier>
88
<AssemblyName>sherpa-onnx</AssemblyName>
99
<Version>{{ version }}</Version>

0 commit comments

Comments
 (0)