openMSX
MSXMixer.hh
Go to the documentation of this file.
1 #ifndef MSXMIXER_HH
2 #define MSXMIXER_HH
3 
4 #include "Schedulable.hh"
5 #include "Observer.hh"
6 #include "InfoTopic.hh"
7 #include "EmuTime.hh"
8 #include "DynamicClock.hh"
9 #include <vector>
10 #include <memory>
11 
12 namespace openmsx {
13 
14 class SoundDevice;
15 class Mixer;
16 class MSXMotherBoard;
17 class MSXCommandController;
18 class GlobalSettings;
19 class ThrottleManager;
20 class IntegerSetting;
21 class StringSetting;
22 class BooleanSetting;
23 class Setting;
24 class AviRecorder;
25 
26 class MSXMixer final : private Schedulable, private Observer<Setting>
27  , private Observer<ThrottleManager>
28 {
29 public:
30  // See SoundDevice::getAmplificationFactor()
31  // and MSXMixer::updateVolumeParams()
32  static constexpr int AMP_BITS = 9;
33 
34 public:
35  MSXMixer(Mixer& mixer, MSXMotherBoard& motherBoard,
36  GlobalSettings& globalSettings);
37  ~MSXMixer();
38 
47  void registerSound(SoundDevice& device, float volume,
48  int balance, unsigned numChannels);
49 
53  void unregisterSound(SoundDevice& device);
54 
59  void updateStream(EmuTime::param time);
60 
64  void updateSoftwareVolume(SoundDevice& device);
65 
72  double getEffectiveSpeed() const;
73 
77  void setSynchronousMode(bool synchronous);
78 
84  void mute();
85  void unmute();
86 
87  // Called by Mixer or SoundDriver
88 
92  void setMixerParams(unsigned fragmentSize, unsigned sampleRate);
93 
103  const DynamicClock& getHostSampleClock() const { return prevTime; }
104 
105  // Called by AviRecorder
106  bool needStereoRecording() const;
107  void setRecorder(AviRecorder* recorder);
108 
109  // Returns the nominal host sample rate (not adjusted for speed setting)
110  unsigned getSampleRate() const { return hostSampleRate; }
111 
112  SoundDevice* findDevice(std::string_view name) const;
113 
114  void reInit();
115 
116 private:
117  struct SoundDeviceInfo {
118  SoundDevice* device;
119  float defaultVolume;
120  std::unique_ptr<IntegerSetting> volumeSetting;
121  std::unique_ptr<IntegerSetting> balanceSetting;
123  std::unique_ptr<StringSetting> recordSetting;
124  std::unique_ptr<BooleanSetting> muteSetting;
125  };
126  std::vector<ChannelSettings> channelSettings;
127  float left1, right1, left2, right2;
128  };
129 
130  void updateVolumeParams(SoundDeviceInfo& info);
131  void updateMasterVolume();
132  void reschedule();
133  void reschedule2();
134  void generate(float* output, EmuTime::param time, unsigned samples);
135 
136  // Schedulable
137  void executeUntil(EmuTime::param time) override;
138 
139  // Observer<Setting>
140  void update(const Setting& setting) override;
141  // Observer<ThrottleManager>
142  void update(const ThrottleManager& throttleManager) override;
143 
144  void changeRecordSetting(const Setting& setting);
145  void changeMuteSetting(const Setting& setting);
146 
147  unsigned fragmentSize;
148  unsigned hostSampleRate; // requested freq by sound driver,
149  // not compensated for speed
150 
151  std::vector<SoundDeviceInfo> infos;
152 
153  Mixer& mixer;
154  MSXMotherBoard& motherBoard;
155  MSXCommandController& commandController;
156 
157  IntegerSetting& masterVolume;
158  IntegerSetting& speedSetting;
159  ThrottleManager& throttleManager;
160 
161  DynamicClock prevTime;
162 
163  struct SoundDeviceInfoTopic final : InfoTopic {
164  explicit SoundDeviceInfoTopic(InfoCommand& machineInfoCommand);
165  void execute(span<const TclObject> tokens,
166  TclObject& result) const override;
167  std::string help(const std::vector<std::string>& tokens) const override;
168  void tabCompletion(std::vector<std::string>& tokens) const override;
169  } soundDeviceInfo;
170 
171  AviRecorder* recorder;
172  unsigned synchronousCounter;
173 
174  unsigned muteCount;
175  float tl0, tr0; // internal DC-filter state
176 };
177 
178 } // namespace openmsx
179 
180 #endif
void mute()
TODO This methods (un)mute the sound.
Definition: MSXMixer.cc:547
void unregisterSound(SoundDevice &device)
Every sounddevice must unregister before it is destructed.
Definition: MSXMixer.cc:123
void updateSoftwareVolume(SoundDevice &device)
Used by SoundDevice::setSoftwareVolume()
Definition: MSXMixer.cc:716
Definition: span.hh:34
void setSynchronousMode(bool synchronous)
If we&#39;re recording, we want to emulate sound at 100% emutime speed.
Definition: MSXMixer.cc:137
void updateStream(EmuTime::param time)
Use this method to force an &#39;early&#39; call to all updateBuffer() methods.
Definition: MSXMixer.cc:161
void setMixerParams(unsigned fragmentSize, unsigned sampleRate)
Set new fragment size and sample frequency.
Definition: MSXMixer.cc:581
std::unique_ptr< StringSetting > recordSetting
Definition: MSXMixer.hh:123
std::unique_ptr< BooleanSetting > muteSetting
Definition: MSXMixer.hh:124
SoundDevice * findDevice(std::string_view name) const
Definition: MSXMixer.cc:741
Every class that wants to get scheduled at some point must inherit from this class.
Definition: Schedulable.hh:33
void registerSound(SoundDevice &device, float volume, int balance, unsigned numChannels)
Use this method to register a given sounddevice.
Definition: MSXMixer.cc:79
A Setting with an integer value.
Represents a clock with a variable frequency.
Definition: DynamicClock.hh:15
MSXMixer(Mixer &mixer, MSXMotherBoard &motherBoard, GlobalSettings &globalSettings)
Definition: MSXMixer.cc:38
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void setRecorder(AviRecorder *recorder)
Definition: MSXMixer.cc:595
static constexpr int AMP_BITS
Definition: MSXMixer.hh:32
bool needStereoRecording() const
Definition: MSXMixer.cc:539
unsigned getSampleRate() const
Definition: MSXMixer.hh:110
Manages the throttle state of openMSX.
double getEffectiveSpeed() const
Returns the ratio of emutime-speed per realtime-speed.
Definition: MSXMixer.cc:154
Generic Gang-of-Four Observer class, templatized edition.
Definition: Observer.hh:9
This class contains settings that are used by several other class (including some singletons)...
const DynamicClock & getHostSampleClock() const
Clock that ticks at the exact moment(s) in time that a host sample should be generated.
Definition: MSXMixer.hh:103