1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-28 02:30:05 +00:00

Code cleanup & WaveshapeProgram documentation

This commit is contained in:
ed 2016-11-14 10:06:48 +00:00
parent e5d677072b
commit b2d0328f73
2 changed files with 30 additions and 44 deletions

View file

@ -57,10 +57,10 @@ struct SynthGrid
Array<DrumPadGridProgram::GridFill> gridFillArray;
Colour baseGridColour = Colours::green;
Colour touchColour = Colours::cyan;
Colour touchColour = Colours::red;
Array<int> tonics = { 4, 12, 20 };
Array<int> notes = { 1, 3, 6, 7, 9, 11, 14, 15, 17, 19, 22, 24 };
Array<int> notes = { 1, 3, 6, 7, 9, 11, 14, 15, 17, 19, 22, 24 };
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SynthGrid)
@ -73,8 +73,7 @@ struct SynthGrid
class MainComponent : public Component,
public TopologySource::Listener,
private TouchSurface::Listener,
private ControlButton::Listener,
private Timer
private ControlButton::Listener
{
public:
MainComponent()
@ -145,7 +144,7 @@ private:
/** Overridden from TouchSurface::Listener. Called when a Touch is received on the Lightpad */
void touchChanged (TouchSurface&, const TouchSurface::Touch& touch) override
{
if (currentMode == waveformSelectionMode && touch.isTouchStart && allowTouch)
if (currentMode == waveformSelectionMode && touch.isTouchStart)
{
// Change the displayed waveshape to the next one
++waveshapeMode;
@ -154,9 +153,6 @@ private:
waveshapeMode = 0;
waveshapeProgram->setWaveshapeType (waveshapeMode);
allowTouch = false;
startTimer (500);
}
else if (currentMode == playMode)
{
@ -243,12 +239,13 @@ private:
{
if (currentMode == waveformSelectionMode)
{
// Create a new BitmapLEDProgram for the LEDGrid
// Create a new WaveshapeProgram for the LEDGrid
waveshapeProgram = new WaveshapeProgram (*grid);
// Set the LEDGrid program
grid->setProgram (waveshapeProgram);
// Initialise the program
waveshapeProgram->setWaveshapeType (waveshapeMode);
waveshapeProgram->generateWaveshapes();
}
@ -267,11 +264,6 @@ private:
}
}
void timerCallback() override
{
allowTouch = true;
}
enum BlocksSynthMode
{
waveformSelectionMode = 0,
@ -292,15 +284,11 @@ private:
Array<juce::Time> touchMessageTimesInLastSecond;
Colour waveshapeColour = Colours::red;
int waveshapeMode = 0;
float scaleX = 0.0;
float scaleY = 0.0;
bool allowTouch = true;
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};

View file

@ -14,10 +14,16 @@
class WaveshapeProgram : public LEDGrid::Program
{
public:
WaveshapeProgram (LEDGrid& lg) : Program (lg)
WaveshapeProgram (LEDGrid& lg) : Program (lg) {}
/** Sets the waveshape type to display on the grid */
void setWaveshapeType (uint8 type)
{
ledGrid.setDataByte (0, type);
}
/** Generates the Y coordinates for 1.5 cycles of each of the four waveshapes and stores them
at the correct offsets in the shared data heap. */
void generateWaveshapes()
{
uint8 sineWaveY[45];
@ -39,14 +45,14 @@ public:
if (currentPhase < double_Pi)
{
if (x == 0)
squareWaveY[x] = 20;
squareWaveY[x] = 255;
else
squareWaveY[x] = 1;
}
else
{
if (squareWaveY[x - 1] == 1)
squareWaveY[x - 1] = 20;
squareWaveY[x - 1] = 255;
squareWaveY[x] = 13;
}
@ -54,8 +60,8 @@ public:
// Saw wave output, set flags for when vertical line should be drawn
sawWaveY[x] = 14 - ((x / 2) % 15);
if (sawWaveY[x] == 0 && sawWaveY[x - 1] != 20)
sawWaveY[x] = 20;
if (sawWaveY[x] == 0 && sawWaveY[x - 1] != 255)
sawWaveY[x] = 255;
// Triangle wave output
triangleWaveY[x] = x < 15 ? x : 14 - (x % 15);
@ -73,31 +79,16 @@ public:
currentPhase += phaseInc;
}
// Store the values for each of the waveshapes at the correct offsets in the shared data heap
for (int i = 0; i < 45; ++i)
{
ledGrid.setDataByte (sineWaveOffset + i, sineWaveY[i]);
int sineByte = ledGrid.getDataByte (sineWaveOffset + i);
jassert (sineByte == sineWaveY[i]);
ledGrid.setDataByte (squareWaveOffset + i, squareWaveY[i]);
int squareByte = ledGrid.getDataByte (squareWaveOffset + i);
jassert (squareByte == squareWaveY[i]);
ledGrid.setDataByte (sawWaveOffset + i, sawWaveY[i]);
int sawByte = ledGrid.getDataByte (sawWaveOffset + i);
jassert (sawByte == sawWaveY[i]);
ledGrid.setDataByte (sineWaveOffset + i, sineWaveY[i]);
ledGrid.setDataByte (squareWaveOffset + i, squareWaveY[i]);
ledGrid.setDataByte (sawWaveOffset + i, sawWaveY[i]);
ledGrid.setDataByte (triangleWaveOffset + i, triangleWaveY[i]);
int triangleByte = ledGrid.getDataByte (triangleWaveOffset + i);
jassert (triangleByte == triangleWaveY[i]);
}
}
void setWaveshapeType (uint8 type)
{
ledGrid.setDataByte (0, type);
}
uint32 getHeapSize() override
{
return totalDataSize;
@ -150,14 +141,17 @@ public:
// Get the waveshape type
int type = getHeapByte (0);
// Calculate the heap offset
int offset = 1 + (type * 45) + yOffset;
for (int x = 0; x < 15; ++x)
{
// Get the corresponding Y coordinate for each X coordinate
int y = getHeapByte (offset + x);
// Draw a vertical line if flag is set or draw an LED circle
if (y == 20)
if (y == 255)
{
for (int i = 0; i < 15; ++i)
drawLEDCircle (x, i);
@ -168,15 +162,19 @@ public:
}
}
// Increment and wrap the Y offset to draw a 'moving' waveshape
if (++yOffset == 30)
yOffset = 0;
}
)littlefoot";
}
private:
//==============================================================================
/** Shared data heap is laid out as below. There is room for the waveshape type and
the Y coordinates for 1.5 cycles of each of the four waveshapes. */
static constexpr uint32 waveshapeType = 0; // 1 byte
static constexpr uint32 sineWaveOffset = 1; // 1 byte * 45
static constexpr uint32 squareWaveOffset = 46; // 1 byte * 45