def changeDuration(self, startTime, length, newLength): ''' Input: startTime(Float): the beginning of the sound interval to have its duration changed (in seconds) length (float): the size of the interval to be changed (in seconds) newLength (float) : the size of the interval after changing its duration Description: Changes the duration of part of a sound file ''' timeScale = newLength / length percentTempoChange = 100 / timeScale Duration.changeGapDuration(self.soundPath, startTime, length, percentTempoChange) # Adjust time in textgrid for tier in self.textgrid: prevEndTime = 0 for interval in tier: # How much of the sound whose duration is being changed # is in this interval? thisIntervalStart = max(interval.start_time, startTime) thisIntervalEnd = min(interval.end_time, startTime + length) timeThisInterval = thisIntervalEnd - thisIntervalStart if timeThisInterval > 0: lengthChange = (timeThisInterval * timeScale) - timeThisInterval interval.end_time += lengthChange shift = prevEndTime - interval.start_time interval.end_time += shift interval.start_time += shift prevEndTime = interval.end_time # Save grid tgt.write_to_file(self.textgrid, self.textgridPath, format = "long")
def pauseInsertion(self, index, duration): """ Input: index, duration index(Int): The index of the word to add time before duration(float): The duration of the silence to insert Description: Inserts silence before a word at the given index """ wordTier = self.textgrid.get_tier_by_name('words') if index == 0: silenceStart = 0 else: wordIntervals = list(filter(lambda x: x.text != "sil" and x.text != "sp", wordTier)) silenceStart = wordIntervals[index-1].end_time # insert silence into wav insertSpaces.insertSilence(self.soundPath, silenceStart, duration) # insert silence into textgrid for tier in self.textgrid: # Shift all later intervals for interval in tier: if interval.start_time > silenceStart: interval.end_time += duration interval.start_time += duration tier.get_annotation_by_start_time(silenceStart).end_time += duration # Save grid tgt.write_to_file(self.textgrid, self.textgridPath, format = "long")