WavLoader.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/MemoryStream.h>
  10. #include <AK/OwnPtr.h>
  11. #include <AK/RefPtr.h>
  12. #include <AK/Stream.h>
  13. #include <AK/String.h>
  14. #include <AK/StringView.h>
  15. #include <AK/WeakPtr.h>
  16. #include <LibAudio/Buffer.h>
  17. #include <LibAudio/Loader.h>
  18. #include <LibCore/File.h>
  19. #include <LibCore/FileStream.h>
  20. namespace Audio {
  21. class Buffer;
  22. // defines for handling the WAV header data
  23. #define WAVE_FORMAT_PCM 0x0001 // PCM
  24. #define WAVE_FORMAT_IEEE_FLOAT 0x0003 // IEEE float
  25. #define WAVE_FORMAT_ALAW 0x0006 // 8-bit ITU-T G.711 A-law
  26. #define WAVE_FORMAT_MULAW 0x0007 // 8-bit ITU-T G.711 µ-law
  27. #define WAVE_FORMAT_EXTENSIBLE 0xFFFE // Determined by SubFormat
  28. // Parses a WAV file and produces an Audio::Buffer.
  29. class WavLoaderPlugin : public LoaderPlugin {
  30. public:
  31. WavLoaderPlugin(const StringView& path);
  32. WavLoaderPlugin(const ByteBuffer& buffer);
  33. virtual bool sniff() override { return valid; }
  34. virtual bool has_error() override { return !m_error_string.is_null(); }
  35. virtual const String& error_string() override { return m_error_string; }
  36. // The Buffer returned contains input data resampled at the
  37. // destination audio device sample rate.
  38. virtual RefPtr<Buffer> get_more_samples(size_t max_bytes_to_read_from_input = 128 * KiB) override;
  39. virtual void reset() override { return seek(0); }
  40. // sample_index 0 is the start of the raw audio sample data
  41. // within the file/stream.
  42. virtual void seek(const int sample_index) override;
  43. virtual int loaded_samples() override { return m_loaded_samples; }
  44. virtual int total_samples() override { return m_total_samples; }
  45. virtual u32 sample_rate() override { return m_sample_rate; }
  46. virtual u16 num_channels() override { return m_num_channels; }
  47. virtual PcmSampleFormat pcm_format() override { return m_sample_format; }
  48. virtual RefPtr<Core::File> file() override { return m_file; }
  49. private:
  50. bool parse_header();
  51. bool valid { false };
  52. RefPtr<Core::File> m_file;
  53. OwnPtr<AK::InputStream> m_stream;
  54. AK::InputMemoryStream* m_memory_stream;
  55. String m_error_string;
  56. // TODO: We should probably move resampling into the audio server.
  57. //
  58. // It would avoid duplicate resampling code and would allow clients
  59. // to be agnostic of the destination audio device's sample rate.
  60. OwnPtr<ResampleHelper<double>> m_resampler;
  61. u32 m_sample_rate { 0 };
  62. u16 m_num_channels { 0 };
  63. PcmSampleFormat m_sample_format;
  64. size_t m_byte_offset_of_data_samples { 0 };
  65. // FIXME: Get this value from the audio server
  66. int m_device_sample_rate { 44100 };
  67. int m_loaded_samples { 0 };
  68. int m_total_samples { 0 };
  69. };
  70. }