Loader.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2018-2023, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypedTransfer.h>
  7. #include <LibAudio/FlacLoader.h>
  8. #include <LibAudio/Loader.h>
  9. #include <LibAudio/MP3Loader.h>
  10. #include <LibAudio/QOALoader.h>
  11. #include <LibAudio/WavLoader.h>
  12. #include <LibCore/MappedFile.h>
  13. namespace Audio {
  14. LoaderPlugin::LoaderPlugin(NonnullOwnPtr<SeekableStream> stream)
  15. : m_stream(move(stream))
  16. {
  17. }
  18. Loader::Loader(NonnullOwnPtr<LoaderPlugin> plugin)
  19. : m_plugin(move(plugin))
  20. {
  21. }
  22. struct LoaderPluginInitializer {
  23. bool (*sniff)(SeekableStream&);
  24. ErrorOr<NonnullOwnPtr<LoaderPlugin>, LoaderError> (*create)(NonnullOwnPtr<SeekableStream>);
  25. };
  26. #define ENUMERATE_LOADER_PLUGINS \
  27. __ENUMERATE_LOADER_PLUGIN(Wav) \
  28. __ENUMERATE_LOADER_PLUGIN(Flac) \
  29. __ENUMERATE_LOADER_PLUGIN(QOA) \
  30. __ENUMERATE_LOADER_PLUGIN(MP3)
  31. static constexpr LoaderPluginInitializer s_initializers[] = {
  32. #define __ENUMERATE_LOADER_PLUGIN(Type) \
  33. { Type##LoaderPlugin::sniff, Type##LoaderPlugin::create },
  34. ENUMERATE_LOADER_PLUGINS
  35. #undef __ENUMERATE_LOADER_PLUGIN
  36. };
  37. ErrorOr<NonnullRefPtr<Loader>, LoaderError> Loader::create(StringView path)
  38. {
  39. auto stream = TRY(Core::MappedFile::map(path, Core::MappedFile::Mode::ReadOnly));
  40. return adopt_ref(*new (nothrow) Loader(TRY(Loader::create_plugin(move(stream)))));
  41. }
  42. ErrorOr<NonnullRefPtr<Loader>, LoaderError> Loader::create(ReadonlyBytes buffer)
  43. {
  44. auto stream = TRY(try_make<FixedMemoryStream>(buffer));
  45. return adopt_ref(*new (nothrow) Loader(TRY(Loader::create_plugin(move(stream)))));
  46. }
  47. ErrorOr<NonnullOwnPtr<LoaderPlugin>, LoaderError> Loader::create_plugin(NonnullOwnPtr<SeekableStream> stream)
  48. {
  49. for (auto const& loader : s_initializers) {
  50. if (loader.sniff(*stream)) {
  51. TRY(stream->seek(0, SeekMode::SetPosition));
  52. return loader.create(move(stream));
  53. }
  54. TRY(stream->seek(0, SeekMode::SetPosition));
  55. }
  56. return LoaderError { "No loader plugin available" };
  57. }
  58. LoaderSamples Loader::get_more_samples(size_t samples_to_read_from_input)
  59. {
  60. if (m_plugin_at_end_of_stream && m_buffer.is_empty())
  61. return FixedArray<Sample> {};
  62. size_t remaining_samples = total_samples() - loaded_samples();
  63. size_t samples_to_read = min(remaining_samples, samples_to_read_from_input);
  64. auto samples = TRY(FixedArray<Sample>::create(samples_to_read));
  65. size_t sample_index = 0;
  66. if (m_buffer.size() > 0) {
  67. size_t to_transfer = min(m_buffer.size(), samples_to_read);
  68. AK::TypedTransfer<Sample>::move(samples.data(), m_buffer.data(), to_transfer);
  69. if (to_transfer < m_buffer.size())
  70. m_buffer.remove(0, to_transfer);
  71. else
  72. m_buffer.clear_with_capacity();
  73. sample_index += to_transfer;
  74. }
  75. while (sample_index < samples_to_read) {
  76. auto chunk_data = TRY(m_plugin->load_chunks(samples_to_read - sample_index));
  77. chunk_data.remove_all_matching([](auto& chunk) { return chunk.is_empty(); });
  78. if (chunk_data.is_empty()) {
  79. m_plugin_at_end_of_stream = true;
  80. break;
  81. }
  82. for (auto& chunk : chunk_data) {
  83. if (sample_index < samples_to_read) {
  84. auto count = min(samples_to_read - sample_index, chunk.size());
  85. AK::TypedTransfer<Sample>::move(samples.span().offset(sample_index), chunk.data(), count);
  86. // We didn't read all of the chunk; transfer the rest into the buffer.
  87. if (count < chunk.size()) {
  88. auto remaining_samples_count = chunk.size() - count;
  89. // We will always have an empty buffer at this point!
  90. TRY(m_buffer.try_append(chunk.span().offset(count), remaining_samples_count));
  91. }
  92. } else {
  93. // We're now past what the user requested. Transfer the entirety of the data into the buffer.
  94. TRY(m_buffer.try_append(chunk.data(), chunk.size()));
  95. }
  96. sample_index += chunk.size();
  97. }
  98. }
  99. return samples;
  100. }
  101. }