The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. JUCE demo code - use at your own risk!
  4. ==============================================================================
  5. */
  6. #include "StringSynthesiser.h"
  7. #include "StringComponent.h"
  8. //==============================================================================
  9. class StringDemoComponent : public AudioAppComponent
  10. {
  11. public:
  12. StringDemoComponent()
  13. {
  14. createStringComponents();
  15. setSize (800, 560);
  16. // specify the number of input and output channels that we want to open
  17. setAudioChannels (0, 2);
  18. }
  19. ~StringDemoComponent()
  20. {
  21. shutdownAudio();
  22. }
  23. //==============================================================================
  24. void prepareToPlay (int /*samplesPerBlockExpected*/, double sampleRate) override
  25. {
  26. generateStringSynths (sampleRate);
  27. }
  28. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
  29. {
  30. bufferToFill.clearActiveBufferRegion();
  31. for (int channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel)
  32. {
  33. float* const channelData = bufferToFill.buffer->getWritePointer (channel, bufferToFill.startSample);
  34. if (channel == 0)
  35. {
  36. for (auto synth : stringSynths)
  37. synth->generateAndAddData (channelData, bufferToFill.numSamples);
  38. }
  39. else
  40. {
  41. memcpy (channelData,
  42. bufferToFill.buffer->getReadPointer (0),
  43. bufferToFill.numSamples * sizeof (float));
  44. }
  45. }
  46. }
  47. void releaseResources() override
  48. {
  49. stringSynths.clear();
  50. }
  51. //==============================================================================
  52. void paint (Graphics& g) override
  53. {
  54. }
  55. void resized() override
  56. {
  57. int xPos = 20;
  58. int yPos = 20;
  59. int yDistance = 50;
  60. for (auto stringLine : stringLines)
  61. {
  62. stringLine->setTopLeftPosition (xPos, yPos);
  63. yPos += yDistance;
  64. addAndMakeVisible (stringLine);
  65. }
  66. }
  67. private:
  68. void mouseDown (const MouseEvent& e) override
  69. {
  70. mouseDrag (e);
  71. }
  72. void mouseDrag (const MouseEvent& e) override
  73. {
  74. for (int i = 0; i < stringLines.size(); ++i)
  75. {
  76. auto* stringLine = stringLines.getUnchecked(i);
  77. if (stringLine->getBounds().contains (e.getPosition()))
  78. {
  79. float position = (e.position.x - stringLine->getX()) / stringLine->getWidth();
  80. stringLine->stringPlucked (position);
  81. stringSynths.getUnchecked(i)->stringPlucked (position);
  82. }
  83. }
  84. }
  85. //==============================================================================
  86. struct StringParameters
  87. {
  88. StringParameters (int midiNote)
  89. : frequencyInHz (MidiMessage::getMidiNoteInHertz (midiNote)),
  90. lengthInPixels ((int) (760 / (frequencyInHz / MidiMessage::getMidiNoteInHertz (42))))
  91. {
  92. }
  93. double frequencyInHz;
  94. int lengthInPixels;
  95. };
  96. static std::vector<StringParameters> getDefaultStringParameters()
  97. {
  98. return { 42, 44, 46, 49, 51, 54, 56, 58, 61, 63, 66, 68, 70 };
  99. }
  100. void createStringComponents()
  101. {
  102. for (auto stringParams : getDefaultStringParameters())
  103. {
  104. stringLines.add (new StringComponent (stringParams.lengthInPixels,
  105. Colour::fromHSV (Random().nextFloat(), 0.6f, 0.9f, 1.0f)));
  106. }
  107. }
  108. void generateStringSynths (double sampleRate)
  109. {
  110. stringSynths.clear();
  111. for (auto stringParams : getDefaultStringParameters())
  112. {
  113. stringSynths.add (new StringSynthesiser (sampleRate, stringParams.frequencyInHz));
  114. }
  115. }
  116. //==============================================================================
  117. OwnedArray<StringComponent> stringLines;
  118. OwnedArray<StringSynthesiser> stringSynths;
  119. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StringDemoComponent)
  120. };