Loader.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2018-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FixedArray.h>
  8. #include <AK/NonnullOwnPtr.h>
  9. #include <AK/NonnullRefPtr.h>
  10. #include <AK/RefCounted.h>
  11. #include <AK/RefPtr.h>
  12. #include <AK/Result.h>
  13. #include <AK/Span.h>
  14. #include <AK/Stream.h>
  15. #include <AK/StringView.h>
  16. #include <AK/Try.h>
  17. #include <LibAudio/GenericTypes.h>
  18. #include <LibAudio/LoaderError.h>
  19. #include <LibAudio/Metadata.h>
  20. #include <LibAudio/Sample.h>
  21. #include <LibAudio/SampleFormats.h>
  22. namespace Audio {
  23. static constexpr StringView no_plugin_error = "No loader plugin available"sv;
  24. // Experimentally determined to be a decent buffer size on i686:
  25. // 4K (the default) is slightly worse, and 64K is much worse.
  26. // At sufficiently large buffer sizes, the advantage of infrequent read() calls is outweighed by the memmove() overhead.
  27. // There was no intensive fine-tuning done to determine this value, so improvements may definitely be possible.
  28. constexpr size_t const loader_buffer_size = 8 * KiB;
  29. // Two seek points should ideally not be farther apart than this.
  30. // This variable is a heuristic for seek table-constructing loaders.
  31. constexpr u64 const maximum_seekpoint_distance_ms = 1000;
  32. // Seeking should be at least as precise as this.
  33. // That means: The actual achieved seek position must not be more than this amount of time before the requested seek position.
  34. constexpr u64 const seek_tolerance_ms = 5000;
  35. using LoaderSamples = ErrorOr<FixedArray<Sample>, LoaderError>;
  36. using MaybeLoaderError = ErrorOr<void, LoaderError>;
  37. class LoaderPlugin {
  38. public:
  39. explicit LoaderPlugin(NonnullOwnPtr<SeekableStream> stream);
  40. virtual ~LoaderPlugin() = default;
  41. // Load as many audio chunks as necessary to get up to the required samples.
  42. // A chunk can be anything that is convenient for the plugin to load in one go without requiring to move samples around different buffers.
  43. // For example: A FLAC, MP3 or QOA frame.
  44. // The chunks are returned in a vector, so the loader can simply add chunks until the requested sample amount is reached.
  45. // The sample count MAY be surpassed, but only as little as possible. It CAN be undershot when the end of the stream is reached.
  46. // If the loader has no chunking limitations (e.g. WAV), it may return a single exact-sized chunk.
  47. virtual ErrorOr<Vector<FixedArray<Sample>>, LoaderError> load_chunks(size_t samples_to_read_from_input) = 0;
  48. virtual MaybeLoaderError reset() = 0;
  49. virtual MaybeLoaderError seek(int const sample_index) = 0;
  50. // total_samples() and loaded_samples() should be independent
  51. // of the number of channels.
  52. //
  53. // For example, with a three-second-long, stereo, 44.1KHz audio file:
  54. // num_channels() should return 2
  55. // sample_rate() should return 44100 (each channel is sampled at this rate)
  56. // total_samples() should return 132300 (sample_rate * three seconds)
  57. virtual int loaded_samples() = 0;
  58. virtual int total_samples() = 0;
  59. virtual u32 sample_rate() = 0;
  60. virtual u16 num_channels() = 0;
  61. // Human-readable name of the file format, of the form <full abbreviation> (.<ending>)
  62. virtual DeprecatedString format_name() = 0;
  63. virtual PcmSampleFormat pcm_format() = 0;
  64. Metadata const& metadata() const { return m_metadata; }
  65. Vector<PictureData> const& pictures() const { return m_pictures; };
  66. protected:
  67. NonnullOwnPtr<SeekableStream> m_stream;
  68. Vector<PictureData> m_pictures;
  69. Metadata m_metadata;
  70. };
  71. class Loader : public RefCounted<Loader> {
  72. public:
  73. static Result<NonnullRefPtr<Loader>, LoaderError> create(StringView path) { return adopt_ref(*new Loader(TRY(create_plugin(path)))); }
  74. static Result<NonnullRefPtr<Loader>, LoaderError> create(Bytes buffer) { return adopt_ref(*new Loader(TRY(create_plugin(buffer)))); }
  75. // Will only read less samples if we're at the end of the stream.
  76. LoaderSamples get_more_samples(size_t samples_to_read_from_input = 128 * KiB);
  77. MaybeLoaderError reset() const { return m_plugin->reset(); }
  78. MaybeLoaderError seek(int const position) const
  79. {
  80. m_buffer.clear_with_capacity();
  81. return m_plugin->seek(position);
  82. }
  83. int loaded_samples() const { return m_plugin->loaded_samples(); }
  84. int total_samples() const { return m_plugin->total_samples(); }
  85. u32 sample_rate() const { return m_plugin->sample_rate(); }
  86. u16 num_channels() const { return m_plugin->num_channels(); }
  87. DeprecatedString format_name() const { return m_plugin->format_name(); }
  88. u16 bits_per_sample() const { return pcm_bits_per_sample(m_plugin->pcm_format()); }
  89. Metadata const& metadata() const { return m_plugin->metadata(); }
  90. Vector<PictureData> const& pictures() const { return m_plugin->pictures(); };
  91. private:
  92. static Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> create_plugin(StringView path);
  93. static Result<NonnullOwnPtr<LoaderPlugin>, LoaderError> create_plugin(Bytes buffer);
  94. explicit Loader(NonnullOwnPtr<LoaderPlugin>);
  95. mutable NonnullOwnPtr<LoaderPlugin> m_plugin;
  96. mutable Vector<Sample, loader_buffer_size> m_buffer;
  97. };
  98. }