Audio API first pass
This commit is contained in:
90
src/dawnopenal/scene/components/audio/AudioSource.cpp
Normal file
90
src/dawnopenal/scene/components/audio/AudioSource.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "AudioSource.hpp"
|
||||
#include "scene/SceneItem.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
AudioSource::AudioSource(SceneItem *i) : SceneItemComponent(i) {
|
||||
|
||||
}
|
||||
|
||||
void AudioSource::onStart() {
|
||||
alGenSources((ALuint)1, &this->source);
|
||||
|
||||
// In future these will probably be tied to the layer
|
||||
alSourcef(this->source, AL_PITCH, 1);
|
||||
// alSourcef(this->source, AL_GAIN, AL_MAX_GAIN);
|
||||
|
||||
glm::vec3 position = this->transform->getLocalPosition();
|
||||
alSource3f(this->source, AL_POSITION, position.x, position.y, position.z);
|
||||
|
||||
// alSourcei(this->source, AL_SOURCE_RELATIVE, AL_TRUE);
|
||||
// alSourcef(this->source, AL_REFERENCE_DISTANCE, 0);
|
||||
// alSourcef(this->source, AL_ROLLOFF_FACTOR, 0);
|
||||
|
||||
// Velocity is always zero for now
|
||||
alSource3f(this->source, AL_VELOCITY, 0, 0, 0);
|
||||
|
||||
// Looping
|
||||
alSourcei(this->source, AL_LOOPING, this->loop);
|
||||
|
||||
// Source
|
||||
if(this->data != nullptr && this->data->ready) {
|
||||
alSourcei(source, AL_BUFFER, this->data->buffer);
|
||||
}
|
||||
|
||||
// Playing
|
||||
if(this->playing) alSourcePlay(this->source);
|
||||
|
||||
// Listen for events
|
||||
this->transform->eventTransformUpdated.addListener(this, &AudioSource::onTransformUpdate);
|
||||
|
||||
this->ready = true;
|
||||
}
|
||||
|
||||
bool_t AudioSource::isLooping() {
|
||||
return this->loop;
|
||||
}
|
||||
|
||||
void AudioSource::setLoop(bool_t loop) {
|
||||
this->loop = loop;
|
||||
if(this->ready) alSourcei(this->source, AL_LOOPING, loop);
|
||||
}
|
||||
|
||||
bool_t AudioSource::isPlaying() {
|
||||
return this->playing;
|
||||
}
|
||||
|
||||
void AudioSource::play() {
|
||||
this->playing = true;
|
||||
if(this->ready && data != nullptr && data->ready) {
|
||||
alSourcePlay(this->source);
|
||||
}
|
||||
}
|
||||
|
||||
AudioData * AudioSource::getAudioData() {
|
||||
return this->data;
|
||||
}
|
||||
|
||||
void AudioSource::setAudioData(AudioData *data) {
|
||||
this->data = data;
|
||||
if(this->ready && data != nullptr && data->ready) {
|
||||
alSourcei(source, AL_BUFFER, data->buffer);
|
||||
}
|
||||
}
|
||||
|
||||
void AudioSource::onDispose() {
|
||||
this->transform->eventTransformUpdated.removeListener(this, &AudioSource::onTransformUpdate);
|
||||
|
||||
this->ready = false;
|
||||
alDeleteSources((ALuint)1, &this->source);
|
||||
}
|
||||
|
||||
void AudioSource::onTransformUpdate() {
|
||||
glm::vec3 position = this->transform->getWorldPosition();
|
||||
alSource3f(this->source, AL_POSITION, position.x, position.y, position.z);
|
||||
}
|
||||
Reference in New Issue
Block a user