118 lines
3.2 KiB
C++
118 lines
3.2 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 "asset/assets/AudioAsset.hpp"
|
|
|
|
#define AUDIO_SOURCE_BUFFER_COUNT 3
|
|
#define AUDIO_SOURCE_BUFFER_SIZE 8192
|
|
|
|
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;
|
|
ALuint buffers[AUDIO_SOURCE_BUFFER_COUNT];
|
|
size_t bufferPosition = 0;
|
|
|
|
// Settings
|
|
enum AudioSourceState internalState = AUDIO_SOURCE_STATE_STOPPED;
|
|
int32_t layer = 0;
|
|
AudioAsset *data = nullptr;
|
|
|
|
/**
|
|
* Internally update the audio's state based on the current settings.
|
|
*/
|
|
void updateAudioSource();
|
|
|
|
void fillBuffer(ALuint buffer);
|
|
void detatchBuffers();
|
|
void attachBuffers();
|
|
|
|
// Events
|
|
void onSceneUpdate();
|
|
|
|
public:
|
|
bool_t loop = false;
|
|
enum AudioSourceState state = AUDIO_SOURCE_STATE_STOPPED;
|
|
AudioSourcePlayMode playMode;
|
|
|
|
Event<> eventPlaying;
|
|
Event<> eventResumed;
|
|
Event<> eventPaused;
|
|
Event<> eventStopped;
|
|
Event<> eventFinished;
|
|
Event<> eventLooped;
|
|
|
|
/**
|
|
* Creates an Audio Source item component.
|
|
*
|
|
* @param item SceneItem that this audio source is attached to.
|
|
*/
|
|
AudioSource(SceneItem *item);
|
|
|
|
/**
|
|
* 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();
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
AudioAsset * 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(AudioAsset *data);
|
|
|
|
/**
|
|
* Stop playing this audio source. Shorthand for setting the state.
|
|
*/
|
|
void stop();
|
|
|
|
/**
|
|
* Play this audio source. Shorthand for state setting.
|
|
*/
|
|
void play();
|
|
|
|
/**
|
|
* Pause this audio source. Shorthand for state setting.
|
|
*/
|
|
void pause();
|
|
|
|
void onStart() override;
|
|
void onDispose() override;
|
|
};
|
|
} |