示例#1
0
def plotPng(filepath, filename):
    y, sr = librosa.load(filepath + filename, sr=None)
    tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
    y_beats = librosa.clicks(frames=beats, sr=sr, length=len(y))
    times = librosa.frames_to_time(beats, sr=sr)
    y_beat_times = librosa.clicks(times=times, sr=sr)

    # Or with a click frequency of 880Hz and a 500ms sample
    y_beat_times880 = librosa.clicks(times=times,
                                     sr=sr,
                                     click_freq=880,
                                     click_duration=0.5)

    # Display click waveform next to the spectrogram
    plt.figure()
    S = librosa.feature.melspectrogram(y=y, sr=sr)
    ax = plt.subplot(1, 1, 1)
    librosa.display.specshow(librosa.power_to_db(S, ref=np.max),
                             x_axis='time',
                             y_axis='mel')
    # plt.subplot(2, 1, 1, sharex=ax)
    # librosa.display.waveplot(y_beat_times, sr=sr, label='Beat clicks')
    plt.legend()
    plt.xlim(0, 4)
    plt.tight_layout()

    portion = os.path.splitext(filename)
    # os.rename(filename,portion[0]+".png")

    # 切换文件路径,如无路径则要新建或者路径同上。否则会直接生成在当前文件夹下
    os.chdir("F:/研究生记录/研一/语音分离/一阶段/语音分离结果/样本4/")
    plt.savefig(portion[0] + ".png")
示例#2
0
def Audio_Display(data):
    # Sonify detected beat events
    tempo, beats = librosa.beat.beat_track(y=audio_data, sr=sr)
    audio_beats = librosa.clicks(frames=beats, sr=sr)

    # Or generate a signal of the same length as audio_data
    audio_beats = librosa.clicks(frames=beats, sr=sr, length=len(audio_data))

    # Or use timing instead of frame indices
    times = librosa.frames_to_time(beats, sr=sr)
    audio_beat_times = librosa.clicks(times=times, sr=sr)

    # Or with a click frequency of 880Hz and a 500ms sample
    audio_beat_times880 = librosa.clicks(times=times, sr=sr,
                                     click_freq=880, click_duration=0.5)

    # Display click waveform next to the spectrogram
    plt.figure()
    Spec = librosa.feature.melspectrogram(y=audio_data, sr=sr)
    ax = plt.subplot(3, 1, 2)
    librosa.display.specshow(librosa.power_to_db(Spec, ref=np.max), x_axis='time', y_axis='mel')

    plt.subplot(3, 1, 1)# , sharex=ax)
    librosa.display.waveplot(audio_data, sr=sr, label='Beat Clicks')  # Plot raw data to wave shape
    plt.legend()
    plt.xlim()
    plt.tight_layout()

    plt.subplot(3, 1, 3)
    D = librosa.amplitude_to_db(librosa.stft(audio_data), ref=np.max)
    librosa.display.specshow(D, y_axis='linear')
    plt.colorbar(format='%+2.0f dB')
    plt.title('Linear-frequency power spectrogram')
    plt.show()
示例#3
0
def changeTempo(current_tempo, onset_times, desired_tempo):

    hi_hat, _ = librosa.load('./Thrown/test_sounds/sfx/closed_hi_hat.wav')
    drum_hit, _ = librosa.load('./Thrown/test_sounds/sfx/drum_hit.wav')

    desired_tempo_timing = 60 / desired_tempo
    current_tempo_timing = 60 / current_tempo

    scale_factor = desired_tempo_timing / current_tempo_timing

    onset_frames1 = librosa.time_to_frames(onset_times, sr=sr)

    clicks1 = librosa.clicks(frames=onset_frames1,
                             sr=sr,
                             click_duration=.01,
                             length=len(t[2][4]),
                             click=hi_hat)

    sf.write('./Thrown/test_sounds/thrown_w_beat.wav', clicks1 + t[1][2], sr)

    scaled_onset_times = []
    for i in range(0, len(onset_times)):
        scaled_onset_times.append(onset_times[i] * scale_factor)

    scaled_beat_frames = []
    for i in range(0, len(t[1][8])):
        scaled_beat_frames.append(t[1][8][i] * scale_factor)

    scaled_beat_times = []
    for i in range(0, len(t[1][6])):
        scaled_beat_times.append(t[1][6] * scale_factor)

    onset_frames2 = librosa.time_to_frames(scaled_onset_times, sr=sr)

    print(onset_frames2)

    scaled_sample = librosa.effects.time_stretch(t[1][2], 1 / scale_factor)

    clicks2 = librosa.clicks(frames=onset_frames2,
                             sr=sr,
                             click_duration=.01,
                             length=len(scaled_sample),
                             click=hi_hat)
    clicks3 = librosa.clicks(frames=scaled_beat_frames,
                             sr=sr,
                             click_duration=.01,
                             length=len(scaled_sample),
                             click=drum_hit)

    librosa.output.write_wav('./Thrown/test_sounds/thrown_altered_beat.wav',
                             clicks2 + clicks3 + scaled_sample, sr)
    sf.write('./Thrown/test_sounds/altered_beat.wav', clicks2 + clicks3, sr)

    plt.figure(figsize=(14, 5))
    plt.title("Removing Percussive Sample, Scalability of Tempo Events")
    plt.vlines(scaled_onset_times, -1, 1, color='c', linestyles='dashed')
    plt.vlines(scaled_beat_times, -1, 1, color='y', linestyles='dashed')
    plt.ylim(-1, 1)
    plt.xlim(0, 5)
