예제 #1
0
    def _convert_mp3_to_wav(map3_file: str) -> typing.Union[str, None]:
        assert map3_file.endswith(".mp3")
        out_file = nlp.replace_file_name(map3_file, ".mp3", ".wav")
        if os.path.exists(out_file):
            return out_file

        cmd = f"ffmpeg -i {map3_file} {out_file}"
        if execute_cmd(cmd) == 0:
            return out_file

        return None
예제 #2
0
    def _convert_flac_to_wav(flac_file: str) -> typing.Union[str, None]:
        assert flac_file.endswith(".flac")
        out_file = nlp.replace_file_name(flac_file, ".flac", ".wav")
        if os.path.exists(out_file):
            return out_file

        cmd = f"sox {flac_file} {out_file}"
        if nlp.execute_cmd(cmd) == 0:
            return out_file

        return None
예제 #3
0
    def convert_to_16bits(wav_or_flac_file: str) -> typing.Union[str, None]:
        file_ext = nlp.get_file_extension(wav_or_flac_file)
        new_file = nlp.replace_file_name(wav_or_flac_file, f".{file_ext}",
                                         ".16bits.wav")
        if os.path.exists(new_file):
            return new_file

        if nlp.execute_cmd(f"sox {wav_or_flac_file} -b 16 {new_file}") == 0:
            return new_file

        return None
예제 #4
0
    def _convert_sph_to_wav(sph_file: str) -> typing.Union[str, None]:
        assert sph_file.endswith(".sph")
        out_file = nlp.replace_file_name(sph_file, ".sph", ".wav")
        if os.path.exists(out_file):
            return out_file

        cmd = f"sox {sph_file} {out_file}"
        if execute_cmd(cmd) == 0:
            return out_file

        cmd = f"sph2pipe -f rif {sph_file} {out_file}"
        if execute_cmd(cmd) == 0:
            return out_file

        return None
예제 #5
0
    def convert_to_standard_wav(
            wav_or_flac_file: str) -> typing.Union[str, None]:
        '''
    :return: sample-width=2 Bytes, sample rating=16K.
    '''
        if wav_or_flac_file.endswith(".norm.wav"):
            return wav_or_flac_file

        file_ext = nlp.get_file_extension(wav_or_flac_file)
        new_file = nlp.replace_file_name(wav_or_flac_file, f".{file_ext}",
                                         ".norm.wav")
        if os.path.exists(new_file):
            return new_file

        if nlp.execute_cmd(f"sox {wav_or_flac_file} "
                           f"-b 16 -r 16000 {new_file}") == 0:
            return new_file

        return None
예제 #6
0
    def preemphasize_wav(standard_wav_file: str):
        assert standard_wav_file.endswith(".norm.wav")
        new_file = nlp.replace_file_name(standard_wav_file, ".norm.wav",
                                         ".norm.amp.wav")
        if os.path.exists(new_file):
            return new_file

        sample_rate, signal = wavfile.read(standard_wav_file)
        assert sample_rate == 16000

        pre_emphasis = 0.97
        emphasized_signal = numpy.append(
            signal[0], signal[1:] - pre_emphasis * signal[:-1])
        amplified_signal = emphasized_signal * (32768 /
                                                emphasized_signal.max())
        wavfile.write(new_file, sample_rate,
                      amplified_signal.astype(numpy.int16))

        return new_file
예제 #7
0
    def segment_audio(flac_or_wav_file: str, time_segments: list,
                      dest_folder: str) -> typing.Iterator:
        '''
    time_segments: [(12,97, 18.89), (18.43, 27.77) ...] in seconds.
    return: an iterator retuning a new segment file. If one time segment is
    invalid, then the its corresponding segment file name is None.
    '''
        file_ext = nlp.get_file_extension(flac_or_wav_file)
        assert file_ext in ["flac", "wav"]
        assert os.path.exists(dest_folder)

        base_name = os.path.basename(flac_or_wav_file)
        audio = AudioSegment.from_file(flac_or_wav_file, file_ext)
        duration = len(audio)
        for file_id, (time_from, time_to) in enumerate(time_segments):
            t_from = time_from * 1000
            t_to = time_to * 1000

            if not (0 <= t_from < t_to < duration):
                print(f"WARN: {flac_or_wav_file} is not complete. "
                      f"Actual length: {duration / 1000} seconds, "
                      f"while time segment is {time_from}-{time_to}")
                yield None
                continue

            seg_name = os.path.join(
                dest_folder,
                nlp.replace_file_name(base_name, f".{file_ext}",
                                      f".{file_id:04}.{file_ext}"))
            try:
                audio[t_from:t_to].export(seg_name, format=file_ext)
                yield seg_name

            except Exception as error:
                print_flush(error)
                yield None