コード例 #1
0
    songfile = os.path.abspath(sys.argv[1])
    tmpfilemat = os.path.abspath(sys.argv[2])
    if tmpfilemat[-4:] != '.mat':
        tmpfilemat += '.mat'
    if os.path.isfile(tmpfilemat):
        os.remove(tmpfilemat)

    # first encode song with tzanetakis code from HackDay
    TZAN.filename_to_beatfeat_mat(songfile,savefile=tmpfilemat)
    print 'song encoded to matfile'

    # then retrieve stuff from EchoNest
    track = trackEN.upload(songfile)
    identifier = string.split(track.identifier,'/')[-1]
    print 'EN identifier =',identifier
    a,b,c,d,e = EXTRAS.get_our_analysis(identifier)
    segstart, chromas, beatstart, barstart, duration = a,b,c,d,e
    if segstart == None:
        print 'EN gave us None, must start again'
        sys.exit(0)
    analysis_dict = {'segstart':segstart,'chromas':chromas,
                     'beatstart':beatstart,'barstart':barstart,
                     'duration':duration}
    del a,b,c,d,e,segstart,chromas,beatstart,barstart,duration
    print 'analysis retrieved from Echo Nest'
    

    # features from online (positive=False to compare with old school method)
    online_feats = features.get_features(analysis_dict,pSize=8,usebars=2,
                                         keyInv=True,songKeyInv=False,
                                         positive=False,do_resample=True,
コード例 #2
0
def _thread_en(artistsdb,filename=''):
    """
    Thread that load EN data
    For artists receives a SQLlite database containing a table 'artists' with
    a field 'name'.
    Filename is used when downloading a dict from EN. If a filename is provided,
    we retrieve the uri to this file. Therefore, filename must be unique
    for each thread. If no filename is provided, we read straight from
    the stream, and we eventually seg fault.
    """
    cnt_iter = 0
    cnt_provided = 0
    waiting_artists = deque() # for db

    # MAIN LOOP
    while not _stop_en_thread:
        # debug
        cnt_iter += 1

        # get artist
        if len(waiting_artists) == 0:
            artist_list = get_artists_from_db(artistsdb)
            if artist_list == None:
                print 'ERROR,: en_thread, cant get artist from SQL database'
                time.sleep(50)
                continue
            for k in artist_list:
                waiting_artists.append(k)
        artist = waiting_artists.pop() # no thread concurrency here

        # get song
        sids, tmp_tids,tmp_titles,tmp_aids,tmp_artists = en_extras.search_tracks(artist,filename=filename)
        if sids == None or len(sids) == 0:
            continue
        songid = sids[np.random.randint(len(sids))]

        # save EchoNest data to queue
        segstart,chromas,beatstart,barstart,duration = en_extras.get_our_analysis(songid,filename=filename)
        if segstart == None:
            continue
        d = {'segstart':segstart,'chromas':chromas,
             'beatstart':beatstart,'barstart':barstart,'duration':duration}
        # put data in queue, deque is supposed to be thread safe but we have
        # an extra semaphore
        queue_size = _add_data(d)

        # queue full?        
        if queue_size >= _en_queue_size :
            time.sleep(5) # sleep for 5 seconds
            cnt_iter -= 1
            continue

        #print 'added data (artist :',artist,') to _en_queue' #debugging
        # success rate too low? print WARNING
        cnt_provided += 1
        if cnt_provided % 100 == 0:
            prob_provide = cnt_provided*100./cnt_iter
            if prob_provide < 85.:
                print 'WARNING: _en_thread, prob. of providing is low:',prob_provide,'% , artists do not actually have song?'

    # done
    print 'stopping _en_thread, prob. of providing:',cnt_provided*1./cnt_iter