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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
#include <time.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include "imms.h"
#include "strmanip.h"
#include "utils.h"
using std::string;
using std::endl;
using std::cerr;
using std::setprecision;
using std::ofstream;
//////////////////////////////////////////////
// Constants
#define FIRST_SKIP_RATE -6
#define CONS_SKIP_RATE -4
#define FIRST_NON_SKIP_RATE 5
#define CONS_NON_SKIP_RATE 1
#define JUMPED_TO_FINNISHED 7
#define NO_XIDLE_BONUS 1
#define JUMPED_FROM -1
#define JUMPED_TO_SKIPPED 1
#define INTERACTIVE_BONUS 2
#define MAX_TIME 21*DAY
#define MAX_CORRELATION 12.0
#define SECONDARY 0.3
#define CORRELATION_IMPACT 40
#define BPM_IMPACT 25
#define SPECTRUM_IMPACT 10
#define SPECTRUM_RADIUS 3.75
#define BPM_RADIUS 2.5
#define LAST_EXPIRE HOUR
#define TERM_WIDTH 80
//////////////////////////////////////////////
// Imms
Imms::Imms(IMMSServer *server) : server(server)
{
last_skipped = last_jumped = false;
local_max = MAX_TIME;
handpicked.set_on = 0;
last.sid = handpicked.sid = -1;
fout.open(get_imms_root().append("imms.log").c_str(),
ofstream::out | ofstream::app);
time_t t = time(0);
fout << endl << endl << ctime(&t) << setprecision(3);
}
Imms::~Imms()
{
clear_recent();
}
void Imms::setup(bool use_xidle)
{
xidle_enabled = use_xidle;
}
void Imms::get_metacandidates()
{
StackTimer t;
AutoTransaction a;
if (last.sid != -1)
CorrelationDb::get_related(metacandidates, last.sid, 20);
if (handpicked.sid != -1)
CorrelationDb::get_related(metacandidates, handpicked.sid, 30);
}
void Imms::do_events()
{
if (!SongPicker::do_events())
CorrelationDb::expire_recent();
XIdle::query();
}
void Imms::request_playlist_item(int index)
{
return server->request_playlist_item(index);
}
void Imms::playlist_changed(int length)
{
pl_length = length;
local_max = pl_length * 8 * 60;
if (local_max > MAX_TIME)
local_max = MAX_TIME;
ImmsDb::clear_recent();
PlaylistDb::playlist_clear();
SongPicker::playlist_changed(length);
}
void Imms::reset_selection()
{
SongPicker::reset();
server->reset_selection();
local_max = ImmsDb::get_effective_playlist_length() * 8 * 60;
local_max = MAX_TIME;
}
void Imms::start_song(int position, string path)
{
XIdle::reset();
path = path_normalize(path);
revalidate_current(position, path);
try {
AutoTransaction at;
current.set_last(time(0));
print_song_info();
if (last_jumped)
set_lastinfo(handpicked);
StringPair acoustic = current.get_acoustic();
at.commit();
if (acoustic.first == "") {
string epath = rex.replace(path, "'", "'\"'\"'", Regexx::global);
system(string("analyzer '" + epath + "' &").c_str());
}
}
WARNIFFAILED();
}
void Imms::print_song_info()
{
fout << string(TERM_WIDTH - 15, '-') << endl << "[";
const string &path = current.get_path();
if (path.length() > TERM_WIDTH - 2)
fout << "..." << path.substr(path.length() - TERM_WIDTH + 5);
else
fout << path;
fout << "]\n [Rating: " << current.rating;
fout << setiosflags(std::ios::showpos);
if (current.relation)
fout << current.relation << "r";
if (current.newness)
fout << current.newness << "n";
if (current.bpmrating)
fout << current.bpmrating << "b";
if (current.specrating)
fout << current.specrating << "s";
fout << resetiosflags(std::ios::showpos);
fout << "] [Last: " << strtime(current.last_played) <<
(current.last_played == local_max ? "!" : "");
if (current.trend_scale != 1)
fout << "*" << current.trend_scale;
fout << "] ";
fout << (!current.identified ? "[Unknown] " : "");
fout << (!current.get_playcounter() ? "[New] " : "");
fout.flush();
}
void Imms::set_lastinfo(LastInfo &last)
{
last.set_on = time(0);
last.sid = current.get_sid();
last.acoustic = current.get_acoustic();
}
void Imms::end_song(bool at_the_end, bool jumped, bool bad)
{
int mod;
if (at_the_end)
{
if (last_jumped)
mod = JUMPED_TO_FINNISHED;
else if (last_skipped)
mod = FIRST_NON_SKIP_RATE;
else
mod = CONS_NON_SKIP_RATE;
if (!xidle_enabled)
mod += NO_XIDLE_BONUS;
else if (XIdle::is_active())
mod += INTERACTIVE_BONUS;
}
else
{
if (last_jumped && !jumped)
mod = JUMPED_TO_SKIPPED;
else if (jumped)
mod = JUMPED_FROM;
else if (last_skipped)
mod = CONS_SKIP_RATE;
else
mod = FIRST_SKIP_RATE;
if (mod < JUMPED_FROM)
{
if (!xidle_enabled)
mod -= NO_XIDLE_BONUS;
else if (XIdle::is_active())
mod -= INTERACTIVE_BONUS;
}
}
last_skipped = !at_the_end;
if (bad)
mod = 0;
#ifdef DEBUG
cerr << " *** " << path_get_filename(current.get_path()) << endl;
#endif
if (mod >= CONS_NON_SKIP_RATE)
set_lastinfo(last);
if (mod > CONS_NON_SKIP_RATE + INTERACTIVE_BONUS)
set_lastinfo(handpicked);
fout << (jumped ? "[Jumped] " : "");
fout << (!jumped && last_skipped ? "[Skipped] " : "");
fout << "[Delta " << setiosflags(std::ios::showpos) << mod <<
resetiosflags(std::ios::showpos) << "] ";
fout << endl;
last_jumped = jumped;
int new_rating = std::min(
std::max(current.rating + mod, MIN_RATING), MAX_RATING);
int trend = current.get_trend();
if (abs(mod) > CONS_NON_SKIP_RATE + INTERACTIVE_BONUS)
{
if ((trend >= 0 && mod > 0) || (trend <= 0 && mod < 0))
{
trend += mod;
if (abs(trend) > MAX_TREND)
trend = trend > 0 ? MAX_TREND : - MAX_TREND;
}
else
trend = mod;
}
else
{
if (abs(trend) < 3)
trend = 0;
else
trend += (trend > 0 ? -3 : 3);
}
AutoTransaction at;
ImmsDb::add_recent(current.get_uid(), mod);
current.set_trend(trend);
current.set_last(time(0));
current.set_rating(new_rating);
current.increment_playcounter();
at.commit();
}
void Imms::evaluate_transition(SongData &data, LastInfo &last, float weight)
{
// Reset lasts if we had them for too long
if (last.sid != -1 && last.set_on + LAST_EXPIRE < time(0))
last.sid = -1;
if (last.sid == -1)
return;
float rel = ImmsDb::correlate(data.get_sid(), last.sid) / MAX_CORRELATION;
rel = std::max(std::min(rel, (float)1), (float)-1);
data.relation += ROUND(rel * weight * CORRELATION_IMPACT);
StringPair acoustic = data.get_acoustic();
if (last.acoustic.first != "" && acoustic.first != "")
{
float d = rms_string_distance(last.acoustic.first, acoustic.first, 15);
float rel = (SPECTRUM_RADIUS - d) / SPECTRUM_RADIUS;
if (rel < 0)
rel *= 2;
data.specrating += ROUND(rel * weight * SPECTRUM_IMPACT);
}
if (last.acoustic.second != "" && acoustic.second != "")
{
float d = rms_string_distance(
rescale_bpmgraph(last.acoustic.second),
rescale_bpmgraph(acoustic.second));
float rel = (BPM_RADIUS - d) / BPM_RADIUS;
if (rel < 0)
rel *= 2;
data.bpmrating += ROUND(rel * weight * BPM_IMPACT);
}
}
bool Imms::fetch_song_info(SongData &data)
{
if (!InfoFetcher::fetch_song_info(data))
return false;
if (data.last_played > local_max)
data.last_played = local_max;
data.specrating = data.bpmrating = data.relation = 0;
evaluate_transition(data, handpicked, 1);
evaluate_transition(data, last,
SECONDARY * (handpicked.sid == -1 ? 2 : 1));
return true;
}
|