def createMidiFromAudioData():
    i = 0
    for i in range(len(input_audio_filename_array)):
        input_audio_filename = input_audio_filename_array[i]
        bpm = input_bpm_array[i]
        auftakt_16th_notes_number = auftakt_16th_notes_number_array[i]

        # foreground, background, background_percussiveの出力ファイル名
        audio_filename_foreground = input_audio_filename + '_foreground.wav'
        audio_filename_background = input_audio_filename + '_background.wav'
        audio_filename_background_percussive = input_audio_filename + '_background_percussive.wav'

        # 入力wavデータ読み込み、分離処理
        x, sr = librosa.load(input_audio_filename)
        S_foreground, S_background = separateMusicIntoForegroundAndBackground(x, sr)
        background_harmonic, background_percussive = separateMusicIntoHarmonicAndPercussive(S_background)

        # foreground, background, background_percussiveのwavファイル書き出し
        librosa.output.write_wav(audio_filename_foreground, librosa.istft(S_foreground), sr)
        librosa.output.write_wav(audio_filename_background, librosa.istft(S_background), sr)
        librosa.output.write_wav(audio_filename_background_percussive, background_percussive, sr)

        # ビート処理
        onset_envelope = librosa.onset.onset_strength(background_percussive, sr=sr, hop_length=hop_length)
        onset_frames = librosa.util.peak_pick(onset_envelope, 7, 7, 7, 7, 0.8, 5)
        beat_frames_per_16th_note = trackBeatsPer16thNote(background_percussive, bpm, sr=sr, hop_length=hop_length,
                                                          offset_16th_notes=0)
        quantized_onset_frames_per_16th_note, onset_frames_index_of_16th_notes = quantizeOnsetFramesPer16thNote(
            onset_frames, beat_frames_per_16th_note)
        strong_onset_frames, strong_onset_frames_index_of_16th_notes = getStrongOnsetFrames(onset_envelope,
                                                                                            beat_frames_per_16th_note,
                                                                                            onset_frames_index_of_16th_notes)
        weak_onset_frames, weak_onset_frames_index_of_16th_notes = getWeakOnsetFrames(
            strong_onset_frames_index_of_16th_notes, onset_frames_index_of_16th_notes, beat_frames_per_16th_note)

        # クリック音入力
        N = len(background_percussive)
        T = N / float(sr)
        # t = np.linspace(0, T, len(onset_envelope))
        clicks_strong = librosa.clicks(frames=strong_onset_frames, sr=sr, hop_length=hop_length, length=N)
        clicks_weak = librosa.clicks(frames=weak_onset_frames, sr=sr, hop_length=hop_length, click_freq=1000.0,
                                     click_duration=0.01, length=N)
        audio_filename_background_percussive_clicks = audio_filename_background_percussive + '_clicks.wav'
        librosa.output.write_wav(audio_filename_background_percussive_clicks,
                                 background_percussive + clicks_strong + clicks_weak, sr)

        # リズム譜作成
        midi_filename = input_audio_filename + '.mid'
        createMidiRhythmScore(midi_filename, onset_frames_index_of_16th_notes, strong_onset_frames_index_of_16th_notes,
                              weak_onset_frames_index_of_16th_notes, bpm, auftakt_16th_notes_number)
示例#5
0
def generate_click(file, filename, click_freq, click_duration, vol_adj_song, vol_adj_click, convert_folder):
  saveFileExt = filename.rsplit('.', 1)[1].lower()
  saveName = filename

  if(saveFileExt != "wav"):
    saveName = saveName.rsplit('.', 1)[0].lower() + ".wav"

  inputAudio, _, newName = convert_file(file, filename, saveName, convert_folder)
  sr = 44100

  _, beats = beat_track(inputAudio, sr)

  clickAudio = clicks(
            frames = beats, # the beats to place clicks
            sr = sr, # sample rate
            length = len(inputAudio), # length of the song (necessary to align clicktrack and song)
            click_freq = click_freq, # frequency of each click (in Hz)
            click_duration = click_duration # duration of each click (in seconds)
          )

  inputAudio *= vol_adj_song
  clickAudio *= vol_adj_click 

  sf.write(os.path.join(convert_folder, newName), inputAudio + clickAudio, sr)
  return newName
