Support owning track data
This commit is contained in:
parent
dd40936769
commit
3feb2776c1
2 changed files with 19 additions and 0 deletions
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace psemek::audio
|
||||
{
|
||||
|
|
@ -34,6 +35,8 @@ namespace psemek::audio
|
|||
return load(data.data(), data.size());
|
||||
}
|
||||
|
||||
std::shared_ptr<track> load_raw(std::vector<sample> data);
|
||||
|
||||
std::shared_ptr<stream> play(std::shared_ptr<track> s, bool start = true, bool loop = false);
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -45,11 +45,17 @@ namespace psemek::audio
|
|||
: track
|
||||
{
|
||||
Mix_Chunk * chunk;
|
||||
std::vector<sample> buffer;
|
||||
|
||||
track_impl(Mix_Chunk * chunk)
|
||||
: chunk(chunk)
|
||||
{}
|
||||
|
||||
track_impl(Mix_Chunk * chunk, std::vector<sample> buffer)
|
||||
: chunk(chunk)
|
||||
, buffer(std::move(buffer))
|
||||
{}
|
||||
|
||||
sample const * data() const override
|
||||
{
|
||||
return reinterpret_cast<sample const *>(chunk->abuf);
|
||||
|
|
@ -307,6 +313,16 @@ namespace psemek::audio
|
|||
return std::make_shared<track_impl>(chunk);
|
||||
}
|
||||
|
||||
std::shared_ptr<track> engine::load_raw(std::vector<sample> data)
|
||||
{
|
||||
Mix_Chunk * chunk = static_cast<Mix_Chunk *>(malloc(sizeof(Mix_Chunk)));
|
||||
chunk->allocated = 0;
|
||||
chunk->alen = data.size() * sizeof(sample);
|
||||
chunk->volume = 128;
|
||||
chunk->abuf = const_cast<Uint8 *>(reinterpret_cast<Uint8 const *>(data.data()));
|
||||
return std::make_shared<track_impl>(chunk, std::move(data));
|
||||
}
|
||||
|
||||
std::shared_ptr<stream> engine::play(std::shared_ptr<track> s, bool start, bool loop)
|
||||
{
|
||||
auto str = impl().play(std::move(s), loop);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue