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.

149 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. g.fillAll (Colours::black);
  55. }
  56. void resized() override
  57. {
  58. int xPos = 20;
  59. int yPos = 20;
  60. int yDistance = 50;
  61. for (auto stringLine : stringLines)
  62. {
  63. stringLine->setTopLeftPosition (xPos, yPos);
  64. yPos += yDistance;
  65. addAndMakeVisible (stringLine);
  66. }
  67. }
  68. private:
  69. void mouseDown (const MouseEvent& e) override
  70. {
  71. mouseDrag (e);
  72. }
  73. void mouseDrag (const MouseEvent& e) override
  74. {
  75. for (int i = 0; i < stringLines.size(); ++i)
  76. {
  77. auto* stringLine = stringLines.getUnchecked(i);
  78. if (stringLine->getBounds().contains (e.getPosition()))
  79. {
  80. float position = (e.position.x - stringLine->getX()) / stringLine->getWidth();
  81. stringLine->stringPlucked (position);
  82. stringSynths.getUnchecked(i)->stringPlucked (position);
  83. }
  84. }
  85. }
  86. //=======================================================================
  87. struct StringParameters
  88. {
  89. StringParameters (int midiNote)
  90. : frequencyInHz (MidiMessage::getMidiNoteInHertz (midiNote)),
  91. lengthInPixels ((int) (760 / (frequencyInHz / MidiMessage::getMidiNoteInHertz (42))))
  92. {
  93. }
  94. double frequencyInHz;
  95. int lengthInPixels;
  96. };
  97. static std::vector<StringParameters> getDefaultStringParameters()
  98. {
  99. return { 42, 44, 46, 49, 51, 54, 56, 58, 61, 63, 66, 68, 70 };
  100. }
  101. void createStringComponents()
  102. {
  103. for (auto stringParams : getDefaultStringParameters())
  104. {
  105. stringLines.add (new StringComponent (stringParams.lengthInPixels,
  106. Colour::fromHSV (Random().nextFloat(), 0.6f, 0.9f, 1.0f)));
  107. }
  108. }
  109. void generateStringSynths (double sampleRate)
  110. {
  111. stringSynths.clear();
  112. for (auto stringParams : getDefaultStringParameters())
  113. {
  114. stringSynths.add (new StringSynthesiser (sampleRate, stringParams.frequencyInHz));
  115. }
  116. }
  117. //=======================================================================
  118. OwnedArray<StringComponent> stringLines;
  119. OwnedArray<StringSynthesiser> stringSynths;
  120. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StringDemoComponent)
  121. };