示例#1
0
def main():
    dest = "tests/test_1_note_Csharp3.wav"
    tone = librosa.tone(138.59, sr=22050, length=44100)
    soundfile.write(dest, tone, 22050)
    print("Created {0} with note C#3".format(dest))

    dest = "tests/test_1_note_E4.wav"
    tone = librosa.tone(329.63, sr=22050, length=44100)
    soundfile.write(dest, tone, 22050)
    print("Created {0} with note E4".format(dest))

    dest = "tests/test_2_notes_E2_F3.wav"
    tone = numpy.zeros(44100)
    tone += librosa.tone(82.41, sr=22050, length=44100)
    tone += librosa.tone(174.61, sr=22050, length=44100)
    soundfile.write(dest, tone, 22050)
    print("Created {0} with notes E2, F3".format(dest))

    dest = "tests/test_2_notes_G3_Asharp4.wav"
    tone = numpy.zeros(44100)
    tone += librosa.tone(196, sr=22050, length=44100)
    tone += librosa.tone(466.16, sr=22050, length=44100)
    soundfile.write(dest, tone, 22050)
    print("Created {0} with notes G3, A#4".format(dest))

    dest = "tests/test_3_notes_G2_B2_G#3.wav"
    tone = numpy.zeros(44100)
    tone += librosa.tone(98, sr=22050, length=44100)
    tone += librosa.tone(123.47, sr=22050, length=44100)
    tone += librosa.tone(207.65, sr=22050, length=44100)
    soundfile.write(dest, tone, 22050)
    print("Created {0} with notes G2, B2, G#3".format(dest))

    return 0
示例#2
0
def artificial_inst(f: float, sr: float, length: int, phis: dict = None):
    """
    Create artificial instrumental sound
    Args:
        f: fundamental frequency of the instrument
        sr: sampling rate
        length: sample length of the instrumental sound
        phis: phase information for all the harmonics in the artificial
            instrument. key: value -> harmonic number:

    Returns:

    """
    H = (1, 3, 5)
    if phis is None:
        phis = {h: -np.pi / 2 for h in H}

    # add 2, 3, 4, 8 - th subharmonics to enrich sound
    piano_sound = 0
    for h in H:
        piano_sound += 1 / h * librosa.tone(
            h * f, sr=sr, length=length, phi=phis[h])

    # normalize value
    piano_sound /= sum([1 / h for h in H])

    def get_phi(f, phi):
        return 2 * np.pi * f * length / sr + phi

    phis = {h: get_phi(h * f, phis[h]) for h in H}
    # put some decay
    return piano_sound, phis
示例#3
0
def test_mfcc_to_mel(n_mfcc, n_mels, dct_type, lifter):
    y = librosa.tone(440.0, sr=22050, duration=1)
    mfcc = librosa.feature.mfcc(y=y,
                                sr=22050,
                                n_mels=n_mels,
                                n_mfcc=n_mfcc,
                                dct_type=dct_type)

    # check lifter parameter error
    if lifter < 0:
        with pytest.raises(librosa.ParameterError):
            librosa.feature.inverse.mfcc_to_mel(mfcc * 10**3,
                                                n_mels=n_mels,
                                                dct_type=dct_type,
                                                lifter=lifter)

    # check no lifter computations
    elif lifter == 0:
        melspec = librosa.feature.melspectrogram(y=y, sr=22050, n_mels=n_mels)

        mel_recover = librosa.feature.inverse.mfcc_to_mel(mfcc,
                                                          n_mels=n_mels,
                                                          dct_type=dct_type)
        # Quick shape check
        assert melspec.shape == mel_recover.shape

        # Check non-negativity
        assert np.all(mel_recover >= 0)

    # check that runtime warnings are triggered when appropriate
    elif lifter == 2:
        with pytest.warns(UserWarning):
            librosa.feature.inverse.mfcc_to_mel(mfcc * 10**3,
                                                n_mels=n_mels,
                                                dct_type=dct_type,
                                                lifter=lifter)

    # check if mfcc_to_mel works correctly with lifter
    else:
        ones = np.ones(mfcc.shape, dtype=mfcc.dtype)
        n_mfcc = mfcc.shape[0]
        idx = np.arange(1, 1 + n_mfcc, dtype=mfcc.dtype)
        lifter_sine = 1 + lifter * 0.5 * np.sin(
            np.pi * idx / lifter)[:, np.newaxis]

        # compute the recovered mel
        mel_recover = librosa.feature.inverse.mfcc_to_mel(ones * lifter_sine,
                                                          n_mels=n_mels,
                                                          dct_type=dct_type,
                                                          lifter=lifter)

        # compute the expected mel
        mel_expected = librosa.feature.inverse.mfcc_to_mel(ones,
                                                           n_mels=n_mels,
                                                           dct_type=dct_type,
                                                           lifter=0)

        # assert equality of expected and recovered mels
        np.testing.assert_almost_equal(mel_recover, mel_expected, 3)
