Loader.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2018-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullOwnPtr.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/RefPtr.h>
  11. #include <AK/Result.h>
  12. #include <AK/Span.h>
  13. #include <AK/StringView.h>
  14. #include <AK/Try.h>
  15. #include <LibAudio/Buffer.h>
  16. #include <LibAudio/LoaderError.h>
  17. #include <LibCore/File.h>
  18. namespace Audio {
  19. static constexpr StringView no_plugin_error = "No loader plugin available";
  20. using LoaderSamples = Result<FixedArray<Sample>, LoaderError>;
  21. using MaybeLoaderError = Result<void, LoaderError>;
  22. class LoaderPlugin {
  23. public:
  24. virtual ~LoaderPlugin() = default;
  25. virtual MaybeLoaderError initialize() = 0;
  26. virtual LoaderSamples get_more_samples(size_t max_bytes_to_read_from_input = 128 * KiB) = 0;
  27. virtual MaybeLoaderError reset() = 0;
  28. virtual MaybeLoaderError seek(int const sample_index) = 0;
  29. // total_samples() and loaded_samples() should be independent
  30. // of the number of channels.
  31. //
  32. // For example, with a three-second-long, stereo, 44.1KHz audio file:
  33. // num_channels() should return 2
  34. // sample_rate() should return 44100 (each channel is sampled at this rate)
  35. // total_samples() should return 132300 (sample_rate * three seconds)
  36. virtual int loaded_samples() = 0;
  37. virtual int total_samples() = 0;
  38. virtual u32 sample_rate() = 0;
  39. virtual u16 num_channels() = 0;
  40. // Human-readable name of the file format, of the form <full abbreviation> (.<ending>)
  41. virtual String format_name() = 0;
  42. virtual PcmSampleFormat pcm_format() = 0;
  43. virtual RefPtr<Core::File> file() = 0;
  44. };
  45. class Loader : public RefCounted<Loader> {
  46. public:
  47. static Result<NonnullRefPtr<Loader>, LoaderError> create(StringView path) { return adopt_ref(*new Loader(TRY(try_create(path)))); }
  48. static Result<NonnullRefPtr<Loader>, LoaderError> create(Bytes& buffer) { return adopt_ref(*new Loader(TRY(try_create(buffer)))); }
  49. LoaderSamples get_more_samples(size_t max_samples_to_read_from_input = 128 * KiB) const { return m_plugin->get_more_samples(max_samples_to_read_from_input); }
  50. MaybeLoaderError reset() const { return m_plugin->reset(); }
  51. MaybeLoaderError seek(int const position) const { return m_plugin->seek(position); }
  52. int loaded_samples() const { return m_plugin->loaded_samples(); }
  53. int total_samples() const { return m_plugin->total_samples(); }
  54. u32 sample_rate() const { return m_plugin->sample_rate(); }
  55. u16 num_channels() const { return m_plugin->num_channels(); }
  56. String format_name() const { return m_plugin->format_name(); }
  57. u16 bits_per_sample() const { return pcm_bits_per_sample(m_plugin->pcm_format()); }
  58. RefPtr<Core::File> file() const { return m_plugin->file(); }
  59. private:
  60. static Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> try_create(StringView path);
  61. static Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> try_create(Bytes& buffer);
  62. explicit Loader(NonnullOwnPtr<LoaderPlugin>);
  63. mutable NonnullOwnPtr<LoaderPlugin> m_plugin;
  64. };
  65. }