scenarios.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Test scenarios shared by ref.cpp (OpenAL Soft 1.21.1) and ours.cpp (Audio/Hrtf): a mono or stereo signal and,
  2. // per 1024 sample update, where the source is and its AL_GAIN
  3. #pragma once
  4. #include <cmath>
  5. #include <cstdint>
  6. #include <string>
  7. #include <vector>
  8. struct Block {
  9. float x, y, z, gain;
  10. };
  11. constexpr int UpdateSize = 1024;
  12. constexpr int Blocks = 60;
  13. /** a 440 Hz tone with LCG noise on top, the same bits on both sides */
  14. inline std::vector<float> signal (int frames, uint32_t seed) {
  15. std::vector<float> samples (frames);
  16. for (int i = 0; i < frames; i++) {
  17. seed = seed * 1664525u + 1013904223u;
  18. const float noise = static_cast<float> (seed >> 8) / 16777216.0f * 2.0f - 1.0f;
  19. samples[i] = 0.3f * std::sin (2.0f * 3.14159265f * 440.0f * i / 48000.0f) + 0.2f * noise;
  20. }
  21. return samples;
  22. }
  23. inline std::vector<float> impulse (int frames) {
  24. std::vector<float> samples (frames, 0.0f);
  25. samples[0] = 1.0f;
  26. return samples;
  27. }
  28. /** moves around the listener: every azimuth, elevations up and down, 0.3 to 4.3 units away, now and then on it */
  29. inline std::vector<Block> movingPath () {
  30. std::vector<Block> path;
  31. for (int k = 0; k < Blocks; k++) {
  32. const float az = k * 0.37f, el = std::sin (k * 0.21f) * 1.2f;
  33. const float d = k % 17 == 5 ? 0.0f : 0.3f + 4.0f * (0.5f + 0.5f * std::sin (k * 0.13f));
  34. path.push_back (
  35. { d * std::cos (el) * std::sin (az), d * std::sin (el), -d * std::cos (el) * std::cos (az),
  36. 0.4f + 0.5f * (k % 3) / 2.0f }
  37. );
  38. }
  39. return path;
  40. }
  41. struct Scenario {
  42. bool stereo;
  43. std::vector<std::vector<float>> channels;
  44. std::vector<Block> path;
  45. };
  46. /** moving | center | impulse | stereo */
  47. inline Scenario scenario (const std::string& name) {
  48. Scenario s { name == "stereo", {}, {} };
  49. if (s.stereo) {
  50. auto right = signal (Blocks * UpdateSize, 11);
  51. for (auto& v : right) {
  52. v *= 0.5f;
  53. }
  54. s.channels = { signal (Blocks * UpdateSize, 3), right };
  55. for (int k = 0; k < Blocks; k++) {
  56. s.path.push_back ({ 0, 0, 0, k < 30 ? 0.5f : 0.8f });
  57. }
  58. return s;
  59. }
  60. s.channels = { name == "impulse" ? impulse (Blocks * UpdateSize + 48000) : signal (Blocks * UpdateSize + 48000, 7) };
  61. s.path = name == "moving" ? movingPath () : std::vector<Block> (Blocks, Block { 0, 0, 0, 0.9f });
  62. return s;
  63. }