| 1 |
#ifndef SOUNDDEVICE_HH |
| 2 |
#define SOUNDDEVICE_HH |
| 3 |
|
| 4 |
#include "EmuTime.hh" |
| 5 |
#include "WavWriter.hh" |
| 6 |
#include "static_string_view.hh" |
| 7 |
|
| 8 |
#include <array> |
| 9 |
#include <optional> |
| 10 |
#include <string> |
| 11 |
#include <string_view> |
| 12 |
|
| 13 |
namespace openmsx { |
| 14 |
|
| 15 |
class DeviceConfig; |
| 16 |
class DynamicClock; |
| 17 |
class MSXMixer; |
| 18 |
|
| 19 |
class SoundDevice |
| 20 |
{ |
| 21 |
public: |
| 22 |
static constexpr unsigned MAX_CHANNELS = 24; |
| 23 |
|
| 24 |
SoundDevice(const SoundDevice&) = delete; |
| 25 |
SoundDevice(SoundDevice&&) = delete; |
| 26 |
SoundDevice& operator=(const SoundDevice&) = delete; |
| 27 |
SoundDevice& operator=(SoundDevice&&) = delete; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
[[nodiscard]] const std::string& getName() const { return name; } |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
[[nodiscard]] std::string_view getDescription() const { return description; } |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
[[nodiscard]] unsigned getNumChannels() const { return numChannels; } |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
[[nodiscard]] bool hasStereoChannels() const { |
| 46 |
return stereo == 2; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
[[nodiscard]] bool isStereo() const { |
| 52 |
return hasStereoChannels() || !balanceCenter; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
struct AmplificationFactors { |
| 68 |
float left; |
| 69 |
float right; |
| 70 |
}; |
| 71 |
[[nodiscard]] AmplificationFactors getAmplificationFactor() const { |
| 72 |
auto f = getAmplificationFactorImpl(); |
| 73 |
return {.left = f * softwareVolumeLeft, .right = f * softwareVolumeRight}; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
void setSoftwareVolume(float volume, EmuTime time); |
| 87 |
void setSoftwareVolume(float left, float right, EmuTime time); |
| 88 |
|
| 89 |
void recordChannel(unsigned channel, const std::string& filename); |
| 90 |
void muteChannel (unsigned channel, bool muted); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
void setBalance(unsigned channel, float balance); |
| 99 |
virtual void postSetBalance(); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
[[nodiscard]] std::span<const float> getLastBuffer(unsigned channel); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
[[nodiscard]] float getNativeSampleRate() const { return float(inputSampleRate); } |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
[[nodiscard]] unsigned getLastMonoBufferSize() const { |
| 127 |
|
| 128 |
|
| 129 |
return inputSampleRate / 7; |
| 130 |
} |
| 131 |
[[nodiscard]] unsigned getLastBufferSize() const { |
| 132 |
return getLastMonoBufferSize() * stereo; |
| 133 |
} |
| 134 |
|
| 135 |
protected: |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
SoundDevice(MSXMixer& mixer, std::string_view name, static_string_view description, |
| 145 |
unsigned numChannels, unsigned inputRate, bool stereo); |
| 146 |
~SoundDevice(); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
[[nodiscard]] virtual float getAmplificationFactorImpl() const; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
void registerSound(const DeviceConfig& config); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
void unregisterSound(); |
| 171 |
|
| 172 |
|
| 173 |
void updateStream(EmuTime time); |
| 174 |
|
| 175 |
void setInputRate(unsigned sampleRate) { inputSampleRate = sampleRate; } |
| 176 |
[[nodiscard]] unsigned getInputRate() const { return inputSampleRate; } |
| 177 |
|
| 178 |
public: |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
virtual void setOutputRate(unsigned hostSampleRate, double speed) = 0; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
[[nodiscard]] virtual bool updateBuffer(size_t length, float* buffer, |
| 205 |
EmuTime time) = 0; |
| 206 |
|
| 207 |
protected: |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
static void addFill(float*& buffer, float value, unsigned num); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
virtual void generateChannels(std::span<float*> buffers, unsigned num) = 0; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
[[nodiscard]] bool mixChannels(float* dataOut, size_t samples); |
| 246 |
|
| 247 |
|
| 248 |
[[nodiscard]] const DynamicClock& getHostSampleClock() const; |
| 249 |
[[nodiscard]] double getEffectiveSpeed() const; |
| 250 |
|
| 251 |
private: |
| 252 |
struct Balance { |
| 253 |
float left, right; |
| 254 |
}; |
| 255 |
|
| 256 |
MSXMixer& mixer; |
| 257 |
const std::string name; |
| 258 |
const static_string_view description; |
| 259 |
|
| 260 |
std::array<std::optional<Wav16Writer>, MAX_CHANNELS> writer; |
| 261 |
|
| 262 |
float softwareVolumeLeft = 1.0f; |
| 263 |
float softwareVolumeRight = 1.0f; |
| 264 |
unsigned inputSampleRate; |
| 265 |
const unsigned numChannels; |
| 266 |
const unsigned stereo; |
| 267 |
unsigned numRecordChannels = 0; |
| 268 |
std::array<Balance, MAX_CHANNELS> channelBalance; |
| 269 |
std::array<bool, MAX_CHANNELS> channelMuted; |
| 270 |
bool balanceCenter = true; |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
struct PerChannelBuffer { |
| 276 |
std::vector<float> buffer; |
| 277 |
unsigned stopIdx = 0; |
| 278 |
unsigned requestCounter = 0; |
| 279 |
unsigned silent = 999999; |
| 280 |
}; |
| 281 |
std::array<PerChannelBuffer, MAX_CHANNELS> channelBuffers = {}; |
| 282 |
}; |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
#endif |