Track.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "AK/NonnullRefPtr.h"
  7. #include "AK/Userspace.h"
  8. #include <AK/FixedArray.h>
  9. #include <AK/NoAllocationGuard.h>
  10. #include <AK/Optional.h>
  11. #include <AK/StdLibExtras.h>
  12. #include <AK/TypedTransfer.h>
  13. #include <AK/Types.h>
  14. #include <LibDSP/Music.h>
  15. #include <LibDSP/Processor.h>
  16. #include <LibDSP/Track.h>
  17. namespace DSP {
  18. bool Track::add_processor(NonnullRefPtr<Processor> new_processor)
  19. {
  20. m_processor_chain.append(move(new_processor));
  21. if (!check_processor_chain_valid()) {
  22. (void)m_processor_chain.take_last();
  23. return false;
  24. }
  25. return true;
  26. }
  27. bool Track::check_processor_chain_valid_with_initial_type(SignalType initial_type) const
  28. {
  29. Processor const* previous_processor = nullptr;
  30. for (auto& processor : m_processor_chain) {
  31. // The first processor must have the given initial signal type as input.
  32. if (previous_processor == nullptr) {
  33. if (processor.input_type() != initial_type)
  34. return false;
  35. } else if (previous_processor->output_type() != processor.input_type())
  36. return false;
  37. previous_processor = &processor;
  38. }
  39. return true;
  40. }
  41. NonnullRefPtr<Synthesizers::Classic> Track::synth()
  42. {
  43. return static_ptr_cast<Synthesizers::Classic>(m_processor_chain.ptr_at(0));
  44. }
  45. NonnullRefPtr<Effects::Delay> Track::delay()
  46. {
  47. return static_ptr_cast<Effects::Delay>(m_processor_chain.ptr_at(1));
  48. }
  49. bool AudioTrack::check_processor_chain_valid() const
  50. {
  51. return check_processor_chain_valid_with_initial_type(SignalType::Sample);
  52. }
  53. bool NoteTrack::check_processor_chain_valid() const
  54. {
  55. return check_processor_chain_valid_with_initial_type(SignalType::Note);
  56. }
  57. ErrorOr<void> Track::resize_internal_buffers_to(size_t buffer_size)
  58. {
  59. m_secondary_sample_buffer = TRY(FixedArray<Sample>::try_create(buffer_size));
  60. return {};
  61. }
  62. void Track::current_signal(FixedArray<Sample>& output_signal)
  63. {
  64. // This is real-time code. We must NEVER EVER EVER allocate.
  65. NoAllocationGuard guard;
  66. VERIFY(m_secondary_sample_buffer.type() == SignalType::Sample);
  67. VERIFY(output_signal.size() == m_secondary_sample_buffer.get<FixedArray<Sample>>().size());
  68. compute_current_clips_signal();
  69. Signal* source_signal = &m_current_signal;
  70. // This provides an audio buffer of the right size. It is not allocated here, but whenever we are informed about a buffer size change.
  71. Signal* target_signal = &m_secondary_sample_buffer;
  72. for (auto& processor : m_processor_chain) {
  73. // Depending on what the processor needs to have as output, we need to place either a pre-allocated note hash map or a pre-allocated sample buffer in the target signal.
  74. if (processor.output_type() == SignalType::Note)
  75. target_signal = &m_secondary_note_buffer;
  76. else
  77. target_signal = &m_secondary_sample_buffer;
  78. processor.process(*source_signal, *target_signal);
  79. swap(source_signal, target_signal);
  80. }
  81. VERIFY(source_signal->type() == SignalType::Sample);
  82. VERIFY(output_signal.size() == source_signal->get<FixedArray<Sample>>().size());
  83. // This is one final unavoidable memcopy. Otherwise we need to special-case the last processor or
  84. AK::TypedTransfer<Sample>::copy(output_signal.data(), source_signal->get<FixedArray<Sample>>().data(), output_signal.size());
  85. }
  86. void NoteTrack::compute_current_clips_signal()
  87. {
  88. // FIXME: Handle looping properly
  89. u32 start_time = m_transport->time();
  90. VERIFY(m_secondary_sample_buffer.type() == SignalType::Sample);
  91. size_t sample_count = m_secondary_sample_buffer.get<FixedArray<Sample>>().size();
  92. u32 end_time = start_time + static_cast<u32>(sample_count);
  93. // Find the currently playing clips.
  94. // We can't handle more than 32 playing clips at a time, but that is a ridiculous number.
  95. Array<RefPtr<NoteClip>, 32> playing_clips;
  96. size_t playing_clips_index = 0;
  97. for (auto& clip : m_clips) {
  98. // A clip is playing if its start time or end time fall in the current time range.
  99. // Or, if they both enclose the current time range.
  100. if ((clip.start() <= start_time && clip.end() >= end_time)
  101. || (clip.start() >= start_time && clip.start() < end_time)
  102. || (clip.end() > start_time && clip.end() <= end_time)) {
  103. VERIFY(playing_clips_index < playing_clips.size());
  104. playing_clips[playing_clips_index++] = clip;
  105. }
  106. }
  107. auto& current_notes = m_current_signal.get<RollNotes>();
  108. m_current_signal.get<RollNotes>().fill({});
  109. if (playing_clips_index == 0)
  110. return;
  111. for (auto const& playing_clip : playing_clips) {
  112. if (playing_clip.is_null())
  113. break;
  114. for (auto const& note : playing_clip->notes()) {
  115. if (note.is_playing_during(start_time, end_time))
  116. current_notes[note.pitch] = note;
  117. }
  118. }
  119. for (auto const& keyboard_note : m_keyboard->notes()) {
  120. if (!keyboard_note.has_value() || !keyboard_note->is_playing_during(start_time, end_time))
  121. continue;
  122. // Always overwrite roll notes with keyboard notes.
  123. current_notes[keyboard_note->pitch] = keyboard_note;
  124. }
  125. }
  126. void AudioTrack::compute_current_clips_signal()
  127. {
  128. // This is quite involved as we need to look at multiple clips and take looping into account.
  129. TODO();
  130. }
  131. void NoteTrack::set_note(RollNote note)
  132. {
  133. for (auto& clip : m_clips) {
  134. if (clip.start() <= note.on_sample && clip.end() >= note.on_sample)
  135. clip.set_note(note);
  136. }
  137. }
  138. void NoteTrack::remove_note(RollNote note)
  139. {
  140. for (auto& clip : m_clips)
  141. clip.remove_note(note);
  142. }
  143. void NoteTrack::add_clip(u32 start_time, u32 end_time)
  144. {
  145. m_clips.append(AK::make_ref_counted<NoteClip>(start_time, end_time));
  146. }
  147. }