示例#6
0
def onset(x, sr):
    # Short-time Fourier transform (for EQ, must do inverse Fourier transform after)
    X = librosa.stft(x)

    # Find the frames when onsets occur
    onset_frames = librosa.onset.onset_detect(x, sr=sr)
    print("Onset Frames = " + str(onset_frames) + "\n ")

    # Find the times, in seconds, when onsets occur in the audio signal
    onset_times = librosa.frames_to_time(onset_frames, sr=sr)
    print("Onset Times = " + str(onset_times) + "\n ")

    # Convert the onset frames into sample indices to play "BEEB" sound on it
    onset_samples = librosa.frames_to_samples(onset_frames)
    print("Onset Samples = " + str(onset_samples) + "\n ")

    # Use the "length" parameter so the click track is the same length as the original signal
    clicks = librosa.clicks(times=onset_times, length=len(x))

    # Play the click track "added to" the original signal
    sd.play(x + clicks, sr)

    # Display the waveform of the original signal
    librosa.display.waveplot(x, sr)
    plt.title("Original Signal")
    plt.show()  # Close window to resume

    return onset_frames, onset_times, onset_samples
	def clicked():
		bps = k1.get()
		b = np.around(np.linspace(0,60,bps),decimals=2)
		y_beats = librosa.clicks(times= b)
		
		librosa.output.write_wav('metro.wav', y_beats, 22050)
		tt = TapTester()
		c = 0
		player = MediaPlayer('metro.wav')

		for i in range(1000):
			# if tt.listen() == 1:
			# 	if ((time.time()-start)% (2/3)) < 0.3:
			# 		c += 1
			# 		print("on beat")
			# 		if c>1:
			# 			print("streak x"+str(c))
			# 	else:
			# 		print("off beat")
			# 		c = 0

			if tt.listen() == 1:
				if c == 0:
					start = time.time()
				if b[c]-0.1 <= round(time.time()-start,2) <= b[c]+0.1:
					print("on beat x", str(c))
					lbl_streak.configure(text = str(c)+"x Streak")
					root.update()
					c+=1
				else:
					c=0
示例#8
0
def testMusic(musicFile, fps):
    baseAudio, sampleRate = librosa.load(musicFile)
    mmBeats = getBeatTimes(musicFile, fps)
    print("Madmom found", len(mmBeats), "beats.")
    mmClicks = librosa.clicks(mmBeats, sr=sampleRate, length=len(baseAudio))
    librosa.output.write_wav('BEAT_TEST_' + musicFile, baseAudio + mmClicks,
                             sampleRate)
示例#9
0
def overlap_click(original, click_position, sr=44100, click_freq=2000, click_duration=0.5):
    """
    :param click_position: Notice that position should be given in second
    :return: wave
    """
    cwave = librosa.clicks(np.array(click_position), sr=44100, click_freq=4000, click_duration=0.05) / 2
    original, wave = mono_pad_or_truncate(original, cwave)
    return standardize(original + wave)
示例#10
0
def compute_plot_tempogram_PLP(fn_wav,
                               Fs=22050,
                               N=500,
                               H=10,
                               Theta=np.arange(30, 601),
                               title='',
                               figsize=(8, 4),
                               plot_maxtempo=False):
    """Compute and plot Fourier-based tempogram and PLP function
    Notebook: C6/C6S3_PredominantLocalPulse.ipynb"""
    x, Fs = librosa.load(fn_wav, Fs)

    nov, Fs_nov = LibFMP.C6.compute_novelty_spectrum(x,
                                                     Fs=Fs,
                                                     N=2048,
                                                     H=512,
                                                     gamma=100,
                                                     M=10,
                                                     norm=1)
    nov, Fs_nov = LibFMP.C6.resample_signal(nov, Fs_in=Fs_nov, Fs_out=100)

    L = len(nov)
    H = 10
    X, T_coef, F_coef_BPM = LibFMP.C6.compute_tempogram_Fourier(nov,
                                                                Fs=Fs_nov,
                                                                N=N,
                                                                H=H,
                                                                Theta=Theta)
    nov_PLP = compute_PLP(X, Fs_nov, L, N, H, Theta)
    tempogram = np.abs(X)

    fig, ax = plt.subplots(2,
                           2,
                           gridspec_kw={
                               'width_ratios': [1, 0.05],
                               'height_ratios': [2, 1]
                           },
                           figsize=figsize)
    LibFMP.B.plot_matrix(tempogram,
                         T_coef=T_coef,
                         F_coef=F_coef_BPM,
                         title=title,
                         ax=[ax[0, 0], ax[0, 1]],
                         ylabel='Tempo (BPM)',
                         colorbar=True)
    if plot_maxtempo:
        coef_k = np.argmax(tempogram, axis=0)
        ax[0, 0].plot(T_coef, F_coef_BPM[coef_k], 'r.')

    t_nov = np.arange(nov.shape[0]) / Fs_nov
    peaks, properties = signal.find_peaks(nov_PLP, prominence=0.05)
    peaks_sec = t_nov[peaks]
    LibFMP.B.plot_signal(nov_PLP, Fs_nov, color='k', ax=ax[1, 0])
    ax[1, 1].set_axis_off()
    ax[1, 0].plot(peaks_sec, nov_PLP[peaks], 'ro')
    plt.show()
    x_peaks = librosa.clicks(peaks_sec, sr=Fs, click_freq=1000, length=len(x))
    ipd.display(ipd.Audio(x + x_peaks, rate=Fs))
示例#11
0
 def play(self, with_clicks=False):
     if not with_clicks:
         return ipd.Audio(self.path, rate=self.sampling_rate)
     else:
         clicks = librosa.clicks(self.beat_times,
                                 sr=self.sampling_rate,
                                 length=len(self.waveform))
         audio = self.waveform + clicks
         return ipd.Audio(audio, rate=self.sampling_rate)
