Track.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/Optional.h>
  9. #include <AK/StdLibExtras.h>
  10. #include <AK/TypedTransfer.h>
  11. #include <AK/Types.h>
  12. #include <LibDSP/Music.h>
  13. #include <LibDSP/Processor.h>
  14. #include <LibDSP/Track.h>
  15. namespace DSP {
  16. bool Track::add_processor(NonnullRefPtr<Processor> new_processor)
  17. {
  18. m_processor_chain.append(move(new_processor));
  19. if (!check_processor_chain_valid()) {
  20. (void)m_processor_chain.take_last();
  21. return false;
  22. }
  23. return true;
  24. }
  25. bool Track::check_processor_chain_valid_with_initial_type(SignalType initial_type) const
  26. {
  27. Processor const* previous_processor = nullptr;
  28. for (auto& processor : m_processor_chain) {
  29. // The first processor must have the given initial signal type as input.
  30. if (previous_processor == nullptr) {
  31. if (processor.input_type() != initial_type)
  32. return false;
  33. } else if (previous_processor->output_type() != processor.input_type())
  34. return false;
  35. previous_processor = &processor;
  36. }
  37. return true;
  38. }
  39. bool AudioTrack::check_processor_chain_valid() const
  40. {
  41. return check_processor_chain_valid_with_initial_type(SignalType::Sample);
  42. }
  43. bool NoteTrack::check_processor_chain_valid() const
  44. {
  45. return check_processor_chain_valid_with_initial_type(SignalType::Note);
  46. }
  47. ErrorOr<void> Track::resize_internal_buffers_to(size_t buffer_size)
  48. {
  49. m_secondary_sample_buffer = TRY(FixedArray<Sample>::try_create(buffer_size));
  50. return {};
  51. }
  52. void Track::current_signal(FixedArray<Sample>& output_signal)
  53. {
  54. // This is real-time code. We must NEVER EVER EVER allocate.
  55. NoAllocationGuard guard;
  56. VERIFY(output_signal.size() == m_secondary_sample_buffer.get<FixedArray<Sample>>().size());
  57. compute_current_clips_signal();
  58. Signal* source_signal = &m_current_signal;
  59. // This provides an audio buffer of the right size. It is not allocated here, but whenever we are informed about a buffer size change.
  60. Signal* target_signal = &m_secondary_sample_buffer;
  61. for (auto& processor : m_processor_chain) {
  62. // 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.
  63. if (processor.output_type() == SignalType::Note)
  64. target_signal = &m_secondary_note_buffer;
  65. else
  66. target_signal = &m_secondary_sample_buffer;
  67. processor.process(*source_signal, *target_signal);
  68. swap(source_signal, target_signal);
  69. }
  70. VERIFY(source_signal->type() == SignalType::Sample);
  71. VERIFY(output_signal.size() == source_signal->get<FixedArray<Sample>>().size());
  72. // This is one final unavoidable memcopy. Otherwise we need to special-case the last processor or
  73. AK::TypedTransfer<Sample>::copy(output_signal.data(), source_signal->get<FixedArray<Sample>>().data(), output_signal.size());
  74. }
  75. void NoteTrack::compute_current_clips_signal()
  76. {
  77. // Consider the entire time duration.
  78. TODO();
  79. u32 time = m_transport->time();
  80. // Find the currently playing clip.
  81. NoteClip* playing_clip = nullptr;
  82. for (auto& clip : m_clips) {
  83. if (clip.start() <= time && clip.end() >= time) {
  84. playing_clip = &clip;
  85. break;
  86. }
  87. }
  88. auto& current_notes = m_current_signal.get<RollNotes>();
  89. m_current_signal.get<RollNotes>().clear_with_capacity();
  90. if (playing_clip == nullptr)
  91. return;
  92. // FIXME: performance?
  93. for (auto const& note_list : playing_clip->notes()) {
  94. for (auto const& note : note_list) {
  95. if (note.on_sample >= time && note.off_sample >= time)
  96. break;
  97. if (note.on_sample <= time && note.off_sample >= time)
  98. current_notes.set(note.pitch, note);
  99. }
  100. }
  101. }
  102. void AudioTrack::compute_current_clips_signal()
  103. {
  104. // This is quite involved as we need to look at multiple clips and take looping into account.
  105. TODO();
  106. }
  107. }