Serenity Operating System
1/*
2 * Copyright (c) 2020-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
27#include <LibAudio/WavWriter.h>
28
29namespace Audio {
30
31WavWriter::WavWriter(const StringView& path, int sample_rate, int num_channels, int bits_per_sample)
32 : m_sample_rate(sample_rate)
33 , m_num_channels(num_channels)
34 , m_bits_per_sample(bits_per_sample)
35{
36 set_file(path);
37}
38
39WavWriter::WavWriter(int sample_rate, int num_channels, int bits_per_sample)
40 : m_sample_rate(sample_rate)
41 , m_num_channels(num_channels)
42 , m_bits_per_sample(bits_per_sample)
43{
44}
45
46WavWriter::~WavWriter()
47{
48 if (!m_finalized)
49 finalize();
50}
51
52void WavWriter::set_file(const StringView& path)
53{
54 m_file = Core::File::construct(path);
55 if (!m_file->open(Core::IODevice::ReadWrite)) {
56 m_error_string = String::format("Can't open file: %s", m_file->error_string());
57 return;
58 }
59 m_file->seek(44);
60 m_finalized = false;
61}
62
63void WavWriter::write_samples(const u8* samples, size_t size)
64{
65 m_data_sz += size;
66 m_file->write(samples, size);
67}
68
69void WavWriter::finalize()
70{
71 ASSERT(!m_finalized);
72 m_finalized = true;
73 m_file->seek(0);
74 write_header();
75 m_file->close();
76 m_data_sz = 0;
77}
78
79void WavWriter::write_header()
80{
81 // "RIFF"
82 static u32 riff = 0x46464952;
83 m_file->write(reinterpret_cast<u8*>(&riff), sizeof(riff));
84
85 // Size of data + (size of header - previous field - this field)
86 u32 sz = m_data_sz + (44 - 4 - 4);
87 m_file->write(reinterpret_cast<u8*>(&sz), sizeof(sz));
88
89 // "WAVE"
90 static u32 wave = 0x45564157;
91 m_file->write(reinterpret_cast<u8*>(&wave), sizeof(wave));
92
93 // "fmt "
94 static u32 fmt_id = 0x20746D66;
95 m_file->write(reinterpret_cast<u8*>(&fmt_id), sizeof(fmt_id));
96
97 // Size of the next 6 fields
98 static u32 fmt_size = 16;
99 m_file->write(reinterpret_cast<u8*>(&fmt_size), sizeof(fmt_size));
100
101 // 1 for PCM
102 static u16 audio_format = 1;
103 m_file->write(reinterpret_cast<u8*>(&audio_format), sizeof(audio_format));
104
105 m_file->write(reinterpret_cast<u8*>(&m_num_channels), sizeof(m_num_channels));
106
107 m_file->write(reinterpret_cast<u8*>(&m_sample_rate), sizeof(m_sample_rate));
108
109 u32 byte_rate = m_sample_rate * m_num_channels * (m_bits_per_sample / 8);
110 m_file->write(reinterpret_cast<u8*>(&byte_rate), sizeof(byte_rate));
111
112 u16 block_align = m_num_channels * (m_bits_per_sample / 8);
113 m_file->write(reinterpret_cast<u8*>(&block_align), sizeof(block_align));
114
115 m_file->write(reinterpret_cast<u8*>(&m_bits_per_sample), sizeof(m_bits_per_sample));
116
117 // "data"
118 static u32 chunk_id = 0x61746164;
119 m_file->write(reinterpret_cast<u8*>(&chunk_id), sizeof(chunk_id));
120
121 m_file->write(reinterpret_cast<u8*>(&m_data_sz), sizeof(m_data_sz));
122}
123
124}