def saveAudioWithFormBeeps(audio, time, sr=22050, folder_name=''):

    #check if folder exists or make it
    checkMakeFolder(folder_name)

    # generating a track with clicks at the start of forms
    clicks = lr.clicks(times=lr.samples_to_time(time), length=len(audio))

    # adsd the clicks to original audio and save it
    sf.write(folder_name + 'audio_beeped.wav', audio + clicks, int(sr))
示例#13
0
def drum_track(x, r):
    drum_onsets = ADT([filename])[0]
    clicks = librosa.clicks(times=drum_onsets['Kick'], sr=sr, length=len(x))
    clicks = librosa.clicks(times=drum_onsets['Snare'], sr=sr, length=len(x))
    clicks = librosa.clicks(times=drum_onsets['Hihat'], sr=sr, length=len(x))
    # Compute average drum beat signal.
    frame_sz = int(0.100 * sr)

    def normalize(z):
        return z / scipy.linalg.norm(z)

    drum_type = ['Kick', 'Snare', 'Hihat']
    drum_track_list = []
    for iii in drum_type:
        onset_samples = librosa.time_to_samples(iii, sr=sr)
        x_avg = numpy.mean(
            [normalize(x[i:i + frame_sz]) for i in onset_samples], axis=0)
        # Compute average spectrum.
        X = librosa.spectrum.fft.fft(x_avg)
        Xmag = librosa.amplitude_to_db(abs(X))
示例#14
0
def test_click_multi():

    click = np.ones((3, 100))

    yout = librosa.clicks(times=[0, 1, 2], sr=1000, click=click)

    print(yout.shape)
    assert yout.shape[0] == click.shape[0]

    assert np.allclose(yout[..., :100], click)
    assert np.allclose(yout[..., 1000:1100], click)
    assert np.allclose(yout[..., 2000:2100], click)
示例#15
0
def feature(file, num):

    y, sr = librosa.load(file, sr=44100)
    y_harmonic, y_percussive = librosa.effects.hpss(y)
    tempo, beats = librosa.beat.beat_track(y=y,sr=sr)
    tempo, beat_frames = librosa.beat.beat_track(y=y_percussive,sr=sr)
    y_beats = librosa.clicks(frames=beats, sr=sr)
    
    plt.figure(figsize=(6, 4))
    D=librosa.amplitude_to_db(librosa.stft(y),ref=np.max)
    librosa.display.specshow(D, y_axis='linear')
    plt.title('STFT Linear-frequency power spectrogram')
    plt.savefig('..\\SourceData\\STFT\\' + str(num) + '.JPG')
    plt.close()
    
    
    CQT=librosa.amplitude_to_db(librosa.cqt(y,sr = 44100), ref=np.max)
    plt.figure(figsize=(6, 4))
    librosa.display.specshow(CQT,y_axis='cqt_note')
    plt.title('Constant-Q power spectrogram (note)')
    plt.savefig('..\\SourceData\\CQT\\' + str(num) + '.JPG')
    plt.close()
    
    M=librosa.feature.mfcc(y=y, sr=sr)
    plt.figure(figsize=(6, 4))
    librosa.display.specshow(M, x_axis = 'time')
    plt.title('MFCC')
    plt.savefig('..\\SourceData\\MFCC\\' + str(num) + '.JPG')
    plt.close()
    
    Mfcc_delta = librosa.feature.delta(M)
    
    Beat_Mfcc_delta = librosa.util.sync(np.vstack([M, Mfcc_delta]), beat_frames)
    plt.figure(figsize=(6, 4))
    librosa.display.specshow(Beat_Mfcc_delta, x_axis = 'time')
    plt.title('Beat_Mfcc_delta')
    plt.savefig('..\\SourceData\\MFCC_DELTA\\' + str(num) + '.JPG')
    plt.close()
    
    M=librosa.feature.chroma_stft(y=y, sr=sr)
    plt.figure(figsize=(6, 4))
    librosa.display.specshow(M, y_axis='chroma')
    plt.title('Chroma_STFT')
    plt.savefig('..\\SourceData\\Chroma_STFT\\' + str(num) + '.JPG')
    plt.close()
    
    C=librosa.feature.chroma_cqt(y=y, sr=sr)
    plt.figure(figsize=(6, 4))
    librosa.display.specshow(C,y_axis='chroma')
    plt.title('Chroma_CQT')
    plt.savefig('..\\SourceData\\Chroma_CQT\\' + str(num) + '.JPG')
    plt.close()
