def generate_filtered_noise(spectral_envelope_coeffs_list=None, params=None):

    n_gap = params["n_gap"]
    segment_len = params["segment_len"]
    triangle = params["triangle_lin_interpol"]
    sqrt_triangle = np.sqrt(triangle)

    spectral_envelope_extractor = SpectralEnvelopeExtractor(params)

    sound_len = len(spectral_envelope_coeffs_list) * n_gap + segment_len

    sound = np.zeros(sound_len)

    n_gap = params["n_gap"]
    smoother = np.ones(params["segment_len"] // 256)

    for i_segment in range(len(spectral_envelope_coeffs_list)):

        coeffs = spectral_envelope_coeffs_list[i_segment]
        full_spectral_envelope = spectral_envelope_extractor.get_full_spectral_envelope_from_coeffs(
            coeffs)

        segment = np.random.randn(segment_len) * sqrt_triangle
        fft_segment = np.fft.fft(segment)
        white_fft_segment = fft_segment / fftconvolve(np.abs(fft_segment),
                                                      smoother, "same")
        fft_filtered_segment = white_fft_segment * full_spectral_envelope
        filtered_segment = np.real(np.fft.ifft(fft_filtered_segment))
        add_to_segment(i_segment=i_segment,
                       source=filtered_segment * sqrt_triangle,
                       dest=sound,
                       n_gap=n_gap)

    return sound
def generate_periodic_filtered_sound(
        segment_period_expressed_in_sample_list=None,
        spectral_envelope_coeffs_list=None,
        params=None):

    sawtooth = generate_periodic_sound(segment_period_expressed_in_sample_list=
                                       segment_period_expressed_in_sample_list,
                                       params=params)

    filtered_sawtooth = np.zeros(sawtooth.shape[0])

    spectral_envelope_extractor = SpectralEnvelopeExtractor(params)

    apowin2 = params["apowin2"]
    n_gap = params["n_gap"]
    smoother = np.ones(params["segment_len"] // 256)

    for i_segment in range(len(spectral_envelope_coeffs_list)):
        seg = get_segment(sound=sawtooth, i_segment=i_segment, params=params)

        fft_seg = np.fft.fft(seg * apowin2)
        fft_seg_envelope = fftconvolve(np.abs(fft_seg), smoother, "same")
        white_fft_seg = fft_seg / fft_seg_envelope
        coeffs = spectral_envelope_coeffs_list[i_segment]
        spectral_envelope = spectral_envelope_extractor.get_full_spectral_envelope_from_coeffs(
            coeffs)

        filtered_seg = np.real(np.fft.ifft(white_fft_seg * spectral_envelope))
        add_to_segment(i_segment=i_segment,
                       source=filtered_seg,
                       dest=filtered_sawtooth,
                       n_gap=n_gap)

    return filtered_sawtooth
Example #3
0
 def __init__(self, params):
     self.pitch_extractor = PitchEstimator(params=params)
     self.period_and_noise_separator = PeriodicAndNoiseSeparator(
         params=params)
     self.spectral_envelope_extractor = SpectralEnvelopeExtractor(
         params=params)
     self.params = params
class RoughSpectralEnvelopeExtractor:

    def __init__(self, params=None):
        self.params = deepcopy(params)
        self.params["n_triangle_function"] = 15
        self.spectral_features_extractor = SpectralEnvelopeExtractor(params)

    def get_spectral_envelope_from_sound(self, sound=None):
        rough_spectral_envelope_list = self.spectral_features_extractor.get_spectral_envelope_from_sound(sound=sound)
        rough_spectral_envelope_and_delta_list = []
        for it in range(len(rough_spectral_envelope_list)):
            log_rough_spectral_envelope_prev = (get_element_from_list_zero_outside(it, rough_spectral_envelope_list) + 1)
            log_rough_spectral_envelope = (get_element_from_list_zero_outside(it-1, rough_spectral_envelope_list) + 1)
            derivative_log_rough_spectral_envelope = log_rough_spectral_envelope - log_rough_spectral_envelope_prev
            feature_vector = np.concatenate([1*log_rough_spectral_envelope,
                                             10*derivative_log_rough_spectral_envelope], axis=0)

            rough_spectral_envelope_and_delta_list.append(feature_vector)

        return rough_spectral_envelope_and_delta_list
Example #5
0
from feature_extraction.spectral_enveloppe_extractor import SpectralEnvelopeExtractor
from common.count_segments import count_segments
from database_tools.sound_file_loader import get_segment
import matplotlib.pyplot as plt
import numpy as np
from feature_extraction.voice_feature_extractor import voice_feature_extractor

params = get_params()

filename = "/home/monsieur/projets/voice_conversion/data/bruce_willis/Studio/1.wav"

sound, sampling_frequency = get_mono_left_channel_sound_and_sampling_frequency(
    filename)

params = get_params()

params["n_triangle_function"] = 40
rough_spectral_envelope_extractor = SpectralEnvelopeExtractor(params=params)

spectrum_as_list = rough_spectral_envelope_extractor.get_spectral_envelope_from_sound(
    sound)

spectrum = np.zeros([40, len(spectrum_as_list)])

for it in range(len(spectrum_as_list)):
    spectrum[:, it] = spectrum_as_list[it]

plt.imshow(np.log(spectrum + 1E-7))
plt.gca().invert_yaxis()
plt.show()
 def __init__(self, params=None):
     self.params = deepcopy(params)
     self.params["n_triangle_function"] = 15
     self.spectral_features_extractor = SpectralEnvelopeExtractor(params)
Example #7
0
class VoiceFeatureExtractor:
    def __init__(self, params):
        self.pitch_extractor = PitchEstimator(params=params)
        self.period_and_noise_separator = PeriodicAndNoiseSeparator(
            params=params)
        self.spectral_envelope_extractor = SpectralEnvelopeExtractor(
            params=params)
        self.params = params

    def extract(self, sound=None):

        apowin2 = self.params["apowin2"]

        n_segment = count_segments(sound=sound, params=self.params)

        period_list = []
        spectral_envelope_coeffs_noise_list = []
        spectral_envelope_coeffs_harmonic_list = []

        for i_segment in range(n_segment):
            x = get_segment(sound=sound,
                            i_segment=i_segment,
                            params=self.params)
            x_apodized = x * apowin2
            period = self.pitch_extractor.estimate_period(x_apodized)
            periodic, noise = self.period_and_noise_separator.separate_components(
                x_apodized=x_apodized, period=period)
            spectral_envelope_coeffs_periodic = self.spectral_envelope_extractor.get_spectral_envelope_coeffs(
                periodic)
            spectral_envelope_coeffs_noise = self.spectral_envelope_extractor.get_spectral_envelope_coeffs(
                noise)

            spectral_envelope_coeffs_noise_list.append(
                spectral_envelope_coeffs_noise)
            spectral_envelope_coeffs_harmonic_list.append(
                spectral_envelope_coeffs_periodic)
            period_list.append(period)

        return {
            "period_list":
            period_list,
            "spectral_envelope_coeffs_harmonic_list":
            spectral_envelope_coeffs_harmonic_list,
            "spectral_envelope_coeffs_noise_list":
            spectral_envelope_coeffs_noise_list
        }

    def extract_as_array(self, sound=None):

        feature_dict = self.extract(sound=sound)

        period_array = np.array(feature_dict["period_list"]).reshape(
            [-1, 1])  # shape = [n_segment, 1]
        spectral_envelope_coeffs_harmonic_array = np.array(
            feature_dict["spectral_envelope_coeffs_harmonic_list"]
        )  # shape = [n_segment, n_triangle_function]
        spectral_envelope_coeffs_noise_array = np.array(
            feature_dict["spectral_envelope_coeffs_noise_list"]
        )  # shape = [n_segment, n_triangle_function]

        feature_array = np.concatenate([
            period_array, spectral_envelope_coeffs_harmonic_array,
            spectral_envelope_coeffs_noise_array
        ],
                                       axis=1)

        return feature_array
Example #8
0
from feature_extraction.voice_feature_extractor import voice_feature_extractor
from feature_extraction.spectral_enveloppe_extractor import SpectralEnvelopeExtractor
from database_tools.sound_file_loader import get_mono_left_channel_sound_and_sampling_frequency
from alignment.dynamic_time_warping import get_dtw_matrix_and_corresponding_segments
from params.params import get_params
import numpy as np
from alignment.get_corresponding_segments_as_function import get_corresponding_segments_function

import matplotlib.pyplot as plt

params = get_params()

params["n_triangle_function"] = 15
rough_spectral_envelope_extractor = SpectralEnvelopeExtractor(params=params)

file_1 = "/home/monsieur/projets/voice_conversion/data/bruce_willis/Studio/1.wav"
file_2 = "/home/monsieur/projets/voice_conversion/data/pierre_sendorek/Studio/1.wav"

sound_1, _ = get_mono_left_channel_sound_and_sampling_frequency(file_1)
sound_2, _ = get_mono_left_channel_sound_and_sampling_frequency(file_2)

voice_1_features = rough_spectral_envelope_extractor.get_spectral_envelope_from_sound(
    sound=sound_1)
voice_2_features = rough_spectral_envelope_extractor.get_spectral_envelope_from_sound(
    sound=sound_2)


def distance(a, b):
    return np.linalg.norm(np.log(a) - np.log(b))