Audacity 3.2.0
ImportExportCommands.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity - A Digital Audio Editor
4 Copyright 1999-2009 Audacity Team
5 File License: wxWidgets
6
7 Dan Horgan
8 James Crook
9
10******************************************************************//*******************************************************************/
16
17
19
20#include "CommandDispatch.h"
21#include "MenuRegistry.h"
22#include "../CommonCommandFlags.h"
23#include "LoadCommands.h"
24#include "../ProjectFileManager.h"
25#include "ViewInfo.h"
26#include "Export.h"
27#include "../SelectUtilities.h"
28#include "SettingsVisitor.h"
29#include "ShuttleGui.h"
30#include "Track.h"
31#include "wxFileNameWrapper.h"
32#include "CommandContext.h"
33#include "ExportUtils.h"
34#include "ProjectRate.h"
36#include "ExportProgressUI.h"
37
38
40{ XO("Import2") };
41
43
44template<bool Const>
46 S.Define( mFileName, wxT("Filename"), wxString{} );
47 return true;
48}
49
51 { return VisitSettings<false>(S); }
52
54 { return VisitSettings<true>(S); }
55
57{
58 S.AddSpace(0, 5);
59
60 S.StartMultiColumn(2, wxALIGN_CENTER);
61 {
62 S.TieTextBox(XXO("File Name:"),mFileName);
63 }
64 S.EndMultiColumn();
65}
66
68{
69 bool wasEmpty = TrackList::Get(context.project).empty();
70 const bool success =
72
73 if (success && wasEmpty)
75
76 return success;
77}
78
79template<bool Const>
81 wxFileName fn = FileNames::FindDefaultPath(FileNames::Operation::Export);
82 fn.SetName("exported.wav");
83 S.Define(mFileName, wxT("Filename"), fn.GetFullPath());
84 S.Define( mnChannels, wxT("NumChannels"), 1 );
85 return true;
86}
87
89 { return VisitSettings<false>(S); }
90
92 { return VisitSettings<true>(S); }
93
95{ XO("Export2") };
96
98
100{
101 S.AddSpace(0, 5);
102
103 S.StartMultiColumn(2, wxALIGN_CENTER);
104 {
105 S.TieTextBox(XXO("File Name:"),mFileName);
106 S.TieTextBox(XXO("Number of Channels:"),mnChannels);
107 }
108 S.EndMultiColumn();
109}
110
112{
113 double t0, t1;
114 auto &selectedRegion = ViewInfo::Get( context.project ).selectedRegion;
115 t0 = selectedRegion.t0();
116 t1 = selectedRegion.t1();
117
118 // Find the extension and check it's valid
119 int splitAt = mFileName.Find(wxUniChar('.'), true);
120 if (splitAt < 0)
121 {
122 context.Error(wxT("Export filename must have an extension!"));
123 return false;
124 }
125 wxString extension = mFileName.Mid(splitAt+1).MakeUpper();
126
127 auto [plugin, formatIndex] = ExportPluginRegistry::Get().FindFormat(extension);
128
129 if(plugin != nullptr)
130 {
131 auto editor = plugin->CreateOptionsEditor(formatIndex, nullptr);
132 editor->Load(*gPrefs);
133
134 auto builder = ExportTaskBuilder{}
136 .SetNumChannels(std::max(0, mnChannels))
138 .SetPlugin(plugin)
140 .SetRange(t0, t1, true);
141
142 auto result = ExportResult::Error;
144 {
145 result = ExportProgressUI::Show(builder.Build(context.project));
146 });
147 if (result == ExportResult::Success || result == ExportResult::Stopped)
148 {
149 context.Status(wxString::Format(wxT("Exported to %s format: %s"),
151 return true;
152 }
153 }
154
155 context.Error(wxString::Format(wxT("Could not export to %s format!"), extension));
156 return false;
157}
158
159namespace {
160using namespace MenuRegistry;
161
162// Register menu items
163
165 Items( wxT(""),
166 // Note that the PLUGIN_SYMBOL must have a space between words,
167 // whereas the short-form used here must not.
168 // (So if you did write "Compare Audio" for the PLUGIN_SYMBOL name, then
169 // you would have to use "CompareAudio" here.)
170 Command( wxT("Import2"), XXO("Import..."),
172 Command( wxT("Export2"), XXO("Export..."),
174 ),
175 wxT("Optional/Extra/Part2/Scriptables2")
176};
177}
wxT("CloseDown"))
const ReservedCommandFlag & AudioIONotBusyFlag()
XO("Cut/Copy/Paste")
XXO("&Cut/Copy/Paste Toolbar")
audacity::BasicSettings * gPrefs
Definition: Prefs.cpp:68
an object holding per-project preferred sample rate
#define S(N)
Definition: ToChars.cpp:64
declares abstract base class Track, TrackList, and iterators over TrackList
static const auto fn
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
virtual void Error(const wxString &message) const
virtual void Status(const wxString &message, bool bFlush=false) const
AudacityProject & project
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
static const ComponentInterfaceSymbol Symbol
bool Apply(const CommandContext &context) override
void PopulateOrExchange(ShuttleGui &S) override
bool VisitSettings(SettingsVisitorBase< Const > &S)
std::tuple< ExportPlugin *, int > FindFormat(const wxString &format, bool compareWithCase=false) const
Returns first pair of [exportPlugin, formatIndex], such that: exportPlugin->GetFormatInfo(formatIndex...
static ExportPluginRegistry & Get()
ExportTaskBuilder & SetPlugin(const ExportPlugin *plugin, int format=0) noexcept
Definition: Export.cpp:59
ExportTaskBuilder & SetParameters(ExportProcessor::Parameters parameters) noexcept
Definition: Export.cpp:47
ExportTaskBuilder & SetNumChannels(unsigned numChannels) noexcept
Definition: Export.cpp:53
ExportTaskBuilder & SetSampleRate(double sampleRate) noexcept
Definition: Export.cpp:72
ExportTaskBuilder & SetFileName(const wxFileName &filename)
Definition: Export.cpp:33
ExportTaskBuilder & SetRange(double t0, double t1, bool selectedOnly=false) noexcept
Definition: Export.cpp:39
static ExportProcessor::Parameters ParametersFromEditor(const ExportOptionsEditor &editor)
Definition: ExportUtils.cpp:39
bool Apply(const CommandContext &context) override
bool VisitSettings(SettingsVisitorBase< Const > &S)
void PopulateOrExchange(ShuttleGui &S) override
static const ComponentInterfaceSymbol Symbol
double t0() const
Definition: ViewInfo.h:35
bool Import(const FilePath &fileName, bool addToHistory=true)
static ProjectFileManager & Get(AudacityProject &project)
static ProjectRate & Get(AudacityProject &project)
Definition: ProjectRate.cpp:28
double GetRate() const
Definition: ProjectRate.cpp:53
Generates classes whose instances register items at construction.
Definition: Registry.h:388
Visitor of effect or command parameters. This is a base class with lots of virtual functions that do ...
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:640
bool empty() const
Definition: Track.cpp:758
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:314
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:216
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
AUDACITY_DLL_API void OnAudacityCommand(const CommandContext &ctx)
IMPORT_EXPORT_API ExportResult Show(ExportTask exportTask)
void ExceptionWrappedCall(Callable callable)
FILES_API FilePath FindDefaultPath(Operation op)
constexpr auto Items
Definition: MenuRegistry.h:427
constexpr auto Command
Definition: MenuRegistry.h:456
void SelectAllIfNone(AudacityProject &project)
BuiltinCommandsModule::Registration< ImportCommand > reg
BuiltinCommandsModule::Registration< ExportCommand > reg2