示例#4
0
def create_tone(freq, temp_dir):
    temp_path = temp_dir + '/temp.wav'
    tone = librosa.tone(freq / 2, duration=10)
    librosa.output.write_wav(temp_path, tone, 44100)
    out_path = temp_dir + '/out.wav'
    subprocess.call([
        'ffmpeg', '-i', temp_path, '-acodec', 'pcm_s16le', '-ac', '1', '-ar',
        '44100', out_path
    ])
    return out_path
示例#5
0
    def __test(frequency, sr, length, duration, phi):

        y = librosa.tone(frequency=frequency,
                         sr=sr,
                         length=length,
                         duration=duration,
                         phi=phi)

        if length is not None:
            assert len(y) == length
        else:
            assert len(y) == np.ceil(duration * sr)
def test_mel_to_audio():
    y = librosa.tone(440.0, sr=22050, duration=1)

    M = librosa.feature.melspectrogram(y=y, sr=22050)

    y_inv = librosa.feature.inverse.mel_to_audio(M, sr=22050, length=len(y))

    # Sanity check the length
    assert len(y) == len(y_inv)

    # And that it's valid audio
    assert librosa.util.valid_audio(y_inv)
示例#7
0
def test_mel_to_audio():
    y = librosa.tone(440.0, sr=22050, duration=1)

    M = librosa.feature.melspectrogram(y=y, sr=22050)

    y_inv = librosa.feature.inverse.mel_to_audio(M, sr=22050, length=len(y))

    # Sanity check the length
    assert len(y) == len(y_inv)

    # And that it's valid audio
    assert librosa.util.valid_audio(y_inv)
示例#8
0
    def __test(frequency, sr, length, duration, phi):

        y = librosa.tone(frequency=frequency,
                         sr=sr,
                         length=length,
                         duration=duration,
                         phi=phi)

        if length is not None:
            assert len(y) == length
        else:
            assert len(y) == np.ceil(duration * sr)
示例#9
0
def test_chroma_issue1295(freq):

    tone_1 = librosa.tone(frequency=freq, sr=22050, duration=1)
    chroma_1 = librosa.feature.chroma_stft(y=tone_1,
                                           sr=22050,
                                           n_chroma=120,
                                           base_c=True)

    actual_argmax = np.unravel_index(chroma_1.argmax(), chroma_1.shape)

    if freq == 261.63:
        assert actual_argmax == (118, 0)
    elif freq == 440:
        assert actual_argmax == (90, 0)
示例#10
0
def test_mfcc_to_audio(n_mfcc, n_mels, dct_type):
    y = librosa.tone(440.0, sr=22050, duration=1)

    mfcc = librosa.feature.mfcc(y=y, sr=22050,
                                n_mels=n_mels, n_mfcc=n_mfcc, dct_type=dct_type)

    y_inv = librosa.feature.inverse.mfcc_to_audio(mfcc, n_mels=n_mels,
                                                  dct_type=dct_type,
                                                  length=len(y))

    # Sanity check the length
    assert len(y) == len(y_inv)

    # And that it's valid audio
    assert librosa.util.valid_audio(y_inv)
示例#11
0
def test_mfcc_to_audio(n_mfcc, n_mels, dct_type):
    y = librosa.tone(440.0, sr=22050, duration=1)

    mfcc = librosa.feature.mfcc(y=y, sr=22050,
                                n_mels=n_mels, n_mfcc=n_mfcc, dct_type=dct_type)

    y_inv = librosa.feature.inverse.mfcc_to_audio(mfcc, n_mels=n_mels,
                                                  dct_type=dct_type,
                                                  length=len(y))

    # Sanity check the length
    assert len(y) == len(y_inv)

    # And that it's valid audio
    assert librosa.util.valid_audio(y_inv)
