The frame
A GYM file has no other unit of time than the frame: the log was written once for each video frame of the emulated console, and the command 00 marks the end of one. A frame lasts 1/60 s on an NTSC console and 1/50 s on a PAL one; the file does not say which. The Sega Retro page notes the consequence: the accuracy is 16 ms, so a sound that changes faster than that within a frame is lost, and a PAL recording played at 60 frames a second runs 20 % fast.
A player applies the writes of a frame together and then lets the chips run for the length of the frame. At 44,100 Hz a frame is 735 samples (60 a second) or 882 (50 a second).
Length
The length is the number of 00 commands times the frame time. Game_Music_Emu computes it as frames × 50/3 ms, which is 60 frames a second whatever the region; the explorer shows the length for the region you choose in the options bar (NTSC by default).
The loop
The loop start of a GYMX header is how many frames into the song the loop begins; the loop runs to the end of the data and then starts again. A raw file has no loop. A value of 0 means there is none.
Game_Music_Emu reports an intro of N frames for a loop start of N, but its player takes the loop from the start of frame N−1 (counted from 0): it counts the loop start down once at the start of each frame and begins the loop when the count reaches 0. The explorer begins the loop after N frames, as the description says. If the file’s loop start is not before the last frame, the loop is never found and the tune ends at the end of the data.
The explorer plays the intro once and the loop the number of times in the options bar (2 by default), up to the longest time set there.
DAC samples
A game plays a sampled sound (a drum, a voice) through the YM2612’s DAC by writing a new value to register 2A thousands of times a second, with the DAC switched on by bit 7 of register 2B. That is far more than 60 writes a second, so a GYM file holds the writes of a whole frame as a run of 01 2A vv commands before the 00. A player that applied them all at the start of the frame would hear only the last one. The three ways that the sources deal with it:
- At the frame start (Gens and Gens/GS). Every write is applied in order at the start of the frame (
gym_play_nextin both programs). Only the last DAC value of each frame is heard, so a sample is replaced by a 60 Hz buzz. This is faithful to those two programs, whose own logger never writes registers 2A or 2B (YM2612_Writereturns from register 2A before the dump), so a log that they made holds no DAC at all. - Evenly from the frame start (the foobar2000 GYM decoder, MasterBoy’s copy of it). Every register 2A write of the frame is kept, whether the DAC is on or not, and the n writes of a frame of S samples are applied at the samples floor(i × S / n); the last value lasts to the end of the frame. Its source says it follows kpigym and YMAMP.
- Spread over the frame (Game_Music_Emu). The DAC writes of a frame are collected (only while the DAC is on; writes while it is off are dropped) and applied at even intervals through the frame. The first and last frames of a sample have fewer writes than the others, so the player looks at the neighbouring frames: if the previous frame had none and the next has more, the writes are placed at the end of the frame at the rate of the next frame; if the previous frame had more and the next has none, at the rate of the previous frame.
The explorer offers all three (DAC timing in the options bar) and plays the Gens timing by default, as the original program that wrote and read the format does. The other two are recoveries, not the original timing, which the log does not hold: a sample comes out of them as a sample, where the default gives the buzz. The source code of Genecyst and KEGA, the other early players, is not public, and no description of their DAC handling was found (Sega Retro describes neither), so their timing is not documented here.
In the explorer
The Tune tab shows the number of frames, the length and the loop; the Commands tab shows the frame and the time of every command.
Divergence from the specification
- The loop start: the description and Game_Music_Emu’s length agree, Game_Music_Emu’s player starts one frame earlier. The explorer follows the description.
- The frame rate: Game_Music_Emu always uses 60; the explorer offers 60 and 50.
- The DAC: the description does not mention it. Three timings are offered; the default is Gens’s.
- Game_Music_Emu places each DAC step in a band-limited buffer and the explorer writes it to the YM2612 model’s DAC register at the sample where the step falls, so the two differ in the exact position by up to one sample.
Sources
- vgmrips, “GYM File Format” (Internet Archive copy of 30 September 2020): the frame time and the loop start.
- Sega Retro, “GYM”: the 1/60 s accuracy.
- Game_Music_Emu,
gme/Gym_Emu.cpp:track_info_(length, intro),parse_frame(the loop count and the DAC collection),run_dac(the spreading). - Gens,
src/gens/util/sound/gym.c: one frame of sound for each 00, the writes applied in order;ym2612.c: the dumper skips registers 2A and 2B. - Gens/GS,
src/gens/util/sound/gym.cppandgens_core/sound/ym2612.cpp(the beaumanvienna/gens-gs copy): the same player and the same dumper. - MasterBoy’s foobar2000 GYM decoder,
psp/gym/gym_play.cpp(GYM_Next): the DAC writes collected and spaced evenly. - Sonic Retro, post 511269: “Gens/GS lacks still the accuracy for good vgm dumps with DAC or PWM”.