Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
488 changes: 488 additions & 0 deletions rootex/framework/components/audio/audio_bus.cpp

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions rootex/framework/components/audio/audio_bus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once
#include <vector>
#include <string>
#include "audio_listener_component.h"
#include "entity.h"
#include "imgui.h"
#include "systems/audio_system.h"
#include "core/audio/audio_source.h"
#include "component.h"


struct AudioBus
{
std::string name;
bool isMaster;
float volume;
std::vector<AudioBus *> children;
AudioBus* parent;
};

class AudioSourceChild : public AudioSource
{
public:
ALuint m_SourceID = AudioSource::m_SourceID;
virtual float getDuration() const override;
void setVolume(int volume)
{
AudioSource::setVolume(volume);
}
};

class AudioBusCollection : public Component
{
COMPONENT(AudioBusCollection, Category::Audio);
int m_temp;

public:
AudioBusCollection(Entity& owner, const JSON::json& data);
~AudioBusCollection() = default;
static Vector<String> all_buses;
static Vector<String> all_buses_parent;
static Vector<float> all_buses_volume;
int count = 0;
void to_json(JSON::json& j, const AudioBusCollection& a);
void from_json(const JSON::json& j, AudioBusCollection& a);
void setAudioBusName(int ind, String name);
void setAudioParentBusName(int ind, String name);
void setAudioBusVolume(int ind, float volume);
//AudioBus *getAudioBusByName(String name, AudioBus *master);
//AudioBus *addAudioBus(String name,bool isMaster,float volume,AudioBus *parentBus);
void volumeChange(String name, float val);
//void setParentOfAudioBus(AudioBus *bus, AudioBus *parentBus);
void removeAudioBus(String name);
int getIndexByName(String name);
void draw();
};
DECLARE_COMPONENT(AudioBusCollection);
7 changes: 6 additions & 1 deletion rootex/framework/components/audio/audio_component.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "audio_component.h"

#include "al.h"
#include "systems/render_system.h"
#include "systems/audio_system.h"
#include "components/physics/box_collider_component.h"
#include "components/physics/sphere_collider_component.h"
#include "components/physics/capsule_collider_component.h"
Expand Down Expand Up @@ -175,5 +177,8 @@ void AudioComponent::draw()
ImGui::DragFloat("Reference Distance", &m_ReferenceDistance, 1.0f, 0.0f, 100.0f);
ImGui::DragFloat("Rolloff Factor", &m_RolloffFactor, 1.0f, 0.0f, 100.0f);
ImGui::DragFloat("Max Distance", &m_MaxDistance, 1.0f, 0.0f, 100.0f);
ImGui::DragFloat("Volume", &m_Volume, 0.01f, 0.0f, 100.0f);
if(ImGui::DragFloat("Volume", &m_Volume, 0.01f, 0.0f, 100.0f))
{
AL_CHECK(alSourcef(1, AL_GAIN, m_Volume));
}
}
1 change: 1 addition & 0 deletions rootex/framework/components/audio/audio_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class AudioComponent : public Component
void setPlaying(bool enabled);
void play();
void stop();
// void getVolume(float ratio) { m_Volume=ratio*m_Volume; }

void setLooping(bool enabled);
bool isLooping();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "audio_listener_component.h"

#include "audio_bus.h"
#include "entity.h"
#include "systems/audio_system.h"

Expand Down Expand Up @@ -90,5 +90,10 @@ RigidBodyComponent* AudioListenerComponent::getCollider()

void AudioListenerComponent::draw()
{
//AudioBusCollection obj;
//JSON::json j;
//obj.from_json(j,obj);
//obj.draw();
//obj.to_json(j,obj);
ImGui::DragFloat("Volume", &m_Volume, 0.01f, 0.001f, FLT_MAX);
}
74 changes: 74 additions & 0 deletions rootex/framework/components/audio/music_component.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "music_component.h"
#include "audio_bus.h"
#include <string>

Map<ALuint, String> MusicComponent::music_comp_audio_bus = Map<ALuint, String>();
Map<ALuint, ALfloat> MusicComponent::music_comp_volume = Map<ALuint, ALfloat>();

DEFINE_COMPONENT(MusicComponent);

Expand All @@ -15,6 +20,7 @@ MusicComponent::MusicComponent(Entity& owner, const JSON::json& data)
(ALfloat)data.value("maxDistance", 100.0f))
, m_AudioFile(ResourceLoader::CreateAudioResourceFile(data.value("audio", "rootex/assets/ball.wav")))
{
volume_comp = data.value("volume", 1.0f);
}

MusicComponent::~MusicComponent()
Expand All @@ -33,6 +39,54 @@ bool MusicComponent::setupData()
return AudioComponent::setupData();
}

