-
Notifications
You must be signed in to change notification settings - Fork 3
/
Demo.cpp
177 lines (146 loc) · 4.26 KB
/
Demo.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
172
173
174
175
176
177
#include "FastSinusoids.hpp"
// Required by `Pulsejet::Encode` and `Pulsejet::Decode`
namespace Pulsejet::Shims
{
inline float CosF(float x)
{
return FastSinusoids::CosF(x);
}
inline float Exp2f(float x)
{
return exp2f(x);
}
inline float SinF(float x)
{
return FastSinusoids::SinF(x);
}
inline float SqrtF(float x)
{
return sqrtf(x);
}
}
#include <Pulsejet/Pulsejet.hpp>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
static void PrintUsage(const char **argv)
{
cout << "Usage:\n";
cout << " encode: " << argv[0] << " -e <target bit rate in kbps> <input.raw> <output.pulsejet>\n";
cout << " decode: " << argv[0] << " -d <input.pulsejet> <output.raw>\n";
}
static void ErrorInvalidArgs(const char **argv)
{
cout << "ERROR: Invalid args\n\n";
PrintUsage(argv);
}
static vector<uint8_t> ReadFile(const char *fileName)
{
ifstream inputFile(fileName, ios::binary | ios::ate);
const auto inputFileSize = inputFile.tellg();
inputFile.seekg(0, ios::beg);
vector<uint8_t> ret(inputFileSize);
inputFile.read(reinterpret_cast<char *>(ret.data()), inputFileSize);
return ret;
}
static vector<float> ReadFileAsFloats(const char *fileName)
{
ifstream inputFile(fileName, ios::binary | ios::ate);
const auto inputFileSize = inputFile.tellg();
if (inputFileSize % sizeof(float))
{
cout << "ERROR: Input size is not aligned to float size\n\n";
return 1;
}
inputFile.seekg(0, ios::beg);
vector<float> ret(inputFileSize);
inputFile.read(reinterpret_cast<char *>(ret.data()), inputFileSize);
return ret;
}
int main(int argc, const char **argv)
{
if (argc < 4)
{
ErrorInvalidArgs(argv);
return 1;
}
cout << "library version: " << Pulsejet::LibraryVersionString() << "\n";
cout << "codec version: " << Pulsejet::CodecVersionString() << "\n";
FastSinusoids::Init();
if (!strcmp(argv[1], "-e"))
{
if (argc != 5)
{
ErrorInvalidArgs(argv);
return 1;
}
const double targetBitRate = stod(argv[2]);
const auto inputFileName = argv[3];
const auto outputFileName = argv[4];
cout << "reading ... " << flush;
const auto input = ReadFileAsFloats(inputFileName);
cout << "ok\n";
cout << "encoding ... " << flush;
const uint32_t numSamples = input.size();
const double sampleRate = 44100.0;
double totalBitsEstimate;
const auto encodedSample = Pulsejet::Encode(input.data(), numSamples, sampleRate, targetBitRate, totalBitsEstimate);
const auto bitRateEstimate = totalBitsEstimate / 1000.0 / (static_cast<double>(numSamples) / sampleRate);
cout << "ok, compressed size estimate: " << static_cast<uint32_t>(ceil(totalBitsEstimate / 8.0)) << " byte(s) (~" << setprecision(4) << bitRateEstimate << "kbps)\n";
cout << "writing ... " << flush;
ofstream outputFile(outputFileName, ios::binary);
outputFile.write(reinterpret_cast<const char *>(encodedSample.data()), encodedSample.size());
cout << "ok\n";
cout << "encoding successful!\n";
}
else if (!strcmp(argv[1], "-d"))
{
if (argc != 4)
{
ErrorInvalidArgs(argv);
return 1;
}
const auto inputFileName = argv[2];
const auto outputFileName = argv[3];
cout << "reading ... " << flush;
const auto input = ReadFile(inputFileName);
cout << "ok\n";
cout << "sample check ... " << flush;
if (!Pulsejet::CheckSample(input.data()))
{
cout << "ERROR: Input is not a pulsejet sample\n\n";
return 1;
}
cout << "ok\n";
cout << "sample version: " << Pulsejet::SampleVersionString(input.data()) << "\n";
cout << "sample version check ... " << flush;
if (!Pulsejet::CheckSampleVersion(input.data()))
{
cout << "ERROR: Incompatible codec and sample versions\n\n";
return 1;
}
cout << "ok\n";
cout << "decoding ... " << flush;
uint32_t numDecodedSamples;
const auto decodedSample = Pulsejet::Decode(input.data(), &numDecodedSamples);
cout << "ok, " << numDecodedSamples << " samples\n";
cout << "writing ... " << flush;
ofstream outputFile(outputFileName, ios::binary);
outputFile.write(reinterpret_cast<const char *>(decodedSample), numDecodedSamples * sizeof(float));
cout << "ok\n";
cout << "cleanup ... " << flush;
delete [] decodedSample;
cout << "ok\n";
cout << "decoding successful!\n";
}
else
{
ErrorInvalidArgs(argv);
return 1;
}
return 0;
}