Track.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Optional.h>
  7. #include <AK/Types.h>
  8. #include <LibDSP/Processor.h>
  9. #include <LibDSP/Track.h>
  10. using namespace std;
  11. namespace LibDSP {
  12. bool Track::add_processor(NonnullRefPtr<Processor> new_processor)
  13. {
  14. m_processor_chain.append(move(new_processor));
  15. if (!check_processor_chain_valid()) {
  16. (void)m_processor_chain.take_last();
  17. return false;
  18. }
  19. return true;
  20. }
  21. bool Track::check_processor_chain_valid_with_initial_type(SignalType initial_type) const
  22. {
  23. Processor const* previous_processor = nullptr;
  24. for (auto& processor : m_processor_chain) {
  25. // The first processor must have the given initial signal type as input.
  26. if (previous_processor == nullptr) {
  27. if (processor.input_type() != initial_type)
  28. return false;
  29. } else if (previous_processor->output_type() != processor.input_type())
  30. return false;
  31. previous_processor = &processor;
  32. }
  33. return true;
  34. }
  35. bool AudioTrack::check_processor_chain_valid() const
  36. {
  37. return check_processor_chain_valid_with_initial_type(SignalType::Sample);
  38. }
  39. bool NoteTrack::check_processor_chain_valid() const
  40. {
  41. return check_processor_chain_valid_with_initial_type(SignalType::Note);
  42. }
  43. Sample Track::current_signal()
  44. {
  45. compute_current_clips_signal();
  46. Optional<Signal> the_signal;
  47. for (auto& processor : m_processor_chain) {
  48. the_signal = processor.process(the_signal.value_or(m_current_signal));
  49. }
  50. VERIFY(the_signal.has_value() && the_signal->type() == SignalType::Sample);
  51. return the_signal->get<Sample>();
  52. }
  53. void NoteTrack::compute_current_clips_signal()
  54. {
  55. u32 time = m_transport->time();
  56. // Find the currently playing clip.
  57. NoteClip* playing_clip = nullptr;
  58. for (auto& clip : m_clips) {
  59. if (clip.start() <= time && clip.end() >= time) {
  60. playing_clip = &clip;
  61. break;
  62. }
  63. }
  64. auto& current_notes = m_current_signal.get<RollNotes>();
  65. m_current_signal.get<RollNotes>().clear_with_capacity();
  66. if (playing_clip == nullptr)
  67. return;
  68. // FIXME: performance?
  69. for (auto const& note_list : playing_clip->notes()) {
  70. for (auto const& note : note_list) {
  71. if (note.on_sample >= time && note.off_sample >= time)
  72. break;
  73. if (note.on_sample <= time && note.off_sample >= time)
  74. current_notes.set(note.pitch, note);
  75. }
  76. }
  77. }
  78. void AudioTrack::compute_current_clips_signal()
  79. {
  80. // Find the currently playing clip.
  81. u32 time = m_transport->time();
  82. AudioClip* playing_clip = nullptr;
  83. for (auto& clip : m_clips) {
  84. if (clip.start() <= time && clip.end() >= time) {
  85. playing_clip = &clip;
  86. break;
  87. }
  88. }
  89. if (playing_clip == nullptr) {
  90. m_current_signal = Signal(static_cast<Sample const&>(SAMPLE_OFF));
  91. }
  92. // Index into the clip's samples.
  93. u32 effective_sample = time - playing_clip->start();
  94. m_current_signal = Signal(playing_clip->sample_at(effective_sample));
  95. }
  96. }