示例#12
0
def test_mfcc_to_mel(n_mfcc, n_mels, dct_type):
    y = librosa.tone(440.0, sr=22050, duration=1)

    mfcc = librosa.feature.mfcc(y=y, sr=22050,
                                n_mels=n_mels, n_mfcc=n_mfcc, dct_type=dct_type)

    melspec = librosa.feature.melspectrogram(y=y, sr=22050, n_mels=n_mels)
    
    mel_recover = librosa.feature.inverse.mfcc_to_mel(mfcc, n_mels=n_mels,
                                                      dct_type=dct_type)

    # Quick shape check
    assert melspec.shape == mel_recover.shape

    # Check non-negativity
    assert np.all(mel_recover >= 0)
示例#13
0
def test_mfcc_to_mel(n_mfcc, n_mels, dct_type):
    y = librosa.tone(440.0, sr=22050, duration=1)

    mfcc = librosa.feature.mfcc(y=y, sr=22050,
                                n_mels=n_mels, n_mfcc=n_mfcc, dct_type=dct_type)

    melspec = librosa.feature.melspectrogram(y=y, sr=22050, n_mels=n_mels)
    
    mel_recover = librosa.feature.inverse.mfcc_to_mel(mfcc, n_mels=n_mels,
                                                      dct_type=dct_type)

    # Quick shape check
    assert melspec.shape == mel_recover.shape

    # Check non-negativity
    assert np.all(mel_recover >= 0)
示例#14
0
    M = librosa.feature.melspectrogram(y=y, sr=22050)

    y_inv = librosa.feature.inverse.mel_to_audio(M, sr=22050, length=len(y))

    # Sanity check the length
    assert len(y) == len(y_inv)

    # And that it's valid audio
    assert librosa.util.valid_audio(y_inv)


@pytest.mark.parametrize("n_mfcc", [13, 20])
@pytest.mark.parametrize("n_mels", [64, 128])
@pytest.mark.parametrize("dct_type", [2, 3])
@pytest.mark.parametrize("lifter", [-1, 0, 1, 2, 3])
@pytest.mark.parametrize("y", [librosa.tone(440.0, sr=22050, duration=1)])
def test_mfcc_to_mel(y, n_mfcc, n_mels, dct_type, lifter):
    mfcc = librosa.feature.mfcc(y=y,
                                sr=22050,
                                n_mels=n_mels,
                                n_mfcc=n_mfcc,
                                dct_type=dct_type)

    # check lifter parameter error
    if lifter < 0:
        with pytest.raises(librosa.ParameterError):
            librosa.feature.inverse.mfcc_to_mel(mfcc * 10**3,
                                                n_mels=n_mels,
                                                dct_type=dct_type,
                                                lifter=lifter)
示例#15
0
def noisy_sinewave(freq, sample_rate, duration):
    signal = librosa.tone(freq, sr=sample_rate, duration=duration)
    noise = np.random.normal(0, 0.1, sample_rate * duration)**2
    return peak_normalize(signal + noise, -3)
示例#16
0
import librosa as lib
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
y = lib.tone(440, sr=22050, length=22050)
plt.plot(y[2000:2100])
plt.show()
spect = lib.stft(y, n_fft=2048, hop_length=512)
spect_db = lib.amplitude_to_db(np.abs(spect))
librosa.display.specshow(spect_db, x_axis='time', y_axis='log')
plt.title('spectrogram')
plt.colorbar()
plt.show()
"""
Created on Sat Jun  5 11:35:07 2021

@author: Admin
"""
示例#17
0
sample_duration = librosa.get_duration(y=sample_array, sr=sample_rate)
print("Sample duration: ", sample_duration)
print("Sample: ", sample_array)
print("Sample rate: ", sample_rate)

# Plot the sound file
plt.figure()
plt.subplot(3, 1, 1)
librosa.display.waveplot(sample_array, sr=sample_rate)
plt.title(requested_sample + " - mono")
plt.show()

# Save new file
new_sample = input("Enter new file name: ")
librosa.output.write_wav(new_sample, sample_array, sample_rate)

# Plot cosine signal
plt.figure()
plt.subplot(3, 1, 1)
cosine_map_plot = librosa.tone(1000, sr=sample_rate, duration=0.002)
cosine_map = librosa.tone(1000, sr=sample_rate, duration=sample_duration)
print("Cosine map: ", cosine_map)

# print("Cosine sample rate: ", sample_rate_cosine)
librosa.display.waveplot(cosine_map_plot, sr=sample_rate)
plt.title(requested_sample + " - cosine wave (1KHz)")
plt.show()

# Save Cosine file
cosine_file = input("Enter file name for cosine sample: ")
librosa.output.write_wav(cosine_file, cosine_map, sample_rate)
示例#18
0
def y_cqt_110(sr_cqt):
    return librosa.tone(110.0, sr_cqt, duration=0.75)