code: def process_alignment(
      alignment_file: str, manifest: str, clips_dir: str, args
  ):
      """Cut original audio file into audio segments based on alignment_file
  
      Args:
          alignment_file: path to the file with segmented text and corresponding time stamps.
              The first line of the file contains the path to the original audio file
          manifest: path to .json manifest to save segments metadata
          clips_dir: path to a directory to save audio clips
          args: main script args
      """
      if not os.path.exists(alignment_file):
          raise ValueError(f"{alignment_file} not found")
  
      base_name = os.path.basename(alignment_file).replace(
          "_segments.txt", ""
      )
  
      # read the segments, note the first line contains the path to the original audio
      segments = []
      ref_text_processed = []
      ref_text_no_preprocessing = []
      ref_text_normalized = []
      with open(alignment_file, "r") as f:
          for line in f:
              line = line.split("|")
              # read audio file name from the first line
              if len(line) == 1:
                  audio_file = line[0].strip()
                  continue
              ref_text_processed.append(line[1].strip())
              ref_text_no_preprocessing.append(line[2].strip())
              ref_text_normalized.append(line[3].strip())
              line = line[0].split()
              segments.append(
                  (
                      float(line[0]) + args.offset / 1000,
                      float(line[1]) + args.offset / 1000,
                      float(line[2]),
                  )
              )
  
      # cut the audio into segments and save the final manifests at output_dir
      sampling_rate, signal = wavfile.read(audio_file)
      original_duration = len(signal) / sampling_rate
  
      num_samples = int(args.edge_duration * args.sample_rate)
      low_score_dur = 0
      high_score_dur = 0
      with open(manifest, "a", encoding="utf8") as f:
          for i, (st, end, score) in enumerate(segments):
              segment = signal[
                  round(st * sampling_rate) : round(end * sampling_rate)
              ]
              duration = len(segment) / sampling_rate
              if duration > args.max_duration:
                  continue
              if duration > 0:
                  text_processed = ref_text_processed[i].strip()
                  text_no_preprocessing = ref_text_no_preprocessing[
                      i
                  ].strip()
                  text_normalized = ref_text_normalized[i].strip()
                  if score >= args.threshold:
                      high_score_dur += duration
                      audio_filepath = os.path.join(
                          clips_dir, f"{base_name}_{i:04}.wav"
                      )
                      wavfile.write(
                          audio_filepath, sampling_rate, segment
                      )
  
                      assert (
                          len(signal.shape) == 1
                          and sampling_rate == args.sample_rate
                      ), "check sampling rate"
  
                      info = {
                          "audio_filepath": audio_filepath,
                          "duration": duration,
                          "text": text_processed,
                          "text_no_preprocessing": text_no_preprocessing,
                          "text_normalized": text_normalized,
                          "score": round(score, 2),
                          "start_abs": float(
                              np.mean(np.abs(segment[:num_samples]))
                          ),
                          "end_abs": float(
                              np.mean(np.abs(segment[-num_samples:]))
                          ),
                      }
                      json.dump(info, f, ensure_ascii=False)
                      f.write("\n")
                  else:
                      low_score_dur += duration
  
      # keep track of duration of the deleted segments
      del_duration = 0
      begin = 0
  
      for i, (st, end, _) in enumerate(segments):
          if st - begin > 0.01:
              segment = signal[
                  int(begin * sampling_rate) : int(st * sampling_rate)
              ]
              duration = len(segment) / sampling_rate
              del_duration += duration
          begin = end
  
      segment = signal[int(begin * sampling_rate) :]
      duration = len(segment) / sampling_rate
      del_duration += duration
  
      stats = (
          args.output_dir,
          base_name,
          round(original_duration),
          round(high_score_dur),
          round(low_score_dur),
          round(del_duration),
      )
      return stats
  
imports: import argparse
  import json
  import os
  from glob import glob
  import numpy
  from scipy.io import wavfile
  from tqdm import tqdm
label_desc: Use image node_id 1 for calling a global function with example usage: process_alignment(alignment_file, manifest, clips_dir, args) and returns: stats