File: picker.cc

package info (click to toggle)
imms 3.1.0~rc8-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,228 kB
  • ctags: 1,243
  • sloc: cpp: 7,839; ansic: 1,845; xml: 252; sh: 189; makefile: 140
file content (222 lines) | stat: -rw-r--r-- 5,413 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 IMMS: Intelligent Multimedia Management System
 Copyright (C) 2001-2009 Michael Grigoriev

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#include <iostream>
#include <algorithm>
#include <map>

#include <math.h>

#include "picker.h"
#include "strmanip.h"
#include "immsutil.h"

#define     SAMPLE_SIZE             100
#define     MIN_SAMPLE_SIZE         35
#define     MAX_ATTEMPTS            (SAMPLE_SIZE*2)

#define     EXP                     1.10
#define     _TICKETS(x)             pow(EXP, x) * 99.0 / pow(EXP, 100)
#define     TICKETS(x)              ROUND(_TICKETS(x) + 1)

using std::endl;
using std::cerr;
using std::map;

SongPicker::SongPicker()
    : current(0, "current"), acquired(0), winner(0, "winner")
{
    reschedule_requested = playlist_known = 0;
    reset();
}

void SongPicker::reset()
{
    candidates.clear();
    metacandidates.clear();
    acquired = attempts = 0;
    selection_ready = false;
    if (reschedule_requested)
        --reschedule_requested;
}

void SongPicker::playlist_changed(int length)
{
    playlist_known = 0;
    reset();
}

bool SongPicker::add_candidate(bool urgent)
{
    int want = urgent ? MIN_SAMPLE_SIZE : SAMPLE_SIZE;
    if (candidates.empty() && metacandidates.empty())
        get_metacandidates(want);

    if (attempts > MAX_ATTEMPTS)
        return false;
    ++attempts;

    if (acquired >= want || metacandidates.empty())
        return false;

    int position = metacandidates.back();
    metacandidates.pop_back();

    string path = ImmsDb::get_item_from_playlist(position);

    request_playlist_item(position);

    if (path == "")
        return false;

    SongData data(position, path);

    if (find(candidates.begin(), candidates.end(), data)
            != candidates.end())
        return true;

    if (fetch_song_info(data))
    {
        ++acquired;
        candidates.push_back(data);
        if (urgent && data.effective_rating > 80)
            attempts = MAX_ATTEMPTS + 1;
    }

    return true;
}

bool SongPicker::do_events()
{
    if (!playlist_known || !pl_length)
        return true;

    for (int i = 0; i < 5 && !selection_ready ; ++i)
        if (!add_candidate())
        {
            selection_ready = true;
            if (reschedule_requested)
            {
                reschedule_requested = 0;
                reset_selection();
            }
        }

    if (playlist_known == 2)
        return false;

    int pos = ImmsDb::get_unknown_playlist_item();
    if (pos < 0)
    {
        playlist_known = 2;
        return false;
    }
    identify_playlist_item(pos);
    return true;
}

void SongPicker::revalidate_current(int pos, const string &path)
{
    if (winner.position == pos && winner.get_path() == path)
    {
        current = winner;
    }
    else if (current.get_path() != path || current.position != pos)
    {
        current = SongData(pos, path);
        fetch_song_info(current);
    }
}

int SongPicker::select_next()
{
    if (PlaylistDb::get_real_playlist_length() < pl_length)
        return -1;

    if (add_candidate())
        request_reschedule();
    if (candidates.size() < MIN_SAMPLE_SIZE)
        while (add_candidate(true));

    if (candidates.empty())
    {
        LOG(ERROR) << "warning: no candidates!" << endl;
        return 0;
    }

    typedef map<int, vector<SongData *> > Ratings;
    Ratings ratings;

    Candidates::iterator i;
    float max_last_played = 0;

    for (i = candidates.begin(); i != candidates.end(); ++i)
        if (i->last_played > max_last_played)
            max_last_played = i->last_played;

    for (i = candidates.begin(); i != candidates.end(); ++i)
    {
        i->effective_rating += i->relation + i->acoustic;
        i->effective_rating = ROUND(i->effective_rating
                * i->last_played / max_last_played);
        int tickets = TICKETS(i->effective_rating);
        ratings[tickets].push_back(&*i);
    }

    unsigned total = 0;
    for (Ratings::iterator i = ratings.begin(); i != ratings.end(); ++i)
        total += i->first;

    int winning_ticket = imms_random(total);

#ifdef DEBUG
    cerr << string(80, '-') << endl;
    cerr << " >>> ";
#endif
    for (Ratings::iterator i = ratings.begin(); i != ratings.end(); ++i)
    {
#ifdef DEBUG
        cerr << i->first << ":" << i->second.size();
#endif
        winning_ticket -= i->first;
        if (winning_ticket < 0)
        {
#ifdef DEBUG
            cerr << "* ";
#endif
            winner = *i->second[imms_random(i->second.size())];
#ifndef DEBUG
            break;
        }
#else
            winning_ticket = INT_MAX;
        }
        else
            cerr << " ";
#endif
    }
#ifdef DEBUG
    cerr << endl;
#endif

    reset();

    next_sid = winner.get_sid();

    return winner.position;
}