コード例 #1
0
ファイル: views.py プロジェクト: jkal/sndidx
def graph_query(request, id):
    res = queries.find_one({
        'id': id
    }, {
        'segments': 1
    })

    if not res:
        raise Http404

    segments = res['segments']

    pitch_data = []
    timbre_data = []
    loudness_data = []

    for i, segment in enumerate(segments):
        x = segment['start']
        pitch = scipy.array(segment['pitches']).mean()
        timbre = scipy.array(segment['timbre']).var()
        loudness = segment['loudness_max']

        pitch_data.append({'x': x, 'y': pitch})
        timbre_data.append({'x': x, 'y': timbre})
        loudness_data.append({'x': x, 'y': loudness})

    return render(request, 'graph.html', {
        'pitch_data': json.dumps(pitch_data),
        'timbre_data': json.dumps(timbre_data),
        'loudness_data': json.dumps(loudness_data),
    })
コード例 #2
0
ファイル: views.py プロジェクト: jkal/sndidx
def results(request, code):
    k = int(request.GET.get('k', 1))

    query = queries.find_one({
        'md5': code
    })

    if not query:
        raise Http404

    segments = query['segments']

    all_song_ids = []
    q_timbre_values = []
    q_loudness_values = []
    q_pitch_values = []

    for segment in segments:
        loudness = segment['loudness_max']
        timbre = scipy.array(segment['timbre']).var()
        pitch = scipy.array(segment['pitches']).mean()

        q_timbre_values.append(timbre)
        q_loudness_values.append(loudness)
        q_pitch_values.append(pitch)

        bbox = (
            timbre, loudness, pitch,
            timbre, loudness, pitch
        )

        nearest = idx.nearest(bbox, 1, objects=True)
        node_ids = [x.object for x in nearest]
        all_song_ids.extend(node_ids)

    # Drop duplicates.
    ids = set(all_song_ids)

    # Fetch the nearest documents from Mongo.
    res = col.find({
        'song_id': {'$in': list(ids)}
    }, {
        'song_id': 1, 'title': 1, 'artist': 1, 'features': 1
    })

    # Compute distances.
    results = []
    for doc in res:
        features = doc['features']
        timbre_values = map(lambda x: x['timbre'], features)
        loudness_values = map(lambda x: x['loudness'], features)
        pitch_values = map(lambda x: x['pitch'], features)

        # Calculate distances
        timbre_dis = distance(timbre_values, q_timbre_values)
        loudness_dis = distance(loudness_values, q_loudness_values)
        pitch_dis = distance(pitch_values, q_pitch_values)

        doc['timbre_dis'] = timbre_dis
        doc['loudness_dis'] = loudness_dis
        doc['pitch_dis'] = pitch_dis
        doc['all'] = (timbre_dis * pitch_dis) + 100*loudness_dis

        results.append(doc)

    sorted_res = sorted(results, key=operator.itemgetter('all'))

    return render(request, 'results_content.html', {
        'results': sorted_res[:k],
        'query': query
    })