Track.cpp 3.1 KB

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