ScummVM API documentation
c64.music.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include "audio/sid.h"
23 #include "common/debug.h"
24 #include "freescape/music.h"
25 
26 namespace Freescape {
27 
28 class DrillerSIDPlayer : public MusicPlayer {
29 
30  // --- Voice State Structure ---
31  struct VoiceState {
32  // Pointers & Indices
33  const uint8_t *trackDataPtr; // Pointer to current track data array
34  uint8_t trackIndex; // Index within trackDataPtr
35  const uint8_t *patternDataPtr; // Pointer to current pattern data array
36  uint8_t patternIndex; // Index within patternDataPtr
37  uint8_t instrumentIndex; // Current instrument (0-21, scaled by 8 for lookup)
38 
39  // Playback Control & Tempo
40  int8_t delayCounter; // Counts down frames for note duration (maps to voiceX_ctrl2)
41  uint8_t noteDuration; // Duration set by FD xx (maps to voiceX_something+2)
42  uint8_t gateMask; // Control gating/retriggering (maps to control3 behavior)
43 
44  // Note & Frequency
45  uint8_t currentNote; // Current raw note value (0-95)
46  uint8_t portaTargetNote; // Target note for portamento
47  uint16_t currentFreq; // Current frequency being sent to SID
48  uint16_t baseFreq; // Note frequency without effects
49  uint16_t targetFreq; // Used for portamento target
50 
51  // Pulse Width
52  uint16_t pulseWidth; // Current pulse width
53  // Placeholder for PWM effects if needed later
54 
55  // ADSR
56  uint8_t attackDecay; // SID Attack / Decay register value
57  uint8_t sustainRelease; // SID Sustain / Release register value
58 
59  // Effects State
60  uint8_t effect; // Current active effect (0:None, 1:Arpeggio, 2:Vibrato, 3:Portamento Up, 4:Portamento Down, 5: PWM LFO?)
61  uint8_t arpeggioIndex; // Index in arpeggio table
62  uint8_t arpeggioSpeed; // Counter divisor for arpeggio step
63  uint8_t arpeggioCounter; // Counter for arpeggio step
64  uint8_t arpeggioNoteOffsetIndex; // 0, 1, 2 for arpeggio notes
65 
66  int16_t vibratoDepth; // Depth for vibrato effect
67  uint8_t vibratoSpeed; // Speed/delay for vibrato effect
68  uint8_t vibratoDelay; // Counter for vibrato step
69  uint8_t vibratoDirection; // 0: up, 1: down
70  int16_t vibratoCurrentOffset; // Current frequency offset for vibrato
71 
72  int16_t portaSpeed; // Speed for portamento effect (positive for up, negative for down)
73 
74  // Hard Restart / Buzz Effect (from L1005, possibly instrument related)
75  uint8_t hardRestartValue; // Value from instrument table (a1+5)
76  uint8_t hardRestartDelay; // Counter for delay phase (voiceX_whatever+3)
77  uint8_t hardRestartCounter; // Counter for frequency change phase (voiceX_whatever+4)
78  bool hardRestartActive; // Is the effect currently running?
79 
80  // From disassembly variables (mapping might need refinement)
81  // voice1_whatever: 0CCE[5] - effect state? (arp, vib, porta)
82  uint8_t whatever0; // 0CCE - Vibrato state? (0=off, 1=active, 3-4=sweep?)
83  uint8_t whatever1; // 0CCF - Arpeggio state? (0=off, 1=active)
84  uint8_t whatever2; // 0CD0 - Portamento type (0=off, 1=down(FB), 2=up(FC), 3=down H(FB), 4=up H(FC))?
85  uint8_t whatever3; // 0CD1 - Hard restart delay counter
86  uint8_t whatever4; // 0CD2 - Hard restart step counter
87 
88  // voice1_whatever2: 0CD4 - Vibrato direction toggle?
89  uint8_t whatever2_vibDirToggle;
90 
91  // voice1_something: 0CE3[3] - Porta speed?, Note duration
92  uint16_t portaStepRaw; // 0CE3/4 - Raw value from FB/FC command
93  // uint8_t noteDuration // 0CE5 - Covered above
94 
95  // voice1_something_else: 0CE7[3] - PW Low, PW High? (ADSR in disassembly?) - Needs clarification
96  uint8_t something_else[3]; // Re-add this array as it's used in the code logic
97 
98  // voice1_ctrl0: 0CF8 - ADSR lower nibble (Sustain/Release)
99  uint8_t ctrl0; // Re-add this as it's used in the code logic
100 
101  // voice1_ctrl1: 0CF9 - Arpeggio table index / Arp speed upper nibble
102  uint8_t arpTableIndex;
103  uint8_t arpSpeedHiNibble;
104 
105  // voice1_ctrl2: 0CFE - Note delay counter (covered by delayCounter)
106 
107  // voice1_stuff: 0D14[7] - Current freq, base freq, hard restart freq store? Arp counter?
108  uint16_t stuff_freq_porta_vib; // 0D14/15 - Current frequency including porta/vib
109  uint16_t stuff_freq_base; // 0D16/17 - Base frequency of the note
110  uint16_t stuff_freq_hard_restart; // 0D18/19 - Frequency stored during hard restart buzz
111  uint8_t stuff_arp_counter; // 0D1A - Arpeggio counter (0..speed-1)
112  uint8_t stuff_arp_note_index; // 0D1B - Index into arp notes (0, 1, 2)
113 
114  // voice1_things: 0D29[7] - Vibrato state/params
115  uint8_t things_vib_state; // 0D29 - Vibrato state (0=down1, 1=up, 2=down2, 3=sweepdown1, 4=sweepup)
116  uint16_t things_vib_depth; // 0D2A/2B - Vibrato depth
117  uint8_t things_vib_delay_reload; // 0D2C - Vibrato delay reload value
118  uint8_t things_vib_delay_ctr; // 0D2D - Vibrato delay counter
119  // 0D2E/F unused?
120  uint8_t currentNoteSlideTarget; // 0D30 - Last played note (used for slide target?)
121 
122  // voice1_two_ctr: 0D3E - Glide down timer? (Instrument related)
123  uint8_t glideDownTimer;
124 
125  // Temp values during processing
126  uint8_t waveform; // Current waveform for SID
127  bool keyOn; // Current key state (attack vs release)
128  bool sync; // Sync bit state
129  bool ringMod; // Ring mod bit state
130 
131  // Pulse width parts matching something_else (if it maps to PW)
132  uint8_t pwLo() const { return something_else[0]; } // Example mapping
133  uint8_t pwHi() const { return something_else[2]; } // Example mapping
134  void setPwLo(uint8_t val) { something_else[0] = val; }
135  void setPwHi(uint8_t val) { something_else[2] = val; }
136 
137  void reset() {
138  trackDataPtr = nullptr;
139  trackIndex = 0;
140  patternDataPtr = nullptr;
141  patternIndex = 0;
142  instrumentIndex = 0;
143  delayCounter = 0;
144  noteDuration = 0;
145  gateMask = 0xFF;
146  currentNote = 0;
147  portaTargetNote = 0;
148  currentFreq = 0;
149  baseFreq = 0;
150  targetFreq = 0;
151  pulseWidth = 0;
152  attackDecay = 0;
153  sustainRelease = 0;
154  effect = 0;
155  arpeggioIndex = 0;
156  arpeggioSpeed = 0;
157  arpeggioCounter = 0;
158  arpeggioNoteOffsetIndex = 0;
159  vibratoDepth = 0;
160  vibratoSpeed = 0;
161  vibratoDelay = 0;
162  vibratoDirection = 0;
163  vibratoCurrentOffset = 0;
164  portaSpeed = 0;
165  hardRestartValue = 0;
166  hardRestartDelay = 0;
167  hardRestartCounter = 0;
168  hardRestartActive = false;
169  waveform = 0x10; // Default to triangle?
170  keyOn = false;
171  sync = false;
172  ringMod = false;
173 
174  // Reset mapped vars
175  whatever0 = 0;
176  whatever1 = 0;
177  whatever2 = 0;
178  whatever3 = 0;
179  whatever4 = 0;
180  whatever2_vibDirToggle = 0;
181  portaStepRaw = 0;
182  memset(something_else, 0, sizeof(something_else)); // Reset the array
183  ctrl0 = 0; // Reset the added member
184  arpTableIndex = 0;
185  arpSpeedHiNibble = 0;
186  stuff_freq_porta_vib = 0;
187  stuff_freq_base = 0;
188  stuff_freq_hard_restart = 0;
189  stuff_arp_counter = 0;
190  stuff_arp_note_index = 0;
191  things_vib_state = 0;
192  things_vib_depth = 0;
193  things_vib_delay_reload = 0;
194  things_vib_delay_ctr = 0;
195  currentNoteSlideTarget = 0;
196  glideDownTimer = 0;
197  }
198  };
199 
200  // --- Member Variables ---
201  SID::SID *_sid;
202 
203  // Player State
204  enum PlayState { STOPPED,
205  PLAYING,
206  CHANGING_TUNE };
207  enum ContinuousEffectEntry {
208  kVoiceDone,
209  kFullEffectPath,
210  kPortamentoOnlyPath
211  };
212  PlayState _playState;
213  uint8_t _targetTuneIndex; // Tune index requested via startMusic
214 
215  // Global Timing
216  uint8_t _globalTempo; // Tempo value for current tune (0xD10)
217  int8_t _globalTempoCounter; // Frame counter for tempo (0xD12), signed to handle < 0 check
218 
219  // Voice States
220  VoiceState _voiceState[3];
221 
222  // Gate mask is now per-voice (v.gateMask) matching assembly's control3
223 
224 public:
225  DrillerSIDPlayer(int tuneIndex = 1);
226  ~DrillerSIDPlayer();
227  void startMusic() override;
228  void stopMusic() override;
229  bool isPlaying() const override;
230 
231 private:
232  void initSID();
233  void destroySID();
234  void SID_Write(int reg, uint8_t data);
235  void onTimer();
236  void handleChangeTune(int tuneIndex);
237  void handleResetVoices();
238  void playVoice(int voiceIndex);
239  void applyNote(VoiceState &v, int sidOffset, const uint8_t *instA0);
240  ContinuousEffectEntry postNoteEffectSetup(VoiceState &v, const uint8_t *instA0, const uint8_t *instA1);
241  void applyContinuousEffects(VoiceState &v, int sidOffset, const uint8_t *instA0, const uint8_t *instA1, bool startAtPortamento);
242  void applyHardRestart(VoiceState &v, int sidOffset, const uint8_t *instA1);
243  };
244 
245 } // namespace Freescape
Definition: area.h:36
Definition: sid.h:44
Definition: c64.music.h:28
Definition: music.h:29