Skip to content

Commit

Permalink
Remove audio examples. Create a <non working yet> example of spatial …
Browse files Browse the repository at this point in the history
…audio + renderer example
  • Loading branch information
MagrosThornrime committed Jan 2, 2025
1 parent cc82df2 commit 9738c70
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 12 deletions.
116 changes: 116 additions & 0 deletions archimedes_bin/examples/SpatialAudioTestApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#pragma once

#include <cmath>
#include <mutex>
#include <thread>

#include <Ecs.h>
#include <Engine.h>
#include <Scene.h>
#include <audio/AudioManager.h>
#include <audio/SoundDevice.h>

namespace audio = arch::audio;
namespace ecs = arch::ecs;
namespace scene = arch::scene;



class SpatialAudioTestApp: public arch::Application {

const std::string _soundFile = "./sounds/wind.mp3";

//initialize OpenAL context
audio::SoundDevice _device;

//initialize SoundBank
audio::SoundBank _soundBank;

//AudioManager should be initialized after launching the app
std::mutex _mutex;
audio::AudioManager* _audioManager{};
std::jthread* _audioThread{};

// counter variable using in sound's source's movement calculation
int _currentStep{};

void init() override {
//initialize test scene
arch::Ref<scene::Scene> testScene = arch::createRef<scene::Scene>();

//initialize AudioManager
ecs::Domain* domain = &testScene->domain();
_audioManager = new audio::AudioManager(&_soundBank, domain, _mutex);
_audioThread = new std::jthread(&audio::AudioManager::play, &_audioManager);

//add a sound to SoundBank and load it
_soundBank.addClip(_soundFile);
_soundBank.loadInitialGroups();

// add a "listening" triangle which will be in center of the scene
// it resembles the Listener of the sound
{
ecs::Entity e = testScene->newEntity();
testScene->domain().addComponent<scene::components::TransformComponent>(
e,
{
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f },
{ .5f, .5f, .5f },
}
);
testScene->domain().addComponent<scene::components::MeshComponent>(e, { /*mesh*/ });
}

// add a "source" triangle - this resembles the sound's source
// it will be moved while moving the source
{
ecs::Entity e = testScene->newEntity();
testScene->domain().addComponent<scene::components::TransformComponent>(
e,
{
{ 0.0f, 2.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f },
arch::float3(0.2f)
}
);
testScene->domain().addComponent<scene::components::MeshComponent>(e, { /*mesh*/ });
auto source = &testScene->domain().addComponent<audio::AudioSource>(e);
source->path = _soundFile;
source->gain = 0.5f;
source->isLooped = true;
source->positionX = 0.0f;
source->positionY = 2.0f;
source->play();
}

scene::SceneManager::get()->changeScene(testScene);
}

void update() override {
auto view = scene::SceneManager::get()
->currentScene()
->domain()
.view<scene::components::TransformComponent, audio::AudioSource>();

for (auto [entity, transform, audioSource] : view.all()) {
// tells how many steps a triangle needs to take to do a full circle
const int stepsPerCircle = 100;
const float distance = 2.0f + 2.0f * std::sin(_currentStep * 2 * std::numbers::pi / stepsPerCircle);
float positionX = distance * std::cos(_currentStep * 2 * std::numbers::pi / stepsPerCircle);
float positionY = distance * std::sin(_currentStep * 2 * std::numbers::pi / stepsPerCircle);
{
auto lock = std::lock_guard(_mutex);
audioSource.positionX = positionX;
audioSource.positionY = positionY;
}
transform.position = {positionX, positionY, 0.0f};
}
_currentStep++;
}

~SpatialAudioTestApp() override {
delete _audioManager;
}

};
17 changes: 5 additions & 12 deletions archimedes_bin/main.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
#include "examples/NvrhiRendererTestApp.h"
#include "examples/SpatialAudioTestApp.h"
#include <Engine.h>
#include <Logger.h>
#include <audio/AudioManager.h>
#include <audio/SoundDevice.h>

const std::string sounds = "/home/anon/dev/archimedes/archimedes_bin/sounds/";

namespace audio = arch::audio;
namespace ecs = arch::ecs;

int main() {
arch::Logger::init(arch::LogLevel::trace);

arch::Ref<arch::Application> myApp = arch::createRef<NvrhiRendererTestApp>();
arch::Ref<arch::Application> myApp = arch::createRef<SpatialAudioTestApp>();

arch::EngineConfig config{ .windowWidth = 600,
.windowHeight = 480,
.windowTitle = "Archimedes Test",
.backgroundColor = arch::Color(.03f, .03f, .03, 1.f),
.renderingApi = arch::gfx::RenderingAPI::Nvrhi_VK };

// testSimpleSound(domain);
// testControl(domain);
testSpatialAudio(domain);

// arch::Ref<MyApp> myApp = arch::createRef<MyApp>();
//
// arch::Engine engine { config, myApp };
// engine.start();

arch::Engine engine { config, myApp };
engine.start();
}

0 comments on commit 9738c70

Please sign in to comment.