Clip.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2021-2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <AK/RefCounted.h>
  9. #include <AK/SinglyLinkedList.h>
  10. #include <AK/Types.h>
  11. #include <LibDSP/Music.h>
  12. namespace DSP {
  13. // A clip is a self-contained snippet of notes or audio that can freely move inside and in between tracks.
  14. class Clip : public RefCounted<Clip> {
  15. public:
  16. Clip(u32 start, u32 length)
  17. : m_start(start)
  18. , m_length(length)
  19. {
  20. }
  21. virtual ~Clip() = default;
  22. u32 start() const { return m_start; }
  23. u32 length() const { return m_length; }
  24. u32 end() const { return m_start + m_length; }
  25. protected:
  26. u32 m_start;
  27. u32 m_length;
  28. };
  29. class AudioClip final : public Clip {
  30. public:
  31. Sample sample_at(u32 time);
  32. Vector<Sample> const& samples() const { return m_samples; }
  33. private:
  34. Vector<Sample> m_samples;
  35. };
  36. class NoteClip final : public Clip {
  37. public:
  38. NoteClip(u32 start, u32 length)
  39. : Clip(start, length)
  40. {
  41. }
  42. Optional<RollNote> note_at(u32 time, u8 pitch) const;
  43. void set_note(RollNote note);
  44. // May do nothing; that's fine.
  45. void remove_note(RollNote note);
  46. ReadonlySpan<RollNote> notes() const { return m_notes.span(); }
  47. RollNote operator[](size_t index) const { return m_notes[index]; }
  48. RollNote operator[](size_t index) { return m_notes[index]; }
  49. bool is_empty() const { return m_notes.is_empty(); }
  50. private:
  51. // FIXME: Better datastructures to think about here: B-Trees or good ol' RBTrees (not very cache friendly)
  52. Vector<RollNote> m_notes;
  53. };
  54. }