Serenity Operating System
1/*
2 * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include "Clip.h"
8
9namespace DSP {
10
11Sample AudioClip::sample_at(u32 time)
12{
13 VERIFY(time < m_length);
14 return m_samples[time];
15}
16
17Optional<RollNote> NoteClip::note_at(u32 time, u8 pitch) const
18{
19 for (auto& note : m_notes) {
20 if (time >= note.on_sample && time <= note.off_sample && pitch == note.pitch)
21 return note;
22 }
23 return {};
24}
25
26void NoteClip::set_note(RollNote note)
27{
28 m_notes.remove_all_matching([&](auto const& other) {
29 return other.pitch == note.pitch && other.overlaps_with(note);
30 });
31 m_notes.append(note);
32}
33
34void NoteClip::remove_note(RollNote note)
35{
36 // FIXME: See header; this could be much faster with a better datastructure.
37 m_notes.remove_first_matching([note](auto const& element) {
38 return element.on_sample == note.on_sample && element.off_sample == note.off_sample && element.pitch == note.pitch;
39 });
40}
41
42}