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