WaveFile.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. // ---------------------------------------------------------------------------------
  4. // Class: WaveFileWriter
  5. // Description: Simple utility class to make it easy to write long 16-bit stereo
  6. // audio streams to disk.
  7. // Use Start() to start recording to a file, and AddStereoSamples to add wave data.
  8. // The float variant will convert from -1.0-1.0 range and clamp.
  9. // Alternatively, AddSamplesBE for big endian wave data.
  10. // If Stop is not called when it destructs, the destructor will call Stop().
  11. // ---------------------------------------------------------------------------------
  12. #pragma once
  13. #include <array>
  14. #include <string>
  15. #include "Common/CommonTypes.h"
  16. #include "Common/IOFile.h"
  17. class WaveFileWriter
  18. {
  19. public:
  20. WaveFileWriter();
  21. ~WaveFileWriter();
  22. WaveFileWriter(const WaveFileWriter&) = delete;
  23. WaveFileWriter& operator=(const WaveFileWriter&) = delete;
  24. WaveFileWriter(WaveFileWriter&&) = delete;
  25. WaveFileWriter& operator=(WaveFileWriter&&) = delete;
  26. bool Start(const std::string& filename, u32 sample_rate_divisor);
  27. void Stop();
  28. void SetSkipSilence(bool skip) { skip_silence = skip; }
  29. // big endian
  30. void AddStereoSamplesBE(const short* sample_data, u32 count, u32 sample_rate_divisor,
  31. int l_volume, int r_volume);
  32. u32 GetAudioSize() const { return audio_size; }
  33. private:
  34. static constexpr size_t BUFFER_SIZE = 32 * 1024;
  35. void Write(u32 value);
  36. void Write4(const char* ptr);
  37. File::IOFile file;
  38. std::string basename;
  39. u32 file_index = 0;
  40. u32 audio_size = 0;
  41. u32 current_sample_rate_divisor;
  42. std::array<short, BUFFER_SIZE> conv_buffer{};
  43. bool skip_silence = false;
  44. };