Loader.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2018-2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/RefCounted.h>
  29. #include <AK/RefPtr.h>
  30. #include <AK/StringView.h>
  31. #include <LibCore/File.h>
  32. namespace Audio {
  33. class LoaderPlugin {
  34. public:
  35. virtual ~LoaderPlugin() { }
  36. virtual bool sniff() = 0;
  37. virtual bool has_error() { return false; }
  38. virtual const char* error_string() { return ""; }
  39. virtual RefPtr<Buffer> get_more_samples(size_t max_bytes_to_read_from_input = 128 * KiB) = 0;
  40. virtual void reset() = 0;
  41. virtual void seek(const int position) = 0;
  42. virtual int loaded_samples() = 0;
  43. virtual int total_samples() = 0;
  44. virtual u32 sample_rate() = 0;
  45. virtual u16 num_channels() = 0;
  46. virtual u16 bits_per_sample() = 0;
  47. virtual RefPtr<Core::File> file() = 0;
  48. };
  49. class Loader : public RefCounted<Loader> {
  50. public:
  51. static NonnullRefPtr<Loader> create(const StringView& path) { return adopt(*new Loader(path)); }
  52. static NonnullRefPtr<Loader> create(const ByteBuffer& buffer) { return adopt(*new Loader(buffer)); }
  53. bool has_error() const { return m_plugin ? m_plugin->has_error() : true; }
  54. const char* error_string() const { return m_plugin ? m_plugin->error_string() : "No loader plugin available"; }
  55. 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; }
  56. void reset() const
  57. {
  58. if (m_plugin)
  59. m_plugin->reset();
  60. }
  61. void seek(const int position) const
  62. {
  63. if (m_plugin)
  64. m_plugin->seek(position);
  65. }
  66. int loaded_samples() const { return m_plugin ? m_plugin->loaded_samples() : 0; }
  67. int total_samples() const { return m_plugin ? m_plugin->total_samples() : 0; }
  68. u32 sample_rate() const { return m_plugin ? m_plugin->sample_rate() : 0; }
  69. u16 num_channels() const { return m_plugin ? m_plugin->num_channels() : 0; }
  70. u16 bits_per_sample() const { return m_plugin ? m_plugin->bits_per_sample() : 0; }
  71. RefPtr<Core::File> file() const { return m_plugin ? m_plugin->file() : nullptr; }
  72. private:
  73. Loader(const StringView& path);
  74. Loader(const ByteBuffer& buffer);
  75. mutable OwnPtr<LoaderPlugin> m_plugin;
  76. };
  77. }