Skip to content

Commit 02ad820

Browse files
📝 Add docstrings to refactor
Docstrings generation was requested by @Raffaello. * #285 (comment) The following files were modified: * `sdl2-hyper-sonic-drivers/examples/adl-play.cpp` * `sdl2-hyper-sonic-drivers/sdl2-hyper-sonic-drivers.cpp` * `sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/files/westwood/ADLFile.cpp` * `sdl2-hyper-sonic-drivers/test/HyperSonicDrivers/files/westwood/TestADLFile.cpp`
1 parent 5088807 commit 02ad820

File tree

4 files changed

+156
-1
lines changed

4 files changed

+156
-1
lines changed

sdl2-hyper-sonic-drivers/examples/adl-play.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@ using files::westwood::ADLFile;
2929
using drivers::westwood::ADLDriver;
3030

3131

32+
/**
33+
* @brief Play an ADL file using a chosen OPL emulator/type until the user exits.
34+
*
35+
* Plays the given ADL file through an OPL device constructed from the specified
36+
* emulator and OPL type. The function creates an ADLDriver bound to the
37+
* Music channel group and enters an SDL event-driven loop that starts playback
38+
* (initial track index is 5) and responds to key presses to control playback.
39+
*
40+
* Behavior:
41+
* - Selects an OPL device based on `type`:
42+
* - OPL2 -> Adlib + Opl
43+
* - DUAL_OPL2-> SbPro + Opl
44+
* - OPL3 -> SbPro2 + Opl
45+
* - If the provided mixer is not ready, logs an error and returns immediately.
46+
* - Starts playing the current track when not already playing.
47+
* - Key controls (during playback):
48+
* - ESC: stop all channels and return (exit function)
49+
* - RIGHT: stop channels and advance to the next track (wraps to 0)
50+
* - LEFT: stop channels and go to the previous track (wraps to last)
51+
*
52+
* This function blocks until ESC is pressed.
53+
*
54+
* @param emu OPL emulator selection used when constructing the device.
55+
* @param type OPL hardware type used to choose the concrete device implementation.
56+
* @param filename Path to the ADL file to load and play.
57+
*/
3258
void adl_play(const OplEmulator emu, const OplType type, std::shared_ptr<audio::IMixer> mixer, const std::string& filename)
3359
{
3460
using devices::make_device;
@@ -106,6 +132,20 @@ void adl_play(const OplEmulator emu, const OplType type, std::shared_ptr<audio::
106132
} while (true);
107133
}
108134

135+
/**
136+
* @brief Program entry point for the ADL playback demo using SDL2 and OPL emulation.
137+
*
138+
* Initializes SDL video, creates a minimal SDL window, and starts an SDL-based audio mixer.
139+
* Iterates over configured OPL emulator and OPL type combinations, prints a colored header
140+
* for each pair, and invokes adl_play to run the ADL playback demo for "DUNE0.ADL".
141+
* Cleans up SDL resources before exiting.
142+
*
143+
* Return codes:
144+
* - 0 : Normal exit after running demos and cleanup.
145+
* - 1 : Mixer initialization failed.
146+
* - -1 : SDL video initialization failed.
147+
* - -2 : SDL window creation failed.
148+
*/
109149
int main(int argc, char* argv[])
110150
{
111151
if (SDL_Init(SDL_INIT_VIDEO) != 0)

sdl2-hyper-sonic-drivers/sdl2-hyper-sonic-drivers.cpp

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// sdl2-hyper-sonic-drivers.cpp : Defines the entry point for the application.
1+
// sdl2-hyper-sonic-drivers.cpp : Defines the entry point for the application.
22
// TODO: delete this file and its target, this is kinda scratch pad
33

44
#include <iostream>
@@ -336,6 +336,16 @@ void testMT32()
336336
}
337337

338338

