SoundSynthesis.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "Sample.h"
  5. namespace Urho3D
  6. {
  7. class Node;
  8. class BufferedSoundStream;
  9. }
  10. /// Sound synthesis example.
  11. /// This sample demonstrates:
  12. /// - Playing back a sound stream produced on-the-fly by a simple CPU synthesis algorithm
  13. class SoundSynthesis : public Sample
  14. {
  15. URHO3D_OBJECT(SoundSynthesis, Sample);
  16. public:
  17. /// Construct.
  18. explicit SoundSynthesis(Context* context);
  19. /// Setup before engine initialization. Modifies the engine parameters.
  20. void Setup() override;
  21. /// Setup after engine initialization and before running the main loop.
  22. void Start() override;
  23. protected:
  24. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  25. String GetScreenJoystickPatchString() const override { return
  26. "<patch>"
  27. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button2']]\">"
  28. " <attribute name=\"Is Visible\" value=\"false\" />"
  29. " </add>"
  30. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">"
  31. " <attribute name=\"Is Visible\" value=\"false\" />"
  32. " </add>"
  33. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />"
  34. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Up</replace>"
  35. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">"
  36. " <element type=\"Text\">"
  37. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  38. " <attribute name=\"Text\" value=\"UP\" />"
  39. " </element>"
  40. " </add>"
  41. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />"
  42. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Down</replace>"
  43. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">"
  44. " <element type=\"Text\">"
  45. " <attribute name=\"Name\" value=\"KeyBinding\" />"
  46. " <attribute name=\"Text\" value=\"DOWN\" />"
  47. " </element>"
  48. " </add>"
  49. "</patch>";
  50. }
  51. private:
  52. /// Construct the sound stream and start playback.
  53. void CreateSound();
  54. /// Buffer more sound data.
  55. void UpdateSound();
  56. /// Construct an instruction text to the UI.
  57. void CreateInstructions();
  58. /// Subscribe to application-wide logic update events.
  59. void SubscribeToEvents();
  60. /// Handle the logic update event.
  61. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  62. /// Scene node for the sound component.
  63. SharedPtr<Node> node_;
  64. /// Sound stream that we update.
  65. SharedPtr<BufferedSoundStream> soundStream_;
  66. /// Instruction text.
  67. SharedPtr<Text> instructionText_;
  68. /// Filter coefficient for the sound.
  69. float filter_;
  70. /// Synthesis accumulator.
  71. float accumulator_;
  72. /// First oscillator.
  73. float osc1_;
  74. /// Second oscillator.
  75. float osc2_;
  76. };