Synthesizers.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2021-2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashMap.h>
  7. #include <AK/Math.h>
  8. #include <AK/Random.h>
  9. #include <AK/RefPtr.h>
  10. #include <AK/StdLibExtras.h>
  11. #include <LibAudio/Sample.h>
  12. #include <LibDSP/Envelope.h>
  13. #include <LibDSP/Music.h>
  14. #include <LibDSP/Processor.h>
  15. #include <LibDSP/Synthesizers.h>
  16. namespace DSP::Synthesizers {
  17. Classic::Classic(NonnullRefPtr<Transport> transport)
  18. : DSP::SynthesizerProcessor(move(transport))
  19. , m_waveform("Waveform"_string, Waveform::Saw)
  20. , m_attack("Attack"_string, 0.01, 2000, 5, Logarithmic::Yes)
  21. , m_decay("Decay"_string, 0.01, 20'000, 80, Logarithmic::Yes)
  22. , m_sustain("Sustain"_string, 0.001, 1, 0.725, Logarithmic::No)
  23. , m_release("Release"_string, 0.01, 6'000, 120, Logarithmic::Yes)
  24. {
  25. m_parameters.append(m_waveform);
  26. m_parameters.append(m_attack);
  27. m_parameters.append(m_decay);
  28. m_parameters.append(m_sustain);
  29. m_parameters.append(m_release);
  30. }
  31. void Classic::process_impl(Signal const& input_signal, [[maybe_unused]] Signal& output_signal)
  32. {
  33. auto const& in = input_signal.get<RollNotes>();
  34. auto& output_samples = output_signal.get<FixedArray<Sample>>();
  35. // Do this for every time step and set the signal accordingly.
  36. for (size_t sample_index = 0; sample_index < output_samples.size(); ++sample_index) {
  37. Sample& out = output_samples[sample_index];
  38. out = {};
  39. u32 sample_time = m_transport->time() + sample_index;
  40. Array<Optional<PitchedEnvelope>, note_frequencies.size()> playing_envelopes;
  41. // "Press" the necessary notes in the internal representation,
  42. // and "release" all of the others
  43. for (u8 i = 0; i < note_frequencies.size(); ++i) {
  44. if (auto maybe_note = in[i]; maybe_note.has_value())
  45. m_playing_notes[i] = maybe_note;
  46. if (m_playing_notes[i].has_value()) {
  47. Envelope note_envelope = m_playing_notes[i]->to_envelope(sample_time, m_attack * m_transport->ms_sample_rate(), m_decay * m_transport->ms_sample_rate(), m_release * m_transport->ms_sample_rate());
  48. // There are two conditions for removing notes:
  49. // 1. The envelope has expired, regardless of whether the note was still given to us in the input.
  50. if (!note_envelope.is_active()) {
  51. m_playing_notes[i] = {};
  52. continue;
  53. }
  54. // 2. The envelope has not expired, but the note was not given to us.
  55. // This means that the note abruptly stopped playing; i.e. the audio infrastructure didn't know the length of the notes initially.
  56. // That basically means we're dealing with a keyboard note. Chop its end time to end now.
  57. if (!note_envelope.is_release() && !in[i].has_value()) {
  58. // dbgln("note {} not released, setting release phase, envelope={}", i, note_envelope.envelope);
  59. note_envelope.set_release(0);
  60. auto real_note = *m_playing_notes[i];
  61. real_note.off_sample = sample_time;
  62. m_playing_notes[i] = real_note;
  63. }
  64. playing_envelopes[i] = PitchedEnvelope { note_envelope, i };
  65. }
  66. }
  67. for (auto envelope : playing_envelopes) {
  68. if (!envelope.has_value())
  69. continue;
  70. double volume = volume_from_envelope(*envelope);
  71. double wave = wave_position(sample_time, envelope->note);
  72. out += volume * wave;
  73. }
  74. }
  75. }
  76. // Linear ADSR envelope with no peak adjustment.
  77. double Classic::volume_from_envelope(Envelope const& envelope) const
  78. {
  79. switch (static_cast<EnvelopeState>(envelope)) {
  80. case EnvelopeState::Off:
  81. return 0;
  82. case EnvelopeState::Attack:
  83. return envelope.attack();
  84. case EnvelopeState::Decay:
  85. // As we fade from high (1) to low (headroom above the sustain level) here, use 1-decay as the interpolation.
  86. return (1. - envelope.decay()) * (1. - m_sustain) + m_sustain;
  87. case EnvelopeState::Sustain:
  88. return m_sustain;
  89. case EnvelopeState::Release:
  90. // Same goes for the release fade from high to low.
  91. return (1. - envelope.release()) * m_sustain;
  92. }
  93. VERIFY_NOT_REACHED();
  94. }
  95. double Classic::wave_position(u32 sample_time, u8 note)
  96. {
  97. switch (m_waveform) {
  98. case Sine:
  99. return sin_position(sample_time, note);
  100. case Triangle:
  101. return triangle_position(sample_time, note);
  102. case Square:
  103. return square_position(sample_time, note);
  104. case Saw:
  105. return saw_position(sample_time, note);
  106. case Noise:
  107. return noise_position(sample_time, note);
  108. }
  109. VERIFY_NOT_REACHED();
  110. }
  111. double Classic::samples_per_cycle(u8 note) const
  112. {
  113. return m_transport->sample_rate() / note_frequencies[note];
  114. }
  115. double Classic::sin_position(u32 sample_time, u8 note) const
  116. {
  117. double spc = samples_per_cycle(note);
  118. double cycle_pos = sample_time / spc;
  119. return AK::sin(cycle_pos * 2 * AK::Pi<double>);
  120. }
  121. // Absolute value of the saw wave "flips" the negative portion into the positive, creating a ramp up and down.
  122. double Classic::triangle_position(u32 sample_time, u8 note) const
  123. {
  124. double saw = saw_position(sample_time, note);
  125. return AK::fabs(saw) * 2 - 1;
  126. }
  127. // The first half of the cycle period is 1, the other half -1.
  128. double Classic::square_position(u32 sample_time, u8 note) const
  129. {
  130. double spc = samples_per_cycle(note);
  131. double progress = AK::fmod(static_cast<double>(sample_time), spc) / spc;
  132. return progress >= 0.5 ? -1 : 1;
  133. }
  134. // Modulus creates inverse saw, which we need to flip and scale.
  135. double Classic::saw_position(u32 sample_time, u8 note) const
  136. {
  137. double spc = samples_per_cycle(note);
  138. double unscaled = spc - AK::fmod(static_cast<double>(sample_time), spc);
  139. return unscaled / (samples_per_cycle(note) / 2.) - 1;
  140. }
  141. // We resample the noise twenty times per cycle.
  142. double Classic::noise_position(u32 sample_time, u8 note)
  143. {
  144. double spc = samples_per_cycle(note);
  145. u32 getrandom_interval = max(static_cast<u32>(spc / 2), 1);
  146. // Note that this code only works well if the processor is called for every increment of time.
  147. if (sample_time % getrandom_interval == 0)
  148. last_random[note] = (get_random<u16>() / static_cast<double>(NumericLimits<u16>::max()) - .5) * 2;
  149. return last_random[note];
  150. }
  151. }