ScummVM API documentation
Synth.h
1 /* Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009 Dean Beeler, Jerome Fisher
2  * Copyright (C) 2011-2022 Dean Beeler, Jerome Fisher, Sergey V. Mikayev
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef MT32EMU_SYNTH_H
19 #define MT32EMU_SYNTH_H
20 
21 #include <cstdarg>
22 #include <cstddef>
23 #include <cstring>
24 
25 #include "globals.h"
26 #include "Types.h"
27 #include "Enumerations.h"
28 
29 namespace MT32Emu {
30 
31 class Analog;
32 class BReverbModel;
33 class Extensions;
34 class MemoryRegion;
35 class MidiEventQueue;
36 class Part;
37 class Poly;
38 class Partial;
39 class PartialManager;
40 class Renderer;
41 class ROMImage;
42 
43 class PatchTempMemoryRegion;
44 class RhythmTempMemoryRegion;
45 class TimbreTempMemoryRegion;
46 class PatchesMemoryRegion;
47 class TimbresMemoryRegion;
48 class SystemMemoryRegion;
49 class DisplayMemoryRegion;
50 class ResetMemoryRegion;
51 
52 struct ControlROMFeatureSet;
53 struct ControlROMMap;
54 struct PCMWaveEntry;
55 struct MemParams;
56 
57 const Bit8u SYSEX_MANUFACTURER_ROLAND = 0x41;
58 
59 const Bit8u SYSEX_MDL_MT32 = 0x16;
60 const Bit8u SYSEX_MDL_D50 = 0x14;
61 
62 const Bit8u SYSEX_CMD_RQ1 = 0x11; // Request data #1
63 const Bit8u SYSEX_CMD_DT1 = 0x12; // Data set 1
64 const Bit8u SYSEX_CMD_WSD = 0x40; // Want to send data
65 const Bit8u SYSEX_CMD_RQD = 0x41; // Request data
66 const Bit8u SYSEX_CMD_DAT = 0x42; // Data set
67 const Bit8u SYSEX_CMD_ACK = 0x43; // Acknowledge
68 const Bit8u SYSEX_CMD_EOD = 0x45; // End of data
69 const Bit8u SYSEX_CMD_ERR = 0x4E; // Communications error
70 const Bit8u SYSEX_CMD_RJC = 0x4F; // Rejection
71 
72 // This value isn't quite correct: the new-gen MT-32 control ROMs (ver. 2.XX) are twice as big.
73 // Nevertheless, this is still relevant for library internal usage because the higher half
74 // of those ROMs only contains the demo songs in all cases.
75 const Bit32u CONTROL_ROM_SIZE = 64 * 1024;
76 
77 // Set of multiplexed output streams appeared at the DAC entrance.
78 template <class T>
80  T *nonReverbLeft;
81  T *nonReverbRight;
82  T *reverbDryLeft;
83  T *reverbDryRight;
84  T *reverbWetLeft;
85  T *reverbWetRight;
86 };
87 
88 // Class for the client to supply callbacks for reporting various errors and information
89 class MT32EMU_EXPORT ReportHandler {
90 public:
91  virtual ~ReportHandler() {}
92 
93  // Callback for debug messages, in vprintf() format
94  virtual void printDebug(const char *fmt, va_list list);
95  // Callbacks for reporting errors
96  virtual void onErrorControlROM() {}
97  virtual void onErrorPCMROM() {}
98  // Callback for reporting about displaying a new custom message on LCD
99  virtual void showLCDMessage(const char *message);
100  // Callback for reporting actual processing of a MIDI message
101  virtual void onMIDIMessagePlayed() {}
102  // Callback for reporting an overflow of the input MIDI queue.
103  // Returns true if a recovery action was taken and yet another attempt to enqueue the MIDI event is desired.
104  virtual bool onMIDIQueueOverflow() { return false; }
105  // Callback invoked when a System Realtime MIDI message is detected at the input.
106  virtual void onMIDISystemRealtime(Bit8u /* systemRealtime */) {}
107  // Callbacks for reporting system events
108  virtual void onDeviceReset() {}
109  virtual void onDeviceReconfig() {}
110  // Callbacks for reporting changes of reverb settings
111  virtual void onNewReverbMode(Bit8u /* mode */) {}
112  virtual void onNewReverbTime(Bit8u /* time */) {}
113  virtual void onNewReverbLevel(Bit8u /* level */) {}
114  // Callbacks for reporting various information
115  virtual void onPolyStateChanged(Bit8u /* partNum */) {}
116  virtual void onProgramChanged(Bit8u /* partNum */, const char * /* soundGroupName */, const char * /* patchName */) {}
117 };
118 
119 // Extends ReportHandler, so that the client may supply callbacks for reporting signals about updated display state.
120 class MT32EMU_EXPORT_V(2.6) ReportHandler2 : public ReportHandler {
121 public:
122  virtual ~ReportHandler2() {}
123 
124  // Invoked to signal about a change of the emulated LCD state. Use method Synth::getDisplayState to retrieve the actual data.
125  // This callback will not be invoked on further changes, until the client retrieves the LCD state.
126  virtual void onLCDStateUpdated() {}
127  // Invoked when the emulated MIDI MESSAGE LED changes state. The ledState parameter represents whether the LED is ON.
128  virtual void onMidiMessageLEDStateUpdated(bool /* ledState */) {}
129 };
130 
131 class Synth {
132 friend class DefaultMidiStreamParser;
133 friend class Display;
134 friend class MemoryRegion;
135 friend class Part;
136 friend class Partial;
137 friend class PartialManager;
138 friend class Poly;
139 friend class Renderer;
140 friend class RhythmPart;
141 friend class SamplerateAdapter;
142 friend class SoxrAdapter;
143 friend class TVA;
144 friend class TVF;
145 friend class TVP;
146 
147 private:
148  // **************************** Implementation fields **************************
149 
150  PatchTempMemoryRegion *patchTempMemoryRegion;
151  RhythmTempMemoryRegion *rhythmTempMemoryRegion;
152  TimbreTempMemoryRegion *timbreTempMemoryRegion;
153  PatchesMemoryRegion *patchesMemoryRegion;
154  TimbresMemoryRegion *timbresMemoryRegion;
155  SystemMemoryRegion *systemMemoryRegion;
156  DisplayMemoryRegion *displayMemoryRegion;
157  ResetMemoryRegion *resetMemoryRegion;
158 
159  Bit8u *paddedTimbreMaxTable;
160 
161  PCMWaveEntry *pcmWaves; // Array
162 
163  const ControlROMFeatureSet *controlROMFeatures;
164  const ControlROMMap *controlROMMap;
165  Bit8u controlROMData[CONTROL_ROM_SIZE];
166  Bit16s *pcmROMData;
167  size_t pcmROMSize; // This is in 16-bit samples, therefore half the number of bytes in the ROM
168 
169  Bit8u soundGroupIx[128]; // For each standard timbre
170  const char (*soundGroupNames)[9]; // Array
171 
172  Bit32u partialCount;
173  Bit8u nukeme[16]; // FIXME: Nuke it. For binary compatibility only.
174 
175  MidiEventQueue *midiQueue;
176  volatile Bit32u lastReceivedMIDIEventTimestamp;
177  volatile Bit32u renderedSampleCount;
178 
179  MemParams &mt32ram, &mt32default;
180 
181  BReverbModel *reverbModels[4];
182  BReverbModel *reverbModel;
183  bool reverbOverridden;
184 
185  MIDIDelayMode midiDelayMode;
186  DACInputMode dacInputMode;
187 
188  float outputGain;
189  float reverbOutputGain;
190 
191  bool reversedStereoEnabled;
192 
193  bool opened;
194  bool activated;
195 
196  bool isDefaultReportHandler; // No longer used, retained for binary compatibility only.
197  ReportHandler *reportHandler;
198 
199  PartialManager *partialManager;
200  Part *parts[9];
201 
202  // When a partial needs to be aborted to free it up for use by a new Poly,
203  // the controller will busy-loop waiting for the sound to finish.
204  // We emulate this by delaying new MIDI events processing until abortion finishes.
205  Poly *abortingPoly;
206 
207  Analog *analog;
208  Renderer *renderer;
209 
210  // Binary compatibility helper.
211  Extensions &extensions;
212 
213  // **************************** Implementation methods **************************
214 
215  Bit32u addMIDIInterfaceDelay(Bit32u len, Bit32u timestamp);
216  bool isAbortingPoly() const { return abortingPoly != NULL; }
217 
218  void writeSysexGlobal(Bit32u addr, const Bit8u *sysex, Bit32u len);
219  void readSysex(Bit8u channel, const Bit8u *sysex, Bit32u len) const;
220  void initMemoryRegions();
221  void deleteMemoryRegions();
222  MemoryRegion *findMemoryRegion(Bit32u addr);
223  void writeMemoryRegion(const MemoryRegion *region, Bit32u addr, Bit32u len, const Bit8u *data);
224  void readMemoryRegion(const MemoryRegion *region, Bit32u addr, Bit32u len, Bit8u *data);
225 
226  bool loadControlROM(const ROMImage &controlROMImage);
227  bool loadPCMROM(const ROMImage &pcmROMImage);
228 
229  bool initPCMList(Bit16u mapAddress, Bit16u count);
230  bool initTimbres(Bit16u mapAddress, Bit16u offset, Bit16u timbreCount, Bit16u startTimbre, bool compressed);
231  bool initCompressedTimbre(Bit16u drumNum, const Bit8u *mem, Bit32u memLen);
232  void initReverbModels(bool mt32CompatibleMode);
233  void initSoundGroups(char newSoundGroupNames[][9]);
234 
235  void refreshSystemMasterTune();
236  void refreshSystemReverbParameters();
237  void refreshSystemReserveSettings();
238  void refreshSystemChanAssign(Bit8u firstPart, Bit8u lastPart);
239  void refreshSystemMasterVol();
240  void refreshSystem();
241  void reset();
242  void dispose();
243 
244  void printPartialUsage(Bit32u sampleOffset = 0);
245 
246  void rhythmNotePlayed() const;
247  void voicePartStateChanged(Bit8u partNum, bool activated) const;
248  void newTimbreSet(Bit8u partNum) const;
249  const char *getSoundGroupName(const Part *part) const;
250  const char *getSoundGroupName(Bit8u timbreGroup, Bit8u timbreNumber) const;
251  void printDebug(const char *fmt, ...);
252 
253  // partNum should be 0..7 for Part 1..8, or 8 for Rhythm
254  const Part *getPart(Bit8u partNum) const;
255 
256  void resetMasterTunePitchDelta();
257  Bit32s getMasterTunePitchDelta() const;
258 
259 public:
260  static inline Bit16s clipSampleEx(Bit32s sampleEx) {
261  // Clamp values above 32767 to 32767, and values below -32768 to -32768
262  // FIXME: Do we really need this stuff? I think these branches are very well predicted. Instead, this introduces a chain.
263  // The version below is actually a bit faster on my system...
264  //return ((sampleEx + 0x8000) & ~0xFFFF) ? Bit16s((sampleEx >> 31) ^ 0x7FFF) : (Bit16s)sampleEx;
265  return ((-0x8000 <= sampleEx) && (sampleEx <= 0x7FFF)) ? Bit16s(sampleEx) : Bit16s((sampleEx >> 31) ^ 0x7FFF);
266  }
267 
268  static inline float clipSampleEx(float sampleEx) {
269  return sampleEx;
270  }
271 
272  template <class S>
273  static inline void muteSampleBuffer(S *buffer, Bit32u len) {
274  if (buffer == NULL) return;
275  memset(buffer, 0, len * sizeof(S));
276  }
277 
278  static inline void muteSampleBuffer(float *buffer, Bit32u len) {
279  if (buffer == NULL) return;
280  // FIXME: Use memset() where compatibility is guaranteed (if this turns out to be a win)
281  while (len--) {
282  *(buffer++) = 0.0f;
283  }
284  }
285 
286  static inline Bit16s convertSample(float sample) {
287  return Synth::clipSampleEx(Bit32s(sample * 32768.0f)); // This multiplier corresponds to normalised floats
288  }
289 
290  static inline float convertSample(Bit16s sample) {
291  return float(sample) / 32768.0f; // This multiplier corresponds to normalised floats
292  }
293 
294  // Returns library version as an integer in format: 0x00MMmmpp, where:
295  // MM - major version number
296  // mm - minor version number
297  // pp - patch number
298  MT32EMU_EXPORT static Bit32u getLibraryVersionInt();
299  // Returns library version as a C-string in format: "MAJOR.MINOR.PATCH"
300  MT32EMU_EXPORT static const char *getLibraryVersionString();
301 
302  MT32EMU_EXPORT static Bit32u getShortMessageLength(Bit32u msg);
303  MT32EMU_EXPORT static Bit8u calcSysexChecksum(const Bit8u *data, const Bit32u len, const Bit8u initChecksum = 0);
304 
305  // Returns output sample rate used in emulation of stereo analog circuitry of hardware units.
306  // See comment for AnalogOutputMode.
307  MT32EMU_EXPORT static Bit32u getStereoOutputSampleRate(AnalogOutputMode analogOutputMode);
308 
309  // Optionally sets callbacks for reporting various errors, information and debug messages
310  MT32EMU_EXPORT explicit Synth(ReportHandler *useReportHandler = NULL);
311  MT32EMU_EXPORT ~Synth();
312 
313  // Sets an implementation of ReportHandler2 interface for reporting various errors, information and debug messages.
314  // If the argument is NULL, the default implementation is installed as a fallback.
315  MT32EMU_EXPORT_V(2.6) void setReportHandler2(ReportHandler2 *reportHandler2);
316 
317  // Used to initialise the MT-32. Must be called before any other function.
318  // Returns true if initialization was successful, otherwise returns false.
319  // controlROMImage and pcmROMImage represent full Control and PCM ROM images for use by synth.
320  // usePartialCount sets the maximum number of partials playing simultaneously for this session (optional).
321  // analogOutputMode sets the mode for emulation of analogue circuitry of the hardware units (optional).
322  MT32EMU_EXPORT bool open(const ROMImage &controlROMImage, const ROMImage &pcmROMImage, Bit32u usePartialCount = DEFAULT_MAX_PARTIALS, AnalogOutputMode analogOutputMode = AnalogOutputMode_COARSE);
323 
324  // Overloaded method which opens the synth with default partial count.
325  MT32EMU_EXPORT bool open(const ROMImage &controlROMImage, const ROMImage &pcmROMImage, AnalogOutputMode analogOutputMode);
326 
327  // Closes the MT-32 and deallocates any memory used by the synthesizer
328  MT32EMU_EXPORT void close();
329 
330  // Returns true if the synth is in completely initialized state, otherwise returns false.
331  MT32EMU_EXPORT bool isOpen() const;
332 
333  // All the enqueued events are processed by the synth immediately.
334  MT32EMU_EXPORT void flushMIDIQueue();
335 
336  // Sets size of the internal MIDI event queue. The queue size is set to the minimum power of 2 that is greater or equal to the size specified.
337  // The queue is flushed before reallocation.
338  // Returns the actual queue size being used.
339  MT32EMU_EXPORT Bit32u setMIDIEventQueueSize(Bit32u requestedSize);
340 
341  // Configures the SysEx storage of the internal MIDI event queue.
342  // Supplying 0 in the storageBufferSize argument makes the SysEx data stored
343  // in multiple dynamically allocated buffers per MIDI event. These buffers are only disposed
344  // when a new MIDI event replaces the SysEx event in the queue, thus never on the rendering thread.
345  // This is the default behaviour.
346  // In contrast, when a positive value is specified, SysEx data will be stored in a single preallocated buffer,
347  // which makes this kind of storage safe for use in a realtime thread. Additionally, the space retained
348  // by a SysEx event, that has been processed and thus is no longer necessary, is disposed instantly.
349  // Note, the queue is flushed and recreated in the process so that its size remains intact.
350  MT32EMU_EXPORT void configureMIDIEventQueueSysexStorage(Bit32u storageBufferSize);
351 
352  // Returns current value of the global counter of samples rendered since the synth was created (at the native sample rate 32000 Hz).
353  // This method helps to compute accurate timestamp of a MIDI message to use with the methods below.
354  MT32EMU_EXPORT Bit32u getInternalRenderedSampleCount() const;
355 
356  // Enqueues a MIDI event for subsequent playback.
357  // The MIDI event will be processed not before the specified timestamp.
358  // The timestamp is measured as the global rendered sample count since the synth was created (at the native sample rate 32000 Hz).
359  // The minimum delay involves emulation of the delay introduced while the event is transferred via MIDI interface
360  // and emulation of the MCU busy-loop while it frees partials for use by a new Poly.
361  // Calls from multiple threads must be synchronised, although, no synchronisation is required with the rendering thread.
362  // The methods return false if the MIDI event queue is full and the message cannot be enqueued.
363 
364  // Enqueues a single short MIDI message to play at specified time. The message must contain a status byte.
365  MT32EMU_EXPORT bool playMsg(Bit32u msg, Bit32u timestamp);
366  // Enqueues a single well formed System Exclusive MIDI message to play at specified time.
367  MT32EMU_EXPORT bool playSysex(const Bit8u *sysex, Bit32u len, Bit32u timestamp);
368 
369  // Enqueues a single short MIDI message to be processed ASAP. The message must contain a status byte.
370  MT32EMU_EXPORT bool playMsg(Bit32u msg);
371  // Enqueues a single well formed System Exclusive MIDI message to be processed ASAP.
372  MT32EMU_EXPORT bool playSysex(const Bit8u *sysex, Bit32u len);
373 
374  // WARNING:
375  // The methods below don't ensure minimum 1-sample delay between sequential MIDI events,
376  // and a sequence of NoteOn and immediately succeeding NoteOff messages is always silent.
377  // A thread that invokes these methods must be explicitly synchronised with the thread performing sample rendering.
378 
379  // Sends a short MIDI message to the synth for immediate playback. The message must contain a status byte.
380  // See the WARNING above.
381  MT32EMU_EXPORT void playMsgNow(Bit32u msg);
382  // Sends unpacked short MIDI message to the synth for immediate playback. The message must contain a status byte.
383  // See the WARNING above.
384  MT32EMU_EXPORT void playMsgOnPart(Bit8u part, Bit8u code, Bit8u note, Bit8u velocity);
385 
386  // Sends a single well formed System Exclusive MIDI message for immediate processing. The length is in bytes.
387  // See the WARNING above.
388  MT32EMU_EXPORT void playSysexNow(const Bit8u *sysex, Bit32u len);
389  // Sends inner body of a System Exclusive MIDI message for direct processing. The length is in bytes.
390  // See the WARNING above.
391  MT32EMU_EXPORT void playSysexWithoutFraming(const Bit8u *sysex, Bit32u len);
392  // Sends inner body of a System Exclusive MIDI message for direct processing. The length is in bytes.
393  // See the WARNING above.
394  MT32EMU_EXPORT void playSysexWithoutHeader(Bit8u device, Bit8u command, const Bit8u *sysex, Bit32u len);
395  // Sends inner body of a System Exclusive MIDI message for direct processing. The length is in bytes.
396  // See the WARNING above.
397  MT32EMU_EXPORT void writeSysex(Bit8u channel, const Bit8u *sysex, Bit32u len);
398 
399  // Allows to disable wet reverb output altogether.
400  MT32EMU_EXPORT void setReverbEnabled(bool reverbEnabled);
401  // Returns whether wet reverb output is enabled.
402  MT32EMU_EXPORT bool isReverbEnabled() const;
403  // Sets override reverb mode. In this mode, emulation ignores sysexes (or the related part of them) which control the reverb parameters.
404  // This mode is in effect until it is turned off. When the synth is re-opened, the override mode is unchanged but the state
405  // of the reverb model is reset to default.
406  MT32EMU_EXPORT void setReverbOverridden(bool reverbOverridden);
407  // Returns whether reverb settings are overridden.
408  MT32EMU_EXPORT bool isReverbOverridden() const;
409  // Forces reverb model compatibility mode. By default, the compatibility mode corresponds to the used control ROM version.
410  // Invoking this method with the argument set to true forces emulation of old MT-32 reverb circuit.
411  // When the argument is false, emulation of the reverb circuit used in new generation of MT-32 compatible modules is enforced
412  // (these include CM-32L and LAPC-I).
413  MT32EMU_EXPORT void setReverbCompatibilityMode(bool mt32CompatibleMode);
414  // Returns whether reverb is in old MT-32 compatibility mode.
415  MT32EMU_EXPORT bool isMT32ReverbCompatibilityMode() const;
416  // Returns whether default reverb compatibility mode is the old MT-32 compatibility mode.
417  MT32EMU_EXPORT bool isDefaultReverbMT32Compatible() const;
418  // If enabled, reverb buffers for all modes are kept around allocated all the time to avoid memory
419  // allocating/freeing in the rendering thread, which may be required for realtime operation.
420  // Otherwise, reverb buffers that are not in use are deleted to save memory (the default behaviour).
421  MT32EMU_EXPORT void preallocateReverbMemory(bool enabled);
422  // Sets new DAC input mode. See DACInputMode for details.
423  MT32EMU_EXPORT void setDACInputMode(DACInputMode mode);
424  // Returns current DAC input mode. See DACInputMode for details.
425  MT32EMU_EXPORT DACInputMode getDACInputMode() const;
426  // Sets new MIDI delay mode. See MIDIDelayMode for details.
427  MT32EMU_EXPORT void setMIDIDelayMode(MIDIDelayMode mode);
428  // Returns current MIDI delay mode. See MIDIDelayMode for details.
429  MT32EMU_EXPORT MIDIDelayMode getMIDIDelayMode() const;
430 
431  // Sets output gain factor for synth output channels. Applied to all output samples and unrelated with the synth's Master volume,
432  // it rather corresponds to the gain of the output analog circuitry of the hardware units. However, together with setReverbOutputGain()
433  // it offers to the user a capability to control the gain of reverb and non-reverb output channels independently.
434  MT32EMU_EXPORT void setOutputGain(float gain);
435  // Returns current output gain factor for synth output channels.
436  MT32EMU_EXPORT float getOutputGain() const;
437 
438  // Sets output gain factor for the reverb wet output channels. It rather corresponds to the gain of the output
439  // analog circuitry of the hardware units. However, together with setOutputGain() it offers to the user a capability
440  // to control the gain of reverb and non-reverb output channels independently.
441  //
442  // Note: We're currently emulate CM-32L/CM-64 reverb quite accurately and the reverb output level closely
443  // corresponds to the level of digital capture. Although, according to the CM-64 PCB schematic,
444  // there is a difference in the reverb analogue circuit, and the resulting output gain is 0.68
445  // of that for LA32 analogue output. This factor is applied to the reverb output gain.
446  MT32EMU_EXPORT void setReverbOutputGain(float gain);
447  // Returns current output gain factor for reverb wet output channels.
448  MT32EMU_EXPORT float getReverbOutputGain() const;
449 
450  // Sets (or removes) an override for the current volume (output level) on a specific part.
451  // When the part volume is overridden, the MIDI controller Volume (7) on the MIDI channel this part is assigned to
452  // has no effect on the output level of this part. Similarly, the output level value set on this part via a SysEx that
453  // modifies the Patch temp structure is disregarded.
454  // To enable the override mode, argument volumeOverride should be in range 0..100, setting a value outside this range
455  // disables the previously set override, if any.
456  // Note: Setting volumeOverride to 0 mutes the part completely, meaning no sound is generated at all.
457  // This is unlike the behaviour of real devices - setting 0 volume on a part may leave it still producing
458  // sound at a very low level.
459  // Argument partNumber should be 0..7 for Part 1..8, or 8 for Rhythm.
460  MT32EMU_EXPORT_V(2.6) void setPartVolumeOverride(Bit8u partNumber, Bit8u volumeOverride);
461  // Returns the overridden volume previously set on a specific part; a value outside the range 0..100 means no override
462  // is currently in effect.
463  // Argument partNumber should be 0..7 for Part 1..8, or 8 for Rhythm.
464  MT32EMU_EXPORT_V(2.6) Bit8u getPartVolumeOverride(Bit8u partNumber) const;
465 
466  // Swaps left and right output channels.
467  MT32EMU_EXPORT void setReversedStereoEnabled(bool enabled);
468  // Returns whether left and right output channels are swapped.
469  MT32EMU_EXPORT bool isReversedStereoEnabled() const;
470 
471  // Allows to toggle the NiceAmpRamp mode.
472  // In this mode, we want to ensure that amp ramp never jumps to the target
473  // value and always gradually increases or decreases. It seems that real units
474  // do not bother to always check if a newly started ramp leads to a jump.
475  // We also prefer the quality improvement over the emulation accuracy,
476  // so this mode is enabled by default.
477  MT32EMU_EXPORT void setNiceAmpRampEnabled(bool enabled);
478  // Returns whether NiceAmpRamp mode is enabled.
479  MT32EMU_EXPORT bool isNiceAmpRampEnabled() const;
480 
481  // Allows to toggle the NicePanning mode.
482  // Despite the Roland's manual specifies allowed panpot values in range 0-14,
483  // the LA-32 only receives 3-bit pan setting in fact. In particular, this
484  // makes it impossible to set the "middle" panning for a single partial.
485  // In the NicePanning mode, we enlarge the pan setting accuracy to 4 bits
486  // making it smoother thus sacrificing the emulation accuracy.
487  // This mode is disabled by default.
488  MT32EMU_EXPORT void setNicePanningEnabled(bool enabled);
489  // Returns whether NicePanning mode is enabled.
490  MT32EMU_EXPORT bool isNicePanningEnabled() const;
491 
492  // Allows to toggle the NicePartialMixing mode.
493  // LA-32 is known to mix partials either in-phase (so that they are added)
494  // or in counter-phase (so that they are subtracted instead).
495  // In some cases, this quirk isn't highly desired because a pair of closely
496  // sounding partials may occasionally cancel out.
497  // In the NicePartialMixing mode, the mixing is always performed in-phase,
498  // thus making the behaviour more predictable.
499  // This mode is disabled by default.
500  MT32EMU_EXPORT void setNicePartialMixingEnabled(bool enabled);
501  // Returns whether NicePartialMixing mode is enabled.
502  MT32EMU_EXPORT bool isNicePartialMixingEnabled() const;
503 
504  // Selects new type of the wave generator and renderer to be used during subsequent calls to open().
505  // By default, RendererType_BIT16S is selected.
506  // See RendererType for details.
507  MT32EMU_EXPORT void selectRendererType(RendererType);
508  // Returns previously selected type of the wave generator and renderer.
509  // See RendererType for details.
510  MT32EMU_EXPORT RendererType getSelectedRendererType() const;
511 
512  // Returns actual sample rate used in emulation of stereo analog circuitry of hardware units.
513  // See comment for render() below.
514  MT32EMU_EXPORT Bit32u getStereoOutputSampleRate() const;
515 
516  // Renders samples to the specified output stream as if they were sampled at the analog stereo output.
517  // When AnalogOutputMode is set to ACCURATE (OVERSAMPLED), the output signal is upsampled to 48 (96) kHz in order
518  // to retain emulation accuracy in whole audible frequency spectra. Otherwise, native digital signal sample rate is retained.
519  // getStereoOutputSampleRate() can be used to query actual sample rate of the output signal.
520  // The length is in frames, not bytes (in 16-bit stereo, one frame is 4 bytes). Uses NATIVE byte ordering.
521  MT32EMU_EXPORT void render(Bit16s *stream, Bit32u len);
522  // Same as above but outputs to a float stereo stream.
523  MT32EMU_EXPORT void render(float *stream, Bit32u len);
524 
525  // Renders samples to the specified output streams as if they appeared at the DAC entrance.
526  // No further processing performed in analog circuitry emulation is applied to the signal.
527  // NULL may be specified in place of any or all of the stream buffers to skip it.
528  // The length is in samples, not bytes. Uses NATIVE byte ordering.
529  MT32EMU_EXPORT void renderStreams(Bit16s *nonReverbLeft, Bit16s *nonReverbRight, Bit16s *reverbDryLeft, Bit16s *reverbDryRight, Bit16s *reverbWetLeft, Bit16s *reverbWetRight, Bit32u len);
530  MT32EMU_EXPORT void renderStreams(const DACOutputStreams<Bit16s> &streams, Bit32u len);
531  // Same as above but outputs to float streams.
532  MT32EMU_EXPORT void renderStreams(float *nonReverbLeft, float *nonReverbRight, float *reverbDryLeft, float *reverbDryRight, float *reverbWetLeft, float *reverbWetRight, Bit32u len);
533  MT32EMU_EXPORT void renderStreams(const DACOutputStreams<float> &streams, Bit32u len);
534 
535  // Returns true when there is at least one active partial, otherwise false.
536  MT32EMU_EXPORT bool hasActivePartials() const;
537 
538  // Returns true if the synth is active and subsequent calls to render() may result in non-trivial output (i.e. silence).
539  // The synth is considered active when either there are pending MIDI events in the queue, there is at least one active partial,
540  // or the reverb is (somewhat unreliably) detected as being active.
541  MT32EMU_EXPORT bool isActive();
542 
543  // Returns the maximum number of partials playing simultaneously.
544  MT32EMU_EXPORT Bit32u getPartialCount() const;
545 
546  // Fills in current states of all the parts into the array provided. The array must have at least 9 entries to fit values for all the parts.
547  // If the value returned for a part is true, there is at least one active non-releasing partial playing on this part.
548  // This info is useful in emulating behaviour of LCD display of the hardware units.
549  MT32EMU_EXPORT void getPartStates(bool *partStates) const;
550 
551  // Returns current states of all the parts as a bit set. The least significant bit corresponds to the state of part 1,
552  // total of 9 bits hold the states of all the parts. If the returned bit for a part is set, there is at least one active
553  // non-releasing partial playing on this part. This info is useful in emulating behaviour of LCD display of the hardware units.
554  MT32EMU_EXPORT Bit32u getPartStates() const;
555 
556  // Fills in current states of all the partials into the array provided. The array must be large enough to accommodate states of all the partials.
557  MT32EMU_EXPORT void getPartialStates(PartialState *partialStates) const;
558 
559  // Fills in current states of all the partials into the array provided. Each byte in the array holds states of 4 partials
560  // starting from the least significant bits. The state of each partial is packed in a pair of bits.
561  // The array must be large enough to accommodate states of all the partials (see getPartialCount()).
562  MT32EMU_EXPORT void getPartialStates(Bit8u *partialStates) const;
563 
564  // Fills in information about currently playing notes on the specified part into the arrays provided. The arrays must be large enough
565  // to accommodate data for all the playing notes. The maximum number of simultaneously playing notes cannot exceed the number of partials.
566  // Argument partNumber should be 0..7 for Part 1..8, or 8 for Rhythm.
567  // Returns the number of currently playing notes on the specified part.
568  MT32EMU_EXPORT Bit32u getPlayingNotes(Bit8u partNumber, Bit8u *keys, Bit8u *velocities) const;
569 
570  // Returns name of the patch set on the specified part.
571  // Argument partNumber should be 0..7 for Part 1..8, or 8 for Rhythm.
572  // The returned value is a null-terminated string which is guaranteed to remain valid until the next call to one of render methods.
573  MT32EMU_EXPORT const char *getPatchName(Bit8u partNumber) const;
574 
575  // Retrieves the name of the sound group the timbre identified by arguments timbreGroup and timbreNumber is associated with.
576  // Values 0-3 of timbreGroup correspond to the timbre banks GROUP A, GROUP B, MEMORY and RHYTHM.
577  // For all but the RHYTHM timbre bank, allowed values of timbreNumber are in range 0-63. The number of timbres
578  // contained in the RHYTHM bank depends on the used control ROM version.
579  // The argument soundGroupName must point to an array of at least 8 characters. The result is a null-terminated string.
580  // Returns whether the specified timbre has been found and the result written in soundGroupName.
581  MT32EMU_EXPORT_V(2.7) bool getSoundGroupName(char *soundGroupName, Bit8u timbreGroup, Bit8u timbreNumber) const;
582  // Retrieves the name of the timbre identified by arguments timbreGroup and timbreNumber.
583  // Values 0-3 of timbreGroup correspond to the timbre banks GROUP A, GROUP B, MEMORY and RHYTHM.
584  // For all but the RHYTHM timbre bank, allowed values of timbreNumber are in range 0-63. The number of timbres
585  // contained in the RHYTHM bank depends on the used control ROM version.
586  // The argument soundName must point to an array of at least 11 characters. The result is a null-terminated string.
587  // Returns whether the specified timbre has been found and the result written in soundName.
588  MT32EMU_EXPORT_V(2.7) bool getSoundName(char *soundName, Bit8u timbreGroup, Bit8u timbreNumber) const;
589 
590  // Stores internal state of emulated synth into an array provided (as it would be acquired from hardware).
591  MT32EMU_EXPORT void readMemory(Bit32u addr, Bit32u len, Bit8u *data);
592 
593  // Retrieves the current state of the emulated MT-32 display facilities.
594  // Typically, the state is updated during the rendering. When that happens, a related callback from ReportHandler2 is invoked.
595  // However, there might be no need to invoke this method after each update, e.g. when the render buffer is just a few milliseconds
596  // long.
597  // The argument targetBuffer must point to an array of at least 21 characters. The result is a null-terminated string.
598  // The optional argument narrowLCD enables a condensed representation of the displayed information in some cases. This is mainly
599  // intended to route the result to a hardware LCD that is only 16 characters wide. Automatic scrolling of longer strings
600  // is not supported.
601  // Returns whether the MIDI MESSAGE LED is ON and fills the targetBuffer parameter.
602  MT32EMU_EXPORT_V(2.6) bool getDisplayState(char *targetBuffer, bool narrowLCD = false) const;
603 
604  // Resets the emulated LCD to the main mode (Master Volume). This has the same effect as pressing the Master Volume button
605  // while the display shows some other message. Useful for the new-gen devices as those require a special Display Reset SysEx
606  // to return to the main mode e.g. from showing a custom display message or a checksum error.
607  MT32EMU_EXPORT_V(2.6) void setMainDisplayMode();
608 
609  // Permits to select an arbitrary display emulation model that does not necessarily match the actual behaviour implemented
610  // in the control ROM version being used.
611  // Invoking this method with the argument set to true forces emulation of the old-gen MT-32 display features.
612  // Otherwise, emulation of the new-gen devices is enforced (these include CM-32L and LAPC-I as if these were connected to an LCD).
613  MT32EMU_EXPORT_V(2.6) void setDisplayCompatibility(bool oldMT32CompatibilityEnabled);
614  // Returns whether the currently configured features of the emulated display are compatible with the old-gen MT-32 devices.
615  MT32EMU_EXPORT_V(2.6) bool isDisplayOldMT32Compatible() const;
616  // Returns whether the emulated display features configured by default depending on the actual control ROM version
617  // are compatible with the old-gen MT-32 devices.
618  MT32EMU_EXPORT_V(2.6) bool isDefaultDisplayOldMT32Compatible() const;
619 }; // class Synth
620 
621 } // namespace MT32Emu
622 
623 #endif // #ifndef MT32EMU_SYNTH_H
Definition: Part.h:139
Definition: Analog.h:26
Definition: Structures.h:132
Definition: MemoryRegion.h:127
Definition: Synth.h:79
Definition: Structures.h:186
Definition: SoxrAdapter.h:28
Definition: TVP.h:30
Definition: MemoryRegion.h:121
Definition: BReverbModel.h:28
Definition: Structures.h:238
Definition: MemoryRegion.h:105
Definition: Synth.h:89
Definition: Partial.h:40
Definition: TVA.h:62
Definition: PartialManager.h:32
Definition: SamplerateAdapter.h:28
Definition: MemoryRegion.h:109
Definition: Synth.h:131
Definition: MemoryRegion.h:97
Definition: Analog.h:37
Definition: Poly.h:30
Definition: MidiEventQueue.h:36
Definition: TVF.h:30
Definition: Structures.h:203
Definition: MemoryRegion.h:35
Definition: Display.h:29
Definition: MemoryRegion.h:101
Definition: MidiStreamParser.h:104
Definition: MemoryRegion.h:113
Definition: MemoryRegion.h:117
Definition: Synth.h:120
Definition: Part.h:47
Definition: ROMInfo.h:88