载入中...
搜索中...
未找到
Decoder.cpp
浏览该文件的文档.
1#include "Decoder.h"
2
3#include "common/Exception.h"
4#if !defined(__EMSCRIPTEN__)
5#include "medialoader/sound/Decoder.h"
6#endif
7
8#include <algorithm>
9#include <cmath>
10
11namespace eve {
12namespace sound {
13
14#if defined(__EMSCRIPTEN__)
15
16Decoder::Decoder(std::shared_ptr<const std::vector<std::uint8_t>> pcm, int sampleRate, int channels, int bufferSize)
17 : browserPcm(std::move(pcm)), browserSampleRate(sampleRate), browserChannels(channels) {
18 if (!browserPcm || sampleRate <= 0 || (channels != 1 && channels != 2) || bufferSize <= 0)
19 throw eve::Exception("Invalid browser sound decoder");
20 const auto frameBytes = static_cast<std::size_t>(channels) * 2;
21 browserChunkBytes = std::max(frameBytes, static_cast<std::size_t>(bufferSize) / frameBytes * frameBytes);
22 browserBuffer.reserve(browserChunkBytes);
23}
24
25Decoder::~Decoder() = default;
26Decoder* Decoder::clone() const {
27 return new Decoder(browserPcm, browserSampleRate, browserChannels, static_cast<int>(browserChunkBytes));
28}
29int Decoder::decode() {
30 const auto count = std::min(browserChunkBytes, browserPcm->size() - browserOffset);
31 browserBuffer.assign(browserPcm->begin() + browserOffset, browserPcm->begin() + browserOffset + count);
32 browserOffset += count;
33 return static_cast<int>(count);
34}
35int Decoder::getSize() const { return static_cast<int>(browserBuffer.size()); }
36void* Decoder::getBuffer() const { return const_cast<std::uint8_t*>(browserBuffer.data()); }
37bool Decoder::seek(double seconds) {
38 if (!std::isfinite(seconds) || seconds < 0.0 || seconds > getDuration()) return false;
39 const auto frame = static_cast<std::size_t>(seconds * browserSampleRate);
40 browserOffset = std::min(frame * static_cast<std::size_t>(browserChannels) * 2, browserPcm->size());
41 browserBuffer.clear();
42 return true;
43}
44bool Decoder::rewind() {
45 browserOffset = 0;
46 browserBuffer.clear();
47 return true;
48}
49bool Decoder::isSeekable() { return true; }
50bool Decoder::isFinished() { return browserOffset >= browserPcm->size(); }
51int Decoder::getChannelCount() const { return browserChannels; }
52int Decoder::getBitDepth() const { return 16; }
53int Decoder::getSampleRate() const { return browserSampleRate; }
54double Decoder::getDuration() {
55 return static_cast<double>(browserPcm->size()) / (static_cast<double>(browserSampleRate) * browserChannels * 2);
56}
57
58#else
59
60Decoder::Decoder(std::unique_ptr<medialoader::Decoder> impl, std::vector<char> ownedData)
61 : Decoder(std::move(impl), std::make_shared<const std::vector<char>>(std::move(ownedData))) {}
62
63Decoder::Decoder(std::unique_ptr<medialoader::Decoder> impl, std::shared_ptr<const std::vector<char>> ownedData)
64 : ownedData(std::move(ownedData)), impl(std::move(impl)) {
65 if (!this->impl)
66 throw eve::Exception("Invalid sound decoder");
67}
68
69Decoder::~Decoder() = default;
70
72 auto cloned = std::unique_ptr<medialoader::Decoder>(impl->clone());
73 if (!cloned)
74 throw eve::Exception("Could not clone sound decoder");
75 // Provider clones retain the original pointer rather than rebinding it to
76 // a byte-for-byte copy. Keep that exact allocation alive for every clone.
77 return new Decoder(std::move(cloned), ownedData);
78}
79
80int Decoder::decode() { return impl->decode(); }
81int Decoder::getSize() const { return impl->getSize(); }
82void *Decoder::getBuffer() const { return impl->getBuffer(); }
83bool Decoder::seek(double seconds) { return impl->seek(seconds); }
84bool Decoder::rewind() { return impl->rewind(); }
85bool Decoder::isSeekable() { return impl->isSeekable(); }
86bool Decoder::isFinished() { return impl->isFinished(); }
87int Decoder::getChannelCount() const { return impl->getChannelCount(); }
88int Decoder::getBitDepth() const { return impl->getBitDepth(); }
89int Decoder::getSampleRate() const { return impl->getSampleRate(); }
90double Decoder::getDuration() { return impl->getDuration(); }
91
92#endif
93
94} // namespace sound
95} // namespace eve
std::map< std::string, std::vector< Key >, std::less<> > channels
void * impl
ssq::Object frame
int count
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Streaming audio decoder (medialoader-backed). Native builds own encoded bytes and decode incrementall...
Definition Decoder.h:23
void * getBuffer() const
Internal decoded buffer.
Definition Decoder.cpp:82
double getDuration()
Total duration in seconds.
Definition Decoder.cpp:90
bool isFinished()
True when the stream is fully decoded.
Definition Decoder.cpp:86
int getSampleRate() const
Returns the sample rate.
Definition Decoder.cpp:89
bool isSeekable()
True when seeking is supported.
Definition Decoder.cpp:85
int getChannelCount() const
Format metadata.
Definition Decoder.cpp:87
Decoder(std::unique_ptr< medialoader::Decoder > impl, std::vector< char > ownedData)
Wraps a medialoader decoder and the encoded bytes it needs.
Definition Decoder.cpp:60
Decoder * clone() const
Creates a caller-owned decoder positioned at the start of the stream.
Definition Decoder.cpp:71
bool rewind()
Rewinds to the start; false when unsupported.
Definition Decoder.cpp:84
bool seek(double seconds)
Seeks to a time in seconds; false when unsupported.
Definition Decoder.cpp:83
int getSize() const
Bytes decoded so far into the internal buffer.
Definition Decoder.cpp:81
int getBitDepth() const
Returns the bit depth.
Definition Decoder.cpp:88
int decode()
Decodes the next chunk; returns bytes produced (0 = end/error).
Definition Decoder.cpp:80
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16