示例#16
0
def upload(request):
    if request.method == 'POST':
        myfile = request.FILES['audio']
        fs = FileSystemStorage()
        if fs.exists(myfile.name):
            os.remove(os.path.join(settings.MEDIA_ROOT, myfile.name))
        filename = fs.save(myfile.name, myfile)
        # uploaded_file_url = fs.url(filename)
        # filename_only = os.path.splitext(os.path.basename(uploaded_file_url))[0]
        filename_only = filename.split('.')[-2]

        x_2, sr_2 = librosa.load('media/' + filename)
        total_time_original = len(x_2) / sr_2

        x, sr = librosa.load('media/' + filename, duration=60)
        onset_env = librosa.onset.onset_strength(y=x, sr=sr)
        tempo, beat_times = librosa.beat.beat_track(onset_envelope=onset_env, sr=sr, start_bpm=120, units='time')
        total_time = len(x) / sr
        # time = np.linspace(0, total_time, len(x))

        # plt.plot(time, x, color='b', label='waveform')
        # plt.vlines(beat_times, -1.5, 1.5, color='r', label='beat')
        # plt.legend(['waveform','beat'])
        # plt.xlabel("Times")
        # plt.ylabel("Amplitude")

        # start_sec = 10  # keyboard
        # end_sec = 20  # keyboard
        # plt.xlim(start_sec, end_sec)
        # plt.show()
        # dir_name = 'static/' + filename_only + '.png'
        # plt.savefig(dir_name)

        original_music = 'media/' + filename
        beat_music = 'static/' + filename_only + '_beat.wav'
        clicks = librosa.clicks(beat_times, sr=sr, length=len(x))
        write(beat_music, sr, x + clicks)

        beat_times_file = 'static/' + filename_only + '_beat.txt'
        with open(beat_times_file, 'w') as f:
            for item in beat_times:
                f.write("%s\n" % round(item, 2))
            f.close()

        return render(request, 'list.html', {'filename': filename_only, 'tempo': int(tempo), 'time': int(total_time),
                                             'soundfile': '/' + beat_music, 'beat_times': '/' + beat_times_file,
                                             'soundfile_original': '/' + original_music, 'time_original': int(total_time_original),
                                            'beat': beat_times})

    return redirect('')
示例#17
0
def generate_rhythm_clicks(rhythm, click_interval=0.25, sr=44100):
    step_length_samples = int(librosa.time_to_samples(click_interval, sr=sr))
    rhythm_length_samples = step_length_samples * (len(rhythm))

    # Generate click times
    pulse_click_times, step_click_times = generate_rhythm_times(
        rhythm, click_interval)

    # Generate pulse clicks
    pulse_click_times = np.array(
        [i * click_interval for i in range(len(rhythm)) if rhythm[i] != 0])
    pulse_clicks = librosa.clicks(times=pulse_click_times,
                                  click_freq=2000.0,
                                  sr=sr,
                                  length=rhythm_length_samples)

    # Generate step clicks
    step_click_times = np.array(
        [i * click_interval for i in range(len(rhythm))])
    step_clicks = librosa.clicks(times=step_click_times,
                                 click_freq=1000.0,
                                 sr=sr,
                                 length=rhythm_length_samples)
    step_clicks = np.hstack(
        (step_clicks, np.zeros(step_length_samples,
                               dtype="int")))  # add last step samples

    # Add zeros to pulse clicks so that it's the same length as the step clicks signal
    pulse_clicks = np.hstack((pulse_clicks,
                              np.zeros(len(step_clicks) - len(pulse_clicks),
                                       dtype="int")))

    # Ensure proper length
    pulse_clicks = pulse_clicks[:rhythm_length_samples]
    step_clicks = step_clicks[:rhythm_length_samples]

    return (pulse_clicks, step_clicks)
示例#18
0
def clicks(wav, beats, clicks_volume=8):
    r"""Returns a wav with clicks at the specified beats.

    Arguments:
        wav (1d numpy array): An audio wav at the sampling rate determined by
            `beatfinder.constants`.
        beats (1d numpy array): The beats in units of seconds.
        clicks_volume (float between 0 and 10): The volume of the clicks (it
            can also be set to 11).
    """

    clicks = librosa.clicks(times=beats, sr=constants.sr, length=len(wav))
    clicks *= clicks_volume / 10

    return wav + clicks
示例#19
0
def export_audio_with_click(proc_res, path_audio, path_output, sr=44100):
    # extract time
    times_beat = proc_res[np.where(proc_res[:, 1] != 1)][:, 0]
    times_downbeat = proc_res[np.where(proc_res[:, 1] == 1)][:, 0]

    # load
    y, _ = librosa.core.load(path_audio, sr=sr)

    # click audio
    y_beat = librosa.clicks(
        times=times_beat, sr=sr, click_freq=1200, click_duration=0.5) * 0.6
    y_downbeat = librosa.clicks(times=times_downbeat,
                                sr=sr,
                                click_freq=600,
                                click_duration=0.5)

    # merge
    max_len = max(len(y), len(y_beat), len(y_downbeat))
    y_integrate = np.zeros(max_len)
    y_integrate[:len(y_beat)] += y_beat
    y_integrate[:len(y_downbeat)] += y_downbeat
    y_integrate[:len(y)] += y

    librosa.output.write_wav(path_output, y_integrate, sr)
