Example #1
0
def load_wav_file(file_path: str) -> Sample:
    """Loads a wav file and returns the corresponding Sample data object.

    :param file_path: path to the wav file

    :raise ValueError: on bad file rate (!= CD_QUALITY_RATE)
    :returns: the associated RawSample data object
    """
    # Read the .wav file
    rate, data = wavfile.read(file_path)

    # cut the number of data points to the chosen power of 2
    data = np.array(data[:N])

    if rate != CD_QUALITY_RATE:
        raise ValueError(f'Invalid file rate, found {rate} Hz but '
                         f'expected {CD_QUALITY_RATE} Hz')

    # Extract file meta data
    file_name = Path(file_path).name
    raw_phoneme = file_name.split('_')[0]
    try:
        phoneme = Phoneme(raw_phoneme.lower())
    except ValueError:
        raise ValueError(f'Invalid phoneme "{raw_phoneme.lower()}"')

    # Instantiate the associated data object
    return Sample(phoneme, file_name, data)
    def __init__(self, mp4_file):
        self.input_file = mp4_file
        stem = Path(mp4_file).stem
        self.album, self.artist, self.title = stem.split('  ')

        parent = Path(mp4_file).parent
        self.output_file = str(parent.joinpath(
            '%s  %s.m4a' % (self.artist, self.title)))

        self.artwork_file = None
        for ext in ('.jpg', '.png'):
            imgfile = parent.joinpath(stem + ext)
            if imgfile.exists():
                self.artwork_file = str(imgfile)
                break

        if not self.artwork_file:
            self.tempdir = tempfile.mkdtemp()
            self.artwork_file = op.join(self.tempdir, 'artwork.jpg')