101 lines
3.7 KiB
C++
101 lines
3.7 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
#pragma once
|
|
|
|
#include "container.hpp"
|
|
#include "mixer.hpp"
|
|
#include "semantic_cues.hpp"
|
|
#include "original.hpp"
|
|
|
|
#include <SDL.h>
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <string>
|
|
|
|
namespace openbmh::audio {
|
|
|
|
struct CueRequest {
|
|
std::uint64_t session_id{};
|
|
std::uint64_t event_id{};
|
|
std::uint8_t original_id{};
|
|
};
|
|
|
|
struct SemanticCueRequest {
|
|
std::uint64_t session_id{};
|
|
std::uint64_t event_id{};
|
|
SemanticCue cue{};
|
|
};
|
|
|
|
enum class CueStatus { queued, muted, disabled, duplicate, unsupported, invalid };
|
|
|
|
class Playback {
|
|
public:
|
|
Playback() = default;
|
|
~Playback();
|
|
Playback(const Playback&) = delete;
|
|
Playback& operator=(const Playback&) = delete;
|
|
|
|
bool initialize(std::string& error);
|
|
// Full Original driver, streamed by SDL's audio callback so synchronous
|
|
// simulation and modal windows cannot starve music or the crowd loop.
|
|
bool initialize_original(OriginalAssets assets, std::string& error);
|
|
bool original_ready() const noexcept;
|
|
void set_original_options(OriginalAudioOptions options) noexcept;
|
|
OriginalAudioStats original_stats() const noexcept;
|
|
void shutdown();
|
|
bool available() const noexcept { return device_ != 0; }
|
|
void set_muted(bool muted) noexcept;
|
|
bool muted() const noexcept { return muted_; }
|
|
std::uint32_t queued_bytes() const noexcept;
|
|
void set_volume(float volume) noexcept;
|
|
float volume() const noexcept { return volume_; }
|
|
void stop();
|
|
|
|
// Optional software music. It is off by default. pump() adds at most two
|
|
// bounded blocks of up to 20 ms; total queued audio stays at or below 40 ms.
|
|
// Music requires 22050 Hz and a device block no larger than 256 frames.
|
|
bool start_music_loop(const PcmSample& loop, std::string& error);
|
|
void stop_music() noexcept;
|
|
bool music_active() const noexcept { return mixer_.has_music(); }
|
|
bool needs_pump() const noexcept { return !original_mode_ && mixer_.active(); }
|
|
bool pump(std::string& error);
|
|
|
|
// The controller supplies stable session/event identities. IDs are not
|
|
// generated by audio; requests are consumed once in increasing event order.
|
|
void reset_session(std::uint64_t session_id) noexcept;
|
|
CueStatus submit(CueRequest request, std::string& detail);
|
|
CueStatus submit(SemanticCueRequest request, std::string& detail);
|
|
|
|
// Legacy PCM-only bank loader. initialize_original provides the complete
|
|
// sequencer and instrument path for songs and all effects 10..22.
|
|
bool load_original_bank(std::span<const std::uint8_t> bytes, std::string& error);
|
|
|
|
// Plays caller-supplied mono S16 native-endian PCM. A 2 MiB queue ceiling
|
|
// prevents a caller from accumulating unbounded playback. This is a backend
|
|
// primitive, not an original Sound ID mapping.
|
|
bool play_pcm(const std::int16_t* samples, std::size_t count,
|
|
std::uint32_t sample_rate, std::string& error);
|
|
bool play_original_id(std::uint8_t id, std::string& error);
|
|
bool play_semantic_cue(SemanticCue cue, std::string& error);
|
|
|
|
private:
|
|
static void original_callback(void* userdata, Uint8* stream, int length);
|
|
CueStatus consume_event(std::uint64_t session_id, std::uint64_t event_id,
|
|
std::string& detail);
|
|
SDL_AudioDeviceID device_{};
|
|
SDL_AudioSpec obtained_{};
|
|
bool initialized_audio_{};
|
|
bool muted_{};
|
|
float volume_{1.0F};
|
|
std::uint64_t session_id_{};
|
|
std::uint64_t last_event_id_{};
|
|
std::array<std::optional<PcmSample>, 13> original_cues_{};
|
|
Mixer mixer_;
|
|
OriginalSynth original_;
|
|
bool original_mode_{};
|
|
OriginalAudioOptions original_options_;
|
|
};
|
|
|
|
} // namespace openbmh::audio
|