示例#20
0
 def sonify_music(self):
     """Function to sonify MFCC, Spectral Contrast, HFC and Tempo of the input signal"""
     self.mfcc_outputs = []
     self.contrast_outputs = []
     self.hfcs = []
     self.f0s = []
     self.ats = []
     for frame in self.FrameGenerator():
         self.window()
         self.Spectrum()
         self.Phase(self.fft(self.windowed_x))
         self.MFCC_seq()
         self.mfcc_outputs.append(self.reconstruct_signal_from_mfccs())
         self.contrast()
         fft_filter = self.filter_by_valleys()
         self.contrast_outputs.append(self.ISTFT(fft_filter))
         self.hfcs.append(self.hfc())
         self.spectral_peaks()
         self.fundamental_frequency()
         self.f0s.append(self.f0)
     self.mfcc_outputs = self.output_array(self.mfcc_outputs)
     self.contrast_outputs = self.output_array(self.contrast_outputs)
     self.f0 = Counter(self.f0s).most_common()[0][0]
     self.mel_bands_global()
     self.bpm()
     self.tempo_onsets = clicks(frames=self.ticks * self.fs,
                                length=len(self.signal),
                                sr=self.fs,
                                hop_length=self.H)
     starts = self.ticks * self.fs
     for i in starts:
         self.frame = self.signal[int(i):int(i) + 1024]
         self.Envelope()
         self.AttackTime()
         self.ats.append(self.attack_time)
     starts = starts[np.where(self.ats < np.mean(self.ats))]
     self.ats = np.array(self.ats)[np.where(self.ats < np.mean(self.ats))]
     attacks = np.int64((np.array(self.ats) * self.fs) + starts)
     self.attacks = self.create_clicks(attacks)
     self.hfcs /= max(self.hfcs)
     fir = firwin(11, 1.0 / 8, window="hamming")
     self.filtered = np.convolve(self.hfcs, fir, mode="same")
     self.climb_hills()
     self.hfc_locs = np.array(
         [i for i, x in enumerate(self.Filtered) if x > 0]) * self.H
     self.hfc_locs = self.create_clicks(self.hfc_locs)
示例#21
0
def overlay_rhythm_onto_audio(rhythm,
                              audio_samples,
                              measure_samples,
                              sr=44100,
                              plt_size=(16, 4),
                              click_colors={
                                  "measure": "r",
                                  "pulse": "r",
                                  "step": "r"
                              }):

    # Get overlay data
    pulse_times, step_times, pulse_clicks, step_clicks = generate_rhythm_overlay(
        rhythm, measure_samples, len(rhythm), sr)
    measure_times = librosa.samples_to_time(measure_samples, sr=sr)
    measure_clicks = librosa.clicks(times=measure_times,
                                    sr=sr,
                                    click_freq=3000.0,
                                    length=len(audio_samples))

    # Calculate max length in samples
    available_lengths = [
        len(audio_samples),
        len(measure_clicks),
        len(pulse_clicks),
        len(step_clicks)
    ]
    length_samples = min(available_lengths)

    # Plot original waveform
    plt.figure(figsize=plt_size)
    librosa.display.waveplot(audio_samples, sr=sr, alpha=0.5)

    # Plot rhythm clicks
    plt.vlines(measure_times, -1, 1, color=click_colors["measure"])
    plt.vlines(pulse_times, -0.5, 0.5, color=click_colors["pulse"])
    plt.vlines(step_times, -0.25, 0.25, color=click_colors["step"], alpha=0.75)

    # Play both clicks together with audio track
    concatenated_audio_samples = ((audio_samples[:length_samples] * 2.0) +
                                  (measure_clicks[:length_samples] * 0.25) +
                                  (pulse_clicks[:length_samples] * 0.25) +
                                  (step_clicks[:length_samples] * 0.25))
    audio_display = ipd.Audio(concatenated_audio_samples, rate=sr)
    return audio_display
示例#22
0
文件: ai.py 项目: nakfoury/dedbeetz
def add_clicks(f):
    x, sr = soundfile.read(io.BytesIO(f.read()))
    if x.shape[1] > 1:
        x = x[:, 0]
    o_env = librosa.onset.onset_strength(x, sr=sr)
    onset_frames = librosa.onset.onset_detect(onset_envelope=o_env,
                                              sr=sr,
                                              hop_length=128)[1:-1]
    cowbell, _ = librosa.load('audio/cowbell.wav')
    clicks = librosa.clicks(frames=onset_frames,
                            sr=sr,
                            length=len(x),
                            click=cowbell)
    fout = io.BytesIO()
    soundfile.write(fout, clicks + x, sr, format='WAV')
    print(f'sample rate: {sr}\nlength of file: {len(x)}')
    fout.seek(0)
    return fout
示例#23
0
def upload(request):
    if request.method == 'POST':
        myfile = request.FILES['audio']
        fs = FileSystemStorage()
        if fs.exists(myfile.name):
            os.remove(os.path.join(settings.MEDIA_ROOT, myfile.name))
        filename = fs.save(myfile.name, myfile)
        filename_only = filename.split('.')[-2]

        x_2, sr_2 = librosa.load('media/' + filename)
        total_time_original = len(x_2) / sr_2

        x, sr = librosa.load('media/' + filename, duration=60)
        onset_env = librosa.onset.onset_strength(y=x, sr=sr)
        tempo, beat_times = librosa.beat.beat_track(onset_envelope=onset_env,
                                                    sr=sr,
                                                    start_bpm=120,
                                                    units='time')
        total_time = len(x) / sr

        original_music = 'media/' + filename
        beat_music = 'static/outputs/musics' + filename_only + '_beat.wav'
        clicks = librosa.clicks(beat_times, sr=sr, length=len(x))
        write(beat_music, sr, x + clicks)

        beat_times_file = 'static/outputs/beats' + filename_only + '_beat.txt'
        with open(beat_times_file, 'w') as f:
            for item in beat_times:
                f.write("%s\n" % round(item, 2))
            f.close()

        return render(
            request, 'list.html', {
                'filename': filename_only,
                'tempo': int(tempo),
                'time': int(total_time),
                'soundfile': '/' + beat_music,
                'beat_times': '/' + beat_times_file,
                'soundfile_original': '/' + original_music,
                'time_original': int(total_time_original)
            })

    return redirect('home1')