339+
/**
340+
* @brief Load a WAV fixture, append it to itself, and play the result to completion.
341+
*
342+
* Initializes an SDL2 mixer, loads "test/fixtures/test_renderer_adlib_mame2.wav" into two sound
343+
* objects, concatenates them into a single PCM sound, and plays that combined sound via a
344+
* PCMDriver. The function blocks, polling until playback finishes.
345+
*
346+
* @note This function is a test utility: it uses a hard-coded test fixture path and performs
347+
* synchronous waiting while playback is active.
348+
*/
339349
void pcm_sound_append()
340350
{
341351
auto mixer = audio::make_mixer<audio::sdl2::Mixer>(8, 44100, 1024);
@@ -356,6 +366,24 @@ void pcm_sound_append()
356366
}
357367
}
358368

369+
/**
370+
* @brief Test harness that plays Westwood ADL music tracks through an Adlib/OPL driver using SDL2.
371+
*
372+
* This routine initializes an SDL2-backed audio mixer and an Adlib/OPL device, loads a Westwood
373+
* ADL file (fixed to "adl/KYRA1A.ADL"), and uses drivers::westwood::ADLDriver to play each
374+
* track (tracks are iterated from index 1 to getNumTracks()-1). Each track is played up to three
375+
* times. The function creates a small SDL window and pumps SDL events while waiting for playback
376+
* to finish; pressing Escape or receiving SDL_QUIT will abort the test, and pressing Return stops
377+
* the current track. After completing playback or aborting, the SDL window is destroyed.
378+
*
379+
* Side effects:
380+
* - Initializes SDL event and video subsystems and creates an SDL window.
381+
* - Initializes audio mixer and constructs Adlib/OPL device and ADL driver.
382+
* - Blocks while tracks are playing (polls events and sleeps).
383+
*
384+
* Notes:
385+
* - File name and iteration counts are hard-coded for this test harness.
386+
*/
359387
void adldune2filestest()
360388
{
361389
auto mixer = audio::make_mixer<audio::sdl2::Mixer>(8, 44100, 1024);
@@ -414,6 +442,18 @@ void adldune2filestest()
414442
SDL_DestroyWindow(window);
415443
}
416444

445+
/**
446+
* @brief Plays VOC files through an Adlib/OPL-backed PCM driver using SDL for event handling.
447+
*
448+
* Initializes an SDL-backed audio mixer and Adlib/OPL device, scans the local "voc/" directory,
449+
* and plays each VOC file's PCM sound multiple times. While a sound is playing the function
450+
* processes SDL events so the user can quit (window close or Escape) or stop the current playback
451+
* early (Enter). A small SDL window is created for event delivery and destroyed on exit.
452+
*
453+
* This is a test/harness routine with side effects: it initializes SDL subsystems, creates an
454+
* audio mixer and device, opens a window, iterates the filesystem, and drives playback via a
455+
* PCMDriver. It does not return any value.
456+
*/
417457
void vocdune2filestest()
418458
{
419459
auto mixer = audio::make_mixer<audio::sdl2::Mixer>(8, 44100, 1024);
@@ -466,6 +506,19 @@ void vocdune2filestest()
466506
SDL_DestroyWindow(window);
467507
}
468508

509+
/**
510+
* @brief Program entry point that runs selected test routines.
511+
*
512+
* The main function serves as the test harness entry. It dispatches to various
513+
* SDL2/HyperSonicDrivers test routines; currently it invokes adldune2filestest()
514+
* and then returns immediately. The function performs global initialization
515+
* and teardown only for the test routines it calls (handled by those routines).
516+
*
517+
* Note: many test calls are present but commented out; uncommenting changes the
518+
* executed test sequence.
519+
*
520+
* @return int Process exit code (0 on normal completion).
521+
*/
469522
int main(int argc, char* argv[])
470523
{
471524
//newMixerTest();

sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/files/westwood/ADLFile.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ namespace HyperSonicDrivers::files::westwood
9191
V3_DATA_HEADER_SIZE
9292
};
9393

