Track.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/FixedArray.h>
  7. #include <AK/NoAllocationGuard.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/Optional.h>
  10. #include <AK/StdLibExtras.h>
  11. #include <AK/TypedTransfer.h>
  12. #include <AK/Types.h>
  13. #include <LibDSP/Music.h>
  14. #include <LibDSP/Processor.h>
  15. #include <LibDSP/Track.h>
  16. #include <unistd.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.ptr();
  38. }
  39. return true;
  40. }
  41. NonnullRefPtr<Synthesizers::Classic> Track::synth()
  42. {
  43. return static_ptr_cast<Synthesizers::Classic>(m_processor_chain[0]);
  44. }
  45. NonnullRefPtr<Effects::Delay> Track::delay()
  46. {
  47. return static_ptr_cast<Effects::Delay>(m_processor_chain[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>::create(buffer_size));
  60. FixedArray<Sample> cache = TRY(FixedArray<Sample>::create(buffer_size));
  61. bool false_variable = false;
  62. while (!m_sample_lock.compare_exchange_strong(false_variable, true))
  63. usleep(1);
  64. m_cached_sample_buffer.swap(cache);
  65. m_sample_lock.store(false);
  66. return {};
  67. }
  68. void Track::current_signal(FixedArray<Sample>& output_signal)
  69. {
  70. // This is real-time code. We must NEVER EVER EVER allocate.
  71. NoAllocationGuard guard;
  72. VERIFY(m_secondary_sample_buffer.type() == SignalType::Sample);
  73. VERIFY(output_signal.size() == m_secondary_sample_buffer.get<FixedArray<Sample>>().size());
  74. compute_current_clips_signal();
  75. Signal* source_signal = &m_current_signal;
  76. // This provides an audio buffer of the right size. It is not allocated here, but whenever we are informed about a buffer size change.
  77. Signal* target_signal = &m_secondary_sample_buffer;
  78. for (auto& processor : m_processor_chain) {
  79. // 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.
  80. if (processor->output_type() == SignalType::Note)
  81. target_signal = &m_secondary_note_buffer;
  82. else
  83. target_signal = &m_secondary_sample_buffer;
  84. processor->process(*source_signal, *target_signal);
  85. swap(source_signal, target_signal);
  86. }
  87. VERIFY(source_signal->type() == SignalType::Sample);
  88. VERIFY(output_signal.size() == source_signal->get<FixedArray<Sample>>().size());
  89. // The last processor is the fixed mastering processor. This can write directly to the output data. We also just trust this processor that it does the right thing :^)
  90. m_track_mastering->process_to_fixed_array(*source_signal, output_signal);
  91. bool false_variable = false;
  92. if (m_sample_lock.compare_exchange_strong(false_variable, true)) {
  93. AK::TypedTransfer<Sample>::copy(m_cached_sample_buffer.data(), output_signal.data(), m_cached_sample_buffer.size());
  94. m_sample_lock.store(false);
  95. }
  96. }
  97. void Track::write_cached_signal_to(Span<Sample> output_signal)
  98. {
  99. bool false_variable = false;
  100. while (!m_sample_lock.compare_exchange_strong(false_variable, true)) {
  101. usleep(1);
  102. }
  103. VERIFY(output_signal.size() == m_cached_sample_buffer.size());
  104. AK::TypedTransfer<Sample>::copy(output_signal.data(), m_cached_sample_buffer.data(), m_cached_sample_buffer.size());
  105. m_sample_lock.store(false);
  106. }
  107. void NoteTrack::compute_current_clips_signal()
  108. {
  109. // FIXME: Handle looping properly
  110. u32 start_time = m_transport->time();
  111. VERIFY(m_secondary_sample_buffer.type() == SignalType::Sample);
  112. size_t sample_count = m_secondary_sample_buffer.get<FixedArray<Sample>>().size();
  113. u32 end_time = start_time + static_cast<u32>(sample_count);
  114. // Find the currently playing clips.
  115. // We can't handle more than 32 playing clips at a time, but that is a ridiculous number.
  116. Array<RefPtr<NoteClip>, 32> playing_clips;
  117. size_t playing_clips_index = 0;
  118. for (auto& clip : m_clips) {
  119. // A clip is playing if its start time or end time fall in the current time range.
  120. // Or, if they both enclose the current time range.
  121. if ((clip->start() <= start_time && clip->end() >= end_time)
  122. || (clip->start() >= start_time && clip->start() < end_time)
  123. || (clip->end() > start_time && clip->end() <= end_time)) {
  124. VERIFY(playing_clips_index < playing_clips.size());
  125. playing_clips[playing_clips_index++] = clip;
  126. }
  127. }
  128. auto& current_notes = m_current_signal.get<RollNotes>();
  129. m_current_signal.get<RollNotes>().fill({});
  130. if (playing_clips_index == 0)
  131. return;
  132. for (auto const& playing_clip : playing_clips) {
  133. if (playing_clip.is_null())
  134. break;
  135. for (auto const& note : playing_clip->notes()) {
  136. if (note.is_playing_during(start_time, end_time))
  137. current_notes[note.pitch] = note;
  138. }
  139. }
  140. for (auto const& keyboard_note : m_keyboard->notes()) {
  141. if (!keyboard_note.has_value() || !keyboard_note->is_playing_during(start_time, end_time))
  142. continue;
  143. // Always overwrite roll notes with keyboard notes.
  144. current_notes[keyboard_note->pitch] = keyboard_note;
  145. }
  146. }
  147. void AudioTrack::compute_current_clips_signal()
  148. {
  149. // This is quite involved as we need to look at multiple clips and take looping into account.
  150. TODO();
  151. }
  152. Optional<RollNote> NoteTrack::note_at(u32 time, u8 pitch) const
  153. {
  154. for (auto& clip : m_clips) {
  155. if (time >= clip->start() && time <= clip->end())
  156. return clip->note_at(time, pitch);
  157. }
  158. return {};
  159. }
  160. void NoteTrack::set_note(RollNote note)
  161. {
  162. for (auto& clip : m_clips) {
  163. if (clip->start() <= note.on_sample && clip->end() >= note.on_sample)
  164. clip->set_note(note);
  165. }
  166. }
  167. void NoteTrack::remove_note(RollNote note)
  168. {
  169. for (auto& clip : m_clips)
  170. clip->remove_note(note);
  171. }
  172. void NoteTrack::add_clip(u32 start_time, u32 end_time)
  173. {
  174. m_clips.append(AK::make_ref_counted<NoteClip>(start_time, end_time));
  175. }
  176. }