Synthesizers.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2021, 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 <LibDSP/Envelope.h>
  10. #include <LibDSP/Processor.h>
  11. #include <LibDSP/Synthesizers.h>
  12. #include <math.h>
  13. namespace LibDSP::Synthesizers {
  14. Classic::Classic(NonnullRefPtr<Transport> transport)
  15. : LibDSP::SynthesizerProcessor(transport)
  16. , m_waveform("Waveform"sv, Waveform::Saw)
  17. , m_attack("Attack"sv, 0.01, 2000, 5, Logarithmic::Yes)
  18. , m_decay("Decay"sv, 0.01, 20'000, 80, Logarithmic::Yes)
  19. , m_sustain("Sustain"sv, 0.001, 1, 0.725, Logarithmic::No)
  20. , m_release("Release", 0.01, 6'000, 120, Logarithmic::Yes)
  21. {
  22. m_parameters.append(m_waveform);
  23. m_parameters.append(m_attack);
  24. m_parameters.append(m_decay);
  25. m_parameters.append(m_sustain);
  26. m_parameters.append(m_release);
  27. }
  28. Signal Classic::process_impl(Signal const& input_signal)
  29. {
  30. auto& in = input_signal.get<RollNotes>();
  31. Sample out;
  32. SinglyLinkedList<PitchedEnvelope> playing_envelopes;
  33. // "Press" the necessary notes in the internal representation,
  34. // and "release" all of the others
  35. for (u8 i = 0; i < note_count; ++i) {
  36. if (auto maybe_note = in.get(i); maybe_note.has_value())
  37. m_playing_notes.set(i, maybe_note.value());
  38. if (m_playing_notes.contains(i)) {
  39. Envelope note_envelope = m_playing_notes.get(i)->to_envelope(m_transport->time(), m_attack * m_transport->ms_sample_rate(), m_decay * m_transport->ms_sample_rate(), m_release * m_transport->ms_sample_rate());
  40. if (!note_envelope.is_active()) {
  41. m_playing_notes.remove(i);
  42. continue;
  43. }
  44. playing_envelopes.append(PitchedEnvelope { note_envelope, i });
  45. }
  46. }
  47. for (auto envelope : playing_envelopes) {
  48. double volume = volume_from_envelope(envelope);
  49. double wave = wave_position(envelope.note);
  50. out += volume * wave;
  51. }
  52. return out;
  53. }
  54. // Linear ADSR envelope with no peak adjustment.
  55. double Classic::volume_from_envelope(Envelope const& envelope)
  56. {
  57. switch (static_cast<EnvelopeState>(envelope)) {
  58. case EnvelopeState::Off:
  59. return 0;
  60. case EnvelopeState::Attack:
  61. return envelope.attack();
  62. case EnvelopeState::Decay:
  63. // As we fade from high (1) to low (headroom above the sustain level) here, use 1-decay as the interpolation.
  64. return (1. - envelope.decay()) * (1. - m_sustain) + m_sustain;
  65. case EnvelopeState::Sustain:
  66. return m_sustain;
  67. case EnvelopeState::Release:
  68. // Same goes for the release fade from high to low.
  69. return (1. - envelope.release()) * m_sustain;
  70. }
  71. VERIFY_NOT_REACHED();
  72. }
  73. double Classic::wave_position(u8 note)
  74. {
  75. switch (m_waveform) {
  76. case Sine:
  77. return sin_position(note);
  78. case Triangle:
  79. return triangle_position(note);
  80. case Square:
  81. return square_position(note);
  82. case Saw:
  83. return saw_position(note);
  84. case Noise:
  85. return noise_position(note);
  86. }
  87. VERIFY_NOT_REACHED();
  88. }
  89. double Classic::samples_per_cycle(u8 note)
  90. {
  91. return m_transport->sample_rate() / note_frequencies[note];
  92. }
  93. double Classic::sin_position(u8 note)
  94. {
  95. double spc = samples_per_cycle(note);
  96. double cycle_pos = m_transport->time() / spc;
  97. return AK::sin(cycle_pos * 2 * AK::Pi<double>);
  98. }
  99. // Absolute value of the saw wave "flips" the negative portion into the positive, creating a ramp up and down.
  100. double Classic::triangle_position(u8 note)
  101. {
  102. double saw = saw_position(note);
  103. return AK::fabs(saw) * 2 - 1;
  104. }
  105. // The first half of the cycle period is 1, the other half -1.
  106. double Classic::square_position(u8 note)
  107. {
  108. double spc = samples_per_cycle(note);
  109. double progress = AK::fmod(static_cast<double>(m_transport->time()), spc) / spc;
  110. return progress >= 0.5 ? -1 : 1;
  111. }
  112. // Modulus creates inverse saw, which we need to flip and scale.
  113. double Classic::saw_position(u8 note)
  114. {
  115. double spc = samples_per_cycle(note);
  116. double unscaled = spc - AK::fmod(static_cast<double>(m_transport->time()), spc);
  117. return unscaled / (samples_per_cycle(note) / 2.) - 1;
  118. }
  119. // We resample the noise twenty times per cycle.
  120. double Classic::noise_position(u8 note)
  121. {
  122. double spc = samples_per_cycle(note);
  123. u32 getrandom_interval = max(static_cast<u32>(spc / 2), 1);
  124. // Note that this code only works well if the processor is called for every increment of time.
  125. if (m_transport->time() % getrandom_interval == 0)
  126. last_random[note] = (get_random<u16>() / static_cast<double>(NumericLimits<u16>::max()) - .5) * 2;
  127. return last_random[note];
  128. }
  129. }