Repository navigation
Expand file tree
/
Copy pathdecode_example.cpp
More file actions
117 lines (104 loc) · 4.54 KB
/
Copy pathdecode_example.cpp
File metadata and controls
117 lines (104 loc) · 4.54 KB
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
// decode_example.cpp - decode a .falcon file with the Falcon C++ API.
//
// Usage: decode_example <file.falcon>
//
// Demonstrates both the streaming Decoder (RAII wrapper) and the one-shot
// falcon::decodeAll helper, and cross-checks that they agree.
#include <falcon.hpp>
#include <cinttypes>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <vector>
static bool loadFile(const char* path, std::vector<std::uint8_t>& out) {
std::ifstream f(path, std::ios::binary | std::ios::ate);
if (!f) return false;
const std::streamsize size = f.tellg();
f.seekg(0, std::ios::beg);
out.resize(static_cast<std::size_t>(size));
return static_cast<bool>(f.read(reinterpret_cast<char*>(out.data()), size));
}
int main(int argc, char** argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: %s <file.falcon>\n", argv[0]);
return 2;
}
std::printf("falcon version: %s\n", falcon::version());
std::vector<std::uint8_t> file;
if (!loadFile(argv[1], file)) {
std::fprintf(stderr, "error: cannot read %s\n", argv[1]);
return 1;
}
std::printf("input: %s (%zu bytes)\n", argv[1], file.size());
// --- Streaming decode ---------------------------------------------------
falcon::Decoder dec = falcon::Decoder::open(file.data(), file.size());
if (!dec) {
std::fprintf(stderr, "error: falcon_decoder_open failed (invalid stream?)\n");
return 1;
}
const int channels = dec.channels();
const int sampleRate = dec.sampleRate();
const std::uint64_t total = dec.totalSamples();
std::printf("channels: %d\nsample rate: %d Hz\ntotal frames (per channel): %" PRIu64 "\n",
channels, sampleRate, total);
std::printf("duration: %.3f s\n", sampleRate > 0 ? double(total) / sampleRate : 0.0);
constexpr std::size_t kBlockFrames = 1024;
std::vector<float> block(kBlockFrames * static_cast<std::size_t>(channels));
std::vector<float> streamed;
streamed.reserve(static_cast<std::size_t>(total) * channels);
std::uint64_t framesRead = 0;
double peak = 0.0, sumSq = 0.0;
for (;;) {
const std::int64_t n = dec.read(block.data(), kBlockFrames);
if (n < 0) {
std::fprintf(stderr, "error: read failed: %s\n", falcon::errorMessage(static_cast<int>(n)));
return 1;
}
if (n == 0) break; // end of stream
const std::size_t count = static_cast<std::size_t>(n) * channels;
streamed.insert(streamed.end(), block.begin(), block.begin() + count);
for (std::size_t i = 0; i < count; ++i) {
const double s = block[i];
sumSq += s * s;
if (std::fabs(s) > peak) peak = std::fabs(s);
}
framesRead += static_cast<std::uint64_t>(n);
}
std::printf("streaming: read %" PRIu64 " frames, peak %.4f, RMS %.4f\n",
framesRead, peak,
framesRead ? std::sqrt(sumSq / (double(framesRead) * channels)) : 0.0);
// --- Seek check: re-read the second half and compare -------------------
const std::uint64_t mid = total / 2;
if (dec.seek(mid) != falcon::Error::Ok) {
std::fprintf(stderr, "error: seek(%" PRIu64 ") failed\n", mid);
return 1;
}
const std::int64_t n = dec.read(block.data(), kBlockFrames);
bool seekOk = n > 0;
if (seekOk) {
const std::size_t off = static_cast<std::size_t>(mid) * channels;
for (std::int64_t i = 0; i < n * channels; ++i) {
if (streamed[off + static_cast<std::size_t>(i)] != block[static_cast<std::size_t>(i)]) {
seekOk = false;
break;
}
}
}
std::printf("seek(%" PRIu64 ") + reread: %s\n", mid, seekOk ? "matches linear decode" : "MISMATCH");
// --- One-shot decode cross-check ----------------------------------------
falcon::DecodedAudio all;
const falcon::Error rc = falcon::decodeAll(file.data(), file.size(), all);
if (rc != falcon::Error::Ok) {
std::fprintf(stderr, "error: decodeAll failed: %s\n", falcon::errorMessage(rc));
return 1;
}
const bool agree = all.frames == framesRead &&
all.channels == channels &&
all.sampleRate == sampleRate &&
all.samples == streamed;
std::printf("one-shot: %" PRIu64 " frames, %d ch, %d Hz -> %s\n",
all.frames, all.channels, all.sampleRate,
agree ? "identical to streaming decode" : "MISMATCH vs streaming decode");
return agree && seekOk && framesRead == total ? 0 : 1;
}