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
/*
* RowVisitor.cpp
* --------------
* Purpose: Class for managing which rows of a song has already been visited. Useful for detecting backwards jumps, loops, etc.
* Notes : The class keeps track of rows that have been visited by the player before.
* This way, we can tell when the module starts to loop, i.e. we can determine the song length,
* or find out that a given point of the module can never be reached.
*
* Specific implementations:
*
* Length detection code:
* As the ModPlug engine already deals with pattern loops sufficiently (though not always correctly),
* there's no problem with (infinite) pattern loops in this code.
*
* Normal player code:
* Bear in mind that rows inside pattern loops should only be evaluated once, or else the algorithm will cancel too early!
* So in that case, the pattern loop rows have to be reset when looping back.
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "Sndfile.h"
#include "RowVisitor.h"
OPENMPT_NAMESPACE_BEGIN
RowVisitor::RowVisitor(const CSoundFile &sf, SEQUENCEINDEX sequence)
: m_sndFile(sf)
, m_currentOrder(0)
, m_sequence(sequence)
{
Initialize(true);
}
RowVisitor& RowVisitor::operator=(RowVisitor &&other)
{
m_visitedRows = std::move(other.m_visitedRows);
return *this;
}
const ModSequence &RowVisitor::Order() const
{
if(m_sequence >= m_sndFile.Order.GetNumSequences())
return m_sndFile.Order();
else
return m_sndFile.Order(m_sequence);
}
// Resize / Clear the row vector.
// If reset is true, the vector is not only resized to the required dimensions, but also completely cleared (i.e. all visited rows are unset).
void RowVisitor::Initialize(bool reset)
{
auto &order = Order();
const ORDERINDEX endOrder = order.GetLengthTailTrimmed();
m_visitedRows.resize(endOrder);
if(reset)
{
m_visitOrder.clear();
// Pre-allocate maximum amount of memory most likely needed for keeping track of visited rows in a pattern
if(m_visitOrder.capacity() < MAX_PATTERN_ROWS)
{
ROWINDEX maxRows = 0;
for(const auto &pat :m_sndFile.Patterns)
{
maxRows = std::max(maxRows, pat.GetNumRows());
}
m_visitOrder.reserve(maxRows);
}
}
for(ORDERINDEX ord = 0; ord < endOrder; ord++)
{
auto &row = m_visitedRows[ord];
const size_t size = GetVisitedRowsVectorSize(order[ord]);
if(reset)
{
// If we want to reset the vectors completely, we overwrite existing items with false.
row.assign(size, false);
} else
{
row.resize(size, false);
}
}
}
// (Un)sets a given row as visited.
// order, row - which row should be (un)set
// If visited is true, the row will be set as visited.
void RowVisitor::SetVisited(ORDERINDEX ord, ROWINDEX row, bool visited)
{
auto &order = Order();
if(ord >= order.size() || row >= GetVisitedRowsVectorSize(order[ord]))
{
return;
}
// The module might have been edited in the meantime - so we have to extend this a bit.
if(ord >= m_visitedRows.size() || row >= m_visitedRows[ord].size())
{
Initialize(false);
// If it's still past the end of the vector, this means that ord >= order.GetLengthTailTrimmed(), i.e. we are trying to play an empty order.
if(ord >= m_visitedRows.size())
{
return;
}
}
m_visitedRows[ord][row] = visited;
if(visited)
{
AddVisitedRow(ord, row);
}
}
// Returns whether a given row has been visited yet.
// If autoSet is true, the queried row will automatically be marked as visited.
// Use this parameter instead of consecutive IsRowVisited / SetRowVisited calls.
bool RowVisitor::IsVisited(ORDERINDEX ord, ROWINDEX row, bool autoSet)
{
if(ord >= Order().size())
{
return false;
}
// The row slot for this row has not been assigned yet - Just return false, as this means that the program has not played the row yet.
if(ord >= m_visitedRows.size() || row >= m_visitedRows[ord].size())
{
if(autoSet)
{
SetVisited(ord, row, true);
}
return false;
}
if(m_visitedRows[ord][row])
{
// We visited this row already - this module must be looping.
return true;
} else if(autoSet)
{
m_visitedRows[ord][row] = true;
AddVisitedRow(ord, row);
}
return false;
}
// Get the needed vector size for pattern nPat.
size_t RowVisitor::GetVisitedRowsVectorSize(PATTERNINDEX pattern) const
{
if(m_sndFile.Patterns.IsValidPat(pattern))
{
return static_cast<size_t>(m_sndFile.Patterns[pattern].GetNumRows());
} else
{
// Invalid patterns consist of a "fake" row.
return 1;
}
}
// Find the first row that has not been played yet.
// The order and row is stored in the order and row variables on success, on failure they contain invalid values.
// If onlyUnplayedPatterns is true (default), only completely unplayed patterns are considered, otherwise a song can start anywhere.
// Function returns true on success.
bool RowVisitor::GetFirstUnvisitedRow(ORDERINDEX &ord, ROWINDEX &row, bool onlyUnplayedPatterns) const
{
const auto &order = Order();
const ORDERINDEX endOrder = order.GetLengthTailTrimmed();
for(ord = 0; ord < endOrder; ord++)
{
const PATTERNINDEX pattern = order[ord];
if(!m_sndFile.Patterns.IsValidPat(pattern))
{
continue;
}
if(ord >= m_visitedRows.size())
{
// Not yet initialized => unvisited
row = 0;
return true;
}
const auto &visitedRows = m_visitedRows[ord];
auto foundRow = std::find(visitedRows.begin(), visitedRows.end(), onlyUnplayedPatterns);
if(onlyUnplayedPatterns && foundRow == visitedRows.end())
{
// No row of this pattern has been played yet.
row = 0;
return true;
} else if(!onlyUnplayedPatterns)
{
// Return the first unplayed row in this pattern
if(foundRow != visitedRows.end())
{
row = static_cast<ROWINDEX>(std::distance(visitedRows.begin(), foundRow));
return true;
}
if(visitedRows.size() < m_sndFile.Patterns[pattern].GetNumRows())
{
// History is not fully initialized
row = static_cast<ROWINDEX>(visitedRows.size());
return true;
}
}
}
// Didn't find anything :(
ord = ORDERINDEX_INVALID;
row = ROWINDEX_INVALID;
return false;
}
// Set all rows of a previous pattern loop as unvisited.
void RowVisitor::ResetPatternLoop(ORDERINDEX ord, ROWINDEX startRow)
{
MPT_ASSERT(ord == m_currentOrder); // Shouldn't trigger, unless we're jumping around in the GUI during a pattern loop.
// Unvisit all rows that are in the visited row buffer, until we hit the start row for this pattern loop.
ROWINDEX row = ROWINDEX_INVALID;
for(auto iter = m_visitOrder.crbegin(); iter != m_visitOrder.crend() && row != startRow; iter++)
{
row = *iter;
Unvisit(ord, row);
}
}
// Add a row to the visited row memory for this pattern.
void RowVisitor::AddVisitedRow(ORDERINDEX ord, ROWINDEX row)
{
if(ord != m_currentOrder)
{
// We're in a new pattern! Forget about which rows we previously visited...
m_visitOrder.clear();
m_currentOrder = ord;
}
if(m_visitOrder.empty())
{
m_visitOrder.reserve(GetVisitedRowsVectorSize(Order()[ord]));
}
// And now add the played row to our memory.
m_visitOrder.push_back(row);
}
OPENMPT_NAMESPACE_END