Loader.h 5.1 KB

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