Compare commits

..

No commits in common. "c5171ffc038f8c433b8ea344e9f6f8d94d1b562a" and "cccc6ca8c3dfcef3142c9e45c24cfda512a982fc" have entirely different histories.

10 changed files with 149 additions and 142 deletions

View File

@ -6,15 +6,17 @@
namespace base { namespace base {
// Compile-time string hashing function.
template <size_t N> template <size_t N>
constexpr inline size_t KR2Hash(const char (&str)[N], size_t Len = N - 1) { constexpr inline size_t Hash(const char (&str)[N], size_t Len = N - 1) {
size_t hash_value = 0; size_t hash_value = 0;
for (int i = 0; str[i] != '\0'; ++i) for (int i = 0; str[i] != '\0'; ++i)
hash_value = str[i] + 31 * hash_value; hash_value = str[i] + 31 * hash_value;
return hash_value; return hash_value;
} }
inline size_t KR2Hash(const std::string& str) { // The same hashing function for run-time.
inline size_t Hash(const std::string& str) {
size_t hash_value = 0; size_t hash_value = 0;
for (std::string::value_type c : str) for (std::string::value_type c : str)
hash_value = c + 31 * hash_value; hash_value = c + 31 * hash_value;

View File

@ -41,6 +41,8 @@ void ThreadPool::Shutdown() {
} }
void ThreadPool::PostTask(Location from, Closure task, bool front) { void ThreadPool::PostTask(Location from, Closure task, bool front) {
DCHECK((!threads_.empty()));
task_runner_.PostTask(from, std::move(task), front); task_runner_.PostTask(from, std::move(task), front);
semaphore_.release(); semaphore_.release();
} }
@ -49,6 +51,8 @@ void ThreadPool::PostTaskAndReply(Location from,
Closure task, Closure task,
Closure reply, Closure reply,
bool front) { bool front) {
DCHECK((!threads_.empty()));
task_runner_.PostTaskAndReply(from, std::move(task), std::move(reply), front); task_runner_.PostTaskAndReply(from, std::move(task), std::move(reply), front);
semaphore_.release(); semaphore_.release();
} }

View File

@ -17,10 +17,6 @@ using namespace base;
namespace eng { namespace eng {
AudioMixer::Resource::~Resource() {
DLOG(1) << "Destroying audio resource: " << uintptr_t(this);
}
AudioMixer::AudioMixer() AudioMixer::AudioMixer()
: main_thread_task_runner_(TaskRunner::GetThreadLocalTaskRunner()), : main_thread_task_runner_(TaskRunner::GetThreadLocalTaskRunner()),
#if defined(__ANDROID__) #if defined(__ANDROID__)
@ -32,102 +28,131 @@ AudioMixer::AudioMixer()
CHECK(res) << "Failed to initialize audio sink."; CHECK(res) << "Failed to initialize audio sink.";
} }
AudioMixer::~AudioMixer() { AudioMixer::~AudioMixer() = default;
audio_sink_.reset();
uint64_t AudioMixer::CreateResource() {
uint64_t resource_id = ++last_resource_id_;
resources_[resource_id] = std::make_shared<Resource>();
return resource_id;
} }
std::shared_ptr<void> AudioMixer::CreateResource() { void AudioMixer::DestroyResource(uint64_t resource_id) {
auto resource = std::make_shared<Resource>(); auto it = resources_.find(resource_id);
DLOG(1) << "Audio resource created: " << uintptr_t(resource.get()); if (it == resources_.end())
return resource; return;
if (it->second->active) {
it->second->restart_cb = nullptr;
it->second->flags.fetch_or(kStopped, std::memory_order_relaxed);
}
resources_.erase(it);
} }
void AudioMixer::Play(std::shared_ptr<void> resource, void AudioMixer::Play(uint64_t resource_id,
std::shared_ptr<AudioBus> audio_bus, std::shared_ptr<AudioBus> audio_bus,
float amplitude, float amplitude,
bool reset_pos) { bool reset_pos) {
if (!audio_enabled_) if (!audio_enabled_)
return; return;
auto resource_ptr = std::static_pointer_cast<Resource>(resource); auto it = resources_.find(resource_id);
if (it == resources_.end())
return;
if (resource_ptr->active) { if (it->second->active) {
if (reset_pos) if (reset_pos)
resource_ptr->flags.fetch_or(kStopped, std::memory_order_relaxed); it->second->flags.fetch_or(kStopped, std::memory_order_relaxed);
if (resource_ptr->flags.load(std::memory_order_relaxed) & kStopped) if (it->second->flags.load(std::memory_order_relaxed) & kStopped)
resource_ptr->restart_cb = [&, resource, audio_bus, amplitude, it->second->restart_cb = [&, resource_id, audio_bus, amplitude,
reset_pos]() -> void { reset_pos]() -> void {
Play(resource, audio_bus, amplitude, reset_pos); Play(resource_id, audio_bus, amplitude, reset_pos);
}; };
return; return;
} }
if (reset_pos) { if (reset_pos) {
resource_ptr->src_index = 0; it->second->src_index = 0;
resource_ptr->accumulator = 0; it->second->accumulator = 0;
audio_bus->ResetStream(); audio_bus->ResetStream();
} else if (resource_ptr->src_index >= audio_bus->samples_per_channel()) { } else if (it->second->src_index >= audio_bus->samples_per_channel()) {
return; return;
} }
resource_ptr->active = true; it->second->active = true;
resource_ptr->flags.fetch_and(~kStopped, std::memory_order_relaxed); it->second->flags.fetch_and(~kStopped, std::memory_order_relaxed);
resource_ptr->audio_bus = audio_bus; it->second->audio_bus = audio_bus;
if (amplitude >= 0) if (amplitude >= 0)
resource_ptr->amplitude = amplitude; it->second->amplitude = amplitude;
std::lock_guard<std::mutex> scoped_lock(lock_); std::lock_guard<std::mutex> scoped_lock(lock_);
play_list_[0].push_back(resource_ptr); play_list_[0].push_back(it->second);
} }
void AudioMixer::Stop(std::shared_ptr<void> resource) { void AudioMixer::Stop(uint64_t resource_id) {
auto resource_ptr = std::static_pointer_cast<Resource>(resource); auto it = resources_.find(resource_id);
if (resource_ptr->active) { if (it == resources_.end())
resource_ptr->restart_cb = nullptr; return;
resource_ptr->flags.fetch_or(kStopped, std::memory_order_relaxed);
if (it->second->active) {
it->second->restart_cb = nullptr;
it->second->flags.fetch_or(kStopped, std::memory_order_relaxed);
} }
} }
void AudioMixer::SetLoop(std::shared_ptr<void> resource, bool loop) { void AudioMixer::SetLoop(uint64_t resource_id, bool loop) {
auto resource_ptr = std::static_pointer_cast<Resource>(resource); auto it = resources_.find(resource_id);
if (it == resources_.end())
return;
if (loop) if (loop)
resource_ptr->flags.fetch_or(kLoop, std::memory_order_relaxed); it->second->flags.fetch_or(kLoop, std::memory_order_relaxed);
else else
resource_ptr->flags.fetch_and(~kLoop, std::memory_order_relaxed); it->second->flags.fetch_and(~kLoop, std::memory_order_relaxed);
} }
void AudioMixer::SetSimulateStereo(std::shared_ptr<void> resource, void AudioMixer::SetSimulateStereo(uint64_t resource_id, bool simulate) {
bool simulate) { auto it = resources_.find(resource_id);
auto resource_ptr = std::static_pointer_cast<Resource>(resource); if (it == resources_.end())
return;
if (simulate) if (simulate)
resource_ptr->flags.fetch_or(kSimulateStereo, std::memory_order_relaxed); it->second->flags.fetch_or(kSimulateStereo, std::memory_order_relaxed);
else else
resource_ptr->flags.fetch_and(~kSimulateStereo, std::memory_order_relaxed); it->second->flags.fetch_and(~kSimulateStereo, std::memory_order_relaxed);
} }
void AudioMixer::SetResampleStep(std::shared_ptr<void> resource, size_t step) { void AudioMixer::SetResampleStep(uint64_t resource_id, size_t step) {
auto resource_ptr = std::static_pointer_cast<Resource>(resource); auto it = resources_.find(resource_id);
resource_ptr->step.store(step + 100, std::memory_order_relaxed); if (it == resources_.end())
return;
it->second->step.store(step + 100, std::memory_order_relaxed);
} }
void AudioMixer::SetMaxAmplitude(std::shared_ptr<void> resource, void AudioMixer::SetMaxAmplitude(uint64_t resource_id, float max_amplitude) {
float max_amplitude) { auto it = resources_.find(resource_id);
auto resource_ptr = std::static_pointer_cast<Resource>(resource); if (it == resources_.end())
resource_ptr->max_amplitude.store(max_amplitude, std::memory_order_relaxed); return;
it->second->max_amplitude.store(max_amplitude, std::memory_order_relaxed);
} }
void AudioMixer::SetAmplitudeInc(std::shared_ptr<void> resource, void AudioMixer::SetAmplitudeInc(uint64_t resource_id, float amplitude_inc) {
float amplitude_inc) { auto it = resources_.find(resource_id);
auto resource_ptr = std::static_pointer_cast<Resource>(resource); if (it == resources_.end())
resource_ptr->amplitude_inc.store(amplitude_inc, std::memory_order_relaxed); return;
it->second->amplitude_inc.store(amplitude_inc, std::memory_order_relaxed);
} }
void AudioMixer::SetEndCallback(std::shared_ptr<void> resource, void AudioMixer::SetEndCallback(uint64_t resource_id, base::Closure cb) {
base::Closure cb) { auto it = resources_.find(resource_id);
auto resource_ptr = std::static_pointer_cast<Resource>(resource); if (it == resources_.end())
resource_ptr->end_cb = std::move(cb); return;
it->second->end_cb = std::move(cb);
} }
void AudioMixer::Suspend() { void AudioMixer::Suspend() {
@ -152,8 +177,8 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
memset(output_buffer, 0, sizeof(float) * num_frames * kChannelCount); memset(output_buffer, 0, sizeof(float) * num_frames * kChannelCount);
for (auto it = play_list_[1].begin(); it != play_list_[1].end();) { for (auto it = play_list_[1].begin(); it != play_list_[1].end();) {
auto audio_bus = (*it)->audio_bus.get(); auto audio_bus = it->get()->audio_bus.get();
unsigned flags = (*it)->flags.load(std::memory_order_relaxed); unsigned flags = it->get()->flags.load(std::memory_order_relaxed);
bool marked_for_removal = false; bool marked_for_removal = false;
if (flags & kStopped) { if (flags & kStopped) {
@ -165,14 +190,14 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
src[1] = src[0]; // mono. src[1] = src[0]; // mono.
size_t num_samples = audio_bus->samples_per_channel(); size_t num_samples = audio_bus->samples_per_channel();
size_t src_index = (*it)->src_index; size_t src_index = it->get()->src_index;
size_t step = (*it)->step.load(std::memory_order_relaxed); size_t step = it->get()->step.load(std::memory_order_relaxed);
size_t accumulator = (*it)->accumulator; size_t accumulator = it->get()->accumulator;
float amplitude = (*it)->amplitude; float amplitude = it->get()->amplitude;
float amplitude_inc = float amplitude_inc =
(*it)->amplitude_inc.load(std::memory_order_relaxed); it->get()->amplitude_inc.load(std::memory_order_relaxed);
float max_amplitude = float max_amplitude =
(*it)->max_amplitude.load(std::memory_order_relaxed); it->get()->max_amplitude.load(std::memory_order_relaxed);
size_t channel_offset = size_t channel_offset =
(flags & kSimulateStereo) ? audio_bus->sample_rate() / 10 : 0; (flags & kSimulateStereo) ? audio_bus->sample_rate() / 10 : 0;
@ -212,9 +237,11 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
break; break;
} }
if (!(*it)->streaming_in_progress.load(std::memory_order_acquire)) { if (!it->get()->streaming_in_progress.load(
std::memory_order_acquire)) {
src_index %= num_samples; src_index %= num_samples;
(*it)->streaming_in_progress.store(true, std::memory_order_relaxed); it->get()->streaming_in_progress.store(true,
std::memory_order_relaxed);
// Swap buffers and start streaming in background. // Swap buffers and start streaming in background.
audio_bus->SwapBuffers(); audio_bus->SwapBuffers();
@ -234,9 +261,9 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
} }
} }
(*it)->src_index = src_index; it->get()->src_index = src_index;
(*it)->accumulator = accumulator; it->get()->accumulator = accumulator;
(*it)->amplitude = amplitude; it->get()->amplitude = amplitude;
} }
if (marked_for_removal) { if (marked_for_removal) {
@ -248,7 +275,7 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
} }
for (auto it = end_list_.begin(); it != end_list_.end();) { for (auto it = end_list_.begin(); it != end_list_.end();) {
if (!(*it)->streaming_in_progress.load(std::memory_order_relaxed)) { if (!it->get()->streaming_in_progress.load(std::memory_order_relaxed)) {
main_thread_task_runner_->PostTask( main_thread_task_runner_->PostTask(
HERE, std::bind(&AudioMixer::EndCallback, this, *it)); HERE, std::bind(&AudioMixer::EndCallback, this, *it));
it = end_list_.erase(it); it = end_list_.erase(it);

View File

@ -5,6 +5,7 @@
#include <list> #include <list>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <unordered_map>
#include "base/closure.h" #include "base/closure.h"
#include "engine/audio/audio_sink.h" #include "engine/audio/audio_sink.h"
@ -25,20 +26,21 @@ class AudioMixer : public AudioSink::Delegate {
AudioMixer(); AudioMixer();
~AudioMixer(); ~AudioMixer();
std::shared_ptr<void> CreateResource(); uint64_t CreateResource();
void DestroyResource(uint64_t resource_id);
void Play(std::shared_ptr<void> resource, void Play(uint64_t resource_id,
std::shared_ptr<AudioBus> audio_bus, std::shared_ptr<AudioBus> audio_bus,
float amplitude, float amplitude,
bool reset_pos); bool reset_pos);
void Stop(std::shared_ptr<void> resource); void Stop(uint64_t resource_id);
void SetLoop(std::shared_ptr<void> resource, bool loop); void SetLoop(uint64_t resource_id, bool loop);
void SetSimulateStereo(std::shared_ptr<void> resource, bool simulate); void SetSimulateStereo(uint64_t resource_id, bool simulate);
void SetResampleStep(std::shared_ptr<void> resource, size_t step); void SetResampleStep(uint64_t resource_id, size_t step);
void SetMaxAmplitude(std::shared_ptr<void> resource, float max_amplitude); void SetMaxAmplitude(uint64_t resource_id, float max_amplitude);
void SetAmplitudeInc(std::shared_ptr<void> resource, float amplitude_inc); void SetAmplitudeInc(uint64_t resource_id, float amplitude_inc);
void SetEndCallback(std::shared_ptr<void> resource, base::Closure cb); void SetEndCallback(uint64_t resource_id, base::Closure cb);
void SetEnableAudio(bool enable) { audio_enabled_ = enable; } void SetEnableAudio(bool enable) { audio_enabled_ = enable; }
@ -72,10 +74,11 @@ class AudioMixer : public AudioSink::Delegate {
// Accessed by audio thread and decoder thread. // Accessed by audio thread and decoder thread.
std::atomic<bool> streaming_in_progress{false}; std::atomic<bool> streaming_in_progress{false};
~Resource();
}; };
std::unordered_map<uint64_t, std::shared_ptr<Resource>> resources_;
uint64_t last_resource_id_ = 0;
std::list<std::shared_ptr<Resource>> play_list_[2]; std::list<std::shared_ptr<Resource>> play_list_[2];
std::mutex lock_; std::mutex lock_;

View File

@ -5,7 +5,6 @@
#include <sstream> #include <sstream>
#include <unordered_set> #include <unordered_set>
#include "base/hash.h"
#include "base/log.h" #include "base/log.h"
#include "base/vecmath.h" #include "base/vecmath.h"
#include "engine/asset/image.h" #include "engine/asset/image.h"
@ -616,28 +615,21 @@ bool RendererOpenGL::BindAttributeLocation(GLuint id,
GLint RendererOpenGL::GetUniformLocation( GLint RendererOpenGL::GetUniformLocation(
GLuint id, GLuint id,
const std::string& name, const std::string& name,
std::vector<std::pair<size_t, GLuint>>& uniforms) { std::unordered_map<std::string, GLuint>& uniforms) {
// Check if we've encountered this uniform before. // Check if we've encountered this uniform before.
auto hash = KR2Hash(name); auto i = uniforms.find(name);
auto it = std::find_if(uniforms.begin(), uniforms.end(),
[&](auto& r) { return hash == std::get<0>(r); });
GLint index; GLint index;
if (it != uniforms.end()) { if (i != uniforms.end()) {
// Yes, we already have the mapping. // Yes, we already have the mapping.
index = std::get<1>(*it); index = i->second;
} else { } else {
// No, ask the driver for the mapping and save it. // No, ask the driver for the mapping and save it.
index = glGetUniformLocation(id, name.c_str()); index = glGetUniformLocation(id, name.c_str());
if (index >= 0) { if (index >= 0)
DCHECK(std::find_if(uniforms.begin(), uniforms.end(), uniforms[name] = index;
[&](auto& r) { return hash == std::get<0>(r); }) == else
uniforms.end())
<< "Hash collision";
uniforms.emplace_back(std::make_pair(hash, index));
} else {
LOG(0) << "Cannot find uniform " << name.c_str() << " (shader: " << id LOG(0) << "Cannot find uniform " << name.c_str() << " (shader: " << id
<< ")"; << ")";
}
} }
return index; return index;
} }

View File

@ -4,7 +4,6 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <utility>
#include <vector> #include <vector>
#include "engine/renderer/opengl/opengl.h" #include "engine/renderer/opengl/opengl.h"
@ -95,10 +94,7 @@ class RendererOpenGL final : public Renderer {
struct ShaderOpenGL { struct ShaderOpenGL {
GLuint id = 0; GLuint id = 0;
std::vector<std::pair<size_t, // Uniform name hash std::unordered_map<std::string, GLuint> uniforms;
GLuint // Uniform index
>>
uniforms;
bool enable_depth_test = false; bool enable_depth_test = false;
}; };
@ -144,7 +140,7 @@ class RendererOpenGL final : public Renderer {
bool BindAttributeLocation(GLuint id, const VertexDescription& vd); bool BindAttributeLocation(GLuint id, const VertexDescription& vd);
GLint GetUniformLocation(GLuint id, GLint GetUniformLocation(GLuint id,
const std::string& name, const std::string& name,
std::vector<std::pair<size_t, GLuint>>& uniforms); std::unordered_map<std::string, GLuint>& uniforms);
}; };
} // namespace eng } // namespace eng

View File

@ -5,7 +5,6 @@
#include <sstream> #include <sstream>
#include <utility> #include <utility>
#include "base/hash.h"
#include "base/log.h" #include "base/log.h"
#include "base/vecmath.h" #include "base/vecmath.h"
#include "engine/asset/image.h" #include "engine/asset/image.h"
@ -1877,27 +1876,19 @@ bool RendererVulkan::CreatePipelineLayout(
size_t offset = 0; size_t offset = 0;
for (uint32_t j = 0; j < pconstants_vertex[0]->member_count; j++) { for (uint32_t j = 0; j < pconstants_vertex[0]->member_count; j++) {
DCHECK(std::find_if(
shader.variables.begin(), shader.variables.end(),
[hash = KR2Hash(pconstants_vertex[0]->members[j].name)](
auto& r) { return hash == std::get<0>(r); }) ==
shader.variables.end())
<< "Hash collision";
DLOG(0) << __func__ DLOG(0) << __func__
<< " name: " << pconstants_vertex[0]->members[j].name << " name: " << pconstants_vertex[0]->members[j].name
<< " size: " << pconstants_vertex[0]->members[j].size << " size: " << pconstants_vertex[0]->members[j].size
<< " padded_size: " << " padded_size: "
<< pconstants_vertex[0]->members[j].padded_size; << pconstants_vertex[0]->members[j].padded_size;
shader.variables.emplace_back( shader.variables[pconstants_vertex[0]->members[j].name] = {
std::make_tuple(KR2Hash(pconstants_vertex[0]->members[j].name), pconstants_vertex[0]->members[j].size, offset};
pconstants_vertex[0]->members[j].size, offset));
offset += pconstants_vertex[0]->members[j].padded_size; offset += pconstants_vertex[0]->members[j].padded_size;
} }
} }
// Use the same layout for all descriptor sets. // Use the same layout for all decriptor sets.
std::vector<VkDescriptorSetLayout> desc_set_layouts; std::vector<VkDescriptorSetLayout> desc_set_layouts;
for (size_t i = 0; i < binding_count; ++i) for (size_t i = 0; i < binding_count; ++i)
desc_set_layouts.push_back(descriptor_set_layout_); desc_set_layouts.push_back(descriptor_set_layout_);
@ -2041,21 +2032,17 @@ template <typename T>
bool RendererVulkan::SetUniformInternal(ShaderVulkan& shader, bool RendererVulkan::SetUniformInternal(ShaderVulkan& shader,
const std::string& name, const std::string& name,
T val) { T val) {
auto hash = KR2Hash(name); auto it = shader.variables.find(name);
auto it = std::find_if(shader.variables.begin(), shader.variables.end(),
[&](auto& r) { return hash == std::get<0>(r); });
if (it == shader.variables.end()) { if (it == shader.variables.end()) {
DLOG(0) << "No variable found with name " << name; DLOG(0) << "No variable found with name " << name;
return false; return false;
} }
if (it->second[0] != sizeof(val)) {
if (std::get<1>(*it) != sizeof(val)) {
DLOG(0) << "Size mismatch for variable " << name; DLOG(0) << "Size mismatch for variable " << name;
return false; return false;
} }
auto* dst = auto* dst = reinterpret_cast<T*>(shader.push_constants.get() + it->second[1]);
reinterpret_cast<T*>(shader.push_constants.get() + std::get<2>(*it));
*dst = val; *dst = val;
return true; return true;
} }

View File

@ -106,11 +106,7 @@ class RendererVulkan final : public Renderer {
}; };
struct ShaderVulkan { struct ShaderVulkan {
std::vector<std::tuple<size_t, // Variable name hash std::unordered_map<std::string, std::array<size_t, 2>> variables;
size_t, // Variable size
size_t // Push constant offset
>>
variables;
std::unique_ptr<char[]> push_constants; std::unique_ptr<char[]> push_constants;
size_t push_constants_size = 0; size_t push_constants_size = 0;
std::vector<std::string> sampler_uniform_names; std::vector<std::string> sampler_uniform_names;

View File

@ -11,10 +11,10 @@ using namespace base;
namespace eng { namespace eng {
SoundPlayer::SoundPlayer() SoundPlayer::SoundPlayer()
: resource_(Engine::Get().GetAudioMixer()->CreateResource()) {} : resource_id_(Engine::Get().GetAudioMixer()->CreateResource()) {}
SoundPlayer::~SoundPlayer() { SoundPlayer::~SoundPlayer() {
Engine::Get().GetAudioMixer()->Stop(resource_); Engine::Get().GetAudioMixer()->DestroyResource(resource_id_);
} }
void SoundPlayer::SetSound(const std::string& asset_name) { void SoundPlayer::SetSound(const std::string& asset_name) {
@ -30,15 +30,15 @@ void SoundPlayer::Play(bool loop, float fade_in_duration) {
return; return;
int step = variate_ ? Engine::Get().GetRandomGenerator().Roll(3) - 2 : 0; int step = variate_ ? Engine::Get().GetRandomGenerator().Roll(3) - 2 : 0;
Engine::Get().GetAudioMixer()->SetResampleStep(resource_, step * 12); Engine::Get().GetAudioMixer()->SetResampleStep(resource_id_, step * 12);
Engine::Get().GetAudioMixer()->SetLoop(resource_, loop); Engine::Get().GetAudioMixer()->SetLoop(resource_id_, loop);
if (fade_in_duration > 0) if (fade_in_duration > 0)
Engine::Get().GetAudioMixer()->SetAmplitudeInc( Engine::Get().GetAudioMixer()->SetAmplitudeInc(
resource_, 1.0f / (sound_->sample_rate() * fade_in_duration)); resource_id_, 1.0f / (sound_->sample_rate() * fade_in_duration));
else else
Engine::Get().GetAudioMixer()->SetAmplitudeInc(resource_, 0); Engine::Get().GetAudioMixer()->SetAmplitudeInc(resource_id_, 0);
Engine::Get().GetAudioMixer()->Play( Engine::Get().GetAudioMixer()->Play(
resource_, sound_, fade_in_duration > 0 ? 0 : max_amplitude_, true); resource_id_, sound_, fade_in_duration > 0 ? 0 : max_amplitude_, true);
} }
void SoundPlayer::Resume(float fade_in_duration) { void SoundPlayer::Resume(float fade_in_duration) {
@ -47,8 +47,8 @@ void SoundPlayer::Resume(float fade_in_duration) {
if (fade_in_duration > 0) if (fade_in_duration > 0)
Engine::Get().GetAudioMixer()->SetAmplitudeInc( Engine::Get().GetAudioMixer()->SetAmplitudeInc(
resource_, 1.0f / (sound_->sample_rate() * fade_in_duration)); resource_id_, 1.0f / (sound_->sample_rate() * fade_in_duration));
Engine::Get().GetAudioMixer()->Play(resource_, sound_, Engine::Get().GetAudioMixer()->Play(resource_id_, sound_,
fade_in_duration > 0 ? 0 : -1, false); fade_in_duration > 0 ? 0 : -1, false);
} }
@ -58,9 +58,9 @@ void SoundPlayer::Stop(float fade_out_duration) {
if (fade_out_duration > 0) if (fade_out_duration > 0)
Engine::Get().GetAudioMixer()->SetAmplitudeInc( Engine::Get().GetAudioMixer()->SetAmplitudeInc(
resource_, -1.0f / (sound_->sample_rate() * fade_out_duration)); resource_id_, -1.0f / (sound_->sample_rate() * fade_out_duration));
else else
Engine::Get().GetAudioMixer()->Stop(resource_); Engine::Get().GetAudioMixer()->Stop(resource_id_);
} }
void SoundPlayer::SetVariate(bool variate) { void SoundPlayer::SetVariate(bool variate) {
@ -68,16 +68,16 @@ void SoundPlayer::SetVariate(bool variate) {
} }
void SoundPlayer::SetSimulateStereo(bool simulate) { void SoundPlayer::SetSimulateStereo(bool simulate) {
Engine::Get().GetAudioMixer()->SetSimulateStereo(resource_, simulate); Engine::Get().GetAudioMixer()->SetSimulateStereo(resource_id_, simulate);
} }
void SoundPlayer::SetMaxAplitude(float max_amplitude) { void SoundPlayer::SetMaxAplitude(float max_amplitude) {
max_amplitude_ = max_amplitude; max_amplitude_ = max_amplitude;
Engine::Get().GetAudioMixer()->SetMaxAmplitude(resource_, max_amplitude); Engine::Get().GetAudioMixer()->SetMaxAmplitude(resource_id_, max_amplitude);
} }
void SoundPlayer::SetEndCallback(base::Closure cb) { void SoundPlayer::SetEndCallback(base::Closure cb) {
Engine::Get().GetAudioMixer()->SetEndCallback(resource_, cb); Engine::Get().GetAudioMixer()->SetEndCallback(resource_id_, cb);
} }
} // namespace eng } // namespace eng

View File

@ -37,7 +37,7 @@ class SoundPlayer {
void SetEndCallback(base::Closure cb); void SetEndCallback(base::Closure cb);
private: private:
std::shared_ptr<void> resource_; uint64_t resource_id_ = 0;
std::shared_ptr<AudioBus> sound_; std::shared_ptr<AudioBus> sound_;
float max_amplitude_ = 1.0f; float max_amplitude_ = 1.0f;