1 /*
2  * Snd_Defs.h
3  * ----------
4  * Purpose: Basic definitions of data types, enums, etc. for the playback engine core.
5  * Notes  : (currently none)
6  * Authors: Olivier Lapicque
7  *          OpenMPT Devs
8  * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
9  */
10 
11 
12 #pragma once
13 
14 #include "openmpt/all/BuildSettings.hpp"
15 
16 #include "openmpt/base/FlagSet.hpp"
17 
18 
19 OPENMPT_NAMESPACE_BEGIN
20 
21 
22 using ROWINDEX = uint32;
23 inline constexpr ROWINDEX ROWINDEX_INVALID = uint32_max;
24 using CHANNELINDEX = uint16;
25 inline constexpr CHANNELINDEX CHANNELINDEX_INVALID = uint16_max;
26 using ORDERINDEX = uint16;
27 inline constexpr ORDERINDEX ORDERINDEX_INVALID = uint16_max;
28 inline constexpr ORDERINDEX ORDERINDEX_MAX = uint16_max - 1;
29 using PATTERNINDEX = uint16;
30 inline constexpr PATTERNINDEX PATTERNINDEX_INVALID = uint16_max;
31 using PLUGINDEX = uint8;
32 inline constexpr PLUGINDEX PLUGINDEX_INVALID = uint8_max;
33 using SAMPLEINDEX = uint16;
34 inline constexpr SAMPLEINDEX SAMPLEINDEX_INVALID = uint16_max;
35 using INSTRUMENTINDEX = uint16;
36 inline constexpr INSTRUMENTINDEX INSTRUMENTINDEX_INVALID = uint16_max;
37 using SEQUENCEINDEX = uint8;
38 inline constexpr SEQUENCEINDEX SEQUENCEINDEX_INVALID = uint8_max;
39 
40 using SmpLength = uint32;
41 
42 
43 inline constexpr SmpLength MAX_SAMPLE_LENGTH = 0x10000000; // Sample length in frames. Sample size in bytes can be more than this (= 256 MB).
44 
45 inline constexpr ROWINDEX MAX_PATTERN_ROWS       = 1024;
46 inline constexpr ROWINDEX MAX_ROWS_PER_BEAT      = 65536;
47 inline constexpr ORDERINDEX MAX_ORDERS           = ORDERINDEX_MAX + 1;
48 inline constexpr PATTERNINDEX MAX_PATTERNS       = 4000;
49 inline constexpr SAMPLEINDEX MAX_SAMPLES         = 4000;
50 inline constexpr INSTRUMENTINDEX MAX_INSTRUMENTS = 256;
51 inline constexpr PLUGINDEX MAX_MIXPLUGINS        = 250;
52 
53 inline constexpr SEQUENCEINDEX MAX_SEQUENCES     = 50;
54 
55 inline constexpr CHANNELINDEX MAX_BASECHANNELS   = 127; // Maximum pattern channels.
56 inline constexpr CHANNELINDEX MAX_CHANNELS       = 256; // Maximum number of mixing channels.
57 
58 enum { FREQ_FRACBITS = 4 }; // Number of fractional bits in return value of CSoundFile::GetFreqFromPeriod()
59 
60 // String lengths (including trailing null char)
61 enum
62 {
63 	MAX_SAMPLENAME         = 32,
64 	MAX_SAMPLEFILENAME     = 22,
65 	MAX_INSTRUMENTNAME     = 32,
66 	MAX_INSTRUMENTFILENAME = 32,
67 	MAX_PATTERNNAME        = 32,
68 	MAX_CHANNELNAME        = 20,
69 };
70 
71 enum MODTYPE
72 {
73 	MOD_TYPE_NONE = 0x00,
74 	MOD_TYPE_MOD  = 0x01,
75 	MOD_TYPE_S3M  = 0x02,
76 	MOD_TYPE_XM   = 0x04,
77 	MOD_TYPE_MED  = 0x08,
78 	MOD_TYPE_MTM  = 0x10,
79 	MOD_TYPE_IT   = 0x20,
80 	MOD_TYPE_669  = 0x40,
81 	MOD_TYPE_ULT  = 0x80,
82 	MOD_TYPE_STM  = 0x100,
83 	MOD_TYPE_FAR  = 0x200,
84 	MOD_TYPE_DTM  = 0x400,
85 	MOD_TYPE_AMF  = 0x800,
86 	MOD_TYPE_AMS  = 0x1000,
87 	MOD_TYPE_DSM  = 0x2000,
88 	MOD_TYPE_MDL  = 0x4000,
89 	MOD_TYPE_OKT  = 0x8000,
90 	MOD_TYPE_MID  = 0x10000,
91 	MOD_TYPE_DMF  = 0x20000,
92 	MOD_TYPE_PTM  = 0x40000,
93 	MOD_TYPE_DBM  = 0x80000,
94 	MOD_TYPE_MT2  = 0x100000,
95 	MOD_TYPE_AMF0 = 0x200000,
96 	MOD_TYPE_PSM  = 0x400000,
97 	MOD_TYPE_J2B  = 0x800000,
98 	MOD_TYPE_MPT  = 0x1000000,
99 	MOD_TYPE_IMF  = 0x2000000,
100 	MOD_TYPE_DIGI = 0x4000000,
101 	MOD_TYPE_STP  = 0x8000000,
102 	MOD_TYPE_PLM  = 0x10000000,
103 	MOD_TYPE_SFX  = 0x20000000,
104 };
105 DECLARE_FLAGSET(MODTYPE)
106 
107 
108 enum MODCONTAINERTYPE
109 {
110 	MOD_CONTAINERTYPE_NONE = 0x0,
111 	MOD_CONTAINERTYPE_UMX  = 0x3,
112 	MOD_CONTAINERTYPE_XPK  = 0x4,
113 	MOD_CONTAINERTYPE_PP20 = 0x5,
114 	MOD_CONTAINERTYPE_MMCMP= 0x6,
115 	MOD_CONTAINERTYPE_WAV  = 0x7, // WAV as module
116 	MOD_CONTAINERTYPE_UAX  = 0x8, // Unreal sample set as module
117 };
118 
119 
120 // Module channel / sample flags
121 enum ChannelFlags
122 {
123 	// Sample Flags
124 	CHN_16BIT           = 0x01,        // 16-bit sample
125 	CHN_LOOP            = 0x02,        // Looped sample
126 	CHN_PINGPONGLOOP    = 0x04,        // Bidi-looped sample
127 	CHN_SUSTAINLOOP     = 0x08,        // Sample with sustain loop
128 	CHN_PINGPONGSUSTAIN = 0x10,        // Sample with bidi sustain loop
129 	CHN_PANNING         = 0x20,        // Sample with forced panning
130 	CHN_STEREO          = 0x40,        // Stereo sample
131 	CHN_REVERSE         = 0x80,        // Start sample playback from sample / loop end (Velvet Studio feature)
132 	CHN_SURROUND        = 0x100,       // Use surround channel
133 	CHN_ADLIB           = 0x200,       // Adlib / OPL instrument is active on this channel
134 
135 	// Channel Flags
136 	CHN_PINGPONGFLAG    = 0x80,        // When flag is on, sample is processed backwards - this is intentionally the same flag as CHN_REVERSE.
137 	CHN_MUTE            = 0x400,       // Muted channel
138 	CHN_KEYOFF          = 0x800,       // Exit sustain
139 	CHN_NOTEFADE        = 0x1000,      // Fade note (instrument mode)
140 	CHN_WRAPPED_LOOP    = 0x2000,      // Loop just wrapped around to loop start (required for correct interpolation around loop points)
141 	CHN_AMIGAFILTER     = 0x4000,      // Apply Amiga low-pass filter
142 	CHN_FILTER          = 0x8000,      // Apply resonant filter on sample
143 	CHN_VOLUMERAMP      = 0x10000,     // Apply volume ramping
144 	CHN_VIBRATO         = 0x20000,     // Apply vibrato
145 	CHN_TREMOLO         = 0x40000,     // Apply tremolo
146 	CHN_PORTAMENTO      = 0x80000,     // Apply portamento
147 	CHN_GLISSANDO       = 0x100000,    // Glissando (force portamento to semitones) mode
148 	CHN_FASTVOLRAMP     = 0x200000,    // Force usage of global ramping settings instead of ramping over the complete render buffer length
149 	CHN_EXTRALOUD       = 0x400000,    // Force sample to play at 0dB
150 	CHN_REVERB          = 0x800000,    // Apply reverb on this channel
151 	CHN_NOREVERB        = 0x1000000,   // Disable reverb on this channel
152 	CHN_SOLO            = 0x2000000,   // Solo channel
153 	CHN_NOFX            = 0x4000000,   // Dry channel (no plugins)
154 	CHN_SYNCMUTE        = 0x8000000,   // Keep sample sync on mute
155 
156 	// Sample flags (only present in ModSample::uFlags, may overlap with CHN_CHANNELFLAGS)
157 	SMP_MODIFIED        = 0x2000,      // Sample data has been edited in the tracker
158 	SMP_KEEPONDISK      = 0x4000,      // Sample is not saved to file, data is restored from original sample file
159 	SMP_NODEFAULTVOLUME = 0x8000,      // Ignore default volume setting
160 };
161 DECLARE_FLAGSET(ChannelFlags)
162 
163 #define CHN_SAMPLEFLAGS (CHN_16BIT | CHN_LOOP | CHN_PINGPONGLOOP | CHN_SUSTAINLOOP | CHN_PINGPONGSUSTAIN | CHN_PANNING | CHN_STEREO | CHN_PINGPONGFLAG | CHN_REVERSE | CHN_SURROUND | CHN_ADLIB)
164 #define CHN_CHANNELFLAGS (~CHN_SAMPLEFLAGS | CHN_SURROUND)
165 
166 // Sample flags fit into the first 16 bits, and with the current memory layout, storing them as a 16-bit integer packs struct ModSample nicely.
167 using SampleFlags = FlagSet<ChannelFlags, uint16>;
168 
169 
170 // Instrument envelope-specific flags
171 enum EnvelopeFlags : uint8
172 {
173 	ENV_ENABLED = 0x01, // env is enabled
174 	ENV_LOOP    = 0x02, // env loop
175 	ENV_SUSTAIN = 0x04, // env sustain
176 	ENV_CARRY   = 0x08, // env carry
177 	ENV_FILTER  = 0x10, // filter env enabled (this has to be combined with ENV_ENABLED in the pitch envelope's flags)
178 };
179 DECLARE_FLAGSET(EnvelopeFlags)
180 
181 
182 // Envelope value boundaries
183 #define ENVELOPE_MIN   0   // Vertical min value of a point
184 #define ENVELOPE_MID   32  // Vertical middle line
185 #define ENVELOPE_MAX   64  // Vertical max value of a point
186 #define MAX_ENVPOINTS  240 // Maximum length of each instrument envelope
187 
188 
189 // Instrument-specific flags
190 enum InstrumentFlags : uint8
191 {
192 	INS_SETPANNING = 0x01, // Panning enabled
193 	INS_MUTE       = 0x02, // Instrument is muted
194 };
195 DECLARE_FLAGSET(InstrumentFlags)
196 
197 
198 // envelope types in instrument editor
199 enum EnvelopeType : uint8
200 {
201 	ENV_VOLUME = 0,
202 	ENV_PANNING,
203 	ENV_PITCH,
204 
205 	ENV_MAXTYPES
206 };
207 
208 // Filter Modes
209 enum class FilterMode : uint8
210 {
211 	Unchanged = 0xFF,
212 	LowPass   = 0,
213 	HighPass  = 1,
214 };
215 
216 
217 // NNA types (New Note Action)
218 enum class NewNoteAction : uint8
219 {
220 	NoteCut  = 0,
221 	Continue = 1,
222 	NoteOff  = 2,
223 	NoteFade = 3,
224 };
225 
226 // DCT types (Duplicate Check Types)
227 enum class DuplicateCheckType : uint8
228 {
229 	None       = 0,
230 	Note       = 1,
231 	Sample     = 2,
232 	Instrument = 3,
233 	Plugin     = 4,
234 };
235 
236 // DNA types (Duplicate Note Action)
237 enum class DuplicateNoteAction : uint8
238 {
239 	NoteCut  = 0,
240 	NoteOff  = 1,
241 	NoteFade = 2,
242 };
243 
244 
245 // Module flags - contains both song configuration and playback state... Use SONG_FILE_FLAGS and SONG_PLAY_FLAGS distinguish between the two.
246 enum SongFlags
247 {
248 	SONG_FASTVOLSLIDES =       0x02,  // Old Scream Tracker 3.0 volume slides
249 	SONG_ITOLDEFFECTS  =       0x04,  // Old Impulse Tracker effect implementations
250 	SONG_ITCOMPATGXX   =       0x08,  // IT "Compatible Gxx" (IT's flag to behave more like other trackers w/r/t portamento effects)
251 	SONG_LINEARSLIDES  =       0x10,  // Linear slides vs. Amiga slides
252 	SONG_PATTERNLOOP   =       0x20,  // Loop current pattern (pattern editor)
253 	SONG_STEP          =       0x40,  // Song is in "step" mode (pattern editor)
254 	SONG_PAUSED        =       0x80,  // Song is paused (no tick processing, just rendering audio)
255 	SONG_FADINGSONG    =     0x0100,  // Song is fading out
256 	SONG_ENDREACHED    =     0x0200,  // Song is finished
257 	SONG_FIRSTTICK     =     0x1000,  // Is set when the current tick is the first tick of the row
258 	SONG_MPTFILTERMODE =     0x2000,  // Local filter mode (reset filter on each note)
259 	SONG_SURROUNDPAN   =     0x4000,  // Pan in the rear channels
260 	SONG_EXFILTERRANGE =     0x8000,  // Cutoff Filter has double frequency range (up to ~10Khz)
261 	SONG_AMIGALIMITS   =   0x1'0000,  // Enforce amiga frequency limits
262 	SONG_S3MOLDVIBRATO =   0x2'0000,  // ScreamTracker 2 vibrato in S3M files
263 	SONG_BREAKTOROW    =   0x8'0000,  // Break to row command encountered (internal flag, do not touch)
264 	SONG_POSJUMP       =  0x10'0000,  // Position jump encountered (internal flag, do not touch)
265 	SONG_PT_MODE       =  0x20'0000,  // ProTracker 1/2 playback mode
266 	SONG_PLAYALLSONGS  =  0x40'0000,  // Play all subsongs consecutively (libopenmpt)
267 	SONG_ISAMIGA       =  0x80'0000,  // Is an Amiga module and thus qualifies to be played using the Paula BLEP resampler
268 	SONG_IMPORTED      = 0x100'0000,  // Song type does not represent actual module format / was imported from a different format (OpenMPT)
269 };
270 DECLARE_FLAGSET(SongFlags)
271 
272 #define SONG_FILE_FLAGS (SONG_FASTVOLSLIDES|SONG_ITOLDEFFECTS|SONG_ITCOMPATGXX|SONG_LINEARSLIDES|SONG_EXFILTERRANGE|SONG_AMIGALIMITS|SONG_S3MOLDVIBRATO|SONG_PT_MODE|SONG_ISAMIGA|SONG_IMPORTED)
273 #define SONG_PLAY_FLAGS (~SONG_FILE_FLAGS)
274 
275 // Global Options (Renderer)
276 #ifndef NO_AGC
277 #define SNDDSP_AGC            0x40     // Automatic gain control
278 #endif // ~NO_AGC
279 #ifndef NO_DSP
280 #define SNDDSP_MEGABASS       0x02     // Bass expansion
281 #define SNDDSP_SURROUND       0x08     // Surround mix
282 #define SNDDSP_BITCRUSH       0x01
283 #endif // NO_DSP
284 #ifndef NO_REVERB
285 #define SNDDSP_REVERB         0x20     // Apply reverb
286 #endif // NO_REVERB
287 #ifndef NO_EQ
288 #define SNDDSP_EQ             0x80     // Apply EQ
289 #endif // NO_EQ
290 
291 #define SNDMIX_SOFTPANNING    0x10     // Soft panning mode (this is forced with mixmode RC3 and later)
292 
293 // Misc Flags (can safely be turned on or off)
294 #define SNDMIX_MAXDEFAULTPAN  0x80000  // Currently unused (should be used by Amiga MOD loaders)
295 #define SNDMIX_MUTECHNMODE    0x100000 // Notes are not played on muted channels
296 
297 
298 #define MAX_GLOBAL_VOLUME 256u
299 
300 // Resampling modes
301 enum ResamplingMode : uint8
302 {
303 	// ATTENTION: Do not change ANY of these values, as they get written out to files in per instrument interpolation settings
304 	// and old files have these exact values in them which should not change meaning.
305 	SRCMODE_NEAREST   = 0,  // 1 tap, no AA
306 	SRCMODE_LINEAR    = 1,  // 2 tap, no AA
307 	SRCMODE_CUBIC     = 2,  // 4 tap, no AA
308 	SRCMODE_SINC8     = 4,  // 8 tap, no AA   (yes, index 4) (XMMS-ModPlug)
309 	SRCMODE_SINC8LP   = 3,  // 8 tap, with AA (yes, index 3) (Polyphase)
310 
311 	SRCMODE_DEFAULT   = 5,  // Only used for instrument settings, not used inside the mixer
312 
313 	SRCMODE_AMIGA  = 0xFF,  // Not explicitely user-selectable
314 };
315 
316 namespace Resampling
317 {
318 
319 enum class AmigaFilter
320 {
321 	Off        = 0,
322 	A500       = 1,
323 	A1200      = 2,
324 	Unfiltered = 3,
325 };
326 
AllModes()327 inline std::array<ResamplingMode, 5> AllModes() noexcept { return { { SRCMODE_NEAREST, SRCMODE_LINEAR, SRCMODE_CUBIC, SRCMODE_SINC8, SRCMODE_SINC8LP } }; }
328 
AllModesWithDefault()329 inline std::array<ResamplingMode, 6> AllModesWithDefault() noexcept { return { { SRCMODE_NEAREST, SRCMODE_LINEAR, SRCMODE_CUBIC, SRCMODE_SINC8, SRCMODE_SINC8LP, SRCMODE_DEFAULT } }; }
330 
Default()331 constexpr ResamplingMode Default() noexcept { return SRCMODE_SINC8LP; }
332 
IsKnownMode(int mode)333 constexpr bool IsKnownMode(int mode) noexcept { return (mode >= 0) && (mode < SRCMODE_DEFAULT); }
334 
ToKnownMode(int mode)335 constexpr ResamplingMode ToKnownMode(int mode) noexcept
336 {
337 	return Resampling::IsKnownMode(mode) ? static_cast<ResamplingMode>(mode)
338 		: (mode == SRCMODE_AMIGA) ? SRCMODE_LINEAR
339 		: Resampling::Default();
340 }
341 
Length(ResamplingMode mode)342 constexpr int Length(ResamplingMode mode) noexcept
343 {
344 	return mode == SRCMODE_NEAREST ? 1
345 		: mode == SRCMODE_LINEAR ? 2
346 		: mode == SRCMODE_CUBIC ? 4
347 		: mode == SRCMODE_SINC8 ? 8
348 		: mode == SRCMODE_SINC8LP ? 8
349 		: 0;
350 }
351 
HasAA(ResamplingMode mode)352 constexpr bool HasAA(ResamplingMode mode) noexcept { return (mode == SRCMODE_SINC8LP); }
353 
AddAA(ResamplingMode mode)354 constexpr ResamplingMode AddAA(ResamplingMode mode) noexcept { return (mode == SRCMODE_SINC8) ? SRCMODE_SINC8LP : mode; }
355 
RemoveAA(ResamplingMode mode)356 constexpr ResamplingMode RemoveAA(ResamplingMode mode) noexcept { return (mode == SRCMODE_SINC8LP) ? SRCMODE_SINC8 : mode; }
357 
358 }
359 
360 
361 
362 // Release node defines
363 #define ENV_RELEASE_NODE_UNSET	0xFF
364 #define NOT_YET_RELEASED		(-1)
365 static_assert(ENV_RELEASE_NODE_UNSET > MAX_ENVPOINTS);
366 
367 
368 enum PluginPriority
369 {
370 	ChannelOnly,
371 	InstrumentOnly,
372 	PrioritiseInstrument,
373 	PrioritiseChannel,
374 };
375 
376 enum PluginMutePriority
377 {
378 	EvenIfMuted,
379 	RespectMutes,
380 };
381 
382 // Plugin velocity handling options
383 enum PlugVelocityHandling : uint8
384 {
385 	PLUGIN_VELOCITYHANDLING_CHANNEL = 0,
386 	PLUGIN_VELOCITYHANDLING_VOLUME
387 };
388 
389 // Plugin volumecommand handling options
390 enum PlugVolumeHandling : uint8
391 {
392 	PLUGIN_VOLUMEHANDLING_MIDI = 0,
393 	PLUGIN_VOLUMEHANDLING_DRYWET,
394 	PLUGIN_VOLUMEHANDLING_IGNORE,
395 	PLUGIN_VOLUMEHANDLING_CUSTOM,
396 	PLUGIN_VOLUMEHANDLING_MAX,
397 };
398 
399 enum MidiChannel : uint8
400 {
401 	MidiNoChannel		= 0,
402 	MidiFirstChannel	= 1,
403 	MidiLastChannel		= 16,
404 	MidiMappedChannel	= 17,
405 };
406 
407 
408 // Vibrato Types
409 enum VibratoType : uint8
410 {
411 	VIB_SINE = 0,
412 	VIB_SQUARE,
413 	VIB_RAMP_UP,
414 	VIB_RAMP_DOWN,
415 	VIB_RANDOM
416 };
417 
418 
419 // Tracker-specific playback behaviour
420 // Note: The index of every flag has to be fixed, so do not remove flags. Always add new flags at the end!
421 enum PlayBehaviour
422 {
423 	MSF_COMPATIBLE_PLAY,            // No-op - only used during loading (Old general compatibility flag for IT/MPT/XM)
424 	kMPTOldSwingBehaviour,          // MPT 1.16 swing behaviour (IT/MPT, deprecated)
425 	kMIDICCBugEmulation,            // Emulate broken volume MIDI CC behaviour (IT/MPT/XM, deprecated)
426 	kOldMIDIPitchBends,             // Old VST MIDI pitch bend behaviour (IT/MPT/XM, deprecated)
427 	kFT2VolumeRamping,              // Smooth volume ramping like in FT2 (XM)
428 	kMODVBlankTiming,               // F21 and above set speed instead of tempo
429 	kSlidesAtSpeed1,                // Execute normal slides at speed 1 as if they were fine slides
430 	kPeriodsAreHertz,               // Compute note frequency in Hertz rather than periods
431 	kTempoClamp,                    // Clamp tempo to 32-255 range.
432 	kPerChannelGlobalVolSlide,      // Global volume slide memory is per-channel
433 	kPanOverride,                   // Panning commands override surround and random pan variation
434 
435 	kITInstrWithoutNote,            // Avoid instrument handling if there is no note
436 	kITVolColFinePortamento,        // Volume column portamento never does fine portamento
437 	kITArpeggio,                    // IT arpeggio algorithm
438 	kITOutOfRangeDelay,             // Out-of-range delay command behaviour in IT
439 	kITPortaMemoryShare,            // Gxx shares memory with Exx and Fxx
440 	kITPatternLoopTargetReset,      // After finishing a pattern loop, set the pattern loop target to the next row
441 	kITFT2PatternLoop,              // Nested pattern loop behaviour
442 	kITPingPongNoReset,             // Don't reset ping pong direction with instrument numbers
443 	kITEnvelopeReset,               // IT envelope reset behaviour
444 	kITClearOldNoteAfterCut,        // Forget the previous note after cutting it
445 	kITVibratoTremoloPanbrello,     // More IT-like Hxx / hx, Rxx, Yxx and autovibrato handling, including more precise LUTs
446 	kITTremor,                      // Ixx behaves like in IT
447 	kITRetrigger,                   // Qxx behaves like in IT
448 	kITMultiSampleBehaviour,        // Properly update C-5 frequency when changing in multisampled instrument
449 	kITPortaTargetReached,          // Clear portamento target after it has been reached
450 	kITPatternLoopBreak,            // Don't reset loop count on pattern break.
451 	kITOffset,                      // IT-style Oxx edge case handling
452 	kITSwingBehaviour,              // IT's swing behaviour
453 	kITNNAReset,                    // NNA is reset on every note change, not every instrument change
454 	kITSCxStopsSample,              // SCx really stops the sample and does not just mute it
455 	kITEnvelopePositionHandling,    // IT-style envelope position advance + enable/disable behaviour
456 	kITPortamentoInstrument,        // No sample changes during portamento with Compatible Gxx enabled, instrument envelope reset with portamento
457 	kITPingPongMode,                // Don't repeat last sample point in ping pong loop, like IT's software mixer
458 	kITRealNoteMapping,             // Use triggered note rather than translated note for PPS and other effects
459 	kITHighOffsetNoRetrig,          // SAx should not apply an offset effect to a note next to it
460 	kITFilterBehaviour,             // User IT's filter coefficients (unless extended filter range is used)
461 	kITNoSurroundPan,               // Panning and surround are mutually exclusive
462 	kITShortSampleRetrig,           // Don't retrigger already stopped channels
463 	kITPortaNoNote,                 // Don't apply any portamento if no previous note is playing
464 	kITFT2DontResetNoteOffOnPorta,  // Only reset note-off status on portamento in IT Compatible Gxx mode
465 	kITVolColMemory,                // IT volume column effects share their memory with the effect column
466 	kITPortamentoSwapResetsPos,     // Portamento with sample swap plays the new sample from the beginning
467 	kITEmptyNoteMapSlot,            // IT ignores instrument note map entries with no note completely
468 	kITFirstTickHandling,           // IT-style first tick handling
469 	kITSampleAndHoldPanbrello,      // IT-style sample&hold panbrello waveform
470 	kITClearPortaTarget,            // New notes reset portamento target in IT
471 	kITPanbrelloHold,               // Don't reset panbrello effect until next note or panning effect
472 	kITPanningReset,                // Sample and instrument panning is only applied on note change, not instrument change
473 	kITPatternLoopWithJumpsOld,     // Bxx on the same row as SBx terminates the loop in IT (old implementation of kITPatternLoopWithJumps)
474 	kITInstrWithNoteOff,            // Instrument number with note-off recalls default volume
475 
476 	kFT2Arpeggio,                   // FT2 arpeggio algorithm
477 	kFT2Retrigger,                  // Rxx behaves like in FT2
478 	kFT2VolColVibrato,              // Vibrato depth in volume column does not actually execute the vibrato effect
479 	kFT2PortaNoNote,                // Don't play portamento-ed note if no previous note is playing
480 	kFT2KeyOff,                     // FT2-style Kxx handling
481 	kFT2PanSlide,                   // Volume-column pan slides should be handled like fine slides
482 	kFT2ST3OffsetOutOfRange,        // Offset past sample end stops the note
483 	kFT2RestrictXCommand,           // Don't allow MPT extensions to Xxx command in XM
484 	kFT2RetrigWithNoteDelay,        // Retrigger envelopes if there is a note delay with no note
485 	kFT2SetPanEnvPos,               // Lxx only sets the pan env position if the volume envelope's sustain flag is set
486 	kFT2PortaIgnoreInstr,           // Portamento plus instrument number applies the volume settings of the new sample, but not the new sample itself.
487 	kFT2VolColMemory,               // No volume column memory in FT2
488 	kFT2LoopE60Restart,             // Next pattern starts on the same row as the last E60 command
489 	kFT2ProcessSilentChannels,      // Keep processing silent channels for later 3xx pickup
490 	kFT2ReloadSampleSettings,       // Reload sample settings even if a note-off is placed next to an instrument number
491 	kFT2PortaDelay,                 // Portamento with note delay next to it is ignored in FT2
492 	kFT2Transpose,                  // Out-of-range transposed notes in FT2
493 	kFT2PatternLoopWithJumps,       // Bxx or Dxx on the same row as E6x terminates the loop in FT2
494 	kFT2PortaTargetNoReset,         // Portamento target is not reset with new notes in FT2
495 	kFT2EnvelopeEscape,             // FT2 sustain point at end of envelope
496 	kFT2Tremor,                     // Txx behaves like in FT2
497 	kFT2OutOfRangeDelay,            // Out-of-range delay command behaviour in FT2
498 	kFT2Periods,                    // Use FT2's broken period handling
499 	kFT2PanWithDelayedNoteOff,      // Pan command with delayed note-off
500 	kFT2VolColDelay,                // FT2-style volume column handling if there is a note delay
501 	kFT2FinetunePrecision,          // Only take the upper 4 bits of sample finetune.
502 
503 	kST3NoMutedChannels,            // Don't process any effects on muted S3M channels
504 	kST3EffectMemory,               // Most effects share the same memory in ST3
505 	kST3PortaSampleChange,          // Portamento plus instrument number applies the volume settings of the new sample, but not the new sample itself (GUS behaviour).
506 	kST3VibratoMemory,              // Do not remember vibrato type in effect memory
507 	kST3LimitPeriod,                // Cut note instead of limiting  final period (ModPlug Tracker style)
508 	KST3PortaAfterArpeggio,         // Portamento after arpeggio continues at the note where the arpeggio left off
509 
510 	kMODOneShotLoops,               // Allow ProTracker-like oneshot loops
511 	kMODIgnorePanning,              // Do not process any panning commands
512 	kMODSampleSwap,                 // On-the-fly sample swapping
513 
514 	kFT2NoteOffFlags,               // Set and reset the correct fade/key-off flags with note-off and instrument number after note-off
515 	kITMultiSampleInstrumentNumber, // After portamento to different sample within multi-sampled instrument, lone instrument numbers in patterns always recall the new sample's default settings
516 	kRowDelayWithNoteDelay,         // Retrigger note delays on every reptition of a row
517 	kFT2MODTremoloRampWaveform,     // FT2-/ProTracker-compatible tremolo ramp down / triangle waveform
518 	kFT2PortaUpDownMemory,          // Portamento up and down have separate memory
519 
520 	kMODOutOfRangeNoteDelay,        // ProTracker behaviour for out-of-range note delays
521 	kMODTempoOnSecondTick,          // ProTracker sets tempo after the first tick
522 
523 	kFT2PanSustainRelease,          // If the sustain point of a panning envelope is reached before key-off, FT2 does not escape it anymore
524 	kLegacyReleaseNode,             // Legacy release node volume processing
525 	kOPLBeatingOscillators,         // Emulate beating FM oscillators from CDFM / Composer 670
526 	kST3OffsetWithoutInstrument,    // Note without instrument uses same offset as previous note
527 	kReleaseNodePastSustainBug,     // OpenMPT 1.23.01.02 / r4009 broke release nodes past the sustain point, fixed in OpenMPT 1.28
528 	kFT2NoteDelayWithoutInstr,      // Sometime between OpenMPT 1.18.03.00 and 1.19.01.00, delayed instrument-less notes in XM started recalling the default sample volume and panning
529 	kOPLFlexibleNoteOff,            // Full control after note-off over OPL voices, ^^^ sends note cut instead of just note-off
530 	kITInstrWithNoteOffOldEffects,  // Instrument number with note-off recalls default volume - special cases with Old Effects enabled
531 	kMIDIVolumeOnNoteOffBug,        // Update MIDI channel volume on note-off (legacy bug emulation)
532 	kITDoNotOverrideChannelPan,     // Sample / instrument pan does not override channel pan for following samples / instruments that are not panned
533 	kITPatternLoopWithJumps,        // Bxx right of SBx terminates the loop in IT
534 	kITDCTBehaviour,                // DCT="Sample" requires sample instrument, DCT="Note" checks old pattern note against new pattern note (previously was checking old pattern note against new translated note)
535 	kOPLwithNNA,                    // NNA note-off / fade are applied to OPL channels
536 	kST3RetrigAfterNoteCut,         // Qxy does not retrigger note after it has been cut with ^^^ or SCx
537 	kST3SampleSwap,                 // On-the-fly sample swapping (SoundBlaster behaviour)
538 	kOPLRealRetrig,                 // Retrigger effect (Qxy) restarts OPL notes
539 	kOPLNoResetAtEnvelopeEnd,       // Do not reset OPL channel status at end of envelope (OpenMPT 1.28 inconsistency with samples)
540 	kOPLNoteStopWith0Hz,            // Set note frequency to 0 Hz to "stop" OPL notes
541 	kOPLNoteOffOnNoteChange,        // Send note-off events for old note on every note change
542 	kFT2PortaResetDirection,        // Reset portamento direction when reaching portamento target from below
543 	kApplyUpperPeriodLimit,         // Enforce m_nMaxPeriod
544 	kApplyOffsetWithoutNote,        // Offset commands even work when there's no note next to them (e.g. DMF, MDL, PLM formats)
545 	kITPitchPanSeparation,          // Pitch/Pan Separation can be overridden by panning commands (this also fixes a bug where any "special" notes affect PPS)
546 	kImprecisePingPongLoops,        // Use old (less precise) ping-pong overshoot calculation
547 
548 	// Add new play behaviours here.
549 
550 	kMaxPlayBehaviours,
551 };
552 
553 
554 // Tempo swing determines how much every row in modern tempo mode contributes to a beat.
555 class TempoSwing : public std::vector<uint32>
556 {
557 public:
558 	static constexpr uint32 Unity = 1u << 24;
559 	// Normalize the tempo swing coefficients so that they add up to exactly the specified tempo again
560 	void Normalize();
561 	void resize(size_type newSize, value_type val = Unity) { std::vector<uint32>::resize(newSize, val); Normalize(); }
562 
563 	static void Serialize(std::ostream &oStrm, const TempoSwing &swing);
564 	static void Deserialize(std::istream &iStrm, TempoSwing &swing, const size_t);
565 };
566 
567 
568 // Sample position and sample position increment value
569 struct SamplePosition
570 {
571 	using value_t = int64;
572 	using unsigned_value_t = uint64;
573 
574 protected:
575 	value_t v = 0;
576 
577 public:
578 	static constexpr uint32 fractMax = 0xFFFFFFFFu;
579 
SamplePositionSamplePosition580 	MPT_CONSTEXPRINLINE SamplePosition() { }
SamplePositionSamplePosition581 	MPT_CONSTEXPRINLINE explicit SamplePosition(value_t pos) : v(pos) { }
SamplePositionSamplePosition582 	MPT_CONSTEXPRINLINE SamplePosition(int32 intPart, uint32 fractPart) : v((static_cast<value_t>(intPart) * (1ll << 32)) | fractPart) { }
RatioSamplePosition583 	static SamplePosition Ratio(uint32 dividend, uint32 divisor) { return SamplePosition((static_cast<int64>(dividend) << 32) / divisor); }
FromDoubleSamplePosition584 	static SamplePosition FromDouble(double pos) { return SamplePosition(static_cast<value_t>(pos * 4294967296.0)); }
ToDoubleSamplePosition585 	double ToDouble() const { return v / 4294967296.0; }
586 
587 	// Set integer and fractional part
588 	MPT_CONSTEXPRINLINE SamplePosition &Set(int32 intPart, uint32 fractPart = 0) { v = (static_cast<int64>(intPart) << 32) | fractPart; return *this; }
589 	// Set integer part, keep fractional part
SetIntSamplePosition590 	MPT_CONSTEXPRINLINE SamplePosition &SetInt(int32 intPart) { v = (static_cast<value_t>(intPart) << 32) | GetFract(); return *this; }
591 	// Get integer part (as sample length / position)
GetUIntSamplePosition592 	MPT_CONSTEXPRINLINE SmpLength GetUInt() const { return static_cast<SmpLength>(static_cast<unsigned_value_t>(v) >> 32); }
593 	// Get integer part
GetIntSamplePosition594 	MPT_CONSTEXPRINLINE int32 GetInt() const { return static_cast<int32>(static_cast<unsigned_value_t>(v) >> 32); }
595 	// Get fractional part
GetFractSamplePosition596 	MPT_CONSTEXPRINLINE uint32 GetFract() const { return static_cast<uint32>(v); }
597 	// Get the inverted fractional part
GetInvertedFractSamplePosition598 	MPT_CONSTEXPRINLINE SamplePosition GetInvertedFract() const { return SamplePosition(0x100000000ll - GetFract()); }
599 	// Get the raw fixed-point value
GetRawSamplePosition600 	MPT_CONSTEXPRINLINE int64 GetRaw() const { return v; }
601 	// Negate the current value
NegateSamplePosition602 	MPT_CONSTEXPRINLINE SamplePosition &Negate() { v = -v; return *this; }
603 	// Multiply and divide by given integer scalars
MulDivSamplePosition604 	MPT_CONSTEXPRINLINE SamplePosition &MulDiv(uint32 mul, uint32 div) { v = (v * mul) / div; return *this; }
605 	// Removes the integer part, only keeping fractions
RemoveIntSamplePosition606 	MPT_CONSTEXPRINLINE SamplePosition &RemoveInt() { v &= fractMax; return *this; }
607 	// Check if value is 1.0
IsUnitySamplePosition608 	MPT_CONSTEXPRINLINE bool IsUnity() const { return v == 0x100000000ll; }
609 	// Check if value is 0
IsZeroSamplePosition610 	MPT_CONSTEXPRINLINE bool IsZero() const { return v == 0; }
611 	// Check if value is > 0
IsPositiveSamplePosition612 	MPT_CONSTEXPRINLINE bool IsPositive() const { return v > 0; }
613 	// Check if value is < 0
IsNegativeSamplePosition614 	MPT_CONSTEXPRINLINE bool IsNegative() const { return v < 0; }
615 
616 	// Addition / subtraction of another fixed-point number
617 	SamplePosition operator+ (const SamplePosition &other) const { return SamplePosition(v + other.v); }
618 	SamplePosition operator- (const SamplePosition &other) const { return SamplePosition(v - other.v); }
619 	void operator+= (const SamplePosition &other) { v += other.v; }
620 	void operator-= (const SamplePosition &other) { v -= other.v; }
621 
622 	// Multiplication with integer scalar
623 	template<typename T>
624 	SamplePosition operator* (T other) const { return SamplePosition(static_cast<value_t>(v * other)); }
625 	template<typename T>
626 	void operator*= (T other) { v = static_cast<value_t>(v *other); }
627 
628 	// Division by other fractional point number; returns scalar
629 	value_t operator/ (SamplePosition other) const { return v / other.v; }
630 	// Division by scalar; returns fractional point number
631 	SamplePosition operator/ (int div) const { return SamplePosition(v / div); }
632 
633 	MPT_CONSTEXPRINLINE bool operator==(const SamplePosition &other) const { return v == other.v; }
634 	MPT_CONSTEXPRINLINE bool operator!=(const SamplePosition &other) const { return v != other.v; }
635 	MPT_CONSTEXPRINLINE bool operator<=(const SamplePosition &other) const { return v <= other.v; }
636 	MPT_CONSTEXPRINLINE bool operator>=(const SamplePosition &other) const { return v >= other.v; }
637 	MPT_CONSTEXPRINLINE bool operator<(const SamplePosition &other) const { return v < other.v; }
638 	MPT_CONSTEXPRINLINE bool operator>(const SamplePosition &other) const { return v > other.v; }
639 };
640 
641 
642 // Aaaand another fixed-point type, e.g. used for fractional tempos
643 // Note that this doesn't use classical bit shifting for the fixed point part.
644 // This is mostly for the clarity of stored values and to be able to represent any value .0000 to .9999 properly.
645 // For easier debugging, use the Debugger Visualizers available in build/vs/debug/
646 // to easily display the stored values.
647 template<size_t FFact, typename T>
648 struct FPInt
649 {
650 protected:
651 	T v;
FPIntFPInt652 	MPT_CONSTEXPRINLINE FPInt(T rawValue) : v(rawValue) { }
653 
654 public:
655 	enum : size_t { fractFact = FFact };
656 	using store_t = T;
657 
FPIntFPInt658 	MPT_CONSTEXPRINLINE FPInt() : v(0) { }
FPIntFPInt659 	MPT_CONSTEXPRINLINE FPInt(T intPart, T fractPart) : v((intPart * fractFact) + (fractPart % fractFact)) { }
FPIntFPInt660 	explicit MPT_CONSTEXPRINLINE FPInt(float f) : v(mpt::saturate_round<T>(f * float(fractFact))) { }
FPIntFPInt661 	explicit MPT_CONSTEXPRINLINE FPInt(double f) : v(mpt::saturate_round<T>(f * double(fractFact))) { }
662 
663 	// Set integer and fractional part
664 	MPT_CONSTEXPRINLINE FPInt<fractFact, T> &Set(T intPart, T fractPart = 0) { v = (intPart * fractFact) + (fractPart % fractFact); return *this; }
665 	// Set raw internal representation directly
SetRawFPInt666 	MPT_CONSTEXPRINLINE FPInt<fractFact, T> &SetRaw(T value) { v = value; return *this; }
667 	// Retrieve the integer part of the stored value
GetIntFPInt668 	MPT_CONSTEXPRINLINE T GetInt() const { return v / fractFact; }
669 	// Retrieve the fractional part of the stored value
GetFractFPInt670 	MPT_CONSTEXPRINLINE T GetFract() const { return v % fractFact; }
671 	// Retrieve the raw internal representation of the stored value
GetRawFPInt672 	MPT_CONSTEXPRINLINE T GetRaw() const { return v; }
673 	// Formats the stored value as a floating-point value
ToDoubleFPInt674 	MPT_CONSTEXPRINLINE double ToDouble() const { return v / double(fractFact); }
675 
676 	MPT_CONSTEXPRINLINE friend FPInt<fractFact, T> operator+ (const FPInt<fractFact, T> &a, const FPInt<fractFact, T> &b) noexcept { return FPInt<fractFact, T>(a.v + b.v); }
677 	MPT_CONSTEXPRINLINE friend FPInt<fractFact, T> operator- (const FPInt<fractFact, T> &a, const FPInt<fractFact, T> &b) noexcept { return FPInt<fractFact, T>(a.v - b.v); }
678 	MPT_CONSTEXPRINLINE FPInt<fractFact, T> operator+= (const FPInt<fractFact, T> &other) noexcept { v += other.v; return *this; }
679 	MPT_CONSTEXPRINLINE FPInt<fractFact, T> operator-= (const FPInt<fractFact, T> &other) noexcept { v -= other.v; return *this; }
680 
681 	MPT_CONSTEXPRINLINE friend bool operator== (const FPInt<fractFact, T> &a, const FPInt<fractFact, T> &b) noexcept { return a.v == b.v; }
682 	MPT_CONSTEXPRINLINE friend bool operator!= (const FPInt<fractFact, T> &a, const FPInt<fractFact, T> &b) noexcept { return a.v != b.v; }
683 	MPT_CONSTEXPRINLINE friend bool operator<= (const FPInt<fractFact, T> &a, const FPInt<fractFact, T> &b) noexcept { return a.v <= b.v; }
684 	MPT_CONSTEXPRINLINE friend bool operator>= (const FPInt<fractFact, T> &a, const FPInt<fractFact, T> &b) noexcept { return a.v >= b.v; }
685 	MPT_CONSTEXPRINLINE friend bool operator< (const FPInt<fractFact, T> &a, const FPInt<fractFact, T> &b) noexcept { return a.v < b.v; }
686 	MPT_CONSTEXPRINLINE friend bool operator> (const FPInt<fractFact, T> &a, const FPInt<fractFact, T> &b) noexcept { return a.v > b.v; }
687 };
688 
689 using TEMPO = FPInt<10000, uint32>;
690 
691 using OPLPatch = std::array<uint8, 12>;
692 
693 OPENMPT_NAMESPACE_END
694