-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlockRipper.cpp
169 lines (137 loc) · 2.92 KB
/
BlockRipper.cpp
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
#include "BlockRipper.h"
#include "TZX_Blocks.h"
#include <cmath>
using namespace std;
using namespace TZX_Blocks;
using namespace WAV_Class;
size_t BlockRipper::pos = 0;
bool BlockRipper::verboseMode = false;
bool BlockRipper::interactiveMode = true;
bool BlockRipper::predictiveMode = true;
BlockRipper::BlockRipper(WAV *wav)
{
bool newWav = (this->data != wav->data);
this->header = wav->header;
this->data = wav->data;
this->size = wav->size;
block = NULL;
if (newWav) {
pos = 0;
initializeStatesVector();
}
}
BlockRipper::BlockRipper(const BlockRipper& other)
{
this->header = other.header;
this->data = other.data;
this->size = other.size;
block = new Block(*(other.block));
}
BlockRipper::~BlockRipper()
{
this->header = NULL;
this->data = NULL;
}
void BlockRipper::setVerboseMode(bool mode)
{
verboseMode = mode;
}
void BlockRipper::setInteractiveMode(bool mode)
{
interactiveMode = mode;
}
void BlockRipper::setPredictiveMode(bool mode)
{
predictiveMode = mode;
}
void BlockRipper::initializeStatesVector()
{
DWORD lastStatePos = 0;
BYTE currState = getState(0);
BYTE newState;
DWORD samplesCnt = 0;
states.clear();
samples.clear();
for (DWORD i=0; i<size; i++) {
newState = getState(i);
if (newState != STATE_NOCHANGE) {
if (newState != currState && i-lastStatePos>1) {
//cout << WAVTIME(i) << i << " cambio " << (int)(i-lastStatePos) << endl;
samples.push_back(samplesCnt);
states.push_back(i-lastStatePos);
samplesCnt += i-lastStatePos;
lastStatePos = i;
currState = newState;
}
}
//cout << WAVTIME(i) << i << " \t " << std::dec << (unsigned int)(data[i]&0xff) << " \t " << (int)newState << " old:" << (int)currState << endl;
}
}
bool BlockRipper::detectSilence()
{
return detectSilence(pos);
}
bool BlockRipper::detectSilence(DWORD pos)
{
return (!eof(pos) && states[pos] >= THRESHOLD_SILENCE);
}
DWORD BlockRipper::skipSilence()
{
DWORD silenceCount = 0;
while (!eof() && detectSilence()) {
silenceCount += states[pos];
pos++;
}
return silenceCount;
}
DWORD BlockRipper::skipToNextSilence()
{
DWORD skipCount = 0;
while (!eof() && !detectSilence()) {
skipCount += states[pos];
pos++;
}
return skipCount;
}
Block* BlockRipper::getDetectedBlock()
{
return block;
}
DWORD BlockRipper::getPos()
{
return pos;
}
void BlockRipper::incPos()
{
if (!eof()) pos++;
}
DWORD BlockRipper::getSize()
{
return states.size();
}
bool BlockRipper::eof()
{
return pos >= states.size();
}
bool BlockRipper::eof(DWORD posIni)
{
return posIni >= states.size();
}
BYTE BlockRipper::getState(uint32_t i)
{
if (isLow(i)) return STATE_LOW;
if (isHigh(i)) return STATE_HIGH;
return STATE_NOCHANGE;
}
float BlockRipper::getTime(uint32_t i) {
if (i<0) i = pos;
return ((float)samples[std::min((DWORD)i, (DWORD)(samples.size()-1))]/WAVSampleRate);
}
bool BlockRipper::isLow(int i)
{
return data[i] <= -threshold;
}
bool BlockRipper::isHigh(int i)
{
return data[i] >= threshold;
}