text: import numpy as np
  
  
  def get_random_time_segment(segment_ms):
      """
      Gets a random time segment of duration segment_ms in a 10,000 ms audio clip.
  
      Arguments:
      segment_ms -- the duration of the audio clip in ms ("ms" stands for "milliseconds")
  
      Returns:
      segment_time -- a tuple of (segment_start, segment_end) in ms
      """
  
      segment_start = np.random.randint(low=0,
                                        high=10000 - segment_ms)  # Make sure segment doesn't run past the 10sec background
      segment_end = segment_start + segment_ms - 1
  
      return segment_start, segment_end
  
  
  def is_overlapping(segment_time, previous_segments):
      """
      Checks if the time of a segment overlaps with the times of existing segments.
  
      Arguments:
      segment_time -- a tuple of (segment_start, segment_end) for the new segment
      previous_segments -- a list of tuples of (segment_start, segment_end) for the existing segments
  
      Returns:
      True if the time segment overlaps with any of the existing segments, False otherwise
      """
  
      segment_start, segment_end = segment_time
  
      # Step 1: Initialize overlap as a "False" flag. (≈ 1 line)
      overlap = False
  
      # Step 2: loop over the previous_segments start and end times.
      # Compare start/end times and set the flag to True if there is an overlap (≈ 3 lines)
      for previous_start, previous_end in previous_segments:
          if segment_start <= previous_end and segment_end >= previous_start:
              overlap = True
  
      return overlap
  
  
  def insert_audio_clip(background, audio_clip, previous_segments):
      """
      Insert a new audio segment over the background noise at a random time step, ensuring that the
      audio segment does not overlap with existing segments.
  
      Arguments:
      background -- a 10 second background audio recording.
      audio_clip -- the audio clip to be inserted/overlaid.
      previous_segments -- times where audio segments have already been placed
  
      Returns:
      new_background -- the updated background audio
      """
  
      # Get the duration of the audio clip in ms
      segment_ms = len(audio_clip)
  
      # Step 1: Use one of the helper functions to pick a random time segment onto which to insert
      # the new audio clip. (≈ 1 line)
      segment_time = get_random_time_segment(segment_ms)
  
      # Step 2: Check if the new segment_time overlaps with one of the previous_segments. If so, keep
      # picking new segment_time at random until it doesn't overlap. (≈ 2 lines)
      while is_overlapping(segment_time, previous_segments):
          segment_time = get_random_time_segment(segment_ms)
  
      # Step 3: Add the new segment_time to the list of previous_segments (≈ 1 line)
      previous_segments.append(segment_time)
  
      # Step 4: Superpose audio segment and background
      new_background = background.overlay(audio_clip, position=segment_time[0])
  
      return new_background, segment_time
  
  