-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFUF.cpp
132 lines (106 loc) · 3.06 KB
/
FUF.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
#include "FUF.h"
using namespace std;
FUF::~FUF() {
if (sample) delete sample;
if (compressedData) delete compressedData;
}
void FUF::readFromFile(const char* filename, fileExtension ext) {
if (ext == EXTENSION_WAV) {
sample = new WaveReader(filename);
for (int i = 0; i < COMPRESSION_MODE_COUNT; i++) {
compression[i] = NONE;
}
lastCompression = 0;
sample->readWav();
compressedData = new FormatoUltraFodaData(&(sample->data));
}
else if (ext == EXTENSION_FUF) {
compressedData = new FormatoUltraFodaData(filename);
lastCompression = compressedData->fHeader.compressCount;
for (int i = 0; i < lastCompression; i++) {
compression[i] = compressedData->fHeader.modes[i];
}
}
}
void FUF::writeToFile(const char* filename, fileExtension ext) {
if (ext == EXTENSION_WAV) {
WaveData waveData;
waveData.channelCount = compressedData->fHeader.channelCount;
waveData.data = new int*[waveData.channelCount];
for (int i = 0; i< waveData.channelCount; i++) {
waveData.data[i] = compressedData->getData(i);
}
waveData.dataLength = compressedData->getDataLength();
WaveWriter output(compressedData->fHeader.info, waveData);
string fname = string(filename) + ".wav";
cout << "Writing file '" << fname << "'..." << endl;
output.writeWav(fname.c_str());
}
else if (ext == EXTENSION_FUF) {
// Open the file
string fname = string(filename) + ".fuf";
FILE* file = fopen(fname.c_str(), "wb");
cout << "Writing file '" << fname << "'..." << endl;
// Write the headers
compressedData->writeHeaders(file, sample->info, compression, lastCompression);
// Write the data
compressedData->writeData(file);
fclose(file);
}
}
void FUF::compress(compressMode a) {
for (int i = 0; i < lastCompression; i++) {
if (compression[i] == a) {
cout << "This compression was already applied. YOCO, bro!" << endl;
return;
}
}
switch (a) {
case HUFFMAN:
cout << "Applying Huffman..." << endl;
huffmanCompress();
break;
case DIFFERENCE:
cout << "Applying Difference..." << endl;
differencialCompress();
break;
case TRANSFORM:
cout << "Applying Transform..." << endl;
transformCompress();
break;
case NONE:
return;
}
compression[lastCompression] = a;
lastCompression++;
}
void FUF::compress(compressMode a, compressMode b){
compress(a);
compress(b);
}
void FUF::compress(compressMode a, compressMode b, compressMode c){
compress(a);
compress(b);
compress(c);
}
void FUF::decompress(){
while (lastCompression > 0) {
lastCompression--;
switch (compression[lastCompression]){
case HUFFMAN:
cout << "Decompressing Huffman..." << endl;
huffmanDecompress();
break;
case DIFFERENCE:
cout << "Decompressing Difference..." << endl;
differencialDecompress();
break;
case TRANSFORM:
cout << "Decompressing Transform..." << endl;
transformDecompress();
break;
case NONE:
break;
}
}
}