WavWriter.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2020, William McPherson <willmcpherson2@gmail.com>
  3. * Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Endian.h>
  8. #include <LibAudio/WavLoader.h>
  9. #include <LibAudio/WavWriter.h>
  10. namespace Audio {
  11. ErrorOr<NonnullOwnPtr<WavWriter>> WavWriter::create_from_file(StringView path, int sample_rate, u16 num_channels, PcmSampleFormat sample_format)
  12. {
  13. auto wav_writer = TRY(adopt_nonnull_own_or_enomem(new (nothrow) WavWriter(sample_rate, num_channels, sample_format)));
  14. TRY(wav_writer->set_file(path));
  15. return wav_writer;
  16. }
  17. WavWriter::WavWriter(int sample_rate, u16 num_channels, PcmSampleFormat sample_format)
  18. : m_sample_rate(sample_rate)
  19. , m_num_channels(num_channels)
  20. , m_sample_format(sample_format)
  21. {
  22. }
  23. WavWriter::~WavWriter()
  24. {
  25. if (!m_finalized)
  26. (void)finalize();
  27. }
  28. ErrorOr<void> WavWriter::set_file(StringView path)
  29. {
  30. m_file = TRY(Core::File::open(path, Core::File::OpenMode::Write));
  31. TRY(m_file->seek(44, SeekMode::SetPosition));
  32. m_finalized = false;
  33. return {};
  34. }
  35. ErrorOr<void> WavWriter::write_samples(ReadonlySpan<Sample> samples)
  36. {
  37. switch (m_sample_format) {
  38. // FIXME: For non-float formats, we don't add good quantization noise, leading to possibly unpleasant quantization artifacts.
  39. case PcmSampleFormat::Uint8: {
  40. constexpr float scale = static_cast<float>(NumericLimits<u8>::max()) * .5f;
  41. for (auto const& sample : samples) {
  42. u8 left = static_cast<u8>((sample.left + 1) * scale);
  43. u8 right = static_cast<u8>((sample.right + 1) * scale);
  44. TRY(m_file->write_value(left));
  45. TRY(m_file->write_value(right));
  46. }
  47. m_data_sz += samples.size() * 2 * sizeof(u8);
  48. break;
  49. }
  50. case PcmSampleFormat::Int16: {
  51. constexpr float scale = static_cast<float>(NumericLimits<i16>::max());
  52. for (auto const& sample : samples) {
  53. u16 left = AK::convert_between_host_and_little_endian(static_cast<i16>(sample.left * scale));
  54. u16 right = AK::convert_between_host_and_little_endian(static_cast<i16>(sample.right * scale));
  55. TRY(m_file->write_value(left));
  56. TRY(m_file->write_value(right));
  57. }
  58. m_data_sz += samples.size() * 2 * sizeof(u16);
  59. break;
  60. }
  61. default:
  62. VERIFY_NOT_REACHED();
  63. }
  64. return {};
  65. }
  66. ErrorOr<void> WavWriter::finalize()
  67. {
  68. VERIFY(!m_finalized);
  69. m_finalized = true;
  70. if (m_file && m_file->is_open()) {
  71. TRY(m_file->seek(0, SeekMode::SetPosition));
  72. TRY(write_header());
  73. m_file->close();
  74. }
  75. m_data_sz = 0;
  76. return {};
  77. }
  78. ErrorOr<void> WavWriter::write_header()
  79. {
  80. // "RIFF"
  81. static u32 riff = 0x46464952;
  82. TRY(m_file->write_value(riff));
  83. // Size of data + (size of header - previous field - this field)
  84. u32 sz = m_data_sz + (44 - 4 - 4);
  85. TRY(m_file->write_value(sz));
  86. // "WAVE"
  87. static u32 wave = 0x45564157;
  88. TRY(m_file->write_value(wave));
  89. // "fmt "
  90. static u32 fmt_id = 0x20746D66;
  91. TRY(m_file->write_value(fmt_id));
  92. // Size of the next 6 fields
  93. static u32 fmt_size = 16;
  94. TRY(m_file->write_value(fmt_size));
  95. static u16 audio_format = to_underlying(RIFF::WaveFormat::Pcm);
  96. TRY(m_file->write_value(audio_format));
  97. TRY(m_file->write_value(m_num_channels));
  98. TRY(m_file->write_value(m_sample_rate));
  99. VERIFY(m_sample_format == PcmSampleFormat::Int16 || m_sample_format == PcmSampleFormat::Uint8);
  100. u16 bits_per_sample = pcm_bits_per_sample(m_sample_format);
  101. u32 byte_rate = m_sample_rate * m_num_channels * (bits_per_sample / 8);
  102. TRY(m_file->write_value(byte_rate));
  103. u16 block_align = m_num_channels * (bits_per_sample / 8);
  104. TRY(m_file->write_value(block_align));
  105. TRY(m_file->write_value(bits_per_sample));
  106. // "data"
  107. static u32 chunk_id = 0x61746164;
  108. TRY(m_file->write_value(chunk_id));
  109. TRY(m_file->write_value(m_data_sz));
  110. return {};
  111. }
  112. }