1#define QOA_IMPLEMENTATION
12#include "soundbank_bin.h"
26 mmInitDefaultMem((mm_addr)soundbank_bin);
44 if (!
qp ||
qp->info.samplerate == 0)
47 return ae::q20_12_t{0};
49 return ae::q20_12_t{
qp->info.samples} / ae::q20_12_t{
qp->info.samplerate};
54 if (!
qp ||
qp->info.samplerate == 0)
56 return ae::q20_12_t{0};
58 return ae::q20_12_t{
qp->samplePos} / ae::q20_12_t{
qp->info.samplerate};
67 return qp->samplePos / QOA_FRAME_LEN;
82 if (
frame > (
int)
qp->info.samples / QOA_FRAME_LEN)
84 frame =
qp->info.samples / QOA_FRAME_LEN;
87 qp->samplePos =
frame * QOA_FRAME_LEN;
88 qp->sampleDataLen = 0;
89 qp->sampleDataPos = 0;
91 uint32_t offset =
qp->firstFramePos +
frame * qoa_max_frame_size(&
qp->info);
92 fseek(
qp->file, offset, SEEK_SET);
107 int64_t startSample = ((int64_t)startTime.raw_value() *
qp->info.samplerate) >> 12;
110 if (endTime != ae::q20_12_t{-1})
112 int64_t endSample = ((int64_t)endTime.raw_value() *
qp->info.samplerate) >> 12;
113 endFrame = endSample / QOA_FRAME_LEN;
137 if (!
qp || !
qp->file)
142 fseek(
qp->file,
qp->firstFramePos, SEEK_SET);
144 qp->sampleDataLen = 0;
145 qp->sampleDataPos = 0;
150 if (!
qp || !
qp->file || !
qp->buffer || !
qp->sampleData)
155 qp->bufferLen = fread(
qp->buffer, 1, qoa_max_frame_size(&
qp->info),
qp->file);
158 qoa_decode_frame(
qp->buffer,
qp->bufferLen, &
qp->info,
qp->sampleData, &frame_len);
159 qp->sampleDataPos = 0;
160 qp->sampleDataLen = frame_len;
166 if (!
qp || !sample_data)
171 int src_index =
qp->sampleDataPos *
qp->info.channels;
173 for (
int i = 0; i < num_samples; i++)
176 if (
qp->sampleDataLen -
qp->sampleDataPos == 0)
192 for (
int c = 0; c < (int)
qp->info.channels; c++)
194 sample_data[dst_index++] =
qp->sampleData[src_index++];
204 return AudioManager::getInstance().processStream(length, dest, format);
214 if (((
qp->info.channels == 1) && (format != MM_STREAM_16BIT_MONO)) ||
215 ((
qp->info.channels == 2) && (format != MM_STREAM_16BIT_STEREO)))
218 "Audio format from the current audio track does not match the original audio format");
221 uint32_t decoded =
decode((
short*)dest, length);
233 FILE* file = fopen(path.c_str(),
"rb");
241 uint8_t
header[QOA_MIN_FILESIZE];
242 int read = fread(
header, QOA_MIN_FILESIZE, 1, file);
252 uint32_t firstFramePos = qoa_decode_header(
header, QOA_MIN_FILESIZE, &qoa);
261 fseek(file, firstFramePos, SEEK_SET);
265 uint32_t bufferSize = qoa_max_frame_size(&qoa);
266 uint32_t sampleDataSize = qoa.channels * QOA_FRAME_LEN *
sizeof(short) * 2;
278 qp->firstFramePos = firstFramePos;
281 qp->sampleData = (
short*)(((uint8_t*)
qp) +
sizeof(
qoaplay_desc) + bufferSize);
283 qp->info.channels = qoa.channels;
284 qp->info.samplerate = qoa.samplerate;
285 qp->info.samples = qoa.samples;
288 setLoop(loopStartTime, loopEndTime);
292 stream.sampling_rate = qoa.samplerate;
293 stream.buffer_length = bufferSize;
295 stream.format = (qoa.channels == 1) ? MM_STREAM_16BIT_MONO : MM_STREAM_16BIT_STEREO;
296 stream.timer = MM_TIMER0;
297 stream.manual =
true;
299 mmStreamOpen(&stream);
338 mmLoadEffect((
int)sfx);
343 mm_sound_effect effect;
344 effect.id = (int)sfx;
345 effect.rate = (int)(1.0f * (1 << 10));
347 effect.volume = volume;
348 effect.panning = panning;
Manager for hardware specific audio functions.
static mm_word audioCallback(mm_word length, mm_addr dest, mm_stream_formats format)
Callback for maxmod audio streaming, acting as a wrapper for processStream.
void stopSFX()
Stop all currently playing SFXs.
void setLoop(ae::q20_12_t startTime, ae::q20_12_t endTime)
Set the audio looping points.
void registerSFX(SFX sfx)
Register the SFX to be played.
void stopAudio()
Deregister the currently registered audio.
void pauseAudio()
Pause the currently registered audio, if playing.
ae::q20_12_t getDuration()
Get the total duration of the audio.
mm_word processStream(mm_word length, mm_addr dest, mm_stream_formats format)
Processes the next audio data to play.
void rewind()
Move to the start of the audio track.
ae::q20_12_t getTime()
Get the current time of the audio.
void seekFrame(int frame)
Move to the specified audio frame.
void loop()
Looping logic.
void registerAudio(std::string path, ae::q20_12_t loopStartTime, ae::q20_12_t loopEndTime)
Sets up the audio track to be played.
uint32_t decodeFrame()
Decode the current audio frame.
void playSFX(SFX sfx, int volume, int panning)
Play the specified registered SFX.
int getFrame()
Get the current audio frame.
uint32_t decode(short *sample_data, int num_samples)
Decode the given audio sample based on the current frame.
void playAudio()
Play the currently registered audio, if paused.
Holds global variables used game-wide.
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Util to handle error logging.