WavWriter.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2020, William McPherson <willmcpherson2@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibAudio/WavWriter.h>
  27. namespace Audio {
  28. WavWriter::WavWriter(const StringView& path, int sample_rate, int num_channels, int bits_per_sample)
  29. : m_sample_rate(sample_rate)
  30. , m_num_channels(num_channels)
  31. , m_bits_per_sample(bits_per_sample)
  32. {
  33. set_file(path);
  34. }
  35. WavWriter::WavWriter(int sample_rate, int num_channels, int bits_per_sample)
  36. : m_sample_rate(sample_rate)
  37. , m_num_channels(num_channels)
  38. , m_bits_per_sample(bits_per_sample)
  39. {
  40. }
  41. WavWriter::~WavWriter()
  42. {
  43. if (!m_finalized)
  44. finalize();
  45. }
  46. void WavWriter::set_file(const StringView& path)
  47. {
  48. m_file = Core::File::construct(path);
  49. if (!m_file->open(Core::IODevice::ReadWrite)) {
  50. m_error_string = String::formatted("Can't open file: {}", m_file->error_string());
  51. return;
  52. }
  53. m_file->seek(44);
  54. m_finalized = false;
  55. }
  56. void WavWriter::write_samples(const u8* samples, size_t size)
  57. {
  58. m_data_sz += size;
  59. m_file->write(samples, size);
  60. }
  61. void WavWriter::finalize()
  62. {
  63. ASSERT(!m_finalized);
  64. m_finalized = true;
  65. if (m_file) {
  66. m_file->seek(0);
  67. write_header();
  68. m_file->close();
  69. }
  70. m_data_sz = 0;
  71. }
  72. void WavWriter::write_header()
  73. {
  74. // "RIFF"
  75. static u32 riff = 0x46464952;
  76. m_file->write(reinterpret_cast<u8*>(&riff), sizeof(riff));
  77. // Size of data + (size of header - previous field - this field)
  78. u32 sz = m_data_sz + (44 - 4 - 4);
  79. m_file->write(reinterpret_cast<u8*>(&sz), sizeof(sz));
  80. // "WAVE"
  81. static u32 wave = 0x45564157;
  82. m_file->write(reinterpret_cast<u8*>(&wave), sizeof(wave));
  83. // "fmt "
  84. static u32 fmt_id = 0x20746D66;
  85. m_file->write(reinterpret_cast<u8*>(&fmt_id), sizeof(fmt_id));
  86. // Size of the next 6 fields
  87. static u32 fmt_size = 16;
  88. m_file->write(reinterpret_cast<u8*>(&fmt_size), sizeof(fmt_size));
  89. // 1 for PCM
  90. static u16 audio_format = 1;
  91. m_file->write(reinterpret_cast<u8*>(&audio_format), sizeof(audio_format));
  92. m_file->write(reinterpret_cast<u8*>(&m_num_channels), sizeof(m_num_channels));
  93. m_file->write(reinterpret_cast<u8*>(&m_sample_rate), sizeof(m_sample_rate));
  94. u32 byte_rate = m_sample_rate * m_num_channels * (m_bits_per_sample / 8);
  95. m_file->write(reinterpret_cast<u8*>(&byte_rate), sizeof(byte_rate));
  96. u16 block_align = m_num_channels * (m_bits_per_sample / 8);
  97. m_file->write(reinterpret_cast<u8*>(&block_align), sizeof(block_align));
  98. m_file->write(reinterpret_cast<u8*>(&m_bits_per_sample), sizeof(m_bits_per_sample));
  99. // "data"
  100. static u32 chunk_id = 0x61746164;
  101. m_file->write(reinterpret_cast<u8*>(&chunk_id), sizeof(chunk_id));
  102. m_file->write(reinterpret_cast<u8*>(&m_data_sz), sizeof(m_data_sz));
  103. }
  104. }