ref.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // ref <scenario> <out.f32>: renders a scenario through OpenAL Soft 1.21.1 (the version WE's mediaextensions64.dll
  2. // links) on a 48 kHz float stereo loopback device with HRTF on, one alcRenderSamplesSOFT per update
  3. #include "scenarios.h"
  4. #include <AL/al.h>
  5. #include <AL/alc.h>
  6. #include <AL/alext.h>
  7. #include <algorithm>
  8. #include <cstdio>
  9. int main (int argc, char** argv) {
  10. if (argc < 3) {
  11. fprintf (stderr, "usage: %s moving|center|impulse|stereo out.f32\n", argv[0]);
  12. return 1;
  13. }
  14. const Scenario s = scenario (argv[1]);
  15. auto openLoopback = reinterpret_cast<LPALCLOOPBACKOPENDEVICESOFT> (alcGetProcAddress (nullptr, "alcLoopbackOpenDeviceSOFT"));
  16. auto render = reinterpret_cast<LPALCRENDERSAMPLESSOFT> (alcGetProcAddress (nullptr, "alcRenderSamplesSOFT"));
  17. ALCdevice* device = openLoopback (nullptr);
  18. const ALCint attrs[] = { ALC_FREQUENCY, 48000, ALC_FORMAT_CHANNELS_SOFT, ALC_STEREO_SOFT,
  19. ALC_FORMAT_TYPE_SOFT, ALC_FLOAT_SOFT, ALC_HRTF_SOFT, ALC_TRUE,
  20. 0 };
  21. ALCcontext* context = alcCreateContext (device, attrs);
  22. alcMakeContextCurrent (context);
  23. ALCint status = 0;
  24. alcGetIntegerv (device, ALC_HRTF_STATUS_SOFT, 1, &status);
  25. fprintf (stderr, "HRTF %s: %s\n", status ? "on" : "off", status ? alcGetString (device, ALC_HRTF_SPECIFIER_SOFT) : "-");
  26. ALuint buffer, source;
  27. alGenBuffers (1, &buffer);
  28. alGenSources (1, &source);
  29. if (s.stereo) {
  30. std::vector<float> interleaved;
  31. for (size_t i = 0; i < s.channels[0].size (); i++) {
  32. interleaved.push_back (s.channels[0][i]);
  33. interleaved.push_back (s.channels[1][i]);
  34. }
  35. alBufferData (buffer, AL_FORMAT_STEREO_FLOAT32, interleaved.data (), interleaved.size () * 4, 48000);
  36. } else {
  37. alBufferData (buffer, AL_FORMAT_MONO_FLOAT32, s.channels[0].data (), s.channels[0].size () * 4, 48000);
  38. }
  39. alSourcei (source, AL_BUFFER, buffer);
  40. FILE* out = fopen (argv[2], "wb");
  41. std::vector<float> frames (UpdateSize * 2);
  42. for (int k = 0; k < Blocks + 4; k++) {
  43. const Block& b = s.path[std::min (k, Blocks - 1)];
  44. alSource3f (source, AL_POSITION, b.x, b.y, b.z);
  45. alSourcef (source, AL_GAIN, b.gain);
  46. if (k == 0) {
  47. alSourcePlay (source);
  48. }
  49. render (device, frames.data (), UpdateSize);
  50. fwrite (frames.data (), 4, frames.size (), out);
  51. }
  52. fclose (out);
  53. }