refstream.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // refstream <file.wav> <gain> <updates> <out.f32> [stop update] [update frames] [hrtf]: streams a 16 bit WAV at
  2. // its own rate through OpenAL Soft 1.21.1 the way WE's mediaextensions64.dll does (sf::Music queues one second
  3. // buffers, sub_18000D530), 48 kHz float stereo loopback, mixed in updates of the given size (960 = WE's 20 ms on
  4. // WASAPI). The source plays once; with a stop update it gets alSourceStop before that update is rendered. Compare
  5. // with the engine's SDL_AUDIODRIVER=disk output of a "single" sound object from its first non-zero frame
  6. #include <AL/al.h>
  7. #include <AL/alc.h>
  8. #include <AL/alext.h>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cstring>
  12. #include <vector>
  13. int main (int argc, char** argv) {
  14. if (argc < 5) {
  15. fprintf (stderr, "usage: %s file.wav gain updates out.f32 [stop update] [update frames] [hrtf]\n", argv[0]);
  16. return 1;
  17. }
  18. FILE* in = fopen (argv[1], "rb");
  19. std::vector<unsigned char> wav (1 << 26);
  20. wav.resize (fread (wav.data (), 1, wav.size (), in));
  21. fclose (in);
  22. uint16_t channels;
  23. uint32_t rate;
  24. memcpy (&channels, &wav[22], 2);
  25. memcpy (&rate, &wav[24], 4);
  26. size_t offset = 12, dataOffset = 0, dataSize = 0;
  27. while (offset + 8 <= wav.size ()) {
  28. uint32_t size;
  29. memcpy (&size, &wav[offset + 4], 4);
  30. if (memcmp (&wav[offset], "data", 4) == 0) {
  31. dataOffset = offset + 8;
  32. dataSize = size;
  33. break;
  34. }
  35. offset += 8 + size;
  36. }
  37. const int stopAt = argc > 5 ? atoi (argv[5]) : -1;
  38. const int updateFrames = argc > 6 ? atoi (argv[6]) : 960;
  39. const bool hrtf = argc > 7 && atoi (argv[7]) != 0;
  40. auto openLoopback
  41. = reinterpret_cast<LPALCLOOPBACKOPENDEVICESOFT> (alcGetProcAddress (nullptr, "alcLoopbackOpenDeviceSOFT"));
  42. auto render = reinterpret_cast<LPALCRENDERSAMPLESSOFT> (alcGetProcAddress (nullptr, "alcRenderSamplesSOFT"));
  43. ALCdevice* device = openLoopback (nullptr);
  44. const ALCint attrs[] = { ALC_FREQUENCY,
  45. 48000,
  46. ALC_FORMAT_CHANNELS_SOFT,
  47. ALC_STEREO_SOFT,
  48. ALC_FORMAT_TYPE_SOFT,
  49. ALC_FLOAT_SOFT,
  50. ALC_HRTF_SOFT,
  51. hrtf ? ALC_TRUE : ALC_FALSE,
  52. 0 };
  53. alcMakeContextCurrent (alcCreateContext (device, attrs));
  54. ALuint source;
  55. alGenSources (1, &source);
  56. // sf::Music: buffers of rate * channels samples, one second
  57. const size_t chunk = static_cast<size_t> (rate) * channels * 2;
  58. for (size_t done = 0; done < dataSize; done += chunk) {
  59. ALuint buffer;
  60. alGenBuffers (1, &buffer);
  61. alBufferData (
  62. buffer, channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, &wav[dataOffset + done],
  63. static_cast<ALsizei> (std::min (chunk, dataSize - done)), static_cast<ALsizei> (rate)
  64. );
  65. alSourceQueueBuffers (source, 1, &buffer);
  66. }
  67. alSourcef (source, AL_GAIN, atof (argv[2]));
  68. alSourcePlay (source);
  69. FILE* out = fopen (argv[4], "wb");
  70. std::vector<float> frames (static_cast<size_t> (updateFrames) * 2);
  71. for (int k = 0; k < atoi (argv[3]); k++) {
  72. if (k == stopAt) {
  73. alSourceStop (source);
  74. }
  75. render (device, frames.data (), updateFrames);
  76. fwrite (frames.data (), 4, frames.size (), out);
  77. }
  78. fclose (out);
  79. }