예제 #1
0
def main(filename, plot=False):
    wav_sr, wav_data = load_wavfile(filename)
    oss_sr, oss_data = onset_strength.onset_strength_signal(wav_sr, wav_data,
        #plot=True)
        plot=False)
    #print "OSS sr, len(data), seconds:\t", oss_sr, len(oss_data), len(oss_data)/oss_sr
    candidate_bpms = beat_histogram.beat_histogram(oss_sr, oss_data,
        #plot=True)
        plot=False)
    #candidate_bpms = [30, 60, 120, 180]
    #bpms = candidate_bpms
    bpm1, bpm2 = beat_phase.beat_phase(oss_sr, oss_data, candidate_bpms,
        #plot=True)
        plot=plot)

    bpm = late_heuristic.late_heuristic(bpm1, bpm2, candidate_bpms[-1][0])

    #bpm_i = summed_beat_histograms.argmax()
    #bpm = histogram_bpms[bpm_i]
    if plot:
        pylab.show()
    #print bpm
    #return bpm, candidate_bpms
    return bpm
예제 #2
0
def bpm_of_file(defs, filename, plot=False, regen=False):
    ### handle OSS
    pickle_filename = filename + "-onsets-%i.pickle" % (
        defs.OPTIONS_ONSET)
    if os.path.exists(pickle_filename) and not regen:
        pickle_file = open(pickle_filename, 'rb')
        oss_sr, oss_data = pickle.load(pickle_file)
        pickle_file.close()
    else:
        wav_sr, wav_data = load_wavfile(filename)
        oss_sr, oss_data = onset_strength.onset_strength_signal(
            defs, wav_sr, wav_data,
            #plot=False)
            plot=plot)
        pickle_file = open(pickle_filename, 'wb')
        pickle.dump( (oss_sr, oss_data), pickle_file, -1 )
        pickle_file.close()
    #print "OSS sr, len(data), seconds:\t", oss_sr, len(oss_data), len(oss_data)/oss_sr


    if defs.OPTIONS_BH < 0:
        pylab.show()
        exit(1)
    ### handle Beat Histogram
    pickle_filename = filename + "-bh-%i-%i.pickle" % (
        defs.OPTIONS_ONSET, defs.OPTIONS_BH)
    if os.path.exists(pickle_filename) and not regen:
        pickle_file = open(pickle_filename, 'rb')
        candidate_bpms = pickle.load(pickle_file)
        pickle_file.close()
    else:
        candidate_bpms = beat_histogram.beat_histogram(
            defs, oss_sr, oss_data,
            #plot=False)
            plot=plot)
        pickle_file = open(pickle_filename, 'wb')
        pickle.dump( (candidate_bpms), pickle_file, -1 )
        pickle_file.close()

    #return candidate_bpms, [candidate_bpms]

    if defs.OPTIONS_BP < 0:
        cands = numpy.zeros(4*defs.BPM_MAX)
        for i in range(len(candidate_bpms)):
            for j in range(len(candidate_bpms[i])):
                bpm = candidate_bpms[i][j]
                cands[bpm] += 9-j
        if plot:
            pylab.figure()
            pylab.plot(cands)
            pylab.title("combo BPM cands")
            pylab.show()
        bestbpm = 4*cands.argmax()
        fewcands = []
        for i in range(4):
            bpm = cands.argmax()
            cands[bpm] = 0.0
            fewcands.append(4*bpm)
        return bestbpm, fewcands
    ### handle Beat Phase
    pickle_filename = filename + "-bp-%i-%i-%i.pickle" % (
        defs.OPTIONS_ONSET, defs.OPTIONS_BH, defs.OPTIONS_BP)
    if os.path.exists(pickle_filename) and not regen:
        pickle_file = open(pickle_filename, 'rb')
        (bpm, bp) = pickle.load(pickle_file)
        pickle_file.close()
    else:
        bpm, bp = beat_phase.beat_phase(defs, oss_sr, oss_data, candidate_bpms,
            plot=plot)
        pickle_file = open(pickle_filename, 'wb')
        pickle.dump( (bpm, bp), pickle_file, -1 )
        pickle_file.close()


    bpm = late_heuristic.late_heuristic(defs, bpm, bp)

    if plot:
        pylab.show()
    #print bpm
    #return bpm, candidate_bpms
    return bpm, candidate_bpms[-1]
