Audacity  2.2.2
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 
9 ******************************************************************//*******************************************************************/
15 
16 #include "../Audacity.h"
17 #include "ImportExportCommands.h"
18 #include "../Project.h"
19 #include "../Track.h"
20 #include "../export/Export.h"
21 
22 // Import
23 
25 {
26  return wxT("Import");
27 }
28 
30 {
31  auto filenameValidator = make_movable<DefaultValidator>();
32  signature.AddParameter(wxT("Filename"), wxT(""), std::move(filenameValidator));
33 }
34 
35 CommandHolder ImportCommandType::Create(std::unique_ptr<CommandOutputTarget> &&target)
36 {
37  return std::make_shared<ImportCommand>(*this, std::move(target));
38 }
39 
41 {
42  wxString filename = GetString(wxT("Filename"));
43  return context.GetProject()->Import(filename);
44 }
45 
47 { }
48 
49 // Export
50 
52 {
53  return wxT("Export");
54 }
55 
57 {
58  auto modeValidator = make_movable<OptionValidator>();
59  modeValidator->AddOption(wxT("All"));
60  modeValidator->AddOption(wxT("Selection"));
61  signature.AddParameter(wxT("Mode"), wxT("All"), std::move(modeValidator));
62 
63  auto filenameValidator = make_movable<DefaultValidator>();
64  signature.AddParameter(wxT("Filename"), wxT("exported.wav"), std::move(filenameValidator));
65 
66  auto channelsValidator = make_movable<IntValidator>();
67  signature.AddParameter(wxT("Channels"), 1, std::move(channelsValidator));
68 }
69 
70 CommandHolder ExportCommandType::Create(std::unique_ptr<CommandOutputTarget> &&target)
71 {
72  return std::make_shared<ExportCommand>(*this, std::move(target));
73 }
74 
76 {
77  wxString mode = GetString(wxT("Mode"));
78  wxString filename = GetString(wxT("Filename"));
79  long numChannels = GetLong(wxT("Channels"));
80 
81  bool selection = mode.IsSameAs(wxT("Selection"));
82 
83  double t0, t1;
84  if (selection)
85  {
86  t0 = context.GetProject()->mViewInfo.selectedRegion.t0();
87  t1 = context.GetProject()->mViewInfo.selectedRegion.t1();
88  }
89  else
90  {
91  t0 = 0.0;
92  t1 = context.GetProject()->GetTracks()->GetEndTime();
93  }
94 
95  // Find the extension and check it's valid
96  int splitAt = filename.Find(wxUniChar('.'), true);
97  if (splitAt < 0)
98  {
99  Error(wxT("Export filename must have an extension!"));
100  return false;
101  }
102  wxString extension = filename.Mid(splitAt+1).MakeUpper();
103 
104  Exporter exporter;
105 
106  bool exportSuccess = exporter.Process(context.GetProject(),
107  std::max(0L, numChannels),
108  extension, filename,
109  selection, t0, t1);
110 
111  if (exportSuccess)
112  {
113  Status(wxString::Format(wxT("Exported to %s format: %s"),
114  extension, filename));
115  return true;
116  }
117 
118  Error(wxString::Format(wxT("Could not export to %s format!"), extension));
119  return false;
120 }
121 
123 { }
CommandHolder Create(std::unique_ptr< CommandOutputTarget > &&target) override
double t0() const
SelectedRegion selectedRegion
Definition: ViewInfo.h:160
bool Apply(CommandExecutionContext context) override
std::shared_ptr< Command > CommandHolder
void AddParameter(const wxString &name, const wxVariant &dft, movable_ptr< Validator > &&valid)
double GetEndTime() const
Definition: Track.cpp:1409
wxString GetString(const wxString &paramName)
Definition: Command.cpp:165
bool Process(AudacityProject *project, bool selectedOnly, double t0, double t1)
Definition: Export.cpp:338
double t1() const
void BuildSignature(CommandSignature &signature) override
Postcondition: signature is a 'signature' map containing parameter.
long GetLong(const wxString &paramName)
Definition: Command.cpp:149
bool Import(const wxString &fileName, WaveTrackArray *pTrackArray=NULL)
Definition: Project.cpp:4247
void Status(const wxString &status) override
Definition: Command.cpp:179
ViewInfo mViewInfo
Definition: Project.h:542
void BuildSignature(CommandSignature &signature) override
Postcondition: signature is a 'signature' map containing parameter.
bool Apply(CommandExecutionContext context) override
wxString BuildName() override
Represents a context to which a command may be applied.
Definition: Command.h:50
TrackList * GetTracks()
Definition: Project.h:174
AudacityProject * GetProject() const
Definition: Command.h:60
CommandHolder Create(std::unique_ptr< CommandOutputTarget > &&target) override
Class that maps parameter names to default values and validators.
void Error(const wxString &message) override
Definition: Command.cpp:184
wxString BuildName() override