| 1 |
#ifndef MSXMIXER_HH |
| 2 |
#define MSXMIXER_HH |
| 3 |
|
| 4 |
#include "DynamicClock.hh" |
| 5 |
#include "EmuTime.hh" |
| 6 |
#include "InfoTopic.hh" |
| 7 |
#include "Mixer.hh" |
| 8 |
#include "Schedulable.hh" |
| 9 |
|
| 10 |
#include "Observer.hh" |
| 11 |
#include "dynarray.hh" |
| 12 |
|
| 13 |
#include <memory> |
| 14 |
#include <span> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
namespace openmsx { |
| 18 |
|
| 19 |
class SoundDevice; |
| 20 |
class Mixer; |
| 21 |
class MSXMotherBoard; |
| 22 |
class MSXCommandController; |
| 23 |
class GlobalSettings; |
| 24 |
class SpeedManager; |
| 25 |
class ThrottleManager; |
| 26 |
class IntegerSetting; |
| 27 |
class StringSetting; |
| 28 |
class BooleanSetting; |
| 29 |
class Setting; |
| 30 |
class AviRecorder; |
| 31 |
|
| 32 |
class MSXMixer final : private Schedulable, private Observer<Setting> |
| 33 |
, private Observer<SpeedManager> |
| 34 |
, private Observer<ThrottleManager> |
| 35 |
{ |
| 36 |
public: |
| 37 |
|
| 38 |
|
| 39 |
static constexpr int AMP_BITS = 9; |
| 40 |
|
| 41 |
struct SoundDeviceInfo { |
| 42 |
explicit SoundDeviceInfo(unsigned numChannels); |
| 43 |
|
| 44 |
SoundDevice* device = nullptr; |
| 45 |
std::unique_ptr<IntegerSetting> volumeSetting; |
| 46 |
std::unique_ptr<IntegerSetting> balanceSetting; |
| 47 |
struct ChannelSettings { |
| 48 |
std::unique_ptr<StringSetting> record; |
| 49 |
std::unique_ptr<BooleanSetting> mute; |
| 50 |
}; |
| 51 |
dynarray<ChannelSettings> channelSettings; |
| 52 |
float defaultVolume = 0.f; |
| 53 |
float left1 = 0.f, right1 = 0.f, left2 = 0.f, right2 = 0.f; |
| 54 |
}; |
| 55 |
|
| 56 |
public: |
| 57 |
MSXMixer(Mixer& mixer, MSXMotherBoard& motherBoard, |
| 58 |
GlobalSettings& globalSettings); |
| 59 |
MSXMixer(const MSXMixer&) = delete; |
| 60 |
MSXMixer(MSXMixer&&) = delete; |
| 61 |
MSXMixer& operator=(const MSXMixer&) = delete; |
| 62 |
MSXMixer& operator=(MSXMixer&&) = delete; |
| 63 |
~MSXMixer(); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
void registerSound(SoundDevice& device, float volume, |
| 74 |
int balance, unsigned numChannels); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
void unregisterSound(SoundDevice& device); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
void updateStream(EmuTime time); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
void updateSoftwareVolume(SoundDevice& device); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
[[nodiscard]] double getEffectiveSpeed() const; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
void setSynchronousMode(bool synchronous); |
| 104 |
[[nodiscard]] bool isSynchronousMode() const { return synchronousCounter != 0; } |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
void mute(); |
| 112 |
void unmute(); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
void setMixerParams(unsigned fragmentSize, unsigned sampleRate); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
[[nodiscard]] const DynamicClock& getHostSampleClock() const { return prevTime; } |
| 131 |
|
| 132 |
|
| 133 |
[[nodiscard]] bool needStereoRecording() const; |
| 134 |
void setRecorder(AviRecorder* recorder); |
| 135 |
|
| 136 |
|
| 137 |
[[nodiscard]] unsigned getSampleRate() const { return hostSampleRate; } |
| 138 |
|
| 139 |
[[nodiscard]] SoundDevice* findDevice(std::string_view name) const; |
| 140 |
[[nodiscard]] const SoundDeviceInfo* findDeviceInfo(std::string_view name) const; |
| 141 |
[[nodiscard]] const auto& getDeviceInfos() const { return infos; } |
| 142 |
|
| 143 |
void reInit(); |
| 144 |
|
| 145 |
private: |
| 146 |
void updateVolumeParams(SoundDeviceInfo& info) const; |
| 147 |
void updateMasterVolume(); |
| 148 |
void reschedule(); |
| 149 |
void reschedule2(); |
| 150 |
void generate(std::span<StereoFloat> output, EmuTime time); |
| 151 |
|
| 152 |
|
| 153 |
void executeUntil(EmuTime time) override; |
| 154 |
|
| 155 |
|
| 156 |
void update(const Setting& setting) noexcept override; |
| 157 |
|
| 158 |
void update(const SpeedManager& speedManager) noexcept override; |
| 159 |
|
| 160 |
void update(const ThrottleManager& throttleManager) noexcept override; |
| 161 |
|
| 162 |
void changeRecordSetting(const Setting& setting); |
| 163 |
void changeMuteSetting(const Setting& setting); |
| 164 |
|
| 165 |
private: |
| 166 |
unsigned fragmentSize = 0; |
| 167 |
unsigned hostSampleRate = 44100; |
| 168 |
|
| 169 |
|
| 170 |
std::vector<SoundDeviceInfo> infos; |
| 171 |
|
| 172 |
Mixer& mixer; |
| 173 |
MSXMotherBoard& motherBoard; |
| 174 |
MSXCommandController& commandController; |
| 175 |
|
| 176 |
IntegerSetting& masterVolume; |
| 177 |
SpeedManager& speedManager; |
| 178 |
ThrottleManager& throttleManager; |
| 179 |
|
| 180 |
DynamicClock prevTime; |
| 181 |
|
| 182 |
struct SoundDeviceInfoTopic final : InfoTopic { |
| 183 |
explicit SoundDeviceInfoTopic(InfoCommand& machineInfoCommand); |
| 184 |
void execute(std::span<const TclObject> tokens, |
| 185 |
TclObject& result) const override; |
| 186 |
[[nodiscard]] std::string help(std::span<const TclObject> tokens) const override; |
| 187 |
void tabCompletion(std::vector<std::string>& tokens) const override; |
| 188 |
} soundDeviceInfo; |
| 189 |
|
| 190 |
AviRecorder* recorder = nullptr; |
| 191 |
unsigned synchronousCounter = 0; |
| 192 |
|
| 193 |
unsigned muteCount = 1; |
| 194 |
float tl0, tr0; |
| 195 |
}; |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
#endif |