audio.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. struct Audio;
  2. struct AudioDriver {
  3. enum class Format : uint { none, int16, int32, float32, float64 };
  4. AudioDriver(Audio& super) : super(super) {}
  5. virtual ~AudioDriver() = default;
  6. virtual auto create() -> bool { return true; }
  7. virtual auto driver() -> string { return "None"; }
  8. virtual auto ready() -> bool { return true; }
  9. virtual auto hasExclusive() -> bool { return false; }
  10. virtual auto hasContext() -> bool { return false; }
  11. virtual auto hasDevices() -> vector<string> { return {"Default"}; }
  12. virtual auto hasBlocking() -> bool { return false; }
  13. virtual auto hasDynamic() -> bool { return false; }
  14. virtual auto hasChannels() -> vector<uint> { return {2}; }
  15. virtual auto hasFrequencies() -> vector<uint> { return {48000}; }
  16. virtual auto hasLatencies() -> vector<uint> { return {0}; }
  17. auto hasDevice(string device) -> bool { return (bool)hasDevices().find(device); }
  18. auto hasChannels(uint channels) -> bool { return (bool)hasChannels().find(channels); }
  19. auto hasFrequency(uint frequency) -> bool { return (bool)hasFrequencies().find(frequency); }
  20. auto hasLatency(uint latency) -> bool { return (bool)hasLatencies().find(latency); }
  21. virtual auto setExclusive(bool exclusive) -> bool { return true; }
  22. virtual auto setContext(uintptr context) -> bool { return true; }
  23. virtual auto setDevice(string device) -> bool { return true; }
  24. virtual auto setBlocking(bool blocking) -> bool { return true; }
  25. virtual auto setDynamic(bool dynamic) -> bool { return true; }
  26. virtual auto setChannels(uint channels) -> bool { return true; }
  27. virtual auto setFrequency(uint frequency) -> bool { return true; }
  28. virtual auto setLatency(uint latency) -> bool { return true; }
  29. virtual auto clear() -> void {}
  30. virtual auto level() -> double { return 0.5; }
  31. virtual auto output(const double samples[]) -> void {}
  32. protected:
  33. Audio& super;
  34. friend class Audio;
  35. bool exclusive = false;
  36. uintptr context = 0;
  37. string device = "Default";
  38. bool blocking = false;
  39. bool dynamic = false;
  40. uint channels = 2;
  41. uint frequency = 48000;
  42. uint latency = 0;
  43. };
  44. struct Audio {
  45. static auto hasDrivers() -> vector<string>;
  46. static auto hasDriver(string driver) -> bool { return (bool)hasDrivers().find(driver); }
  47. static auto optimalDriver() -> string;
  48. static auto safestDriver() -> string;
  49. Audio() : self(*this) { reset(); }
  50. explicit operator bool() { return instance->driver() != "None"; }
  51. auto reset() -> void { instance = new AudioDriver(*this); }
  52. auto create(string driver = "") -> bool;
  53. auto driver() -> string { return instance->driver(); }
  54. auto ready() -> bool { return instance->ready(); }
  55. auto hasExclusive() -> bool { return instance->hasExclusive(); }
  56. auto hasContext() -> bool { return instance->hasContext(); }
  57. auto hasDevices() -> vector<string> { return instance->hasDevices(); }
  58. auto hasBlocking() -> bool { return instance->hasBlocking(); }
  59. auto hasDynamic() -> bool { return instance->hasDynamic(); }
  60. auto hasChannels() -> vector<uint> { return instance->hasChannels(); }
  61. auto hasFrequencies() -> vector<uint> { return instance->hasFrequencies(); }
  62. auto hasLatencies() -> vector<uint> { return instance->hasLatencies(); }
  63. auto hasDevice(string device) -> bool { return instance->hasDevice(device); }
  64. auto hasChannels(uint channels) -> bool { return instance->hasChannels(channels); }
  65. auto hasFrequency(uint frequency) -> bool { return instance->hasFrequency(frequency); }
  66. auto hasLatency(uint latency) -> bool { return instance->hasLatency(latency); }
  67. auto exclusive() -> bool { return instance->exclusive; }
  68. auto context() -> uintptr { return instance->context; }
  69. auto device() -> string { return instance->device; }
  70. auto blocking() -> bool { return instance->blocking; }
  71. auto dynamic() -> bool { return instance->dynamic; }
  72. auto channels() -> uint { return instance->channels; }
  73. auto frequency() -> uint { return instance->frequency; }
  74. auto latency() -> uint { return instance->latency; }
  75. auto setExclusive(bool exclusive) -> bool;
  76. auto setContext(uintptr context) -> bool;
  77. auto setDevice(string device) -> bool;
  78. auto setBlocking(bool blocking) -> bool;
  79. auto setDynamic(bool dynamic) -> bool;
  80. auto setChannels(uint channels) -> bool;
  81. auto setFrequency(uint frequency) -> bool;
  82. auto setLatency(uint latency) -> bool;
  83. auto clear() -> void;
  84. auto level() -> double;
  85. auto output(const double samples[]) -> void;
  86. protected:
  87. Audio& self;
  88. unique_pointer<AudioDriver> instance;
  89. vector<nall::DSP::Resampler::Cubic> resamplers;
  90. };