载入中...
搜索中...
未找到
SoundData.cpp
浏览该文件的文档.
1#include "SoundData.h"
2
3#include "common/Exception.h"
4
5#include <utility>
6
7namespace eve {
8namespace sound {
9
10SoundData::SoundData(std::vector<uint8_t> pcm, int sampleRate, int bitDepth, int channels)
11 : Resource(""), pcm(std::move(pcm)), sampleRate(sampleRate), bitDepth(bitDepth), channels(channels) {
12 if (sampleRate <= 0)
13 throw eve::Exception("Invalid sample rate");
14 if (!(bitDepth == 8 || bitDepth == 16))
15 throw eve::Exception("Invalid bit depth (expected 8 or 16)");
16 if (!(channels == 1 || channels == 2))
17 throw eve::Exception("Invalid channel count (expected 1 or 2)");
18}
19
20SoundData::~SoundData() = default;
21
22void SoundData::adopt(eve::Resource &replacement) {
23 auto &other = static_cast<SoundData &>(replacement);
24 std::swap(pcm, other.pcm);
25 std::swap(sampleRate, other.sampleRate);
26 std::swap(bitDepth, other.bitDepth);
27 std::swap(channels, other.channels);
28}
29
31 if (bitDepth <= 0 || channels <= 0)
32 return 0;
33 return static_cast<int>(pcm.size() / (static_cast<size_t>(bitDepth / 8) * channels));
34}
35
36int SoundData::getSampleRate() const { return sampleRate; }
37int SoundData::getBitDepth() const { return bitDepth; }
38int SoundData::getChannelCount() const { return channels; }
39
40double SoundData::getDuration() const {
41 if (sampleRate <= 0)
42 return 0.0;
43 return static_cast<double>(getSampleCount()) / static_cast<double>(sampleRate);
44}
45
46void *SoundData::getData() const { return const_cast<uint8_t *>(pcm.data()); }
47size_t SoundData::getSize() const { return pcm.size(); }
48
49} // namespace sound
50} // namespace eve
Resource is a game object that is managed by the ResourceManager. It can be loaded from a file or gen...
Definition Resource.h:35
Raw PCM audio buffer with format metadata (samples/rate/bit depth/channels).
Definition SoundData.h:13
double getDuration() const
Duration in seconds.
Definition SoundData.cpp:40
void * getData() const
Raw PCM access.
Definition SoundData.cpp:46
void adopt(eve::Resource &replacement) override
Replace this buffer with replacement's (cache reload).
Definition SoundData.cpp:22
int getBitDepth() const
Definition SoundData.cpp:37
int getChannelCount() const
Definition SoundData.cpp:38
int getSampleRate() const
Definition SoundData.cpp:36
int getSampleCount() const
Format metadata.
Definition SoundData.cpp:30
size_t getSize() const
Definition SoundData.cpp:47
SoundData(std::vector< uint8_t > pcm, int sampleRate, int bitDepth, int channels)
Takes ownership of the PCM bytes.
Definition SoundData.cpp:10
Definition Build.cpp:11