ours.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // ours <scenario> <out.f32>: the same scenario through the engine's Audio/Hrtf port, with the source parameters
  2. // worked out like OpenAL (CSound::sourceParams / hrtfOutput)
  3. #include "scenarios.h"
  4. #include "WallpaperEngine/Audio/Hrtf.h"
  5. #include <algorithm>
  6. #include <cfloat>
  7. #include <cstdio>
  8. #include <limits>
  9. using namespace WallpaperEngine::Audio;
  10. int main (int argc, char** argv) {
  11. if (argc < 3) {
  12. fprintf (stderr, "usage: %s moving|center|impulse|stereo out.f32\n", argv[0]);
  13. return 1;
  14. }
  15. const Scenario s = scenario (argv[1]);
  16. const auto store = Hrtf::loadDefault (48000);
  17. Hrtf::Mixer mixer (store->irSize);
  18. std::array<Hrtf::Channel, 2> channels {};
  19. const float inf = std::numeric_limits<float>::infinity ();
  20. const std::vector<float> silence (UpdateSize, 0.0f);
  21. std::vector<float> left (UpdateSize), right (UpdateSize), frames (UpdateSize * 2);
  22. bool fading = false;
  23. FILE* out = fopen (argv[2], "wb");
  24. for (int k = 0; k < Blocks + 4; k++) {
  25. const Block& b = s.path[std::min (k, Blocks - 1)];
  26. std::array<Hrtf::Filter, 2> filters {};
  27. if (s.stereo) {
  28. const float angle = 30.0f * 3.14159265358979323846f / 180.0f;
  29. Hrtf::getCoeffs (*store, 0.0f, -angle, inf, 0.0f, filters[0]);
  30. Hrtf::getCoeffs (*store, 0.0f, angle, inf, 0.0f, filters[1]);
  31. filters[0].gain = filters[1].gain = std::clamp (b.gain, 0.0f, 1.0f);
  32. } else {
  33. // alu::Vector::normalize with AL_REFERENCE_DISTANCE 1, then the inverse clamped distance model
  34. float x = b.x, y = b.y, z = b.z, distance = 0.0f, gain = b.gain;
  35. const float limit = std::max (1.0f / 1024.0f, FLT_EPSILON);
  36. if (x * x + y * y + z * z > limit * limit) {
  37. distance = std::sqrt (x * x + y * y + z * z);
  38. const float inverse = 1.0f / distance;
  39. x *= inverse;
  40. y *= inverse;
  41. z *= inverse;
  42. }
  43. gain *= 1.0f / (1.0f + (std::clamp (distance, 1.0f, FLT_MAX) - 1.0f));
  44. gain = std::clamp (gain, 0.0f, 1.0f);
  45. if (distance > FLT_EPSILON) {
  46. Hrtf::getCoeffs (*store, std::asin (std::clamp (y, -1.0f, 1.0f)), std::atan2 (x, -z), distance, 0.0f, filters[0]);
  47. } else {
  48. Hrtf::getCoeffs (*store, 0.0f, 0.0f, inf, 0.0f, filters[0]);
  49. }
  50. filters[0].gain = gain;
  51. }
  52. for (size_t c = 0; c < s.channels.size (); c++) {
  53. const bool more = static_cast<size_t> (k + 1) * UpdateSize <= s.channels[c].size ();
  54. const float* samples = more ? s.channels[c].data () + static_cast<size_t> (k) * UpdateSize : silence.data ();
  55. mixer.mix (channels[c], filters[c], samples, UpdateSize, fading ? UpdateSize : 0);
  56. }
  57. fading = true;
  58. mixer.finish (left.data (), right.data (), UpdateSize);
  59. for (int i = 0; i < UpdateSize; i++) {
  60. frames[2 * i] = left[i];
  61. frames[2 * i + 1] = right[i];
  62. }
  63. fwrite (frames.data (), 4, frames.size (), out);
  64. }
  65. fclose (out);
  66. }