SoundSynthesis.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Audio/BufferedSoundStream.h>
  4. #include <Urho3D/Audio/SoundSource.h>
  5. #include <Urho3D/Core/CoreEvents.h>
  6. #include <Urho3D/Engine/Engine.h>
  7. #include <Urho3D/Input/Input.h>
  8. #include <Urho3D/IO/Log.h>
  9. #include <Urho3D/Scene/Node.h>
  10. #include <Urho3D/UI/Font.h>
  11. #include <Urho3D/UI/Text.h>
  12. #include <Urho3D/UI/UI.h>
  13. #include "SoundSynthesis.h"
  14. #include <Urho3D/DebugNew.h>
  15. // Expands to this example's entry-point
  16. URHO3D_DEFINE_APPLICATION_MAIN(SoundSynthesis)
  17. SoundSynthesis::SoundSynthesis(Context* context) :
  18. Sample(context),
  19. filter_(0.5f),
  20. accumulator_(0.0f),
  21. osc1_(0.0f),
  22. osc2_(180.0f)
  23. {
  24. }
  25. void SoundSynthesis::Setup()
  26. {
  27. // Modify engine startup parameters
  28. Sample::Setup();
  29. engineParameters_[EP_SOUND] = true;
  30. }
  31. void SoundSynthesis::Start()
  32. {
  33. // Execute base class startup
  34. Sample::Start();
  35. // Create the sound stream & start playback
  36. CreateSound();
  37. // Create the UI content
  38. CreateInstructions();
  39. // Hook up to the frame update events
  40. SubscribeToEvents();
  41. // Set the mouse mode to use in the sample
  42. Sample::InitMouseMode(MM_FREE);
  43. }
  44. void SoundSynthesis::CreateSound()
  45. {
  46. // Sound source needs a node so that it is considered enabled
  47. node_ = new Node(context_);
  48. auto* source = node_->CreateComponent<SoundSource>();
  49. soundStream_ = new BufferedSoundStream();
  50. // Set format: 44100 Hz, sixteen bit, mono
  51. soundStream_->SetFormat(44100, true, false);
  52. // Start playback. We don't have data in the stream yet, but the SoundSource will wait until there is data,
  53. // as the stream is by default in the "don't stop at end" mode
  54. source->Play(soundStream_);
  55. }
  56. void SoundSynthesis::UpdateSound()
  57. {
  58. // Try to keep 1/10 seconds of sound in the buffer, to avoid both dropouts and unnecessary latency
  59. float targetLength = 1.0f / 10.0f;
  60. float requiredLength = targetLength - soundStream_->GetBufferLength();
  61. if (requiredLength < 0.0f)
  62. return;
  63. auto numSamples = (unsigned)(soundStream_->GetFrequency() * requiredLength);
  64. if (!numSamples)
  65. return;
  66. // Allocate a new buffer and fill it with a simple two-oscillator algorithm. The sound is over-amplified
  67. // (distorted), clamped to the 16-bit range, and finally lowpass-filtered according to the coefficient
  68. SharedArrayPtr<signed short> newData(new signed short[numSamples]);
  69. for (unsigned i = 0; i < numSamples; ++i)
  70. {
  71. osc1_ = fmodf(osc1_ + 1.0f, 360.0f);
  72. osc2_ = fmodf(osc2_ + 1.002f, 360.0f);
  73. float newValue = Clamp((Sin(osc1_) + Sin(osc2_)) * 100000.0f, -32767.0f, 32767.0f);
  74. accumulator_ = Lerp(accumulator_, newValue, filter_);
  75. newData[i] = (int)accumulator_;
  76. }
  77. // Queue buffer to the stream for playback
  78. soundStream_->AddData(newData, numSamples * sizeof(signed short));
  79. }
  80. void SoundSynthesis::CreateInstructions()
  81. {
  82. auto* cache = GetSubsystem<ResourceCache>();
  83. auto* ui = GetSubsystem<UI>();
  84. // Construct new Text object, set string to display and font to use
  85. instructionText_ = ui->GetRoot()->CreateChild<Text>();
  86. instructionText_->SetText("Use cursor up and down to control sound filtering");
  87. instructionText_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  88. // Position the text relative to the screen center
  89. instructionText_->SetTextAlignment(HA_CENTER);
  90. instructionText_->SetHorizontalAlignment(HA_CENTER);
  91. instructionText_->SetVerticalAlignment(VA_CENTER);
  92. instructionText_->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  93. }
  94. void SoundSynthesis::SubscribeToEvents()
  95. {
  96. // Subscribe HandleUpdate() function for processing update events
  97. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(SoundSynthesis, HandleUpdate));
  98. }
  99. void SoundSynthesis::HandleUpdate(StringHash eventType, VariantMap& eventData)
  100. {
  101. using namespace Update;
  102. // Take the frame time step, which is stored as a float
  103. float timeStep = eventData[P_TIMESTEP].GetFloat();
  104. // Use keys to control the filter constant
  105. auto* input = GetSubsystem<Input>();
  106. if (input->GetKeyDown(KEY_UP))
  107. filter_ += timeStep * 0.5f;
  108. if (input->GetKeyDown(KEY_DOWN))
  109. filter_ -= timeStep * 0.5f;
  110. filter_ = Clamp(filter_, 0.01f, 1.0f);
  111. instructionText_->SetText("Use cursor up and down to control sound filtering\n"
  112. "Coefficient: " + String(filter_));
  113. UpdateSound();
  114. }