-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaveRegionFile.cpp
172 lines (143 loc) · 4.75 KB
/
WaveRegionFile.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
170
171
#include "WaveRegionFile.h"
using namespace RiffFileNamespace;
WaveRegionFile::WaveRegionFile(QFile* f) : WaveFile(f){
regionList = new RegionList;
markerList = new MarkerList;
}
WaveRegionFile::~WaveRegionFile(){}
void WaveRegionFile::addRegion(myDWORD begin, myDWORD end, QString name){
regionList->append(new Region(begin, end, name));
}
void WaveRegionFile::addMark(myDWORD position){
markerList->push_back(new Mark(position));
}
// void WaveRegionFile::createChunks();
void WaveRegionFile::write(){
Chunk* riff = new Chunk("RIFF", file);
ChunkList* riffChildren = riff->getChildren();
Chunk* fmt = new Chunk("fmt ", file);
fmt->addToData(info->toBytes(), info->getBytesCount());
riffChildren->append(fmt);
Chunk* data = new Chunk("data", file);
data->addToData(waveData->data(), waveData->size());
riffChildren->append(data);
Chunk* cue = new Chunk("cue ", file);
riffChildren->append(cue);
Chunk* list = new Chunk("LIST", file);
ChunkList* listChildren = list->getChildren();
Region* r;
int freeID=1;
ChunkList lables;
foreach(r, *regionList){
myDWORD cueID = freeID++;
CuePoint cuePoint1(cueID, r->begin);
cue->addToData(cuePoint1.toBytes(cueID), cuePoint1.getBytesCount());
Chunk* ch = new Chunk("ltxt", file);
Byte* bytes = r->toLtxtChunkBytes(cueID);
ch->addToData(bytes, r->getLtxtByteCount());
listChildren->append(ch);
ch = new Chunk("labl", file);
Byte* bytes2 = r->toLablChunkBytes(cueID);
ch->addToData(bytes2, r->getLablByteCount());
lables.append(ch);
}
*(listChildren) += lables;
Mark* m;
foreach(m, *markerList){
myDWORD cueID = freeID++;
CuePoint cuePoint1(cueID, m->position); // ñîçäàåò îáúåêò cue ýëêìåíòà
cue->addToData(cuePoint1.toBytes(cueID), cuePoint1.getBytesCount());
Chunk* ch = new Chunk("labl", file);
Byte* bytes = m->toLablChunkBytes(cueID);
ch->addToData(bytes, m->getLablByteCount());
listChildren->append(ch);
}
// âñòàâèòü cue Number
riff->write();
}
RegionList* WaveRegionFile::getRegions(){
return regionList;
}
MarkerList* WaveRegionFile::getMarkers(){
return markerList;
}
bool WaveRegionFile::read(){
readChunks();
parseCueChunk();
int i=0;
Chunk* list;
do {
list = rootChunk->findChunkByName("LIST", i++);
} while (list!=NULL && !isRegionAndMarksList(list));
if (list==NULL) {
ERROR_MACRO("there is no list of regions", false);
}
ChunkList* labels = list->getChildren();
Chunk* ch1;
foreach(ch1, *labels){
if (ch1->getName()=="ltxt"){
myDWORD cueID = ch1->readDWORD();
CuePoint* cp = findCueById(cueID);
myDWORD sampleLength = ch1->readDWORD(4);
myDWORD begin = cp->sampleOffset;
Region* r = new Region(begin, begin + sampleLength, cueID);
regionList->append(r);
}
if (ch1->getName()=="labl"){
myDWORD cueID = ch1->readDWORD();
Byte* bytes;
int textSize = ch1->getDataSize();
ch1->readData(bytes, textSize);
char* textChars = new char[textSize-4];
memcpy(textChars, bytes+4, textSize-4);
QString text = QString::fromAscii(textChars);
bool isConnectedWithRegion = false;
Region* r;
foreach(r, *regionList){
if (cueID==r->cueID) {
r->name = text;
isConnectedWithRegion = true;
}
}
if (!isConnectedWithRegion){
CuePoint* cp = findCueById(cueID);
Mark* m = new Mark(cp->sampleOffset);
markerList->append(m);
}
}
}
}
void WaveRegionFile::parseCueChunk(){
cuePointList = new CuePointList;
Chunk* chunk = findChunkByName("cue ");
int size = chunk->getDataSize();
Byte* bytes = new Byte[size];
chunk->readData(bytes, size);
myDWORD cuePointsCount;
memcpy((char*)&cuePointsCount, bytes, 4);
for (int i=0; i<cuePointsCount; i++){
CuePoint* cp = CuePoint::fromBytes(bytes + i*24 + 4);
cuePointList->append(cp);
}
}
bool WaveRegionFile::isRegionAndMarksList(Chunk* ch){
if (ch->getName().toUpper()=="LIST" && ch->getType() == "adtl"){
ChunkList* chl = ch->getChildren();
Chunk* ch;
foreach(ch,*chl){
qDebug() << ch->getName();
if (!(ch->getName()=="ltxt" || ch->getName()=="labl")){
return false;
}
}
return true;
}
return false;
}
CuePoint* WaveRegionFile::findCueById(myDWORD cueID){
CuePoint* cp;
foreach(cp, *cuePointList){
if (cp->cueID==cueID) return cp;
}
return NULL;
}