94+
/**
95+
* @brief Construct an ADLFile by loading and parsing a Westwood ADL file from disk.
96+
*
97+
* Reads the file header, track offsets, instrument offsets and raw data payload
98+
* according to the detected ADL version (V1/V2/V3), validates sizes, computes
99+
* counts for tracks/offsets, then closes the file and converts absolute offsets
100+
* into data-relative offsets (using 0xFFFF as the sentinel for invalid entries).
101+
*
102+
* The constructor:
103+
* - validates the file meets the minimum size for an ADL file,
104+
* - determines the on-disk ADL version and associated metadata,
105+
* - reads version-appropriate header entries,
106+
* - reads track and instrument offset tables,
107+
* - reads the raw data payload and stores its size/header size,
108+
* - sets m_num_tracks from the version metadata and computes the number of
109+
* valid track/instrument offsets,
110+
* - closes the underlying file handle,
111+
* - adjusts offsets to be relative to the in-memory data payload (and maps 0 -> 0xFFFF).
112+
*
113+
* @param filename Path to the ADL file to open and parse.
114+
*/
94115
ADLFile::ADLFile(const std::string& filename) : File(filename)
95116
{
96117
assertValid_(size() >= FILE_SIZE_MIN);
@@ -137,6 +158,16 @@ namespace HyperSonicDrivers::files::westwood
137158
return m_version;
138159
}
139160

161+
/**
162+
* @brief Return the number of tracks (subsongs) in the ADL file.
163+
*
164+
* The value is the internally stored track count determined at file load
165+
* from the version-specific metadata. For some V3 files the in-memory
166+
* header is known to be incorrect, so this returned count reflects the
167+
* metadata-derived value rather than any inferred count from the header.
168+
*
169+
* @return int Number of tracks (subsongs).
170+
*/
140171
int ADLFile::getNumTracks() const noexcept
141172
{
142173
return m_num_tracks;
@@ -155,6 +186,16 @@ namespace HyperSonicDrivers::files::westwood
155186
//return numSubSongs;
156187
}
157188

189+
/**
190+
* @brief Return the number of program (track) offsets present in the ADL file.
191+
*
192+
* This value is determined during construction from the file's version-specific
193+
* metadata and offset tables. It represents how many valid track/program
194+
* offset entries are available (sentinel/invalid entries are counted according
195+
* to the parsed offset table).
196+
*
197+
* @return int Number of track/program offsets.
198+
*/
158199
int ADLFile::getNumTrackOffsets() const noexcept
159200
{
160201
return m_num_track_offsets;
@@ -281,6 +322,19 @@ namespace HyperSonicDrivers::files::westwood
281322
}
282323
}
283324

325+
/**
326+
* @brief Read and populate the in-memory header table from the open file.
327+
*
328+
* Resizes ADLFile::m_header to header_size and fills it by invoking the supplied
329+
* read callback header_size times. The read callback is expected to return the
330+
* next header entry (as a 16-bit value) each time it is called. After reading,
331+
* the method validates that the header vector length matches header_size.
332+
*
333+
* @param header_size Number of header entries to read and store into m_header.
334+
* @param read A zero-argument callable that returns the next header entry as uint16_t.
335+
* The caller is responsible for using the correct byte-width and endianness
336+
* for the target ADL version.
337+
*/
284338
void ADLFile::readHeaderFromFile_(const int header_size, std::function<uint16_t()> read)
285339
{
286340
m_header.resize(header_size);

sdl2-hyper-sonic-drivers/test/HyperSonicDrivers/files/westwood/TestADLFile.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ namespace HyperSonicDrivers::files::westwood
7474
EXPECT_EQ(chan, 9);
7575
}
7676

77+
/**
78+
* @brief Tests ADLFile parsing for a version 3 ADL fixture (LOREINTR.ADL).
79+
*
80+
* Verifies that the file is recognized as version 3, reports the expected
81+
* counts (tracks, track offsets, instrument offsets) and data size, and that
82+
* program offsets for a track resolve to the corresponding track and
83+
* instrument offsets. Also checks that the first track's data byte equals 9.
84+
*/
7785
TEST(ADLFile, ADLv3)
7886
{
7987
ADLFile f("../fixtures/LOREINTR.ADL");

0 commit comments

Comments
 (0)