title: bug in python code azure speech-cognitive-services
body: <p>Hello I have this code from <a href="https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/scenarios/python/console/captioning/" rel="nofollow noreferrer">https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/scenarios/python/console/captioning/</a></p>
  <p>This should print a srt subtitle that should be formatted:</p>
  <pre><code>1
  00:00:00,180 --&gt; 00:00:03,230
  Welcome to applied Mathematics course 201.
  </code></pre>
  <p>But for some reason is not woking for me. I'm not python coder, but in the line 74 i found the variable sequenceNumber that seems an error cause could be sequence_number. I tried but I could not fix the problem, and the output is always without the Number of the subtile line.</p>
  <pre><code>00:00:00,180 --&gt; 00:00:03,230
  Welcome to applied Mathematics course 201.
  </code></pre>
  <p>This is the python code:</p>
  <pre><code>#
  # Copyright (c) Microsoft. All rights reserved.
  # Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
  #
  
  # Notes:
  # - Install the Speech SDK. Run:
  # pip install azure-cognitiveservices-speech
  # - The Python Speech SDK on Windows requires the Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017, 2019, or 2022 on the system. See:
  # https://docs.microsoft.com/azure/cognitive-services/speech-service/quickstarts/setup-platform
  # - Install gstreamer:
  # https://docs.microsoft.com/azure/cognitive-services/speech-service/how-to-use-codec-compressed-audio-input-streams
  
  from datetime import datetime
  from os import linesep, remove
  from os.path import exists
  from pathlib import Path
  from sys import argv
  from time import sleep
  from typing import Any
  import wave
  import azure.cognitiveservices.speech as speechsdk
  import helper
  
  def user_config_from_args(usage : str) -&gt; helper.Read_Only_Dict :
      key = helper.get_cmd_option(&quot;--key&quot;)
      if key is None:
          raise RuntimeError(&quot;Missing subscription key.{}{}&quot;.format(linesep, usage))
      region = helper.get_cmd_option(&quot;--region&quot;)
      if region is None:
          raise RuntimeError(&quot;Missing region.{}{}&quot;.format(linesep, usage))
          
      return helper.Read_Only_Dict({
          &quot;use_compressed_audio&quot; : helper.cmd_option_exists(&quot;--format&quot;),
          &quot;compressed_audio_format&quot; : helper.get_compressed_audio_format(),
          &quot;profanity_option&quot; : helper.get_profanity_option(),
          &quot;suppress_console_output&quot; : helper.cmd_option_exists(&quot;--quiet&quot;),
          &quot;use_sub_rip_text_caption_format&quot; : helper.cmd_option_exists(&quot;--srt&quot;),
          &quot;input_file&quot; : helper.get_cmd_option(&quot;--input&quot;),
          &quot;output_file&quot; : helper.get_cmd_option(&quot;--output&quot;),
          &quot;language_ID_languages&quot; : helper.get_cmd_option(&quot;--languages&quot;),
          &quot;phrase_list&quot; : helper.get_cmd_option(&quot;--phrases&quot;),
          &quot;show_recognizing_results&quot; : helper.get_cmd_option(&quot;--recognizing&quot;),
          &quot;stable_partial_result_threshold&quot; : helper.get_cmd_option(&quot;--threshold&quot;),
          &quot;subscription_key&quot; : key,
          &quot;region&quot; : region,
      })
  
  # Note: Continuous language identification is supported only with v2 endpoints.
  def v2_endpoint_from_region(region : str) -&gt; str :
      return &quot;wss://{}.stt.speech.microsoft.com/speech/universal/v2&quot;.format(region);
  
  def timestamp_from_speech_recognition_result(result : speechsdk.SpeechRecognitionResult, user_config : helper.Read_Only_Dict) -&gt; str :
      start_time = helper.time_from_ticks(ticks = result.offset)
      end_time = helper.time_from_ticks(ticks = result.offset + result.duration)
      time_format = &quot;&quot;
      if user_config[&quot;use_sub_rip_text_caption_format&quot;] :
          # SRT format requires ',' as decimal separator rather than '.'.
          time_format = &quot;%H:%M:%S,%f&quot;
      else :
          time_format = &quot;%H:%M:%S.%f&quot;
      # Truncate microseconds to milliseconds.
      return &quot;{} --&gt; {}&quot;.format(start_time.strftime(time_format)[:-3], end_time.strftime(time_format)[:-3])
  
  def language_from_speech_recognition_result(result : speechsdk.SpeechRecognitionResult, user_config : helper.Read_Only_Dict) -&gt; str :
      if user_config[&quot;language_ID_languages&quot;] is not None :
          return &quot;[{}] &quot;.format(speechsdk.AutoDetectSourceLanguageResult(result).language)
      else :
          return &quot;&quot;
  
  def caption_from_speech_recognition_result(sequence_number : int, result : speechsdk.SpeechRecognitionResult, user_config : helper.Read_Only_Dict) -&gt; str :
      caption = &quot;&quot;
      if not user_config[&quot;show_recognizing_results&quot;] and user_config[&quot;use_sub_rip_text_caption_format&quot;] :
          caption += str(sequenceNumber) + linesep
      caption += timestamp_from_speech_recognition_result(result = result, user_config = user_config) + linesep
      caption += language_from_speech_recognition_result(result = result, user_config = user_config)
      caption += result.text + linesep + linesep
      return caption
  
  def initialize(user_config : helper.Read_Only_Dict) :
      if user_config[&quot;output_file&quot;] is not None and exists(user_config[&quot;output_file&quot;]) :
          remove(user_config[&quot;output_file&quot;])
      if not user_config[&quot;use_sub_rip_text_caption_format&quot;] :
          helper.write_to_console_or_file(text = &quot;WEBVTT{}{}&quot;.format(linesep, linesep), user_config = user_config)
      return
  
  def audio_config_from_user_config(user_config : helper.Read_Only_Dict) -&gt; helper.Read_Only_Dict :
      if user_config[&quot;input_file&quot;] is None :
          return speechsdk.AudioConfig(use_default_microphone = True), None, None, None
      else :
          audio_stream_format = None
          if not user_config[&quot;use_compressed_audio&quot;] :
              reader = wave.open(user_config[&quot;input_file&quot;], mode=None)
              audio_stream_format = speechsdk.audio.AudioStreamFormat(samples_per_second=reader.getframerate(), bits_per_sample=reader.getsampwidth() * 8, channels=reader.getnchannels())
              reader.close()
          else :
              audio_stream_format = speechsdk.audio.AudioStreamFormat(compressed_stream_format=user_config[&quot;compressed_audio_format&quot;])
          callback = helper.BinaryFileReaderCallback(filename=user_config[&quot;input_file&quot;])
          stream = speechsdk.audio.PullAudioInputStream(pull_stream_callback = callback, stream_format = audio_stream_format)
          # We return the BinaryFileReaderCallback, AudioStreamFormat, and PullAudioInputStream
          # because we need to keep them in scope until they are actually used.
          return helper.Read_Only_Dict({
              &quot;audio_config&quot; : speechsdk.audio.AudioConfig(stream=stream),
              &quot;audio_stream_format&quot; : audio_stream_format,
              &quot;pull_input_audio_stream_callback&quot; : callback,
              &quot;pull_input_audio_stream&quot; : stream,
          })
  
  def speech_config_from_user_config(user_config : helper.Read_Only_Dict) -&gt; speechsdk.SpeechConfig :
      speech_config = None
      if user_config[&quot;language_ID_languages&quot;] is not None :
          endpoint = v2_endpoint_from_region(user_config[&quot;region&quot;])
          speech_config = speechsdk.SpeechConfig(endpoint = endpoint, subscription = user_config[&quot;subscription_key&quot;])
      else :
          speech_config = speechsdk.SpeechConfig(subscription = user_config[&quot;subscription_key&quot;], region = user_config[&quot;region&quot;])
  
      speech_config.set_profanity(user_config[&quot;profanity_option&quot;])
  
      if user_config[&quot;stable_partial_result_threshold&quot;] is not None :
          speech_config.set_property(property_id = speechsdk.PropertyId.SpeechServiceResponse_StablePartialResultThreshold, value = user_config[&quot;stable_partial_result_threshold&quot;])
  
      speech_config.set_property(property_id = speechsdk.PropertyId.SpeechServiceResponse_PostProcessingOption, value = &quot;TrueText&quot;)
      speech_config.set_property(property_id = speechsdk.PropertyId.SpeechServiceConnection_ContinuousLanguageIdPriority, value = &quot;Accuracy&quot;)
      
      return speech_config
  
  def speech_recognizer_from_user_config(user_config : helper.Read_Only_Dict) -&gt; helper.Read_Only_Dict :
      audio_config_data = audio_config_from_user_config(user_config)
      speech_config = speech_config_from_user_config(user_config)
      speech_recognizer = None
  
      if user_config[&quot;language_ID_languages&quot;] is not None :
          auto_detect_source_language_config = speechsdk.AutoDetectSourceLanguageConfig(user_config[&quot;language_ID_languages&quot;].split(&quot;;&quot;))
          speech_recognizer = speechsdk.SpeechRecognizer(speech_config = speech_config, audio_config = audio_config_data[&quot;audio_config&quot;], auto_detect_source_language_config = auto_detect_source_language_config)
      else :
          speech_recognizer = speechsdk.SpeechRecognizer(speech_config = speech_config, audio_config = audio_config_data[&quot;audio_config&quot;])
  
      if user_config[&quot;phrase_list&quot;] is not None :
          grammar = speechsdk.PhraseListGrammar.from_recognizer(recognizer = speech_recognizer)
          grammar.addPhrase(user_config[&quot;phrase_list&quot;])
  
      return helper.Read_Only_Dict({
          &quot;speech_recognizer&quot; : speech_recognizer,
          &quot;audio_stream_format&quot; : audio_config_data[&quot;audio_stream_format&quot;],
          &quot;pull_input_audio_stream_callback&quot; : audio_config_data[&quot;pull_input_audio_stream_callback&quot;],
          &quot;pull_input_audio_stream&quot; : audio_config_data[&quot;pull_input_audio_stream&quot;],
      })
  
  def recognize_continuous(speech_recognizer : speechsdk.SpeechRecognizer, user_config : helper.Read_Only_Dict, format : speechsdk.audio.AudioStreamFormat, callback : helper.BinaryFileReaderCallback, stream : speechsdk.audio.PullAudioInputStream) :
      sequence_number = 0
      done = False
  
      def recognizing_handler(e : speechsdk.SpeechRecognitionEventArgs) :
          if speechsdk.ResultReason.RecognizingSpeech == e.result.reason and len(e.result.text) &gt; 0 :
              # We don't show sequence numbers for partial results.
              helper.write_to_console(text = caption_from_speech_recognition_result(sequence_number = 0, result = e.result, user_config = user_config), user_config = user_config)
          elif speechsdk.ResultReason.NoMatch == e.result.reason :
              helper.write_to_console(text = &quot;NOMATCH: Speech could not be recognized.{}&quot;.format(linesep), user_config = user_config)
  
      def recognized_handler(e : speechsdk.SpeechRecognitionEventArgs) :
          if speechsdk.ResultReason.RecognizedSpeech == e.result.reason and len(e.result.text) &gt; 0 :
              nonlocal sequence_number
              sequence_number += 1
              helper.write_to_console_or_file(text = caption_from_speech_recognition_result(sequence_number = sequence_number, result = e.result, user_config = user_config), user_config = user_config)
          elif speechsdk.ResultReason.NoMatch == e.result.reason :
              helper.write_to_console(text = &quot;NOMATCH: Speech could not be recognized.{}&quot;.format(linesep), user_config = user_config)
  
      def canceled_handler(e : speechsdk.SpeechRecognitionCanceledEventArgs) :
          nonlocal done
          # Notes:
          # SpeechRecognitionCanceledEventArgs inherits the result property from SpeechRecognitionEventArgs. See:
          # https://docs.microsoft.com/python/api/azure-cognitiveservices-speech/azure.cognitiveservices.speech.speechrecognitioncanceledeventargs
          # https://docs.microsoft.com/python/api/azure-cognitiveservices-speech/azure.cognitiveservices.speech.speechrecognitioneventargs
          # result is type SpeechRecognitionResult, which inherits the reason property from RecognitionResult. See:
          # https://docs.microsoft.com/python/api/azure-cognitiveservices-speech/azure.cognitiveservices.speech.speechrecognitionresult
          # https://docs.microsoft.com/python/api/azure-cognitiveservices-speech/azure.cognitiveservices.speech.recognitionresult
          # e.result.reason is ResultReason.Canceled. To get the cancellation reason, see e.cancellation_details.reason.
          if speechsdk.CancellationReason.EndOfStream == e.cancellation_details.reason :
              helper.write_to_console(text = &quot;End of stream reached.{}&quot;.format(linesep), user_config = user_config)
              done = True
          elif speechsdk.CancellationReason.CancelledByUser == e.cancellation_details.reason :
              helper.write_to_console(text = &quot;User canceled request.{}&quot;.format(linesep), user_config = user_config)
              done = True
          elif speechsdk.CancellationReason.Error == e.cancellation_details.reason :
              # Error output should not be suppressed, even if suppress output flag is set.
              print(&quot;Encountered error. Cancellation details: {}{}&quot;.format(e.cancellation_details, linesep))
              done = True
          else :
              print(&quot;Request was cancelled for an unrecognized reason. Cancellation details: {}{}&quot;.format(e.cancellation_details, linesep))
              done = True
  
      def stopped_handler(e : speechsdk.SessionEventArgs) :
          nonlocal done
          helper.write_to_console(text = &quot;Session stopped.{}&quot;.format(linesep), user_config = user_config)
          done = True
  
      if user_config[&quot;show_recognizing_results&quot;] :
          speech_recognizer.recognizing.connect(recognizing_handler)
      speech_recognizer.recognized.connect(recognized_handler)
      speech_recognizer.session_stopped.connect(stopped_handler)
      speech_recognizer.canceled.connect(canceled_handler)
  
      speech_recognizer.start_continuous_recognition()
  
      while not done :
          sleep(5)
      speech_recognizer.stop_continuous_recognition()
  
      return
  
  usage = &quot;&quot;&quot;Usage: python captioning.py [...]
  
    HELP
      --help                        Show this help and stop.
  
    CONNECTION
      --key KEY                     Your Azure Speech service subscription key.
      --region REGION               Your Azure Speech service region.
                                    Examples: westus, eastus
  
    LANGUAGE
      --languages LANG1;LANG2       Enable language identification for specified languages.
                                    Example: en-US;ja-JP
  
    INPUT
      --input FILE                  Input audio from file (default input is the microphone.)
      --format FORMAT               Use compressed audio format.
                                    If this is not present, uncompressed format (wav) is assumed.
                                    Valid only with --file.
                                    Valid values: alaw, any, flac, mp3, mulaw, ogg_opus
  
    RECOGNITION
      --recognizing                 Output Recognizing results (default output is Recognized results only.)
                                    These are always written to the console, never to an output file.
                                    --quiet overrides this.
  
    ACCURACY
      --phrases PHRASE1;PHRASE2     Example: Constoso;Jessie;Rehaan
  
    OUTPUT
      --output FILE                 Output captions to text file.
      --srt                         Output captions in SubRip Text format (default format is WebVTT.)
      --quiet                       Suppress console output, except errors.
      --profanity OPTION            Valid values: raw, remove, mask
      --threshold NUMBER            Set stable partial result threshold.
                                    Default value: 3
  &quot;&quot;&quot;
  
  try :
      if helper.cmd_option_exists(&quot;--help&quot;) :
          print(usage)
      else :
          user_config = user_config_from_args(usage)
          initialize(user_config = user_config)
          speech_recognizer_data = speech_recognizer_from_user_config(user_config = user_config)
          recognize_continuous(speech_recognizer = speech_recognizer_data[&quot;speech_recognizer&quot;], user_config = user_config, format = speech_recognizer_data[&quot;audio_stream_format&quot;], callback = speech_recognizer_data[&quot;pull_input_audio_stream_callback&quot;], stream = speech_recognizer_data[&quot;pull_input_audio_stream&quot;])
  except Exception as e:
      print(e)
  </code></pre>