[BUG] Keyword spotter misses a keyword that starts just before its automatic 1.5 s silence reset

Author: medgar591Created Sep 26, 2026Updated Sep 26, 2026
Labelsbug

Important Instructions ⚠️

Before submitting this issue, please read carefully and confirm the following:

  • I am using the latest version of sherpa-onnx. If not, I will upgrade and check if the issue persists.
  • I understand that issues not reproducible on the latest version may be closed without response.
  • I have verified that the error is reproducible using only sherpa-onnx, not code from my own project.
  • I will provide detailed steps to reproduce the issue.
  • I will provide complete logs and error messages.
  • I understand that issues not following these instructions may be closed or receive no response (如果直接删掉这个issue template 里的步骤,我们很有可能不会回复您的issue).

Describe the issue

When a keyword is spoken after a period of silence, the keyword spotter misses it if the keyword happens to start shortly before the spotter's automatic silence reset fires. Because that reset repeats every ~1.6 s for as long as there is silence, this affects roughly 1 in 10 keywords spoken after a pause, which is the typical wake-word situation.

The automatic reset was added in #1689. In DecodeStreams, a stream is reset when the previous result has more than 1.5 s of trailing blanks. Reset calls InitOnlineStream, which resets the keyword result and also the encoder states.

If a keyword has started but its first token has not been emitted yet when the check runs (tokens are emitted some time after the audio for them, and the check uses the previous chunk's result), the reset discards the encoder state that already holds the start of the keyword. Decoding then continues from partway through the word, and the keyword is not detected. After the reset the trailing-blank count starts again from 0, so in silence the reset repeats every ~1.5 s (~1.6 s observed, presumably because the check only runs once per chunk), giving a narrow window that recurs every ~1.6 s.

Keywords in continuous speech seem to be less affected, presumably because tokens matching the start of some keyword are emitted often enough to keep the trailing-blank count below 1.5 s. With the repro below, the full test_wavs/1.wav is detected correctly at every lead-in; only the clip with silence before the keyword shows the problem.

Steps to reproduce

The repro uses only the C# API from the org.k2fsa.sherpa.onnx NuGet package, the sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01 model and that model's own test_wavs/1.wav and test_wavs/test_keywords.txt. It cuts the words "LOVELY CHILD" (5.0 s to 6.5 s) out of 1.wav, puts 0 to 3.2 s of silence (in 0.05 s steps) before it and 1 s after it, feeds each to a fresh stream, and prints what is detected.

  1. Download and unpack the model:

    bash
    curl -LO https://github.com/k2-fsa/sherpa-onnx/releases/download/kws-models/sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01.tar.bz2
    tar xjf sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01.tar.bz2
  2. Create a project with these two files.

    kws-repro.csproj:

    xml
    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net10.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="org.k2fsa.sherpa.onnx" Version="1.13.8" />
      </ItemGroup>
    </Project>

    Program.cs:

    csharp
    // Feeds the same keyword clip to a fresh keyword spotter stream after different
    // amounts of leading silence (0 to 3.2 s), and prints what is detected for each.
    //
    // Usage: dotnet run -- <model dir> <wav file> <keywords file> [<start seconds> <end seconds>]
    // The optional start and end cut a clip out of the wav file.
    using SherpaOnnx;
    
    var modelDir = args[0];
    var wavPath = args[1];
    var keywordsPath = args[2];
    
    var config = new KeywordSpotterConfig();
    config.FeatConfig.SampleRate = 16000;
    config.FeatConfig.FeatureDim = 80;
    config.ModelConfig.Transducer.Encoder = Path.Combine(modelDir, "encoder-epoch-12-avg-2-chunk-16-left-64.onnx");
    config.ModelConfig.Transducer.Decoder = Path.Combine(modelDir, "decoder-epoch-12-avg-2-chunk-16-left-64.onnx");
    config.ModelConfig.Transducer.Joiner = Path.Combine(modelDir, "joiner-epoch-12-avg-2-chunk-16-left-64.onnx");
    config.ModelConfig.Tokens = Path.Combine(modelDir, "tokens.txt");
    config.ModelConfig.NumThreads = 1;
    config.KeywordsFile = keywordsPath;
    
    using var kws = new KeywordSpotter(config);
    var (sampleRate, samples) = ReadWav(wavPath);
    if (args.Length >= 5)
    {
        int start = (int)(double.Parse(args[3], System.Globalization.CultureInfo.InvariantCulture) * sampleRate);
        int end = (int)(double.Parse(args[4], System.Globalization.CultureInfo.InvariantCulture) * sampleRate);
        samples = samples[start..end];
    }
    Console.WriteLine($"{Path.GetFileName(wavPath)}: {samples.Length / (double)sampleRate:F2}s at {sampleRate} Hz");
    
    for (int step = 0; step <= 64; step++)
    {
        double lead = step * 0.05;
        var audio = new float[(int)(lead * sampleRate) + samples.Length + sampleRate];
        Array.Copy(samples, 0, audio, (int)(lead * sampleRate), samples.Length);
    
        using var stream = kws.CreateStream();
        stream.AcceptWaveform(sampleRate, audio);
        stream.InputFinished();
    
        var detected = new List<string>();
        while (kws.IsReady(stream))
        {
            kws.Decode(stream);
            var keyword = kws.GetResult(stream).Keyword;
            if (keyword != "")
            {
                kws.Reset(stream);
                detected.Add(keyword);
            }
        }
        Console.WriteLine($"lead-in {lead:F2}s: {(detected.Count == 0 ? "(none)" : string.Join(", ", detected))}");
    }
    
    // Reads a 16-bit PCM mono WAV file.
    static (int, float[]) ReadWav(string path)
    {
        var bytes = File.ReadAllBytes(path);
        int rate = BitConverter.ToInt32(bytes, 24);
        int pos = 12;
        while (System.Text.Encoding.ASCII.GetString(bytes, pos, 4) != "data")
            pos += 8 + BitConverter.ToInt32(bytes, pos + 4);
        int count = BitConverter.ToInt32(bytes, pos + 4) / 2;
        var samples = new float[count];
        for (int i = 0; i < count; i++)
            samples[i] = BitConverter.ToInt16(bytes, pos + 8 + i * 2) / 32768f;
        return (rate, samples);
    }
  3. Run it:

    M=sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01
    dotnet run -- $M $M/test_wavs/1.wav $M/test_wavs/test_keywords.txt 5.0 6.5

Expected behavior

LOVELY CHILD is detected at every lead-in, since the audio is identical apart from the amount of silence before it.

Actual behavior

It is detected at 59 of the 65 lead-ins, but missed at 1.30, 1.35, 1.40 s and again at 2.90, 2.95, 3.00 s, i.e. whenever the keyword starts in a ~0.15 s window that recurs every ~1.6 s.

We first saw this with our own text-to-speech clip of a single English word after silence, fed in 100 ms chunks and using the int8 model files, with the same pattern.

Possible fixes

We have not been able to test a fix, since that needs a native build, but two ideas:

  • For the automatic silence reset, reset only the keyword result (hypotheses, context state and trailing-blank count) and keep the encoder states, so audio that has already been processed isn't lost. We don't know whether the automatic reset also needs to clear the encoder states for some other reason.
  • Or make the automatic reset optional or its 1.5 s threshold configurable.

For now we work around it by running two streams, each started 1 s after the other and replaced after 2 s, so that a keyword always starts early in some stream's life.

Environment

  • sherpa-onnx version: 1.13.8 (NuGet org.k2fsa.sherpa.onnx, C# API); the reset code is unchanged on master (040afe3)
  • OS: Windows 11 (10.0.26200), x64
  • Python version: N/A (C# API, .NET SDK 10.0.401)
  • onnxruntime version: the one bundled with the 1.13.8 NuGet package
  • GPU (if applicable): N/A (CPU)
  • NPU (if applicable): N/A

Logs

Complete output of the command above:

1.wav: 1.50s at 16000 Hz
lead-in 0.00s: LOVELY CHILD
lead-in 0.05s: LOVELY CHILD
lead-in 0.10s: LOVELY CHILD
lead-in 0.15s: LOVELY CHILD
lead-in 0.20s: LOVELY CHILD
lead-in 0.25s: LOVELY CHILD
lead-in 0.30s: LOVELY CHILD
lead-in 0.35s: LOVELY CHILD
lead-in 0.40s: LOVELY CHILD
lead-in 0.45s: LOVELY CHILD
lead-in 0.50s: LOVELY CHILD
lead-in 0.55s: LOVELY CHILD
lead-in 0.60s: LOVELY CHILD
lead-in 0.65s: LOVELY CHILD
lead-in 0.70s: LOVELY CHILD
lead-in 0.75s: LOVELY CHILD
lead-in 0.80s: LOVELY CHILD
lead-in 0.85s: LOVELY CHILD
lead-in 0.90s: LOVELY CHILD
lead-in 0.95s: LOVELY CHILD
lead-in 1.00s: LOVELY CHILD
lead-in 1.05s: LOVELY CHILD
lead-in 1.10s: LOVELY CHILD
lead-in 1.15s: LOVELY CHILD
lead-in 1.20s: LOVELY CHILD
lead-in 1.25s: LOVELY CHILD
lead-in 1.30s: (none)
lead-in 1.35s: (none)
lead-in 1.40s: (none)
lead-in 1.45s: LOVELY CHILD
lead-in 1.50s: LOVELY CHILD
lead-in 1.55s: LOVELY CHILD
lead-in 1.60s: LOVELY CHILD
lead-in 1.65s: LOVELY CHILD
lead-in 1.70s: LOVELY CHILD
lead-in 1.75s: LOVELY CHILD
lead-in 1.80s: LOVELY CHILD
lead-in 1.85s: LOVELY CHILD
lead-in 1.90s: LOVELY CHILD
lead-in 1.95s: LOVELY CHILD
lead-in 2.00s: LOVELY CHILD
lead-in 2.05s: LOVELY CHILD
lead-in 2.10s: LOVELY CHILD
lead-in 2.15s: LOVELY CHILD
lead-in 2.20s: LOVELY CHILD
lead-in 2.25s: LOVELY CHILD
lead-in 2.30s: LOVELY CHILD
lead-in 2.35s: LOVELY CHILD
lead-in 2.40s: LOVELY CHILD
lead-in 2.45s: LOVELY CHILD
lead-in 2.50s: LOVELY CHILD
lead-in 2.55s: LOVELY CHILD
lead-in 2.60s: LOVELY CHILD
lead-in 2.65s: LOVELY CHILD
lead-in 2.70s: LOVELY CHILD
lead-in 2.75s: LOVELY CHILD
lead-in 2.80s: LOVELY CHILD
lead-in 2.85s: LOVELY CHILD
lead-in 2.90s: (none)
lead-in 2.95s: (none)
lead-in 3.00s: (none)
lead-in 3.05s: LOVELY CHILD
lead-in 3.10s: LOVELY CHILD
lead-in 3.15s: LOVELY CHILD
lead-in 3.20s: LOVELY CHILD

For comparison, running on the whole of 1.wav (without the 5.0 6.5 arguments) detects LOVELY CHILD, FOREVER at all 65 lead-ins.