def _compute_head_process_tail(self, audio_file_mfcc): """ Set the audio file head or tail, by either reading the explicit values from the Task configuration, or using SD to determine them. This function returns the lengths, in seconds, of the (head, process, tail). :rtype: tuple (float, float, float) """ head_length = self.task.configuration["i_a_head"] process_length = self.task.configuration["i_a_process"] tail_length = self.task.configuration["i_a_tail"] head_max = self.task.configuration["i_a_head_max"] head_min = self.task.configuration["i_a_head_min"] tail_max = self.task.configuration["i_a_tail_max"] tail_min = self.task.configuration["i_a_tail_min"] if ((head_length is not None) or (process_length is not None) or (tail_length is not None)): self.log(u"Setting explicit head process tail") else: self.log(u"Detecting head tail...") sd = SD(audio_file_mfcc, self.task.text_file, rconf=self.rconf, logger=self.logger) head_length = TimeValue("0.000") process_length = None tail_length = TimeValue("0.000") if (head_min is not None) or (head_max is not None): self.log(u"Detecting HEAD...") head_length = sd.detect_head(head_min, head_max) self.log([u"Detected HEAD: %.3f", head_length]) self.log(u"Detecting HEAD... done") if (tail_min is not None) or (tail_max is not None): self.log(u"Detecting TAIL...") tail_length = sd.detect_tail(tail_min, tail_max) self.log([u"Detected TAIL: %.3f", tail_length]) self.log(u"Detecting TAIL... done") self.log(u"Detecting head tail... done") self.log([u"Head: %s", gf.safe_float(head_length, None)]) self.log([u"Process: %s", gf.safe_float(process_length, None)]) self.log([u"Tail: %s", gf.safe_float(tail_length, None)]) return (head_length, process_length, tail_length)
def _compute_head_process_tail(self, audio_file_mfcc): """ Set the audio file head or tail, by either reading the explicit values from the Task configuration, or using SD to determine them. This function returns the lengths, in seconds, of the (head, process, tail). :rtype: tuple (float, float, float) """ head_length = self.task.configuration["i_a_head"] process_length = self.task.configuration["i_a_process"] tail_length = self.task.configuration["i_a_tail"] head_max = self.task.configuration["i_a_head_max"] head_min = self.task.configuration["i_a_head_min"] tail_max = self.task.configuration["i_a_tail_max"] tail_min = self.task.configuration["i_a_tail_min"] if ( (head_length is not None) or (process_length is not None) or (tail_length is not None) ): self.log(u"Setting explicit head process tail") else: self.log(u"Detecting head tail...") sd = SD(audio_file_mfcc, self.task.text_file, rconf=self.rconf, logger=self.logger) head_length = TimeValue("0.000") process_length = None tail_length = TimeValue("0.000") if (head_min is not None) or (head_max is not None): self.log(u"Detecting HEAD...") head_length = sd.detect_head(head_min, head_max) self.log([u"Detected HEAD: %.3f", head_length]) self.log(u"Detecting HEAD... done") if (tail_min is not None) or (tail_max is not None): self.log(u"Detecting TAIL...") tail_length = sd.detect_tail(tail_min, tail_max) self.log([u"Detected TAIL: %.3f", tail_length]) self.log(u"Detecting TAIL... done") self.log(u"Detecting head tail... done") self.log([u"Head: %s", gf.safe_float(head_length, None)]) self.log([u"Process: %s", gf.safe_float(process_length, None)]) self.log([u"Tail: %s", gf.safe_float(tail_length, None)]) return (head_length, process_length, tail_length)
def _cut_head_tail(self, audio_file_path): """ Set the audio file head or tail, suitably cutting the audio file on disk, and setting the corresponding parameters in the task configuration. Return a success bool flag """ self._log("Setting head and/or tail") try: configuration = self.task.configuration head_length = configuration.is_audio_file_head_length process_length = configuration.is_audio_file_process_length detect_head_min = configuration.is_audio_file_detect_head_min detect_head_max = configuration.is_audio_file_detect_head_max detect_tail_min = configuration.is_audio_file_detect_tail_min detect_tail_max = configuration.is_audio_file_detect_tail_max # explicit head or process? explicit = (head_length is not None) or (process_length is not None) # at least one detect parameter? detect = ((detect_head_min is not None) or (detect_head_max is not None) or (detect_tail_min is not None) or (detect_tail_max is not None)) if explicit or detect: # we need to load the audio data audio_file = AudioFile(audio_file_path, logger=self.logger) audio_file.load_data() if explicit: self._log("Explicit head or process") else: self._log( "No explicit head or process => detecting head/tail") head = 0.0 if (detect_head_min is not None) or (detect_head_max is not None): self._log("Detecting head...") detect_head_min = gf.safe_float( detect_head_min, gc.SD_MIN_HEAD_LENGTH) detect_head_max = gf.safe_float( detect_head_max, gc.SD_MAX_HEAD_LENGTH) self._log(["detect_head_min is %.3f", detect_head_min]) self._log(["detect_head_max is %.3f", detect_head_max]) sd = SD(audio_file, self.task.text_file, logger=self.logger) head = sd.detect_head(detect_head_min, detect_head_max) self._log(["Detected head: %.3f", head]) tail = 0.0 if (detect_tail_min is not None) or (detect_tail_max is not None): self._log("Detecting tail...") detect_tail_max = gf.safe_float( detect_tail_max, gc.SD_MAX_TAIL_LENGTH) detect_tail_min = gf.safe_float( detect_tail_min, gc.SD_MIN_TAIL_LENGTH) self._log(["detect_tail_min is %.3f", detect_tail_min]) self._log(["detect_tail_max is %.3f", detect_tail_max]) sd = SD(audio_file, self.task.text_file, logger=self.logger) tail = sd.detect_tail(detect_tail_min, detect_tail_max) self._log(["Detected tail: %.3f", tail]) # sanity check head_length = max(0, head) process_length = max(0, audio_file.audio_length - tail - head) # we need to set these values # in the config object for later use self.task.configuration.is_audio_file_head_length = head_length self.task.configuration.is_audio_file_process_length = process_length self._log(["Set head_length: %.3f", head_length]) self._log(["Set process_length: %.3f", process_length]) if head_length is not None: # in case we are reading from config object head_length = float(head_length) if process_length is not None: # in case we are reading from config object process_length = float(process_length) # note that str() is necessary, as one might be None self._log( ["is_audio_file_head_length is %s", str(head_length)]) self._log([ "is_audio_file_process_length is %s", str(process_length) ]) self._log("Trimming audio data...") audio_file.trim(head_length, process_length) self._log("Trimming audio data... done") self._log("Writing audio file...") audio_file.write(audio_file_path) self._log("Writing audio file... done") audio_file.clear_data() else: # nothing to do self._log("No explicit head/process or detect head/tail") self._log("Setting head and/or tail: succeeded") return True except Exception as e: self._log("Setting head and/or tail: failed") self._log(["Message: %s", str(e)]) return False
def _cut_head_tail(self, audio_file_path): """ Set the audio file head or tail, suitably cutting the audio file on disk, and setting the corresponding parameters in the task configuration. Return a success bool flag """ self._log("Setting head and/or tail") try: configuration = self.task.configuration head_length = configuration.is_audio_file_head_length process_length = configuration.is_audio_file_process_length detect_head_min = configuration.is_audio_file_detect_head_min detect_head_max = configuration.is_audio_file_detect_head_max detect_tail_min = configuration.is_audio_file_detect_tail_min detect_tail_max = configuration.is_audio_file_detect_tail_max # explicit head or process? explicit = (head_length is not None) or (process_length is not None) # at least one detect parameter? detect = ( (detect_head_min is not None) or (detect_head_max is not None) or (detect_tail_min is not None) or (detect_tail_max is not None) ) if explicit or detect: # we need to load the audio data audio_file = AudioFile(audio_file_path, logger=self.logger) audio_file.load_data() if explicit: self._log("Explicit head or process") else: self._log("No explicit head or process => detecting head/tail") head = 0.0 if (detect_head_min is not None) or (detect_head_max is not None): self._log("Detecting head...") detect_head_min = gf.safe_float(detect_head_min, gc.SD_MIN_HEAD_LENGTH) detect_head_max = gf.safe_float(detect_head_max, gc.SD_MAX_HEAD_LENGTH) self._log(["detect_head_min is %.3f", detect_head_min]) self._log(["detect_head_max is %.3f", detect_head_max]) sd = SD(audio_file, self.task.text_file, logger=self.logger) head = sd.detect_head(detect_head_min, detect_head_max) self._log(["Detected head: %.3f", head]) tail = 0.0 if (detect_tail_min is not None) or (detect_tail_max is not None): self._log("Detecting tail...") detect_tail_max = gf.safe_float(detect_tail_max, gc.SD_MAX_TAIL_LENGTH) detect_tail_min = gf.safe_float(detect_tail_min, gc.SD_MIN_TAIL_LENGTH) self._log(["detect_tail_min is %.3f", detect_tail_min]) self._log(["detect_tail_max is %.3f", detect_tail_max]) sd = SD(audio_file, self.task.text_file, logger=self.logger) tail = sd.detect_tail(detect_tail_min, detect_tail_max) self._log(["Detected tail: %.3f", tail]) # sanity check head_length = max(0, head) process_length = max(0, audio_file.audio_length - tail - head) # we need to set these values # in the config object for later use self.task.configuration.is_audio_file_head_length = head_length self.task.configuration.is_audio_file_process_length = process_length self._log(["Set head_length: %.3f", head_length]) self._log(["Set process_length: %.3f", process_length]) if head_length is not None: # in case we are reading from config object head_length = float(head_length) if process_length is not None: # in case we are reading from config object process_length = float(process_length) # note that str() is necessary, as one might be None self._log(["is_audio_file_head_length is %s", str(head_length)]) self._log(["is_audio_file_process_length is %s", str(process_length)]) self._log("Trimming audio data...") audio_file.trim(head_length, process_length) self._log("Trimming audio data... done") self._log("Writing audio file...") audio_file.write(audio_file_path) self._log("Writing audio file... done") audio_file.clear_data() else: # nothing to do self._log("No explicit head/process or detect head/tail") self._log("Setting head and/or tail: succeeded") return True except Exception as e: self._log("Setting head and/or tail: failed") self._log(["Message: %s", str(e)]) return False