mirror of
https://github.com/antopilo/Nuake.git
synced 2026-09-21 20:08:40 +03:00
Can now trigger audio emitters through scripts
This commit is contained in:
4
Editor/resources/Scripts/Audio.wren
Normal file
4
Editor/resources/Scripts/Audio.wren
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
class Audio {
|
||||||
|
foreign static PlayAudio(audioFile)
|
||||||
|
}
|
||||||
@@ -36,6 +36,8 @@ class Scene {
|
|||||||
return Trigger.new(id)
|
return Trigger.new(id)
|
||||||
} else if (component == "Brush") {
|
} else if (component == "Brush") {
|
||||||
return Brush.new(id)
|
return Brush.new(id)
|
||||||
|
} else if (component == "AudioEmitter") {
|
||||||
|
return AudioEmitter.new(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,6 +70,9 @@ class Scene {
|
|||||||
//foreign static SetcameraFov(e, fov)
|
//foreign static SetcameraFov(e, fov)
|
||||||
//foreign static GetCameraFov(e) // returns a float
|
//foreign static GetCameraFov(e) // returns a float
|
||||||
|
|
||||||
|
// Audio Emitter
|
||||||
|
foreign static SetAudioEmitterPlaying_(e, playing)
|
||||||
|
|
||||||
// Character controller
|
// Character controller
|
||||||
foreign static MoveAndSlide_(e, x, y, z)
|
foreign static MoveAndSlide_(e, x, y, z)
|
||||||
foreign static IsCharacterControllerOnGround_(e)
|
foreign static IsCharacterControllerOnGround_(e)
|
||||||
@@ -225,7 +230,16 @@ class Camera {
|
|||||||
var dir = Scene.GetCameraRight_(_entityId)
|
var dir = Scene.GetCameraRight_(_entityId)
|
||||||
return Vector3.new(dir[0], dir[1], dir[2])
|
return Vector3.new(dir[0], dir[1], dir[2])
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class AudioEmitter {
|
||||||
|
construct new(id) {
|
||||||
|
_entityId = id
|
||||||
|
}
|
||||||
|
|
||||||
|
SetPlaying(playing) {
|
||||||
|
Scene.SetAudioEmitterPlaying_(_entityId, playing)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Trigger {
|
class Trigger {
|
||||||
|
|||||||
@@ -44,8 +44,6 @@ namespace Nuake {
|
|||||||
|
|
||||||
void AudioManager::SetListenerPosition(const Vector3& position, const Vector3& direction, const Vector3& up)
|
void AudioManager::SetListenerPosition(const Vector3& position, const Vector3& direction, const Vector3& up)
|
||||||
{
|
{
|
||||||
const std::lock_guard<std::mutex> lock(m_AudioQueueMutex);
|
|
||||||
|
|
||||||
if (position != m_ListenerPosition || direction != m_ListenerDirection || up != m_ListenerUp)
|
if (position != m_ListenerPosition || direction != m_ListenerDirection || up != m_ListenerUp)
|
||||||
{
|
{
|
||||||
m_ListenerPosition = position;
|
m_ListenerPosition = position;
|
||||||
@@ -96,13 +94,20 @@ namespace Nuake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SoLoud::handle& voice = m_ActiveClips[request.audioFile];
|
SoLoud::handle& voice = m_ActiveClips[request.audioFile];
|
||||||
m_Soloud->set3dSourcePosition(voice, request.position.x, request.position.y, request.position.z);
|
|
||||||
|
if (request.spatialized)
|
||||||
|
{
|
||||||
|
m_Soloud->set3dSourcePosition(voice, request.position.x, request.position.y, request.position.z);
|
||||||
|
m_Soloud->set3dSourceMinMaxDistance(voice, request.MinDistance, request.MaxDistance);
|
||||||
|
m_Soloud->set3dSourceAttenuation(voice, 1, request.AttenuationFactor);
|
||||||
|
m_Soloud->update3dAudio();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_Soloud->setPan(voice, request.pan);
|
||||||
|
}
|
||||||
m_Soloud->setLooping(voice, request.Loop);
|
m_Soloud->setLooping(voice, request.Loop);
|
||||||
m_Soloud->set3dSourceMinMaxDistance(voice, request.MinDistance, request.MaxDistance);
|
|
||||||
m_Soloud->set3dSourceAttenuation(voice, 1, request.AttenuationFactor);
|
|
||||||
m_Soloud->setRelativePlaySpeed(voice, request.speed);
|
m_Soloud->setRelativePlaySpeed(voice, request.speed);
|
||||||
m_Soloud->setPan(voice, request.pan);
|
|
||||||
m_Soloud->update3dAudio();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioManager::StopVoice(const std::string& filePath)
|
void AudioManager::StopVoice(const std::string& filePath)
|
||||||
@@ -111,6 +116,7 @@ namespace Nuake {
|
|||||||
|
|
||||||
if (!IsVoiceActive(filePath))
|
if (!IsVoiceActive(filePath))
|
||||||
{
|
{
|
||||||
|
// We can't stop a voice that isn't active.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,6 +124,11 @@ namespace Nuake {
|
|||||||
m_Soloud->stop(voice);
|
m_Soloud->stop(voice);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AudioManager::StopAll() const
|
||||||
|
{
|
||||||
|
m_Soloud->stopAll();
|
||||||
|
}
|
||||||
|
|
||||||
bool AudioManager::IsWavLoaded(const std::string& filePath) const
|
bool AudioManager::IsWavLoaded(const std::string& filePath) const
|
||||||
{
|
{
|
||||||
return m_WavSamples.find(filePath) != m_WavSamples.end();
|
return m_WavSamples.find(filePath) != m_WavSamples.end();
|
||||||
@@ -136,15 +147,11 @@ namespace Nuake {
|
|||||||
|
|
||||||
void AudioManager::AudioThreadLoop()
|
void AudioManager::AudioThreadLoop()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
while(m_AudioThreadRunning)
|
while(m_AudioThreadRunning)
|
||||||
{
|
{
|
||||||
// Acquire mutex lock
|
// Acquire mutex lock
|
||||||
const std::lock_guard<std::mutex> lock(m_AudioQueueMutex);
|
const std::lock_guard<std::mutex> lock(m_AudioQueueMutex);
|
||||||
|
|
||||||
//CleanupInactiveVoices();
|
|
||||||
|
|
||||||
// Check if we have audio queued
|
// Check if we have audio queued
|
||||||
while (!m_AudioQueue.empty())
|
while (!m_AudioQueue.empty())
|
||||||
{
|
{
|
||||||
@@ -157,18 +164,18 @@ namespace Nuake {
|
|||||||
soloudHandle = m_Soloud->play(audio);
|
soloudHandle = m_Soloud->play(audio);
|
||||||
m_Soloud->setVolume(soloudHandle, audioRequest.volume);
|
m_Soloud->setVolume(soloudHandle, audioRequest.volume);
|
||||||
m_Soloud->setPan(soloudHandle, audioRequest.pan);
|
m_Soloud->setPan(soloudHandle, audioRequest.pan);
|
||||||
m_Soloud->setRelativePlaySpeed(soloudHandle, audioRequest.speed);
|
|
||||||
m_Soloud->setLooping(soloudHandle, audioRequest.Loop);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const Vector3& position = audioRequest.position;
|
const Vector3& position = audioRequest.position;
|
||||||
soloudHandle = m_Soloud->play3d(audio, position.x, position.y, position.z);
|
soloudHandle = m_Soloud->play3d(audio, position.x, position.y, position.z);
|
||||||
m_Soloud->set3dSourceMinMaxDistance(soloudHandle, audioRequest.MinDistance, audioRequest.MaxDistance);
|
m_Soloud->set3dSourceMinMaxDistance(soloudHandle, audioRequest.MinDistance, audioRequest.MaxDistance);
|
||||||
m_Soloud->set3dSourceAttenuation(soloudHandle, 1, audioRequest.AttenuationFactor);
|
m_Soloud->set3dSourceAttenuation(soloudHandle, SoLoud::AudioSource::ATTENUATION_MODELS::EXPONENTIAL_DISTANCE, audioRequest.AttenuationFactor);
|
||||||
m_Soloud->setLooping(soloudHandle, audioRequest.Loop);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_Soloud->setRelativePlaySpeed(soloudHandle, audioRequest.speed);
|
||||||
|
m_Soloud->setLooping(soloudHandle, audioRequest.Loop);
|
||||||
|
|
||||||
m_ActiveClips[audioRequest.audioFile] = soloudHandle;
|
m_ActiveClips[audioRequest.audioFile] = soloudHandle;
|
||||||
|
|
||||||
// Remove item from queue.
|
// Remove item from queue.
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ namespace Nuake
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
const int MAX_VOICE_COUNT = 32;
|
const int MAX_VOICE_COUNT = 32;
|
||||||
|
float m_GlobalVolume = 1.0f;
|
||||||
|
|
||||||
Ref<SoLoud::Soloud> m_Soloud;
|
Ref<SoLoud::Soloud> m_Soloud;
|
||||||
|
|
||||||
@@ -75,9 +76,12 @@ namespace Nuake
|
|||||||
|
|
||||||
void PlayTTS(const std::string& text);
|
void PlayTTS(const std::string& text);
|
||||||
|
|
||||||
|
float GetGlobalVolume() const { return m_GlobalVolume; }
|
||||||
|
|
||||||
void QueueWavAudio(const AudioRequest& request);
|
void QueueWavAudio(const AudioRequest& request);
|
||||||
void UpdateVoice(const AudioRequest & request);
|
void UpdateVoice(const AudioRequest & request);
|
||||||
void StopVoice(const std::string& filePath);
|
void StopVoice(const std::string& filePath);
|
||||||
|
void StopAll() const;
|
||||||
bool IsWavLoaded(const std::string& filePath) const;
|
bool IsWavLoaded(const std::string& filePath) const;
|
||||||
bool IsVoiceActive(const std::string & voice) const;
|
bool IsVoiceActive(const std::string & voice) const;
|
||||||
void LoadWavAudio(const std::string& filePath);
|
void LoadWavAudio(const std::string& filePath);
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ namespace Nuake
|
|||||||
|
|
||||||
bool AudioSystem::Init()
|
bool AudioSystem::Init()
|
||||||
{
|
{
|
||||||
|
AudioManager::Get().StopAll();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +78,8 @@ namespace Nuake
|
|||||||
audioManager.UpdateVoice(audioRequest);
|
audioManager.UpdateVoice(audioRequest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (isPlaying)
|
|
||||||
|
if (isPlaying)
|
||||||
{
|
{
|
||||||
// Reset the play status to false since the audio has been fired
|
// Reset the play status to false since the audio has been fired
|
||||||
if (!audioEmitterComponent.Loop)
|
if (!audioEmitterComponent.Loop)
|
||||||
@@ -104,6 +107,6 @@ namespace Nuake
|
|||||||
|
|
||||||
void AudioSystem::Exit()
|
void AudioSystem::Exit()
|
||||||
{
|
{
|
||||||
|
AudioManager::Get().StopAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
#include <src/Scene/Components/TriggerZone.h>
|
#include <src/Scene/Components/TriggerZone.h>
|
||||||
#include <src/Scene/Components/BSPBrushComponent.h>
|
#include <src/Scene/Components/BSPBrushComponent.h>
|
||||||
#include <src/Scene/Components/PrefabComponent.h>
|
#include <src/Scene/Components/PrefabComponent.h>
|
||||||
|
#include <src/Scene/Components/AudioEmitterComponent.h>
|
||||||
|
|
||||||
namespace Nuake
|
namespace Nuake
|
||||||
{
|
{
|
||||||
@@ -57,6 +58,8 @@ namespace Nuake
|
|||||||
RegisterMethod("GetCameraDirection_(_)", (void*)GetCameraDirection);
|
RegisterMethod("GetCameraDirection_(_)", (void*)GetCameraDirection);
|
||||||
RegisterMethod("GetCameraRight_(_)", (void*)GetCameraRight);
|
RegisterMethod("GetCameraRight_(_)", (void*)GetCameraRight);
|
||||||
|
|
||||||
|
RegisterMethod("SetAudioEmitterPlaying_(_,_)", (void*)SetAudioEmitterPlaying);
|
||||||
|
|
||||||
RegisterMethod("MoveAndSlide_(_,_,_,_)", (void*)MoveAndSlide);
|
RegisterMethod("MoveAndSlide_(_,_,_,_)", (void*)MoveAndSlide);
|
||||||
RegisterMethod("IsCharacterControllerOnGround_(_)", (void*)IsCharacterControllerOnGround);
|
RegisterMethod("IsCharacterControllerOnGround_(_)", (void*)IsCharacterControllerOnGround);
|
||||||
RegisterMethod("RayCast_(_,_,_,_,_,_)", (void*)Raycast);
|
RegisterMethod("RayCast_(_,_,_,_,_,_)", (void*)Raycast);
|
||||||
@@ -151,6 +154,9 @@ namespace Nuake
|
|||||||
if (name == "Brush") {
|
if (name == "Brush") {
|
||||||
result = ent.HasComponent<BSPBrushComponent>();
|
result = ent.HasComponent<BSPBrushComponent>();
|
||||||
}
|
}
|
||||||
|
if (name == "AudioEmitter") {
|
||||||
|
result = ent.HasComponent<AudioEmitterComponent>();
|
||||||
|
}
|
||||||
|
|
||||||
wrenSetSlotBool(vm, 0, result);
|
wrenSetSlotBool(vm, 0, result);
|
||||||
}
|
}
|
||||||
@@ -242,6 +248,17 @@ namespace Nuake
|
|||||||
wrenInsertInList(vm, 0, 2, 3);
|
wrenInsertInList(vm, 0, 2, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void SetAudioEmitterPlaying(WrenVM* vm)
|
||||||
|
{
|
||||||
|
double handle = wrenGetSlotDouble(vm, 1);
|
||||||
|
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
|
||||||
|
|
||||||
|
auto& audioEmitter = ent.GetComponent<AudioEmitterComponent>();
|
||||||
|
|
||||||
|
bool isPlaying = wrenGetSlotBool(vm, 2);
|
||||||
|
audioEmitter.IsPlaying = isPlaying;
|
||||||
|
}
|
||||||
|
|
||||||
static void IsCharacterControllerOnGround(WrenVM* vm)
|
static void IsCharacterControllerOnGround(WrenVM* vm)
|
||||||
{
|
{
|
||||||
double handle = wrenGetSlotDouble(vm, 1);
|
double handle = wrenGetSlotDouble(vm, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user