File size: 2,163 Bytes
88490a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np
from scipy.interpolate import interp1d


def normalize(audio, min_y=-1.0, max_y=1.0, eps=1e-8):
    assert len(audio.shape) == 1
    max_y -= eps
    min_y += eps
    amax = audio.max()
    amin = audio.min()
    audio = (max_y - min_y) * (audio - amin) / (amax - amin) + min_y
    return audio


def get_stereo(pop_y, midi_y, pop_scale=0.99):
    if len(pop_y) > len(midi_y):
        midi_y = np.pad(midi_y, (0, len(pop_y) - len(midi_y)))
    elif len(pop_y) < len(midi_y):
        pop_y = np.pad(pop_y, (0, -len(pop_y) + len(midi_y)))
    stereo = np.stack((midi_y, pop_y * pop_scale))
    return stereo


def generate_variable_f0_sine_wave(f0, len_y, sr):
    """
    integrate instant frequencies to get pure tone sine wave
    """
    x_sample = np.arange(len(f0))
    intp = interp1d(x_sample, f0, kind="linear")
    f0_audiorate = intp(np.linspace(0, len(f0) - 1, len_y))
    pitch_wave = np.sin((np.nan_to_num(f0_audiorate) / sr * 2 * np.pi).cumsum())
    return pitch_wave


def fluidsynth_without_normalize(self, fs=44100, sf2_path=None):
    """Synthesize using fluidsynth. without signal normalize
    Parameters
    ----------
    fs : int
        Sampling rate to synthesize at.
    sf2_path : str
        Path to a .sf2 file.
        Default ``None``, which uses the TimGM6mb.sf2 file included with
        ``pretty_midi``.
    Returns
    -------
    synthesized : np.ndarray
        Waveform of the MIDI data, synthesized at ``fs``.
    """
    # If there are no instruments, or all instruments have no notes, return
    # an empty array
    if len(self.instruments) == 0 or all(len(i.notes) == 0 for i in self.instruments):
        return np.array([])
    # Get synthesized waveform for each instrument
    waveforms = [i.fluidsynth(fs=fs, sf2_path=sf2_path) for i in self.instruments]
    # Allocate output waveform, with #sample = max length of all waveforms
    synthesized = np.zeros(np.max([w.shape[0] for w in waveforms]))
    # Sum all waveforms in
    for waveform in waveforms:
        synthesized[: waveform.shape[0]] += waveform
    # Normalize
    # synthesized /= np.abs(synthesized).max()
    return synthesized