forked from Tracktion/tracktion_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaybackDemo.h
153 lines (122 loc) · 6.11 KB
/
PlaybackDemo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
The block below describes the properties of this PIP. A PIP is a short snippet
of code that can be read by the Projucer and used to generate a JUCE project.
BEGIN_JUCE_PIP_METADATA
name: PlaybackDemo
version: 0.0.1
vendor: Tracktion
website: www.tracktion.com
description: This example simply loads a project from the command line and plays it back in a loop.
dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats, juce_audio_processors, juce_audio_utils,
juce_core, juce_data_structures, juce_dsp, juce_events, juce_graphics,
juce_gui_basics, juce_gui_extra, juce_osc, tracktion_engine
exporters: linux_make, vs2017, xcode_iphone, xcode_mac
moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1, JUCE_PLUGINHOST_AU=1, JUCE_PLUGINHOST_VST3=1
type: Component
mainClass: PlaybackDemo
END_JUCE_PIP_METADATA
*******************************************************************************/
#pragma once
#include "common/Utilities.h"
#include "common/PlaybackDemoAudio.h"
//==============================================================================
class PlaybackDemo : public Component,
private ChangeListener,
private Timer
{
public:
//==============================================================================
PlaybackDemo()
{
const auto editFilePath = JUCEApplication::getCommandLineParameters().replace ("-NSDocumentRevisionsDebugMode YES", "").unquoted().trim();
const File editFile (editFilePath);
if (editFile.existsAsFile())
{
edit = std::make_unique<te::Edit> (engine, te::loadEditFromFile (engine, editFile, {}), te::Edit::forEditing, nullptr, 0);
auto& transport = edit->getTransport();
transport.setLoopRange ({ 0.0, edit->getLength() });
transport.looping = true;
transport.play (false);
transport.addChangeListener (this);
editNameLabel.setText (editFile.getFileNameWithoutExtension(), dontSendNotification);
}
else
{
auto f = File::createTempFile (".ogg");
f.replaceWithData (PlaybackDemoAudio::BITs_Export_2_ogg, PlaybackDemoAudio::BITs_Export_2_oggSize);
edit = std::make_unique<te::Edit> (engine, te::createEmptyEdit (engine), te::Edit::forEditing, nullptr, 0);
edit->getTransport().addChangeListener (this);
auto clip = EngineHelpers::loadAudioFileAsClip (*edit, f);
EngineHelpers::loopAroundClip (*clip);
editNameLabel.setText ("Demo Song", dontSendNotification);
}
playPauseButton.onClick = [this] { EngineHelpers::togglePlay (*edit); };
// Show the plugin scan dialog
// If you're loading an Edit with plugins in, you'll need to perform a scan first
pluginsButton.onClick = [this]
{
DialogWindow::LaunchOptions o;
o.dialogTitle = TRANS("Plugins");
o.dialogBackgroundColour = Colours::black;
o.escapeKeyTriggersCloseButton = true;
o.useNativeTitleBar = true;
o.resizable = true;
o.useBottomRightCornerResizer = true;
auto v = new PluginListComponent (engine.getPluginManager().pluginFormatManager,
engine.getPluginManager().knownPluginList,
engine.getTemporaryFileManager().getTempFile ("PluginScanDeadMansPedal"),
te::getApplicationSettings());
v->setSize (800, 600);
o.content.setOwned (v);
o.launchAsync();
};
settingsButton.onClick = [this] { EngineHelpers::showAudioDeviceSettings (engine); };
updatePlayButtonText();
editNameLabel.setJustificationType (Justification::centred);
cpuLabel.setJustificationType (Justification::centred);
Helpers::addAndMakeVisible (*this, { &pluginsButton, &settingsButton, &playPauseButton, &editNameLabel, &cpuLabel });
setSize (600, 400);
startTimerHz (5);
}
~PlaybackDemo()
{
engine.getTemporaryFileManager().getTempDirectory().deleteRecursively();
}
//==============================================================================
void paint (Graphics& g) override
{
g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
}
void resized() override
{
auto r = getLocalBounds();
auto topR = r.removeFromTop (30);
pluginsButton.setBounds (topR.removeFromLeft (topR.getWidth() / 3).reduced (2));
settingsButton.setBounds (topR.removeFromLeft (topR.getWidth() / 2).reduced (2));
playPauseButton.setBounds (topR.reduced (2));
auto middleR = r.withSizeKeepingCentre (r.getWidth(), 40);
cpuLabel.setBounds (middleR.removeFromBottom (20));
editNameLabel.setBounds (middleR);
}
private:
//==============================================================================
te::Engine engine { ProjectInfo::projectName };
std::unique_ptr<te::Edit> edit;
TextButton pluginsButton { "Plugins" }, settingsButton { "Settings" }, playPauseButton { "Play" };
Label editNameLabel { "No Edit Loaded" }, cpuLabel;
//==============================================================================
void updatePlayButtonText()
{
if (edit != nullptr)
playPauseButton.setButtonText (edit->getTransport().isPlaying() ? "Pause" : "Play");
}
void changeListenerCallback (ChangeBroadcaster*) override
{
updatePlayButtonText();
}
void timerCallback() override
{
cpuLabel.setText (String::formatted ("CPU: %d%%", int (engine.getDeviceManager().getCpuUsage() * 100)), dontSendNotification);
}
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PlaybackDemo)
};