WavWriter.cpp 4.2 KB

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