void MusicComponent::to_json(JSON::json& j, const MusicComponent& a)
{
// write the code
j["audioBusName"] = JSON::json::array();
auto itr = music_comp_audio_bus.begin();
while (itr != music_comp_audio_bus.end())
{
j["audioBusName"].push_back(itr->second);
itr++;
}
j["ComponentPointer"] = JSON::json::array();
itr = music_comp_audio_bus.begin();
while (itr != music_comp_audio_bus.end())
{
auto val = itr->first;
j["ComponentPointer"].emplace_back(itr->first);
itr++;
}
j["ComponentVolume"] = JSON::json::array();
auto itr2 = music_comp_volume.begin();
while (itr2 != music_comp_volume.end())
{
j["ComponentPointer"].push_back(itr2->second);
itr2++;
}
}

void MusicComponent::from_json(const JSON::json& j, MusicComponent& a)
{
Vector<String> temp;
Vector<float> temp2;
for (auto& name : j.value("audioBusName", Vector<String>()))
{
temp.push_back(name);
}
for (auto& vol : j.value("ComponentVolume", Vector<float>()))
{
temp2.push_back(vol);
}
int i = 0;
for (auto& bus : j.value("ComponentPointer", Vector<ALuint>()))
{
MusicComponent::music_comp_audio_bus.insert({ bus, temp[i] });
MusicComponent::music_comp_volume.insert({ bus, temp2[i] });
i++;
}
}

JSON::json MusicComponent::getJSON() const
{
JSON::json& j = AudioComponent::getJSON();
Expand All @@ -49,8 +103,14 @@ void MusicComponent::setAudioFile(Ref<AudioResourceFile> audioFile)
setupData();
}

// void MusicComponent::updateVolume(float ratio)
// {
// getVolume(ratio);
// }

void MusicComponent::draw()
{
AudioComponent::getAudioSource()->setVolume(volume_comp);
ImGui::BeginGroup();
ImGui::Text("%s", m_AudioFile->getPath().generic_string().c_str());
ImGui::SameLine();
Expand All @@ -75,5 +135,19 @@ void MusicComponent::draw()
setAudioFile(ResourceLoader::CreateAudioResourceFile(path));
}
}
if (ImGui::ListBoxHeader("Choose an audio bus"))
{
bool bl = false;
auto component_pointer = m_StreamingAudioSource.get()->getSourceID();
for (int j = 0; j < AudioBusCollection::all_buses.size(); j++)
{
if (ImGui::RadioButton(AudioBusCollection::all_buses[j].c_str(), bl))
{
music_comp_audio_bus.insert({component_pointer, AudioBusCollection::all_buses[j]});
music_comp_volume.insert({ component_pointer, volume_comp });
}
}
ImGui::ListBoxFooter();
}
AudioComponent::draw();
}
7 changes: 6 additions & 1 deletion rootex/framework/components/audio/music_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ class MusicComponent : public AudioComponent
{
COMPONENT(MusicComponent, Category::Audio);

ALfloat volume_comp;
Ref<StreamingAudioSource> m_StreamingAudioSource;
Ref<StreamingAudioBuffer> m_StreamingAudioBuffer;
Ref<AudioResourceFile> m_AudioFile;

public:
MusicComponent(Entity& owner, const JSON::json& data);
~MusicComponent();

static Map<ALuint, String> music_comp_audio_bus;
static Map<ALuint,ALfloat > music_comp_volume;
AudioResourceFile* getAudioFile() const { return m_AudioFile.get(); }
void to_json(JSON::json& j, const MusicComponent& a);
void from_json(const JSON::json& j, MusicComponent& a);
void setAudioFile(Ref<AudioResourceFile> audioFile);
// void updateVolume(float ratio);

bool setupData() override;
JSON::json getJSON() const override;
Expand Down
3 changes: 2 additions & 1 deletion rootex/framework/components/component_ids.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#pragma once

enum class ComponentIDs : unsigned int
{
Expand All @@ -24,6 +24,7 @@ enum class ComponentIDs : unsigned int
MusicComponent,
ShortMusicComponent,
AudioListenerComponent,
AudioBusCollection,
TransformAnimationComponent,
SkyComponent,
FogComponent,
Expand Down
3 changes: 3 additions & 0 deletions rootex/framework/ecs_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "scene.h"
#include "components/audio/audio_listener_component.h"
#include "components/audio/audio_bus.h"
#include "components/audio/music_component.h"
#include "components/physics/box_collider_component.h"
#include "components/physics/sphere_collider_component.h"
Expand Down Expand Up @@ -106,6 +107,7 @@ void ECSFactory::FillRootEntity(Entity& root)
root.getComponent<ModelComponent>()->setVisible(false);
ECSFactory::AddDefaultCameraComponent(root);
ECSFactory::AddDefaultAudioListenerComponent(root);
//ECSFactory::AddDefaultAudioBusCollection(root);
ECSFactory::AddDefaultSkyComponent(root);
}

Expand All @@ -118,6 +120,7 @@ void ECSFactory::Initialize()
ASSIGN_COMPONENT_SET(TransformAnimationComponent);

ASSIGN_COMPONENT_SET(AudioListenerComponent);
ASSIGN_COMPONENT_SET(AudioBusCollection);
ASSIGN_COMPONENT_SET(MusicComponent);
ASSIGN_COMPONENT_SET(ShortMusicComponent);

Expand Down