Ejemplo n.º 1
0
def beatract(dir_name, file_name=-1, save_dir=-1):
    '''
    at given dir_name/file_name extract beat and save it to txt file at save to.
    Args:
    Return:
    Raise:
        nothing.
    '''

    dest_file = to_wav(dir_name, dir_name, file_name)
    # if want to extract some given length, give load to duration value.
    audio_list, sampling_rate = lb.load(dest_file, offset=0.0)
    print "file opend..."
    music = lb.cqt(audio_list, sr=sampling_rate, fmin=lb.note_to_hz('C1'), n_bins=240, \
    bins_per_octave=12*4)
    print "file CQT finished..."
    threshold = get_threshold(music)
    _, r_harmonic = parse_noise(music, threshold)
    print "file CQT harmonics extracted..."
    note = stage_note(r_harmonic)

    _, note_list, icoef_table, _ = bt2.tie_note(note, 8)
    weights = bt2.weightract(r_harmonic, note, note_list, icoef_table)
    save_to(save_dir, file_name.split(".")[0] + ".txt", weights)
    print "finished extract file..."
    '''
Ejemplo n.º 2
0
def beatract(dir_name, file_name=-1, save_dir=-1, addable_option="-n", \
specific=4, threshold_length=8, show_graph=-1):
    '''
    at given dir_name/file_name extract beat and save it to txt file at save to.
    Args:
    Return:
    Raise:
        nothing.
    '''
    # if file_name is default value, check all file in directory.
    if file_name == -1:
        file_names = os.listdir(dir_name)
    else:
        file_names = [file_name]

    # if save_dir is default value, save_dir is in source directory.
    if save_dir == -1:
        save_dir = dir_name

    # now is now beat extracting number.
    now = 0
    for file_name in file_names:
        now += 1
        print "Strat extracting " + file_name + "... Now " + str(
            now) + " / " + str(len(file_names))

        dest_file = to_wav(dir_name, dir_name, file_name, addable_option)
        # if want to extract some given length, give load to duration value.
        audio_list, sampling_rate = lb.load(dest_file, offset=0.0)
        print "file opend..."
        music = lb.cqt(audio_list, sr=sampling_rate, fmin=lb.note_to_hz('C1'), n_bins=60*specific, \
        bins_per_octave=12*specific)
        print "file CQT finished..."
        threshold = get_threshold(music)
        _, r_harmonic = parse_noise(music, threshold)
        print "file CQT harmonics extracted..."
        note = stage_note(r_harmonic)

        _, note_list, icoef_table, _ = bt2.tie_note(note, threshold_length)
        weights = bt2.weightract(r_harmonic, note, note_list, icoef_table)
        save_to(save_dir, file_name.split(".")[0] + ".txt", weights)
        print "finished extract file..."

        if show_graph != -1:
            plt.figure()
            plt.plot(weights)
            plt.show()
Ejemplo n.º 3
0
def beatract(dir_name, file_name=-1, save_dir=-1, addable_option="-n", \
specific=4, threshold_length=8, show_graph=-1, save_graph=-1, debugmode=-1, \
time_variation=0.5):
    '''
    at given dir_name/file_name extract beat and save it to txt file at save to.
    Args:
    Return:
    Raise:
        nothing.
    '''
    # if file_name is default value, check all file in directory.
    if file_name == -1:
        file_names = os.listdir(dir_name)
    else:
        file_names = [file_name]

    # if save_dir is default value, save_dir is in source directory.
    if save_dir == -1:
        save_dir = dir_name

    # now is now beat extracting number.
    now = 0
    for file_name in file_names:
        now += 1
        if debugmode != -1:
            # if debugmode on, write debugging message to console.
            print "Strat extracting " + file_name + "... Now " + str(now) + " / "+  \
            str(len(file_names))

        dest_file = to_wav(dir_name, dir_name, file_name, addable_option)
        # if want to extract some given length, give load to duration value.
        audio_list, sampling_rate = lb.load(dest_file, offset=0.0)
        if debugmode != -1:
            # if debugmode on, write debugging message to console.
            print "file opend..." + "... Now " + str(now) + " / "+  str(len(file_names))

        music = lb.cqt(audio_list, sr=sampling_rate, fmin=lb.note_to_hz('C1'), n_bins=60*specific, \
        bins_per_octave=12*specific)

        if debugmode != -1:
            # if debugmode on, write debugging message to console.
            print "file CQT finished..." + "... Now " + str(now) + " / "+  str(len(file_names))

        threshold = get_threshold(music)
        _, r_harmonic = parse_noise(music, threshold)

        if debugmode != -1:
            # if debugmode on, write debugging message to console.
            print "file CQT harmonics extracted..." + "... Now " + str(now) + " / " + \
            str(len(file_names))

        note = stage_note(r_harmonic)

        _, note_list, icoef_table, _ = bt2.tie_note(note, threshold_length, debug_mode=1)
        weights = bt2.weightract(r_harmonic, note, note_list, icoef_table)

        # Set Time variation for input values.
        real_weights = bt2.set_time_variation(weights, \
        get_music_time(sampling_rate, len(audio_list)), sampling_rate, \
        time_variation=time_variation)

        save_to(save_dir, file_name.split(".")[0] + ".txt", real_weights)

        if debugmode != -1:
            print "finished extract file..." + "... Now " + str(now) + " / "+  str(len(file_names))

        if show_graph != -1:
            # if show graph is on...
            plt.figure()
            plt.plot(real_weights)
            plt.show()

        if save_graph != -1:
            # if save graph is on...
            plt.figure()
            plt.plot(real_weights)
            plt.savefig(str(dir_name)+"/"+str(file_name.split(".")[0] + ".png"))
Ejemplo n.º 4
0
def beatract(dir_name, file_name=-1, save_dir=-1, addable_option="-n", \
specific=4, threshold_length=8, show_graph=-1, save_graph=-1, debugmode=-1, \
time_variation=0.5, time_warping=60, inner_debug=-1):
    '''
    at given dir_name/file_name extract beat and save it to txt file at save to.
    Args:
    Return:
    Raise:
        nothing.
    '''
    # if file_name is default value, check all file in directory.
    if file_name == -1:
        file_names = os.listdir(dir_name)
    else:
        file_names = [file_name]

    # if save_dir is default value, save_dir is in source directory.
    if save_dir == -1:
        save_dir = dir_name

    # now is now beat extracting number.
    now = 0
    for file_name in file_names:
        now += 1
        if debugmode != -1:
            # if debugmode on, write debugging message to console.
            print("Strat extracting " + file_name + "... Now " + str(now) + " / "+  \
            str(len(file_names)))
        # y, sr for calculate seconds.
        y, sr = lb.load(dir_name + '/' + file_name)
        second = len(y) / sr

        time = 0
        real_output = []

        while second > 0:
            if second > time_warping:
                #Start from times * 60 and with 60 seconds.
                addable_option = addable_option + " -ss " + str(time_warping * time) \
                + " -t " + str(time_warping)

                #to Wav file with same name, so we can get just one file.
                dest_file = to_wav(dir_name, dir_name, file_name,
                                   addable_option)
                time += 1
                second -= time_warping
            else:
                #Start from times * 60 and with remain seconds.
                addable_option = addable_option + " -ss " + str(time_warping * time) \
                + " -t " + str(second)

                #to Wav file with same name, so we can get just one file.
                dest_file = to_wav(dir_name, dir_name, file_name,
                                   addable_option)
                second = 0
                time += 1

            # if want to extract some given length, give load to duration value.
            audio_list, sampling_rate = lb.load(dest_file, offset=0.0)
            if debugmode != -1:
                # if debugmode on, write debugging message to console.
                print("file opend..." + "... Now " + str(now) + " / " +  str(len(file_names))\
                + " " + file_name)

            music = lb.cqt(audio_list, sr=sampling_rate, fmin=lb.note_to_hz('C1'), \
            n_bins=60*specific, bins_per_octave=12*specific)

            if debugmode != -1:
                # if debugmode on, write debugging message to console.
                print("file CQT finished..." + "... Now " + str(now) + " / " + \
                str(len(file_names))\
                + " " + file_name)

            threshold = get_threshold(music)
            _, r_harmonic = parse_noise(music, threshold)

            if debugmode != -1:
                # if debugmode on, write debugging message to console.
                print("file CQT harmonics extracted..." + "... Now " + str(now) + " / " + \
                str(len(file_names))\
                + " " + file_name)

            note = stage_note(r_harmonic)

            if debugmode != -1:
                # if debugmode on, write debugging message to console.
                print("file tie_note..." + "... Now " + str(now) + " / " + \
                str(len(file_names))\
                + " " + file_name)

            _, note_list, icoef_table, _ = bt2.tie_note(note, threshold_length, \
            debug_mode=inner_debug)

            if debugmode != -1:
                # if debugmode on, write debugging message to console.
                print("file weightract..." + "... Now " + str(now) + " / " + \
                str(len(file_names))\
                + " " + file_name)

            weights = bt2.weightract(r_harmonic, note, note_list, icoef_table, \
            debug_mode=inner_debug)

            # Set Time variation for input values.
            real_weights = bt2.set_time_variation(weights, \
            get_music_time(sampling_rate, len(audio_list)), sampling_rate, \
            time_variation=time_variation)

            real_output += real_weights

        save_to(save_dir, file_name.split(".")[0] + ".txt", real_output)

        if debugmode != -1:
            print("finished extract file..." + "... Now " + str(now) + " / "+  str(len(file_names))\
            + " " + file_name)

        if show_graph != -1:
            # if show graph is on...
            plt.figure()
            plt.plot(real_output)
            plt.show()

        if save_graph != -1:
            # if save graph is on...
            plt.figure()
            plt.plot(real_output)
            plt.savefig(
                str(dir_name) + "/" + str(file_name.split(".")[0] + ".png"))