Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
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#pragma once
28
29#include <AK/OwnPtr.h>
30#include <AK/RefPtr.h>
31#include <AK/String.h>
32#include <AK/StringView.h>
33#include <LibAudio/Buffer.h>
34#include <LibCore/File.h>
35
36namespace Audio {
37class Buffer;
38
39// Parses a WAV file and produces an Audio::Buffer.
40class WavLoader {
41public:
42 explicit WavLoader(const StringView& path);
43
44 bool has_error() const { return !m_error_string.is_null(); }
45 const char* error_string() { return m_error_string.characters(); }
46
47 RefPtr<Buffer> get_more_samples(size_t max_bytes_to_read_from_input = 128 * KB);
48
49 void reset();
50 void seek(const int position);
51
52 int loaded_samples() const { return m_loaded_samples; }
53 int total_samples() const { return m_total_samples; }
54 u32 sample_rate() const { return m_sample_rate; }
55 u16 num_channels() const { return m_num_channels; }
56 u16 bits_per_sample() const { return m_bits_per_sample; }
57 RefPtr<Core::File> file() const { return m_file; }
58
59private:
60 bool parse_header();
61 RefPtr<Core::File> m_file;
62 String m_error_string;
63 OwnPtr<ResampleHelper> m_resampler;
64
65 u32 m_sample_rate { 0 };
66 u16 m_num_channels { 0 };
67 u16 m_bits_per_sample { 0 };
68
69 int m_loaded_samples { 0 };
70 int m_total_samples { 0 };
71};
72
73}