WavWriter.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2020, William McPherson <willmcpherson2@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DeprecatedString.h>
  8. #include <AK/Noncopyable.h>
  9. #include <AK/RefPtr.h>
  10. #include <AK/StringView.h>
  11. #include <LibAudio/Sample.h>
  12. #include <LibCore/File.h>
  13. #include <LibCore/Forward.h>
  14. namespace Audio {
  15. class WavWriter {
  16. AK_MAKE_NONCOPYABLE(WavWriter);
  17. AK_MAKE_NONMOVABLE(WavWriter);
  18. public:
  19. static ErrorOr<NonnullOwnPtr<WavWriter>> create_from_file(StringView path, int sample_rate = 44100, u16 num_channels = 2, u16 bits_per_sample = 16);
  20. WavWriter(int sample_rate = 44100, u16 num_channels = 2, u16 bits_per_sample = 16);
  21. ~WavWriter();
  22. ErrorOr<void> write_samples(Span<Sample> samples);
  23. void finalize(); // You can finalize manually or let the destructor do it.
  24. u32 sample_rate() const { return m_sample_rate; }
  25. u16 num_channels() const { return m_num_channels; }
  26. u16 bits_per_sample() const { return m_bits_per_sample; }
  27. Core::File& file() const { return *m_file; }
  28. ErrorOr<void> set_file(StringView path);
  29. void set_num_channels(int num_channels) { m_num_channels = num_channels; }
  30. void set_sample_rate(int sample_rate) { m_sample_rate = sample_rate; }
  31. void set_bits_per_sample(int bits_per_sample) { m_bits_per_sample = bits_per_sample; }
  32. private:
  33. ErrorOr<void> write_header();
  34. OwnPtr<Core::File> m_file;
  35. bool m_finalized { false };
  36. u32 m_sample_rate;
  37. u16 m_num_channels;
  38. u16 m_bits_per_sample;
  39. u32 m_data_sz { 0 };
  40. };
  41. }