Skip to content

Commit

Permalink
Update audio engine
Browse files Browse the repository at this point in the history
Provides a more backend-independent audio system
Also fixes some sounds being played at half speed (chapter introductions)
  • Loading branch information
frabert committed Sep 12, 2018
1 parent 4281dc8 commit aea0e26
Show file tree
Hide file tree
Showing 18 changed files with 926 additions and 621 deletions.
71 changes: 0 additions & 71 deletions src/audio/AudioEngine.cpp

This file was deleted.

213 changes: 161 additions & 52 deletions src/audio/AudioEngine.h
Original file line number Diff line number Diff line change
@@ -1,66 +1,175 @@
#pragma once

#include <string>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <vector>

#include <glm/glm.hpp>

typedef struct ALCdevice_struct ALCdevice;
#include <math/mathlib.h>

namespace Audio
{
class AudioWorld;
enum class State {
Stopped,
Paused,
Playing
};

/** The AudioEngine represents the target OS sound system.
*
* The engine class matches the OpenAL device level.
enum class Format {
Mono,
Stereo
};

/**
* @brief Sounds represent audio sources.
*
* All of the information about a sound should be re-set before it is played
* after it had been stopped.
*
*/
class Sound {
public:
/**
* @brief Set sound pitch, must be positive
*
* 1 means original pitch, values < 1 mean lower, > 1 mean higher.
*
* @return float
*/
virtual void pitch(float) = 0;

/**
* @brief Set sound gain
*
* 1 means original volume, <1 means lower gain, >1 means higher.
*
* @return float
*/
virtual void gain(float) = 0;

/**
* @brief Set the maximum distance from which this sound can be heard.
* Must be positive.
*
*/
virtual void maxDistance(float) = 0;

/**
* @brief Set the coordinates of the sound's position.
*
*/
virtual void position(const Math::float3&) = 0;

/**
* @brief Set the components of the sound's velocity.
*
*/
virtual void velocity(const Math::float3&) = 0;

/**
* @brief Set the componenets of the sound's direction.
*
*/
virtual void direction(const Math::float3&) = 0;

/**
* @brief Set whether the sound's position is relative to the listener.
*
*/
virtual void relative(bool) = 0;

/**
* @brief Set wheter the sound should loop back to the beginning once it
* finishes playing.
*
* This is ignored in the case of streming sounds
*/
virtual void looping(bool) = 0;

/**
* @brief Get the playback state of this sound.
*
* @return State
*/
virtual State state() = 0;

/**
* @brief Starts sound playback.
*
* This method is idempotent: if the sound was already playing,
* nothing happens.
*/
virtual void play() = 0;
/**
* @brief Pauses sound playback.
*
* This method is idempotent: if the sound was already paused,
* nothing happens.
*/
virtual void pause() = 0;
/**
* @brief Stops sound playback.
*
* Playback will resume from the beginning.
* This method is idempotent: if the sound was already stopped,
* nothing happens.
*/
virtual void stop() = 0;
};

using SoundRef = std::shared_ptr<Sound>;
using SoundStream = std::function<int(std::int16_t* buf, std::size_t size)>;

struct Orientation {
Math::float3 at;
Math::float3 up;
};

/** An AudioEngine is the interface to the system's audio driver
*
*/
class AudioEngine final
class AudioEngine
{
public:
/** Initializes the AudioEngine.
*
* The @p device param specifies the device to use and can be determined
* using enumerateDevices().
*
* @param device_name The name of the device to use.
*
* @see AudioEngine::enumerateDevices()
*
*/
AudioEngine(const std::string& device_name = std::string());

/** Deinitializes the AudioEngine.
*/
~AudioEngine();

/** Returns the OpenAL device used by this engine.
*
* @return The OpenAL device or nullptr when initialization has failed.
*/
ALCdevice* getDevice() const { return m_Device; }
/** Enumerates the audio devices available on the current machine.
*
* Note that not all OpenAL implementations support enumeration. In this case you'll
* retrieve a single element list with an empty device name, indicating that it is
* the default device.
*
* @param[out] devices A list of available devices.
*
*/
static void enumerateDevices(std::vector<std::string>& devices);

/** Returns a text representation of an error code.
*
* @param The error code returned by alGetError() or alcGetError().
*
* @return The error text.
*
*/
static const char* getErrorString(size_t errorCode);

private:
ALCdevice* m_Device = nullptr;
/**
* @brief Set master listener gain, must be positive
*
* 1 means original volume, <1 means lower gain, >1 means higher.
*
*/
virtual void gain(float) = 0;

/**
* @brief Set coordinates of listener's position
*
*/
virtual void position(const Math::float3&) = 0;

/**
* @brief Set components of listener's velocity
*
*/
virtual void velocity(const Math::float3&) = 0;

/**
* @brief Set listener's orientation
*
*/
virtual void orientation(const Orientation&) = 0;

/**
* @brief Creates a sound object from an audio buffer
*
* @return SoundRef
*/
virtual SoundRef createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t samplingFreq) = 0;

/**
* @brief Creates a sound object from an audio stream
*
* @return SoundRef
*/
virtual SoundRef createSound(SoundStream, Format, std::size_t samplingFreq) = 0;
};
}
Loading

0 comments on commit aea0e26

Please sign in to comment.