UserSampleQueue.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DisjointChunks.h>
  8. #include <AK/FixedArray.h>
  9. #include <AK/Format.h>
  10. #include <AK/Noncopyable.h>
  11. #include <AK/Vector.h>
  12. #include <LibAudio/Sample.h>
  13. #include <LibThreading/Mutex.h>
  14. namespace Audio {
  15. // A sample queue providing synchronized access to efficiently-stored segmented user-provided audio data.
  16. class UserSampleQueue {
  17. AK_MAKE_NONCOPYABLE(UserSampleQueue);
  18. AK_MAKE_NONMOVABLE(UserSampleQueue);
  19. public:
  20. UserSampleQueue() = default;
  21. void append(FixedArray<Sample>&& samples);
  22. void clear();
  23. // Slice off some amount of samples from the beginning.
  24. void discard_samples(size_t count);
  25. Sample operator[](size_t index);
  26. // The number of samples in the span.
  27. size_t size();
  28. bool is_empty();
  29. size_t remaining_samples();
  30. private:
  31. // Re-initialize the spans after a vector resize.
  32. void fix_spans();
  33. Threading::Mutex m_sample_mutex;
  34. // Sample data view to keep track of what to play next.
  35. DisjointSpans<Sample> m_enqueued_samples;
  36. // The number of samples that were played from the backing store since last discarding its start.
  37. size_t m_samples_to_discard { 0 };
  38. // The backing store for the enqueued sample view.
  39. DisjointChunks<Sample, FixedArray<Sample>> m_backing_samples {};
  40. };
  41. }