Synthesizers.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2021-2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/SinglyLinkedList.h>
  8. #include <LibDSP/Music.h>
  9. #include <LibDSP/Processor.h>
  10. #include <LibDSP/ProcessorParameter.h>
  11. #include <LibDSP/Transport.h>
  12. namespace DSP::Synthesizers {
  13. enum Waveform : u8 {
  14. Sine,
  15. Triangle,
  16. Square,
  17. Saw,
  18. Noise,
  19. };
  20. struct PitchedEnvelope : Envelope {
  21. constexpr PitchedEnvelope() = default;
  22. constexpr PitchedEnvelope(double envelope, u8 note)
  23. : Envelope(envelope)
  24. , note(note)
  25. {
  26. }
  27. constexpr PitchedEnvelope(Envelope envelope, u8 note)
  28. : Envelope(envelope)
  29. , note(note)
  30. {
  31. }
  32. u8 note;
  33. };
  34. class Classic : public SynthesizerProcessor {
  35. public:
  36. Classic(NonnullRefPtr<Transport>);
  37. Waveform wave() const { return m_waveform.value(); }
  38. private:
  39. virtual void process_impl(Signal const&, Signal&) override;
  40. double volume_from_envelope(Envelope const&) const;
  41. double wave_position(u32 sample_time, u8 note);
  42. double samples_per_cycle(u8 note) const;
  43. double sin_position(u32 sample_time, u8 note) const;
  44. double triangle_position(u32 sample_time, u8 note) const;
  45. double square_position(u32 sample_time, u8 note) const;
  46. double saw_position(u32 sample_time, u8 note) const;
  47. double noise_position(u32 sample_time, u8 note);
  48. ProcessorEnumParameter<Waveform> m_waveform;
  49. ProcessorRangeParameter m_attack;
  50. ProcessorRangeParameter m_decay;
  51. ProcessorRangeParameter m_sustain;
  52. ProcessorRangeParameter m_release;
  53. RollNotes m_playing_notes;
  54. Array<double, note_frequencies.size()> last_random;
  55. };
  56. }