-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCDpcmStorage.cpp
94 lines (79 loc) · 2.16 KB
/
CDpcmStorage.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
#include "CDpcmStorage.h"
PurrFX::CDpcmStorage::CDpcmStorage(bool i_bUseClassicAccessMode)
{
m_bUseClassicAccessMode = i_bUseClassicAccessMode;
}
PurrFX::CDpcmStorage::~CDpcmStorage()
{
for (auto* pSample: m_aSamples)
delete pSample;
}
void PurrFX::CDpcmStorage::add(CDpcmSample* i_pSample)
{
m_aSamples.push_back(i_pSample);
if (m_bUseClassicAccessMode)
{
uint16_t nKey = key(i_pSample->dpcmAddress(), i_pSample->dpcmLength());
m_mSamples[nKey] = i_pSample;
}
}
size_t PurrFX::CDpcmStorage::size() const
{
return m_aSamples.size();
}
bool PurrFX::CDpcmStorage::setCurrent(size_t i_nIndex)
{
if (i_nIndex >= m_aSamples.size())
return false;
m_nCurrentIndex = i_nIndex;
m_bIsCurrentIndexSet = true;
return true;
}
const PurrFX::CDpcmSample* PurrFX::CDpcmStorage::get(uint8_t i_nAddress, uint8_t i_nLength) const
{
if (!m_bUseClassicAccessMode)
return nullptr;
uint16_t nKey = key(i_nAddress, i_nLength);
std::map<uint16_t, CDpcmSample*>::const_iterator iSample = m_mSamples.find(nKey);
if (iSample == m_mSamples.cend())
return nullptr;
return iSample->second;
}
const PurrFX::CDpcmSample* PurrFX::CDpcmStorage::get() const
{
if (m_bIsCurrentIndexSet &&
m_nCurrentIndex < m_aSamples.size())
return m_aSamples[m_nCurrentIndex];
return nullptr;
}
const bool PurrFX::CDpcmStorage::del(size_t i_nIndex)
{
if (i_nIndex >= m_aSamples.size())
return false;
CDpcmSample* pDelSample = m_aSamples[i_nIndex];
uint16_t nDelKey = key(pDelSample->dpcmAddress(), pDelSample->dpcmLength());
delete pDelSample;
m_aSamples.erase( m_aSamples.begin()+i_nIndex );
if (m_bUseClassicAccessMode)
{
assert(m_mSamples.count(nDelKey) == 1);
m_mSamples.erase(nDelKey);
for (auto iSample = m_mSamples.cbegin(); iSample != m_mSamples.cend(); iSample++)
{
// Find replacement
CDpcmSample* pSample = iSample->second;
if (key(pSample->dpcmAddress(), pSample->dpcmLength()) == nDelKey)
{
m_mSamples[nDelKey] = pSample;
break;
}
}
}
if (m_nCurrentIndex >= m_aSamples.size())
m_bIsCurrentIndexSet = false;
return true;
}
uint16_t PurrFX::CDpcmStorage::key(uint8_t i_nAddress, uint8_t i_nLength) const
{
return i_nAddress + ( uint16_t(i_nLength) << 8 );
}