Browse Source

Tarted up the audio playback demo slightly, to show a playback cursor and let you drag it with the mouse, zoom with mouse-wheel etc. Fixed a VST plugin host typo. Gave the ImageButton a default constructor.

tags/2021-05-28
jules 14 years ago
parent
commit
a9fbfad8c1
5 changed files with 111 additions and 69 deletions
  1. +77
    -34
      extras/JuceDemo/Source/demos/AudioDemoPlaybackPage.cpp
  2. +1
    -0
      extras/JuceDemo/Source/demos/AudioDemoPlaybackPage.h
  3. +2
    -2
      modules/juce_audio_processors/format_types/juce_VSTPluginFormat.cpp
  4. +28
    -30
      modules/juce_gui_basics/buttons/juce_ImageButton.cpp
  5. +3
    -3
      modules/juce_gui_basics/buttons/juce_ImageButton.h

+ 77
- 34
extras/JuceDemo/Source/demos/AudioDemoPlaybackPage.cpp View File

@@ -24,20 +24,26 @@
#include "AudioDemoPlaybackPage.h"
//[MiscUserDefs] You can add your own user definitions and misc code here...
class DemoThumbnailComp : public Component,
public ChangeListener,
public FileDragAndDropTarget
public FileDragAndDropTarget,
private Timer
{
public:
DemoThumbnailComp()
: thumbnailCache (5),
DemoThumbnailComp (AudioFormatManager& formatManager,
AudioTransportSource& transportSource_,
Slider& zoomSlider_)
: transportSource (transportSource_),
zoomSlider (zoomSlider_),
thumbnailCache (5),
thumbnail (512, formatManager, thumbnailCache)
{
startTime = endTime = 0;
formatManager.registerBasicFormats();
thumbnail.addChangeListener (this);
currentPositionMarker.setFill (Colours::purple.withAlpha (0.7f));
addAndMakeVisible (&currentPositionMarker);
}
~DemoThumbnailComp()
@@ -50,14 +56,17 @@ public:
thumbnail.setSource (new FileInputSource (file));
startTime = 0;
endTime = thumbnail.getTotalLength();
startTimer (1000 / 40);
}
void setZoomFactor (double amount)
{
if (thumbnail.getTotalLength() > 0)
{
double timeDisplayed = jmax (0.001, (thumbnail.getTotalLength() - startTime) * (1.0 - jlimit (0.0, 1.0, amount)));
endTime = startTime + timeDisplayed;
const double newScale = jmax (0.001, thumbnail.getTotalLength() * (1.0 - jlimit (0.0, 0.99, amount)));
const double timeAtCentre = xToTime (getWidth() / 2.0f);
startTime = timeAtCentre - newScale * 0.5;
endTime = timeAtCentre + newScale * 0.5;
repaint();
}
}
@@ -66,10 +75,14 @@ public:
{
if (thumbnail.getTotalLength() > 0)
{
double newStart = startTime + (wheelIncrementX + wheelIncrementY) * (endTime - startTime) / 10.0;
double newStart = startTime - wheelIncrementX * (endTime - startTime) / 10.0;
newStart = jlimit (0.0, thumbnail.getTotalLength() - (endTime - startTime), newStart);
endTime = newStart + (endTime - startTime);
startTime = newStart;
if (wheelIncrementY != 0)
zoomSlider.setValue (zoomSlider.getValue() - wheelIncrementY);
repaint();
}
}
@@ -77,8 +90,7 @@ public:
void paint (Graphics& g)
{
g.fillAll (Colours::white);
g.setColour (Colours::mediumblue);
g.setColour (Colours::lightblue);
if (thumbnail.getTotalLength() > 0)
{
@@ -112,10 +124,44 @@ public:
demoPage->showFile (File (files[0]));
}
AudioFormatManager formatManager;
void mouseDrag (const MouseEvent& e)
{
transportSource.setPosition (jmax (0.0, xToTime ((float) e.x)));
}
void mouseUp (const MouseEvent&)
{
transportSource.start();
}
void timerCallback()
{
currentPositionMarker.setVisible (transportSource.isPlaying() || isMouseButtonDown());
double currentPlayPosition = transportSource.getCurrentPosition();
currentPositionMarker.setRectangle (Rectangle<float> (timeToX (currentPlayPosition) - 0.75f, 0,
1.5f, (float) getHeight()));
}
private:
AudioTransportSource& transportSource;
Slider& zoomSlider;
AudioThumbnailCache thumbnailCache;
AudioThumbnail thumbnail;
double startTime, endTime;
DrawableRectangle currentPositionMarker;
float timeToX (const double time) const
{
return getWidth() * (float) ((time - startTime) / (endTime - startTime));
}
double xToTime (const float x) const
{
return (x / getWidth()) * (endTime - startTime) + startTime;
}
};
//[/MiscUserDefs]
@@ -140,15 +186,6 @@ AudioDemoPlaybackPage::AudioDemoPlaybackPage (AudioDeviceManager& deviceManager_
zoomLabel->setColour (TextEditor::textColourId, Colours::black);
zoomLabel->setColour (TextEditor::backgroundColourId, Colour (0x0));
addAndMakeVisible (thumbnail = new DemoThumbnailComp());
addAndMakeVisible (startStopButton = new TextButton (String::empty));
startStopButton->setButtonText (L"Play/Stop");
startStopButton->addListener (this);
startStopButton->setColour (TextButton::buttonColourId, Colour (0xff79ed7f));
addAndMakeVisible (fileTreeComp = new FileTreeComponent (directoryList));
addAndMakeVisible (explanation = new Label (String::empty,
L"Select an audio file in the treeview above, and this page will display its waveform, and let you play it.."));
explanation->setFont (Font (14.0000f, Font::plain));
@@ -164,6 +201,14 @@ AudioDemoPlaybackPage::AudioDemoPlaybackPage (AudioDeviceManager& deviceManager_
zoomSlider->addListener (this);
zoomSlider->setSkewFactor (2);
addAndMakeVisible (thumbnail = new DemoThumbnailComp (formatManager, transportSource, *zoomSlider));
addAndMakeVisible (startStopButton = new TextButton (String::empty));
startStopButton->setButtonText (L"Play/Stop");
startStopButton->addListener (this);
startStopButton->setColour (TextButton::buttonColourId, Colour (0xff79ed7f));
addAndMakeVisible (fileTreeComp = new FileTreeComponent (directoryList));
//[UserPreSize]
//[/UserPreSize]
@@ -172,7 +217,9 @@ AudioDemoPlaybackPage::AudioDemoPlaybackPage (AudioDeviceManager& deviceManager_
//[Constructor] You can add your own custom stuff here..
directoryList.setDirectory (File::getSpecialLocation (File::userHomeDirectory), true, true);
formatManager.registerBasicFormats();
directoryList.setDirectory (File ("/Users/jules/Music/iTunes/iTunes Music/"), true, true);
thread.startThread (3);
fileTreeComp->setColour (FileTreeComponent::backgroundColourId, Colours::white);
@@ -288,10 +335,6 @@ void AudioDemoPlaybackPage::loadFileIntoTransport (const File& audioFile)
transportSource.setSource (nullptr);
currentAudioFileSource = nullptr;
// get a format manager and set it up with the basic types (wav and aiff).
AudioFormatManager formatManager;
formatManager.registerBasicFormats();
AudioFormatReader* reader = formatManager.createReaderFor (audioFile);
if (reader != nullptr)
@@ -340,15 +383,6 @@ BEGIN_JUCER_METADATA
edBkgCol="0" labelText="zoom:" editableSingleClick="0" editableDoubleClick="0"
focusDiscardsChanges="0" fontname="Default font" fontsize="15"
bold="0" italic="0" justification="34"/>
<GENERICCOMPONENT name="" id="beef657b0e007936" memberName="thumbnail" virtualName=""
explicitFocusOrder="0" pos="16 221R 32M 123" class="DemoThumbnailComp"
params=""/>
<TEXTBUTTON name="" id="abe446e2f3f09420" memberName="startStopButton" virtualName=""
explicitFocusOrder="0" pos="16 46R 150 32" bgColOff="ff79ed7f"
buttonText="Play/Stop" connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<GENERICCOMPONENT name="" id="1de1dc6a18a9032b" memberName="fileTreeComp" virtualName=""
explicitFocusOrder="0" pos="16 8 32M 245M" class="FileTreeComponent"
params="directoryList"/>
<LABEL name="" id="7db7d0a64ef21311" memberName="explanation" virtualName=""
explicitFocusOrder="0" pos="256 82R 275M 64" edTextCol="ff000000"
edBkgCol="0" labelText="Select an audio file in the treeview above, and this page will display its waveform, and let you play it.."
@@ -358,6 +392,15 @@ BEGIN_JUCER_METADATA
explicitFocusOrder="0" pos="72 90R 200 24" min="0" max="1" int="0"
style="LinearHorizontal" textBoxPos="NoTextBox" textBoxEditable="1"
textBoxWidth="80" textBoxHeight="20" skewFactor="2"/>
<GENERICCOMPONENT name="" id="beef657b0e007936" memberName="thumbnail" virtualName=""
explicitFocusOrder="0" pos="16 221R 32M 123" class="DemoThumbnailComp"
params="formatManager, transportSource, *zoomSlider"/>
<TEXTBUTTON name="" id="abe446e2f3f09420" memberName="startStopButton" virtualName=""
explicitFocusOrder="0" pos="16 46R 150 32" bgColOff="ff79ed7f"
buttonText="Play/Stop" connectedEdges="0" needsCallback="1" radioGroupId="0"/>
<GENERICCOMPONENT name="" id="1de1dc6a18a9032b" memberName="fileTreeComp" virtualName=""
explicitFocusOrder="0" pos="16 8 32M 245M" class="FileTreeComponent"
params="directoryList"/>
</JUCER_COMPONENT>
END_JUCER_METADATA


+ 1
- 0
extras/JuceDemo/Source/demos/AudioDemoPlaybackPage.h View File

@@ -71,6 +71,7 @@ public:
private:
//[UserVariables] -- You can add your own custom variables in this section.
AudioDeviceManager& deviceManager;
AudioFormatManager formatManager;
TimeSliceThread thread;
DirectoryContentsList directoryList;


+ 2
- 2
modules/juce_audio_processors/format_types/juce_VSTPluginFormat.cpp View File

@@ -2902,8 +2902,8 @@ FileSearchPath VSTPluginFormat::getDefaultLocationsToSearch()
#elif JUCE_WINDOWS
const String programFiles (File::getSpecialLocation (File::globalApplicationsDirectory).getFullPathName());
return FileSearchPath (WindowsRegistry::getRegistryValue ("HKLM\\Software\\VST\\VSTPluginsPath",
programFiles + "\\Steinberg\\VstPlugins"));
return FileSearchPath (WindowsRegistry::getValue ("HKLM\\Software\\VST\\VSTPluginsPath",
programFiles + "\\Steinberg\\VstPlugins"));
#elif JUCE_LINUX
return FileSearchPath ("/usr/lib/vst");
#endif


+ 28
- 30
modules/juce_gui_basics/buttons/juce_ImageButton.cpp View File

@@ -30,11 +30,7 @@ ImageButton::ImageButton (const String& text_)
: Button (text_),
scaleImageToFit (true),
preserveProportions (true),
alphaThreshold (0),
imageX (0),
imageY (0),
imageW (0),
imageH (0)
alphaThreshold (0)
{
}
@@ -62,10 +58,10 @@ void ImageButton::setImages (const bool resizeButtonNowToFitThisImage,
if (resizeButtonNowToFitThisImage && normalImage.isValid())
{
imageW = normalImage.getWidth();
imageH = normalImage.getHeight();
imageBounds.setSize (normalImage.getWidth(),
normalImage.getHeight());
setSize (imageW, imageH);
setSize (imageBounds.getWidth(), imageBounds.getHeight());
}
scaleImageToFit = rescaleImagesWhenButtonSizeChanges;
@@ -78,7 +74,7 @@ void ImageButton::setImages (const bool resizeButtonNowToFitThisImage,
downOpacity = imageOpacityWhenDown;
downOverlay = overlayColourWhenDown;
alphaThreshold = (unsigned char) jlimit (0, 0xff, roundToInt (255.0f * hitTestAlphaThreshold));
alphaThreshold = (uint8) jlimit (0, 0xff, roundToInt (255.0f * hitTestAlphaThreshold));
repaint();
}
@@ -127,10 +123,10 @@ void ImageButton::paintButton (Graphics& g,
{
const int iw = im.getWidth();
const int ih = im.getHeight();
imageW = getWidth();
imageH = getHeight();
imageX = (imageW - iw) / 2;
imageY = (imageH - ih) / 2;
int w = getWidth();
int h = getHeight();
int x = (w - iw) / 2;
int y = (h - ih) / 2;
if (scaleImageToFit)
{
@@ -138,40 +134,42 @@ void ImageButton::paintButton (Graphics& g,
{
int newW, newH;
const float imRatio = ih / (float) iw;
const float destRatio = imageH / (float) imageW;
const float destRatio = h / (float) w;
if (imRatio > destRatio)
{
newW = roundToInt (imageH / imRatio);
newH = imageH;
newW = roundToInt (h / imRatio);
newH = h;
}
else
{
newW = imageW;
newH = roundToInt (imageW * imRatio);
newW = w;
newH = roundToInt (w * imRatio);
}
imageX = (imageW - newW) / 2;
imageY = (imageH - newH) / 2;
imageW = newW;
imageH = newH;
x = (w - newW) / 2;
y = (h - newH) / 2;
w = newW;
h = newH;
}
else
{
imageX = 0;
imageY = 0;
x = 0;
y = 0;
}
}
if (! scaleImageToFit)
{
imageW = iw;
imageH = ih;
w = iw;
h = ih;
}
imageBounds.setBounds (x, y, w, h);
const bool useDownImage = isButtonDown || getToggleState();
getLookAndFeel().drawImageButton (g, &im, imageX, imageY, imageW, imageH,
getLookAndFeel().drawImageButton (g, &im, x, y, w, h,
useDownImage ? downOverlay
: (isMouseOverButton ? overOverlay
: normalOverlay),
@@ -189,9 +187,9 @@ bool ImageButton::hitTest (int x, int y)
Image im (getCurrentImage());
return im.isNull() || (imageW > 0 && imageH > 0
&& alphaThreshold < im.getPixelAt (((x - imageX) * im.getWidth()) / imageW,
((y - imageY) * im.getHeight()) / imageH).getAlpha());
return im.isNull() || ((! imageBounds.isEmpty())
&& alphaThreshold < im.getPixelAt (((x - imageBounds.getX()) * im.getWidth()) / imageBounds.getWidth(),
((y - imageBounds.getY()) * im.getHeight()) / imageBounds.getHeight()).getAlpha());
}
END_JUCE_NAMESPACE

+ 3
- 3
modules/juce_gui_basics/buttons/juce_ImageButton.h View File

@@ -49,7 +49,7 @@ public:
@param name the name to give the component
*/
explicit ImageButton (const String& name);
explicit ImageButton (const String& name = String::empty);
/** Destructor. */
~ImageButton();
@@ -139,8 +139,8 @@ protected:
private:
//==============================================================================
bool scaleImageToFit, preserveProportions;
unsigned char alphaThreshold;
int imageX, imageY, imageW, imageH;
uint8 alphaThreshold;
Rectangle<int> imageBounds;
Image normalImage, overImage, downImage;
float normalOpacity, overOpacity, downOpacity;
Colour normalOverlay, overOverlay, downOverlay;


Loading…
Cancel
Save