ScummVM API documentation
ay.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 #ifndef FREESCAPE_CASTLE_AY_MUSIC_H
23 #define FREESCAPE_CASTLE_AY_MUSIC_H
24 
25 #include "audio/mixer.h"
26 #include "audio/softsynth/ay8912.h"
27 #include "freescape/music.h"
28 
29 namespace Freescape {
30 
38 public:
40  ~CastleAYMusicPlayer() override;
41 
42  void startMusic() override;
43  void stopMusic() override;
44  bool isPlaying() const override;
45 
46  int readBuffer(int16 *buffer, const int numSamples) override;
47  bool endOfData() const override { return !_musicActive; }
48  bool endOfStream() const override { return false; }
49 
50 private:
51  enum {
52  kChannelCount = 3,
53  kMaxNote = 94
54  };
55 
56  enum ADSRPhase {
57  kPhaseOff,
58  kPhaseAttack,
59  kPhaseDecay,
60  kPhaseSustain,
61  kPhaseRelease
62  };
63 
64  struct ChannelState {
65  const byte *orderList;
66  uint16 orderPosition;
67  byte patternIndex;
68  uint16 patternDataOffset;
69  uint16 patternOffset;
70  uint16 delay;
71  byte instrument;
72  int8 transpose;
73  byte currentNote;
74  uint16 baseSIDFrequency;
75  uint16 basePeriod;
76  uint16 currentPeriod;
77  uint16 adsrVolume;
78  uint16 attackRate;
79  uint16 decayRate;
80  byte sustainLevel;
81  uint16 releaseRate;
82  int16 sidFrequencyOffset;
83  byte vibratoStep;
84  bool vibratoReverse;
85  bool toneEnabled;
86  bool noiseEnabled;
87  bool active;
88  ADSRPhase adsrPhase;
89 
90  void reset(const byte *channelOrderList);
91  };
92 
93  Audio::Mixer *_mixer;
94  Audio::SoundHandle _handle;
95  bool _musicActive;
96  byte _mixerRegister;
97  int _tickSampleCount;
98  uint32 _tick;
99  ChannelState _channels[kChannelCount];
100 
101  void onTimer();
102  void setupSong();
103  void silenceAll();
104  void loadNextPattern(int channel);
105  byte readPatternByte(int channel);
106  void parseCommands(int channel);
107  void noteOn(int channel, byte note);
108  void noteOff(int channel);
109  void writeChannelPeriod(int channel, uint16 period);
110  void noteToPeriod(int note, uint16 &period) const;
111  void applyFrameEffects(int channel);
112  void triggerADSR(int channel, byte attackDecay, byte sustainRelease);
113  void releaseADSR(int channel);
114  void updateADSR(int channel);
115  void setChannelOutput(int channel, bool toneEnabled, bool noiseEnabled);
116  void writeNoisePeriod(uint16 period);
117  byte sidControlForInstrument(byte instrument) const;
118  byte instrumentVolumeScale(byte instrument) const;
119 };
120 
121 } // namespace Freescape
122 
123 #endif
bool endOfStream() const override
Definition: ay.music.h:48
int readBuffer(int16 *buffer, const int numSamples) override
Definition: area.h:36
Definition: ay8912.h:30
Definition: mixer.h:49
Definition: mixer.h:70
Definition: ay.music.h:37
Definition: music.h:29
bool endOfData() const override
Definition: ay.music.h:47