Dawn/src/dawnopenal/scene/components/audio/AudioSource.hpp
2023-01-17 11:53:34 -08:00

153 lines
4.0 KiB
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnopenal.hpp"
#include "scene/SceneItemComponent.hpp"
#include "audio/AudioData.hpp"
namespace Dawn {
enum AudioSourceState {
AUDIO_SOURCE_STATE_STOPPED,
AUDIO_SOURCE_STATE_PLAYING,
AUDIO_SOURCE_STATE_PAUSED
};
enum AudioSourcePlayMode {
AUDIO_PLAY_MODE_ALWAYS,
AUDIO_PLAY_MODE_UNPAUSED
};
class AudioSource : public SceneItemComponent {
private:
ALuint source;
bool_t ready = false;
// Settings
bool_t loop = false;
enum AudioSourceState requestedAudioState = AUDIO_SOURCE_STATE_STOPPED;
enum AudioSourceState currentAudioState = AUDIO_SOURCE_STATE_STOPPED;
int32_t layer = 0;
AudioData *data = nullptr;
AudioSourcePlayMode playMode = AUDIO_PLAY_MODE_UNPAUSED;
/**
* Internally update the audio's state based on the current settings.
*/
void updateAudioSource();
// Events
void onTransformUpdate();
void onGamePause();
void onGameUnpause();
public:
Event<> eventPlaying;
Event<> eventPaused;
Event<> eventStopped;
/**
* Creates an Audio Source item component.
*
* @param item SceneItem that this audio source is attached to.
*/
AudioSource(SceneItem *item);
/**
* Returns whether or not the audio source is set to loop or not.
*
* @return True if the source is looping otherwise false.
*/
bool_t isLooping();
/**
* Sets whether the audio source should loop or not.
*
* @param loop Loop or not.
*/
void setLoop(bool_t loop);
/**
* Returns the requested playing state of the source. Due to some weird
* limitations of hardware, as well as some nice controls to decide how
* sources should play, this is basically "What you would like the sound
* to do" mode, so you really should be controlling this value where
* possible.
*
* @return The requested playing state.
*/
AudioSourceState getPlayingState();
/**
* Sets the ideal playing state you want of this audio source.
*
* @param state State to set.
*/
void setPlayingState(enum AudioSourceState state);
/**
* Returns the real current state of the sound. Refer to getPlayingState
* but basically the actually requested play state and what the hardware
* and internal logic is doing may result in the actual state being a
* little different. You cannot control this, but you can read it.
*
* @return The actual current playing state.
*/
AudioSourceState getRealState();
/**
* Plays the audio source.
*/
void play();
/**
* Pauses the audio source.
*/
void pause();
/**
* Stops the audio source.
*/
void stop();
/**
* Rewinds the audio source. At the time of writing this comment (23017) I
* am unsure of the behaviour when rewinding whilst the source is active.
*/
// void rewind();
/**
* Returns the current audio data for this source.
*
* @return Audio data that is atached to this source.
*/
AudioData * getAudioData();
/**
* Sets the audio data for this source. Currently switching between the
* audio data during playback may have undefined behavior.
*
* @param data Data to set.
*/
void setAudioData(AudioData *data);
/**
* Returns the play mode for the source.
*
* @return Current play mode.
*/
enum AudioSourcePlayMode getPlayMode();
/**
* Sets the play mode for the source.
*
* @param mode Play Mode to use.
*/
void setPlayMode(enum AudioSourcePlayMode mode);
void onStart() override;
void onDispose() override;
};
}