示例#24
0
def compute_plot_sonify_beat(nov, Fs_nov, beat_ref, factor, title=None, figsize=(6,2)):
    """Compute, plot, and sonfy beat sequence from novelty function [FMP, Section 6.3.2]
    Notebook: C6/C6S3_BeatTracking.ipynb"""
    B = compute_beat_sequence(nov, beat_ref=beat_ref, factor=factor)

    beats = np.zeros(len(nov))
    beats[np.array(B,dtype=np.int32)] = 1#
    if title is None:
        tempo = beat_period_to_tempo(beat_ref, Fs_nov)
        title = r'Optimal beat sequence ($\hat{\delta}=%d$, $F_\mathrm{s}=%d$, $\hat{\tau}=%0.0f$ BPM, $\lambda=%0.2f$)'%(beat_ref, Fs_nov, tempo, factor)

    fig, ax, line = LibFMP.B.plot_signal(nov, Fs_nov, color='k', title=title, figsize=figsize)
    T_coef = np.arange(nov.shape[0]) / Fs_nov
    ax.plot(T_coef, beats, ':r', linewidth=1)
    plt.show()

    beats_sec = T_coef[B]
    x_peaks = librosa.clicks(beats_sec, sr=Fs, click_freq=1000, length=len(x))
    ipd.display(ipd.Audio(x + x_peaks, rate=Fs))
示例#25
0
 def sonify_music(self):
     """Function to sonify MFCC, Spectral Contrast, HFC and Tempo of the input signal"""
     self.mfcc_outputs = []
     self.contrast_outputs = []
     self.hfcs = []
     self.f0s = []
     self.ats = []
     for frame in self.FrameGenerator():    
         self.window() 
         self.Spectrum()
         self.Phase(self.fft(self.windowed_x))
         self.MFCC_seq()
         self.mfcc_outputs.append(self.reconstruct_signal_from_mfccs())
         self.contrast()
         fft_filter = self.filter_by_valleys()
         self.contrast_outputs.append(self.ISTFT(fft_filter))
         self.hfcs.append(self.hfc())
         self.spectral_peaks()
         self.fundamental_frequency()
         self.f0s.append(self.f0)
     self.mfcc_outputs = self.output_array(self.mfcc_outputs)
     self.contrast_outputs = self.output_array(self.contrast_outputs)
     self.f0 = Counter(self.f0s).most_common()[0][0]
     self.mel_bands_global()
     self.bpm()
     self.tempo_onsets = clicks(frames = self.ticks * self.fs, length = len(self.signal), sr = self.fs, hop_length = self.H)
     starts = self.ticks * self.fs
     for i in starts:
         self.frame = self.signal[int(i):int(i)+1024]
         self.Envelope()
         self.AttackTime()
         self.ats.append(self.attack_time)
     starts = starts[np.where(self.ats < np.mean(self.ats))]
     self.ats = np.array(self.ats)[np.where(self.ats < np.mean(self.ats))]
     attacks = np.int64((np.array(self.ats) * self.fs) + starts)
     self.attacks = self.create_clicks(attacks) 
     self.hfcs /= max(self.hfcs)
     fir = firwin(11, 1.0 / 8, window = "hamming")  
     self.filtered = np.convolve(self.hfcs, fir, mode="same")
     self.climb_hills()
     self.hfc_locs = np.array([i for i, x in enumerate(self.Filtered) if x > 0]) * self.H
     self.hfc_locs = self.create_clicks(self.hfc_locs)
示例#26
0
    def __test(times, frames, sr, hop_length, click_freq, click_duration, click, length):

        y = librosa.clicks(times=times,
                           frames=frames,
                           sr=sr,
                           hop_length=hop_length,
                           click_freq=click_freq,
                           click_duration=click_duration,
                           click=click,
                           length=length)

        if times is not None:
            nmax = librosa.time_to_samples(times, sr=sr).max()
        else:
            nmax = librosa.frames_to_samples(frames, hop_length=hop_length).max()

        if length is not None:
            assert len(y) == length
        elif click is not None:
            assert len(y) == nmax + len(click)
示例#27
0
    def __test(times, frames, sr, hop_length, click_freq, click_duration, click, length):

        y = librosa.clicks(times=times,
                           frames=frames,
                           sr=sr,
                           hop_length=hop_length,
                           click_freq=click_freq,
                           click_duration=click_duration,
                           click=click,
                           length=length)

        if times is not None:
            nmax = librosa.time_to_samples(times, sr=sr).max()
        else:
            nmax = librosa.frames_to_samples(frames, hop_length=hop_length).max()

        if length is not None:
            assert len(y) == length
        elif click is not None:
            assert len(y) == nmax + len(click)
