19constexpr uint32_t RING_SIZE = 32768;
20constexpr uint32_t RING_MASK = RING_SIZE - 1;
21static_assert(
AUDIO_CHUNK_MAX <= RING_SIZE,
"audio ring must hold at least one audio chunk");
55 bool open(uint32_t sampleRate, uint32_t numChannels)
60 desc.channels = numChannels;
61 desc.samplerate = sampleRate;
64 ring = (uint8_t*)malloc(RING_SIZE);
65 pcm = (
short*)malloc(QOA_FRAME_LEN * numChannels *
sizeof(
short));
81 stream.sampling_rate =
rate;
84 stream.format = (
channels == 1) ? MM_STREAM_16BIT_MONO : MM_STREAM_16BIT_STEREO;
85 stream.timer = MM_TIMER0;
89 mmStreamOpen(&stream);
122 return RING_SIZE -
count;
128 uint32_t tail = (
head +
count) & RING_MASK;
129 uint32_t first = RING_SIZE - tail;
134 if (fread(
ring + tail, 1, first, f) != first)
138 if (size > first && fread(
ring, 1, size - first, f) != size - first)
157 for (uint32_t i = 0; i < 8; i++)
159 h[i] =
ring[(
head + i) & RING_MASK];
161 uint32_t size = ((uint32_t)h[6] << 8) | h[7];
173 uint32_t toEnd = RING_SIZE -
head;
186 qoa_decode_frame(src, size, &
desc,
pcm, &len);
190 if (len == 0 || len > QOA_FRAME_LEN)
204 return played > 0 ? (uint32_t)played : 0;
221 short* out = (
short*)dest;
225 while (done < length)
232 if (n > length - done)
236 const size_t copySamples =
static_cast<size_t>(n) *
static_cast<size_t>(ch);
237 const size_t copyBytes = copySamples *
sizeof(short);
238 memcpy(out + done * ch, a->
pcm + a->
pcmPos * ch, copyBytes);
247 uint32_t pad = length - done;
248 const size_t padSamples =
static_cast<size_t>(pad) *
static_cast<size_t>(ch);
249 const size_t padBytes = padSamples *
sizeof(short);
250 memset(out + done * ch, 0, padBytes);
318 aud->fileEnded =
true;
326 return (
int)(((u64)
aud->clock() * (uint32_t)
fpsInt) /
aud->rate);
356 fpsInt = (int)(iFps.raw_value() >> 12);
358 std::string videoPath =
fatBasePath +
"video/" + iFileName;
360 videoSetMode(MODE_5_2D | DISPLAY_BG3_ACTIVE);
361 videoSetModeSub(MODE_0_2D);
363 vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
364 vramSetBankD(VRAM_D_MAIN_BG_0x06020000);
365 vramSetBankC(VRAM_C_SUB_BG);
368 AudioManager::getInstance().stopAudio();
372 videoFile = fopen(videoPath.c_str(),
"rb");
382 uint32_t audioRate = 0;
383 uint32_t audioChannels = 0;
385 if (hRead == 16 && memcmp(
header,
"VID\0", 4) == 0)
394 audioChannels =
header[14];
420 bg = bgInit(3, BgType_Bmp8, BgSize_B8_256x256, 0, 0);
422 uint16_t palette[256];
423 size_t palRead = fread(palette, 2, 256,
videoFile);
429 for (
int i = 0; i < 256; i++)
431 BG_PALETTE[i] = palette[i];
436 bgExtPaletteEnable();
437 bg = bgInit(3, BgType_Bmp16, BgSize_B16_256x256, 0, 0);
450 if (audioCodec ==
VID_AUDIO_QOA && audioRate > 0 && (audioChannels == 1 || audioChannels == 2))
453 if (
aud && !
aud->open(audioRate, audioChannels))
461 for (
int i = 0; i < 3; i++)
529 size_t r = fread(dest + got, 1, n,
videoFile);
Manager for hardware specific audio functions.
static mm_word videoAudioCallback(mm_word length, mm_addr dest, mm_stream_formats)
static VideoAudio * g_audio
constexpr uint8_t VID_AUDIO_NONE
constexpr uint8_t VID_AUDIO_QOA
constexpr uint16_t AUDIO_CHUNK_MAX
Maximum audio payload (bytes) in one per-frame chunk. Must match MAX_AUDIO_CHUNK in video2vid....
constexpr uint8_t FRAMES_TO_BUFFER
Number of decoded video frames held in the RAM ring buffer.
constexpr uint8_t VIDEO_AUDIO_STREAM_MS
Length of the maxmod audio stream buffer in ms. Larger tolerates slower SD reads.
constexpr int8_t VIDEO_SYNC_OFFSET_MS
Fine A/V sync offset in ms.
constexpr uint8_t READS_PER_UPDATE
Maximum frame reads per update(). Reads stop early if a frame is already due.
constexpr uint16_t VIDEO_READ_SLICE
Size (bytes) of each SD card read slice when loading a video frame.
Streams video frames and multiplexed QOA audio from a single .vid file.
static void create()
Creates the singleton instance if it does not exist.
void pumpAudio()
Feeds the maxmod stream; a no-op when no audio is active.
VideoAudio * aud
Internal audio player; non-null while audio is active.
uint32_t frameSize
Size of one frame in bytes.
void stopInternalAudio()
Shuts down and frees the internal audio player.
uint8_t * ramBuffer
Ring buffer holding FRAMES_TO_BUFFER decoded frames.
static VideoController * instance
Singleton instance.
void cleanup()
Stops playback and releases the file, audio stream and frame buffer.
FILE * videoFile
Open .vid file handle.
static VideoController * getInstance()
uint16_t frameW
Frame width in pixels.
int64_t currentFrame
Index of the next frame to display.
ViewState update()
Advances playback: pumps audio, displays the next due frame and reads ahead.
ViewState nextState
State returned when playback ends.
bool haveChunkHeader
True if the current chunk's audio size has been read.
uint16_t writeIndex
Ring slot the next frame is read into.
uint32_t pendingAudioSize
Audio bytes still to be read for the current chunk.
void setEOF()
Marks end of file and tells the audio player no more data will arrive.
bool refillBuffer()
Reads one audio chunk (into the audio ring) and one video frame.
static void destroy()
Destroys the singleton instance and releases all video resources.
void init(std::string iFileName, ae::q20_12_t iFps, ViewState iNextState)
Opens and prepares a video stream.
int8_t bg
Background id returned by bgInit().
uint32_t silentVblanks
Pacing clock (vblanks) used when there is no audio.
bool fileEOF
True once the file (or a read) is exhausted.
int clockFrame() const
Computes the frame that should be on screen now.
VideoController()=default
uint16_t framesAvailable
Number of frames currently buffered.
uint8_t bpp
Bytes per pixel (1 = 8-bit paletted, 2 = 16-bit).
uint16_t frameH
Frame height in pixels.
uint32_t bufferSize
Total size of the frame ring buffer in bytes.
uint8_t fpsInt
Frame rate in whole fps (from the header or fallback).
uint16_t readIndex
Ring slot of the next frame to display.
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.
bool open(uint32_t sampleRate, uint32_t numChannels)
bool readFrom(FILE *f, uint32_t size)