UserSampleQueue.h 1.4 KB

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