WavWriter.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/Noncopyable.h>
  8. #include <AK/StringView.h>
  9. #include <LibCore/File.h>
  10. namespace Audio {
  11. class WavWriter {
  12. AK_MAKE_NONCOPYABLE(WavWriter);
  13. AK_MAKE_NONMOVABLE(WavWriter);
  14. public:
  15. WavWriter(StringView path, int sample_rate = 44100, u16 num_channels = 2, u16 bits_per_sample = 16);
  16. WavWriter(int sample_rate = 44100, u16 num_channels = 2, u16 bits_per_sample = 16);
  17. ~WavWriter();
  18. bool has_error() const { return !m_error_string.is_null(); }
  19. char const* error_string() const { return m_error_string.characters(); }
  20. void write_samples(u8 const* samples, size_t size);
  21. void finalize(); // You can finalize manually or let the destructor do it.
  22. u32 sample_rate() const { return m_sample_rate; }
  23. u16 num_channels() const { return m_num_channels; }
  24. u16 bits_per_sample() const { return m_bits_per_sample; }
  25. RefPtr<Core::File> file() const { return m_file; }
  26. void set_file(StringView path);
  27. void set_num_channels(int num_channels) { m_num_channels = num_channels; }
  28. void set_sample_rate(int sample_rate) { m_sample_rate = sample_rate; }
  29. void set_bits_per_sample(int bits_per_sample) { m_bits_per_sample = bits_per_sample; }
  30. void clear_error() { m_error_string = String(); }
  31. private:
  32. void write_header();
  33. RefPtr<Core::File> m_file;
  34. String m_error_string;
  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. }