C5 W3 A3 E2 - Trigger Word Detection - insert_audio_clip function

I am getting this error - AssertionError: The segment length must match the audio clip length

However in while loop 2 previous iterations are working fine its the 3rd iteration which is giving this error.

1 Like

Hi @gauravsamant ,

The assertion error is raised because the audio clip is not the same length as the segment length. The loop is for checking overlapping with the previous segment which is passed in as input argument of this function. So you need to check how the segment is obtained.

1 Like

I am getting the same error. I have checked/printed segment_ms - which is 916. and I have printed/checked len(segment_time), and it too is 916. I have no idea what to do next.

1 Like

Hi @Bill_Duhamel

The segments_ms is the length of the audio clip passed in, and the segment_time is based on the length of audio clip, which is segments_ms. So printing these two variables would show they are of the same length.

The while loop of your code is to find a random generated segment that does not overlap with the previous_segments in order to insert the audio clip. This while loop has a max of 5 attempts, it would exit the loop if no suitable segment is found. There is also a last attempt to find an non overlapped segment after exiting the loop.

Please check the instructions if in doubt. You can add print statement in the code and trace where the error might be.

Here is my if statement from exercise 1:
if (segment_start <= previous_end and segment_end >= previous_start):
Note that is contains both conditions, and is_overlapping appears to be fine.

The audio clip is 916ms long. I checked using:
segment_ms = len(audio_clip)
print(segment_ms)

As you say, my code fails in the While loop. But i have printed out segment_time, and each is 916ms long. (The print statements are inserted by me for debugging and will be removed for grading.)
while is_overlapping(segment_time, previous_segments) and retry >= 0:
segment_start = random.randint(0, 10000 - segment_ms)
segment_time = (segment_start, segment_start + segment_ms)
print(segment_time)
retry -= 1

I am just so frustrated

Hi @Bill_Duhamel ,

The problem is with the way you calculate the segment_time. Although your code looked very similar to the helper function get_random_time_segment() which is provided for you, but there is a subtle difference in calculating the segment_end time. You can check out the difference.

thank you very much - I substitued get_random_time_segment(segment_ms) and it worked perfectly. but what is the subtle difference?

Hi @Bill_Duhamel ,

Here is the line of code in the get_random_time_segment() that makes the difference:
segment_end = segment_start + segment_ms - 1

noticed, the end time is 1 ms less

Here is an extract from the notes about overlapping:-

Check if audio clips are overlapping
• Suppose you have inserted audio clips at segments (1000,1800) and (3400,4500).
– The first segment starts at step 1000 and ends at step 1800.
– The second segment starts at 3400 and ends at 4500.
• If we are considering whether to insert a new audio clip at (3000,3600) does this overlap with
one of the previously inserted segments?
– In this case, (3000,3600) and (3400,4500) overlap, so we should decide against inserting
a clip here.
• For the purpose of this function, define (100,200) and (200,250) to be overlapping, since they
overlap at timestep 200.
• (100,199) and (200,250) are non-overlapping.