예제 #3
0
def bpm_of_file(defs, filename, plot=False, regen=False):
    defs.basename = os.path.splitext(os.path.basename(filename))[0]
    ### handle OSS
    pickle_filename = filename + "-onsets-%i.pickle" % (defs.OPTIONS_ONSET)
    if os.path.exists(pickle_filename) and not regen:
        pickle_file = open(pickle_filename, 'rb')
        oss_sr, oss_data = pickle.load(pickle_file)
        pickle_file.close()
    else:
        wav_sr, wav_data = load_wavfile(filename)
        oss_sr, oss_data = onset_strength.onset_strength_signal(
            defs,
            wav_sr,
            wav_data,
            #plot=False)
            plot=plot)
        pickle_file = open(pickle_filename, 'wb')
        pickle.dump((oss_sr, oss_data), pickle_file, -1)
        pickle_file.close()
    #print "OSS sr, len(data), seconds:\t", oss_sr, len(oss_data), len(oss_data)/oss_sr

    if defs.OPTIONS_BH < 0:
        pylab.show()
        exit(1)
    ### handle Beat Histogram
    pickle_filename = filename + "-bh-%i-%i.pickle" % (defs.OPTIONS_ONSET,
                                                       defs.OPTIONS_BH)
    if os.path.exists(pickle_filename) and not regen:
        pickle_file = open(pickle_filename, 'rb')
        candidate_bpms = pickle.load(pickle_file)
        pickle_file.close()
    else:
        candidate_bpms = beat_histogram.beat_histogram(
            defs,
            oss_sr,
            oss_data,
            #plot=False)
            plot=plot)
        pickle_file = open(pickle_filename, 'wb')
        pickle.dump((candidate_bpms), pickle_file, -1)
        pickle_file.close()

    #return candidate_bpms, [candidate_bpms]

    if defs.OPTIONS_BP < 0:
        cands = numpy.zeros(4 * defs.BPM_MAX)
        for i in range(len(candidate_bpms)):
            for j in range(len(candidate_bpms[i])):
                bpm = candidate_bpms[i][j]
                cands[bpm] += 9 - j
        if plot:
            pylab.figure()
            pylab.plot(cands)
            pylab.title("combo BPM cands")
            pylab.show()
        bestbpm = 4 * cands.argmax()
        fewcands = []
        for i in range(4):
            bpm = cands.argmax()
            cands[bpm] = 0.0
            fewcands.append(4 * bpm)
        return bestbpm, fewcands
    ### handle Beat Phase
    pickle_filename = filename + "-bp-%i-%i-%i.pickle" % (
        defs.OPTIONS_ONSET, defs.OPTIONS_BH, defs.OPTIONS_BP)
    if False:
        #if os.path.exists(pickle_filename) and not regen:
        pickle_file = open(pickle_filename, 'rb')
        (bpm, bp) = pickle.load(pickle_file)
        pickle_file.close()
    else:
        bpm, bp = beat_phase.beat_phase(defs,
                                        oss_sr,
                                        oss_data,
                                        candidate_bpms,
                                        plot=plot)
        pickle_file = open(pickle_filename, 'wb')
        pickle.dump((bpm, bp), pickle_file, -1)
        pickle_file.close()

    bpm = late_heuristic.late_heuristic(defs, bpm, bp)

    if plot:
        pylab.show()
    #print bpm
    #return bpm, candidate_bpms
    return bpm, candidate_bpms[-1]