Parent: [c0e4be] (diff)

Child: [bcca0f] (diff)

Download this file

randplayer.cpp    172 lines (148 with data), 5.6 kB

  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
/* Copyright (C) 2014 J.F.Dockes
* 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.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <cstdlib>
#include <QDebug>
#include <time.h>
#include "randplayer.h"
#include "HelperStructs/Helper.h"
#include "upadapt/upputils.h"
using namespace std;
static const unsigned int slicesize(30);
RandPlayer::RandPlayer(PlayMode mode,
const std::vector<UPnPClient::UPnPDirObject>& entries,
QObject *parent)
: QObject(parent), m_playmode(mode), m_entries(entries)
{
qsrand((unsigned int)time(0));
if (m_playmode == PM_TRACKS) {
std::random_shuffle(m_entries.begin(), m_entries.end());
}
}
RandPlayer::~RandPlayer()
{
emit sig_next_group_html("");
}
static bool sameValues(UPnPClient::UPnPDirObject& ref,
UPnPClient::UPnPDirObject& e)
{
return !e.f2s("upnp:album", false).compare(ref.f2s("upnp:album", false)) &&
!e.f2s("upplay:ctpath", false).compare(ref.f2s("upplay:ctpath", false));
}
// When playing by groups, if we just select random tracks and play
// the group they're in, big groups get selected more probably. So we
// random select in the group starts instead; And yes it's inefficient
// to recompute the group starts each time, we could do it once and
// then prune the list in parallel with the main list. But... you
// know... so many MIPS! We're only doing this every few minutes, and
// doing otherwise is not that simple (all posterior start indices
// need to be recomputed after we erase a group etc.)
static vector<unsigned int>
findGStarts(vector<UPnPClient::UPnPDirObject>& ents)
{
vector<unsigned int> out;
UPnPClient::UPnPDirObject ref;
for (unsigned int i = 0; i < ents.size(); i++) {
if (!sameValues(ref, ents[i])) {
out.push_back(i);
ref = ents[i];
}
}
return out;
}
void RandPlayer::selectNextGroup()
{
m_nextgroup.clear();
if (m_entries.empty())
return;
vector<unsigned int> vgstarts = findGStarts(m_entries);
// Pick a random start
double fstart = (double(qrand()) / double(RAND_MAX)) *
(vgstarts.size() - 1);
int istart = vgstarts[round(fstart)];
// Reference values
auto entref = m_entries[istart];
// Look back to beginning of section. Not needed any more now that
// we pick up group starts. Just in case we change our minds
while (istart > 0) {
istart--;
if (!sameValues(entref, m_entries[istart])) {
istart++;
break;
}
}
// Look forward to end, and store entries. Could use the next
// group index instead. Just kept the initial code where we
// selected a random track
vector<UPnPClient::UPnPDirObject>::iterator last =
m_entries.begin() + istart;
while (last != m_entries.end()) {
if (!sameValues(entref, *last)) {
break;
}
m_nextgroup.push_back(*last++);
}
// Erase used entries.
m_entries.erase(m_entries.begin() + istart, last);
}
QString RandPlayer::groupHtml(std::vector<UPnPClient::UPnPDirObject>& ents)
{
string html = "<i></i>";
for (vector<UPnPClient::UPnPDirObject>::iterator it = ents.begin();
it != ents.end(); it++) {
html += string("<b>") + escapeHtml(it->m_title) + "</b><br />";
html += escapeHtml(it->f2s("upnp:album", false)) + "<br />";
html += escapeHtml(it->f2s("upnp:artist", false)) + "<br />";
}
return QString::fromUtf8(html.c_str());
}
void RandPlayer::playNextSlice()
{
qDebug() << "RandPlayer: " << m_entries.size() << " remaining";
if (m_entries.empty() && m_nextgroup.empty()) {
emit sig_randplay_done();
return;
}
vector<UPnPClient::UPnPDirObject> ents;
if (m_playmode == PM_TRACKS) {
// The tracks are already shuffle, just take the front slicesize
vector<UPnPClient::UPnPDirObject>::iterator last =
m_entries.size() > slicesize ? m_entries.begin() + slicesize :
m_entries.end();
ents.insert(ents.begin(), m_entries.begin(), last);
m_entries.erase(m_entries.begin(), last);
} else {
if (m_nextgroup.empty()) {
// 1st time: non-empty list, and empty next-group
selectNextGroup();
if (m_nextgroup.empty()) {
// ?? This should really not happen !
emit sig_randplay_done();
return;
}
}
ents = m_nextgroup;
selectNextGroup();
emit sig_next_group_html(groupHtml(m_nextgroup));
}
qDebug() << "RandPlayer: sending " << ents.size() << " entries to pl";
MetaDataList mdl;
mdl.resize(ents.size());
for (unsigned int i = 0; i < ents.size(); i++) {
udirentToMetadata(&ents[i], &mdl[i]);
}
emit sig_tracks_to_playlist(mdl);
}