summaryrefslogtreecommitdiff
path: root/Libraries/LibAudio/WavWriter.h
diff options
context:
space:
mode:
authorWilliam McPherson <willmcpherson2@gmail.com>2020-02-06 17:33:12 +1100
committerAndreas Kling <kling@serenityos.org>2020-02-06 19:13:53 +0100
commit82d17d2c79e8a680a211fbfd370fc257b6ea494a (patch)
tree618a264f08292ea88ba15825abc162c67d264c1d /Libraries/LibAudio/WavWriter.h
parent31662df3b51fb1f4a4cdc38fd72ebd702cd1d7d6 (diff)
downloadserenity-82d17d2c79e8a680a211fbfd370fc257b6ea494a.zip
LibAudio: Implement Audio::WavWriter
This class mirrors WavLoader and uses the same variable names so they can be read side-by-side. In a simple use-case, you construct a WavWriter object with your file path and audio parameters, call write_samples() with your bytes and then either finalize() to finish writing the file or allow the destructor to do it automatically. finalize() is needed because the WAV header cannot be written until the size of the data section is known. You only need to call this manually if you want to write a WAV file and then immediately load it. You can also use one WavWriter instance multiple times by repeatedly calling finalize() and set_file().
Diffstat (limited to 'Libraries/LibAudio/WavWriter.h')
-rw-r--r--Libraries/LibAudio/WavWriter.h70
1 files changed, 70 insertions, 0 deletions
diff --git a/Libraries/LibAudio/WavWriter.h b/Libraries/LibAudio/WavWriter.h
new file mode 100644
index 0000000000..bab7e297bd
--- /dev/null
+++ b/Libraries/LibAudio/WavWriter.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2020-2020, William McPherson <willmcpherson2@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <AK/StringView.h>
+#include <LibCore/File.h>
+
+namespace Audio {
+
+class WavWriter {
+public:
+ WavWriter(const StringView& path, int sample_rate = 44100, int num_channels = 2, int bits_per_sample = 16);
+ WavWriter(int sample_rate = 44100, int num_channels = 2, int bits_per_sample = 16);
+ ~WavWriter();
+
+ bool has_error() const { return !m_error_string.is_null(); }
+ const char* error_string() const { return m_error_string.characters(); }
+
+ void write_samples(const u8* samples, size_t size);
+ void finalize(); // You can finalize manually or let the destructor do it.
+
+ u32 sample_rate() const { return m_sample_rate; }
+ u16 num_channels() const { return m_num_channels; }
+ u16 bits_per_sample() const { return m_bits_per_sample; }
+ RefPtr<Core::File> file() const { return m_file; }
+
+ void set_file(const StringView& path);
+ void set_num_channels(int num_channels) { m_num_channels = num_channels; }
+ void set_sample_rate(int sample_rate) { m_sample_rate = sample_rate; }
+ void set_bits_per_sample(int bits_per_sample) { m_bits_per_sample = bits_per_sample; }
+
+ void clear_error() { m_error_string = String(); }
+
+private:
+ void write_header();
+ RefPtr<Core::File> m_file;
+ String m_error_string;
+ bool m_finalized { false };
+
+ u32 m_sample_rate;
+ u16 m_num_channels;
+ u16 m_bits_per_sample;
+ u32 m_data_sz { 0 };
+};
+
+}