Clip.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Clip.h"
  7. namespace DSP {
  8. Sample AudioClip::sample_at(u32 time)
  9. {
  10. VERIFY(time < m_length);
  11. return m_samples[time];
  12. }
  13. void NoteClip::set_note(RollNote note)
  14. {
  15. VERIFY(note.pitch >= 0 && note.pitch < note_frequencies.size());
  16. VERIFY(note.off_sample < m_length);
  17. VERIFY(note.length() >= 2);
  18. auto& notes = m_notes[note.pitch];
  19. for (auto it = notes.begin(); !it.is_end();) {
  20. auto iterated_note = *it;
  21. if (iterated_note.on_sample > note.off_sample) {
  22. notes.insert_before(it, note);
  23. return;
  24. }
  25. if (iterated_note.on_sample <= note.on_sample && iterated_note.off_sample >= note.on_sample) {
  26. notes.remove(it);
  27. return;
  28. }
  29. if ((note.on_sample == 0 || iterated_note.on_sample >= note.on_sample - 1) && iterated_note.on_sample <= note.off_sample) {
  30. notes.remove(it);
  31. it = notes.begin();
  32. continue;
  33. }
  34. ++it;
  35. }
  36. notes.append(note);
  37. }
  38. }