Loader.h 2.8 KB

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