def process(audio, fs):
    spectrogram = \
        librosa.amplitude_to_db(abs(librosa.core.stft(audio)), ref=np.max)
    spectral_novelty = librosa.onset.onset_strength(audio, sr=fs)
    peaks = librosa.util.peak_pick(spectral_novelty, 1, 1, 1, 1, 1, 1)
    times = librosa.frames_to_time(np.arange(len(spectral_novelty)), sr=fs)

    plt.subplot(2, 1, 1)
    librosa.display.waveplot(audio)
    plt.xlabel('Time')
    plt.ylabel('Amplitude')
    plt.vlines(times[peaks], audio.min(), audio.max(), color='r')

    plt.subplot(2, 1, 2)
    librosa.display.specshow(spectrogram, x_axis='time', y_axis='log')
    plt.xlabel('Time')
    plt.ylabel('Hz')
    plt.vlines(times[peaks], 0, 10000, color='r')
    plt.show()

    clicks = librosa.clicks(frames=peaks, sr=fs, length=len(audio))
    sd.play(audio + clicks, blocking=True)
示例#29
0
def sonification_librosa( peaks, amplitudes, labels, length, featureRate, fs=44100 ):


    #applying librosa sonification
    sonification = np.zeros( length )

    for i in [1,2,3]:

        idxs = np.where(labels==i)
        current_peaks = peaks[idxs]
        freq = get_frequency(i)

        current_sonification = librosa.clicks(current_peaks, sr=fs, click_freq=freq, length=length)
        sonification += current_sonification

    #scaling the sonificiations with the amplitude of the novelty curvve
    for i in range(len(peaks)-1):
        current_peak = int(peaks[i]*fs)
        length = int(0.1*fs)
        window = np.ones(length) #length of sonification is 100ms
        window = window*amplitudes[i]
        sonification[current_peak:current_peak+length] *= window

    return sonification
示例#30
0
def getFrames(path):
    """Gets onset frames and times

    Parameters:
    song (string): name of the song

    Output:
    .wav song with the clicks for developing purposes
    .csv file with timesptamps of the clicks

    Returns:
    frames_time: list with all the times with onset
    """
    y, sr = librosa.load(path)
    # #get frames
    onset_frames = librosa.onset.onset_detect(y=y, sr=sr)
    # #turn frames into time
    frames_time = librosa.frames_to_time(onset_frames, sr=sr)
    # #create clicks
    clicks = librosa.clicks(frames_time, sr=sr, length=len(y))
    #output
    librosa.output.write_wav(f'./output/audio-onset.wav', y + clicks, sr)
    librosa.output.times_csv(f'./output/audio-onsetimes.csv', frames_time)
    return frames_time
plt.ylim(0)

#peak_picking
#onset_frames = librosa.util.peak_pick(onset_envelope, 7, 7, 7, 7, 0.5, 5)
onset_frames = librosa.util.peak_pick(onset_envelope, 7, 7, 7, 7, 0.8, 5)

#検出したピークに縦線を描画(カラーバー)
plt.plot(t, onset_envelope)
plt.vlines(t[onset_frames], 0, onset_envelope.max(), color='r', alpha=0.7)
plt.xlabel('Time (sec)')
plt.xlim(0, T)
plt.ylim(0)

#検出したピークにクリック音を入れる
clicks = librosa.clicks(frames=onset_frames,
                        sr=sr,
                        hop_length=hop_length,
                        length=N)

#クリック音と共に出力
#librosa.output.write_wav('01.Track_1.background_percussive_click.wav', background_percussive+clicks, sr)
librosa.output.write_wav('01.Track_1.background_percussive_click_delta08.wav',
                         background_percussive + clicks, sr)

#RWC-MDB-P-2001-M06の01はbpm = 90なのでnumpyでarrayをつくる やることは16分でクォンタイズ
#BPM90で1小節が2.666666...秒
#beat, tempoの取得
tempo, beat_times = librosa.beat.beat_track(x,
                                            sr=sr,
                                            start_bpm=90,
                                            units='time')
#アウフタクトの除去
示例#32
0
#3/4,4/4,4/4
rythms = [[0, 0.33, 0.33], [0, 0.5], [0, 0.25, 0.25, 0.25]]  #onset
measures = [4, 4, 4, 4]  #4/4
names = ["ternary", "binary", "quaternary"]
bpm = [60, 120, 240]
sr = 44100
i = 0
for ticks in rythms:
    for j in bpm:
        beat_times = []
        c = 0
        measure = measures[i] * (60 / j)
        for k in ticks:
            c += (measure * k)
            beat_times.append(c)
        click = librosa.clicks(times=beat_times[0],
                               click_freq=1000,
                               sr=sr,
                               length=round(sr * measure))
        clicks = librosa.clicks(times=beat_times[1:len(beat_times)],
                                click_freq=500,
                                sr=sr,
                                length=round(sr * measure))
        s = click + clicks
        signal = []
        for l in range(0, 100):  #number of repetitions
            signal.append(s)
        librosa.output.write_wav(
            "[" + str(j) + "bpm]" + names[i] + '_clicks.wav',
            np.array(signal).flatten(), sr)
    i += 1