Polyphase Game Engine
Loading...
Searching...
No Matches
Audio.h
Go to the documentation of this file.
1#pragma once
2
3#include "EngineTypes.h"
4
5class Stream;
6class SoundWave;
7class Audio3D;
8
10{
11 uint32_t mNumChannels = 2;
12 uint32_t mSampleRate = 44100;
13 uint32_t mBytesPerSample = 2;
14};
15
19
21 uint32_t voiceIndex,
22 SoundWave* soundWave,
23 float volume,
24 float pitch,
25 bool loop,
26 float startTime,
27 bool spatial);
28
29void AUD_Stop(uint32_t voiceIndex);
30bool AUD_IsPlaying(uint32_t voiceIndex);
31void AUD_SetVolume(uint32_t voiceIndex, float leftVolume, float rightVolume);
32void AUD_SetPitch(uint32_t voiceIndex, float pitch);
33
34uint8_t* AUD_AllocWaveBuffer(uint32_t size);
35void AUD_FreeWaveBuffer(void* buffer);
37
38// ------------------------------------------------------------------------------------------------
39// Streaming voices — push-based PCM output for long-form / runtime-generated audio.
40//
41// Intended for use by native addons (e.g. the VideoPlayer) that decode PCM at runtime and
42// can't pre-bake it into a SoundWave asset. Each open stream is backed by a dedicated source
43// voice on the platform mixer. The engine owns the voice; the caller owns the raw PCM buffer
44// which must remain valid until the corresponding SubmitStreamBuffer call returns OR the
45// stream is closed. The backend copies the bytes into its own staging buffer synchronously.
46//
47// Returns: stream id (non-zero on success, 0 on failure).
48// sampleRate: PCM frames per second (e.g. 48000).
49// numChannels: 1 (mono) or 2 (stereo) supported by the Windows/XAudio2 backend.
50// bitsPerSample: 16 supported (other values return 0 on Windows).
51// ------------------------------------------------------------------------------------------------
52uint32_t AUD_OpenStream(uint32_t sampleRate, uint32_t numChannels, uint32_t bitsPerSample);
53void AUD_CloseStream(uint32_t streamId);
54// Returns the number of bytes the backend accepted. 0 means the submit queue is full and the
55// caller should retry next tick. Non-zero but < byteSize is not returned by the current backend
56// (it either fully accepts or fully rejects).
57int32_t AUD_SubmitStreamBuffer(uint32_t streamId, const uint8_t* data, uint32_t byteSize);
58// Total samples-per-channel consumed by the stream since Open. The video player's A/V sync uses
59// this as the master clock: `audio_time_seconds = played_samples / sample_rate`.
60uint64_t AUD_GetStreamPlayedSamples(uint32_t streamId);
61void AUD_SetStreamVolume(uint32_t streamId, float volume);
62void AUD_SetStreamPaused(uint32_t streamId, bool paused);
63// Drop every buffer currently queued on the stream without waiting for them to play.
64// Used on seek / loop so the voice doesn't keep playing stale pre-seek audio.
65// SamplesPlayed keeps climbing (XAudio2 can't reset it without destroying the voice);
66// callers should take a snapshot via GetStreamPlayedSamples() right after Flush and
67// subtract it from subsequent readings to recover a seek-relative clock.
68void AUD_FlushStream(uint32_t streamId);
69
70// ------------------------------------------------------------------------------------------------
71// Audio analysis — real-time RMS / loudness / band magnitude / full spectrum.
72// Works for both static SoundWave voices (AUD_Play) and push-PCM streaming voices.
73// All values are computed over the most-recent ~AUDIO_FFT_SIZE samples of the voice's current
74// playback window. Returns 0.0f when the voice is not playing or PCM is unavailable.
75// numBins must be > 0; outBins must point to at least numBins floats and is zero-filled on failure.
76// Calls are platform-independent — backends only need to wire the three stream hooks in
77// AudioAnalysis.h to make streaming voices analyzable on their platform.
78// ------------------------------------------------------------------------------------------------
79float AUD_GetRMS (uint32_t voiceIndex);
80float AUD_GetLoudness (uint32_t voiceIndex); // normalized [0..1]
81float AUD_GetLoudnessDb (uint32_t voiceIndex); // raw dBFS, clamped [-60, 0]
82float AUD_GetFrequencies (uint32_t voiceIndex, float startHz, float endHz);
83void AUD_GetSpectrum (uint32_t voiceIndex, float startHz, float endHz,
84 float* outBins, uint32_t numBins);
85
86// Streaming counterparts (streamId returned by AUD_OpenStream).
87float AUD_GetStreamRMS (uint32_t streamId);
88float AUD_GetStreamLoudness (uint32_t streamId);
89float AUD_GetStreamLoudnessDb (uint32_t streamId);
90float AUD_GetStreamFrequencies (uint32_t streamId, float startHz, float endHz);
91void AUD_GetStreamSpectrum (uint32_t streamId, float startHz, float endHz,
92 float* outBins, uint32_t numBins);
93
94// Platform Independent
95void AUD_EncodeVorbis(Stream& inStream, Stream& outStream, PcmFormat format);
96void AUD_DecodeVorbis(Stream& inStream, Stream& outStream, PcmFormat format);
void AUD_Initialize()
void AUD_ProcessWaveBuffer(SoundWave *soundWave)
void AUD_Shutdown()
uint8_t * AUD_AllocWaveBuffer(uint32_t size)
void AUD_DecodeVorbis(Stream &inStream, Stream &outStream, PcmFormat format)
Definition Audio.cpp:285
uint32_t AUD_OpenStream(uint32_t sampleRate, uint32_t numChannels, uint32_t bitsPerSample)
void AUD_EncodeVorbis(Stream &inStream, Stream &outStream, PcmFormat format)
Definition Audio.cpp:96
float AUD_GetStreamRMS(uint32_t streamId)
Definition Audio.cpp:58
float AUD_GetStreamFrequencies(uint32_t streamId, float startHz, float endHz)
Definition Audio.cpp:75
void AUD_GetSpectrum(uint32_t voiceIndex, float startHz, float endHz, float *outBins, uint32_t numBins)
Definition Audio.cpp:49
float AUD_GetFrequencies(uint32_t voiceIndex, float startHz, float endHz)
Definition Audio.cpp:42
void AUD_Play(uint32_t voiceIndex, SoundWave *soundWave, float volume, float pitch, bool loop, float startTime, bool spatial)
void AUD_Update()
uint64_t AUD_GetStreamPlayedSamples(uint32_t streamId)
float AUD_GetLoudness(uint32_t voiceIndex)
Definition Audio.cpp:32
int32_t AUD_SubmitStreamBuffer(uint32_t streamId, const uint8_t *data, uint32_t byteSize)
float AUD_GetStreamLoudnessDb(uint32_t streamId)
Definition Audio.cpp:70
bool AUD_IsPlaying(uint32_t voiceIndex)
float AUD_GetRMS(uint32_t voiceIndex)
Definition Audio.cpp:25
void AUD_SetStreamVolume(uint32_t streamId, float volume)
void AUD_GetStreamSpectrum(uint32_t streamId, float startHz, float endHz, float *outBins, uint32_t numBins)
Definition Audio.cpp:82
void AUD_SetPitch(uint32_t voiceIndex, float pitch)
void AUD_Stop(uint32_t voiceIndex)
void AUD_SetVolume(uint32_t voiceIndex, float leftVolume, float rightVolume)
void AUD_CloseStream(uint32_t streamId)
float AUD_GetStreamLoudness(uint32_t streamId)
Definition Audio.cpp:65
void AUD_SetStreamPaused(uint32_t streamId, bool paused)
void AUD_FreeWaveBuffer(void *buffer)
float AUD_GetLoudnessDb(uint32_t voiceIndex)
Definition Audio.cpp:37
void AUD_FlushStream(uint32_t streamId)
Definition Audio3d.h:8
Definition SoundWave.h:6
Definition Stream.h:21
Definition Audio.h:10
uint32_t mBytesPerSample
Definition Audio.h:13
uint32_t mSampleRate
Definition Audio.h:12
uint32_t mNumChannels
Definition Audio.h:11