code: def get_short_segments(
      file_duration,
      file_valid_segment,
      file_vad_segments,
      max_segment_len=15,
      min_segment_len=0.5,
  ):
      """
      Return all segments within (min_segment_len, max_segment_len)
      Trim long vad segments into smaller segments of max_segment_len
  
      We denote (None, x) if the segment starts from beginning
      and (x, None) if the segment finishes at the end of file
  
      If the segment is taken from a specific channel,
      we denote the segment with (start, end, channelid)
      else (start, end, None)
      """
  
      # For files without VAD segments, consider the whole file as segment
      if file_vad_segments is None:
          file_vad_segments = [
              (None, None, None)
          ]  # (start, end, channelid)
  
      # Get the segments within the valid segment
      valid_start, valid_end = file_valid_segment
      valid_segments = []
      for segment in file_vad_segments:
          start, end, channelid = segment
          # start valid from a certain time
          if valid_start is not None:
              if start is None:
                  start = valid_start
              else:
                  start = max(start, valid_start)
          # end valid at a certain time
          if valid_end is not None:
              if end is None:
                  end = valid_end
              else:
                  end = min(end, valid_end)
          # add if segment is valid
          if start is None or end is None or start < end:
              valid_segments.append((start, end, channelid))
  
      # get duration of segment
      def duration(start, end):
          if start is None:
              start = 0
          if end is None:
              end = file_duration
          return end - start
  
      # Trim the valid segments into short segments
      short_segments = []
      for segment in valid_segments:
          start, end, channelid = segment
          if duration(start, end) <= max_segment_len:
              if duration(start, end) >= min_segment_len:
                  short_segments.append((start, end, channelid))
          else:
              chunk_start = start
              while duration(chunk_start, end) > max_segment_len:
                  chunk_end = (
                      chunk_start + max_segment_len
                      if chunk_start is not None
                      else max_segment_len
                  )
                  assert chunk_end <= file_duration, (
                      start,
                      end,
                      file_duration,
                  )
                  short_segments.append(
                      (chunk_start, chunk_end, channelid)
                  )
                  chunk_start = chunk_end
              if duration(chunk_start, end) >= min_segment_len:
                  short_segments.append((chunk_start, end, channelid))
  
      return short_segments
  
file_path: textlesslib/examples/expresso/dataset/processing/create_short_segments_dataset.py
imports: import os
  from tqdm import tqdm
  import soundfile
  from pathlib import Path
  from time import time
  from collections import defaultdict
label_desc: Use image node_id 4 for calling a global function with example usage: get_short_segments(file_duration, file_valid_segment, file_vad_segments, max_segment_len, min_segment_len) and returns: short_segments, unknown