File: Export.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (162 lines) | stat: -rw-r--r-- 4,337 bytes parent folder | download | duplicates (2)
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
154
155
156
157
158
159
160
161
162
/**********************************************************************

  Audacity: A Digital Audio Editor

  Export.cpp

  Dominic Mazzoni

*******************************************************************//**

\class Export
\brief Main class to control the export function.

*//*****************************************************************/

#include "Export.h"

#include <numeric>

#include "BasicUI.h"
#include "ExportPluginRegistry.h"
#include "Mix.h"
#include "Project.h"
#include "WaveTrack.h"
#include "wxFileNameWrapper.h"
#include "StretchingSequence.h"

#include "ExportUtils.h"

ExportTaskBuilder::ExportTaskBuilder() = default;
ExportTaskBuilder::~ExportTaskBuilder() = default;

ExportTaskBuilder& ExportTaskBuilder::SetFileName(const wxFileName& filename)
{
   mFileName = filename;
   return *this;
}

ExportTaskBuilder& ExportTaskBuilder::SetRange(double t0, double t1, bool selectedOnly) noexcept
{
   mT0 = t0;
   mT1 = t1;
   mSelectedOnly = selectedOnly;
   return *this;
}

ExportTaskBuilder& ExportTaskBuilder::SetParameters(ExportProcessor::Parameters parameters) noexcept
{
   mParameters = std::move(parameters);
   return *this;
}

ExportTaskBuilder& ExportTaskBuilder::SetNumChannels(unsigned int numChannels) noexcept
{
   mNumChannels = numChannels;
   return *this;
}

ExportTaskBuilder& ExportTaskBuilder::SetPlugin(const ExportPlugin* plugin, int format) noexcept
{
   mPlugin = plugin;
   mFormat = format;
   return *this;
}

ExportTaskBuilder& ExportTaskBuilder::SetMixerSpec(MixerOptions::Downmix *mixerSpec) noexcept
{
   mMixerSpec = mixerSpec;
   return *this;
}

ExportTaskBuilder& ExportTaskBuilder::SetSampleRate(double sampleRate) noexcept
{
   mSampleRate = sampleRate;
   return *this;
}

ExportTaskBuilder& ExportTaskBuilder::SetTags(const Tags *tags) noexcept
{
   mTags = tags;
   return *this;
}

ExportTask ExportTaskBuilder::Build(AudacityProject& project)
{
   //File rename stuff should be moved out to somewhere else...
   auto filename = mFileName;

   //For safety, if the file already exists we use temporary filename
   //and replace original one export succeeded
   int suffix = 0;
   while (filename.FileExists()) {
      filename.SetName(mFileName.GetName() +
                        wxString::Format(wxT("%d"), suffix));
      suffix++;
   }

   auto processor = mPlugin->CreateProcessor(mFormat);
   if(!processor->Initialize(project,
      mParameters,
      mFileName.GetFullPath(),
      mT0, mT1, mSelectedOnly,
      mSampleRate, mMixerSpec ? mMixerSpec->GetNumChannels() : mNumChannels,
      mMixerSpec,
      mTags))
   {
      return ExportTask([](ExportProcessorDelegate&){ return ExportResult::Cancelled; });
   }

   return ExportTask([actualFilename = filename,
      targetFilename = mFileName,
      processor = std::shared_ptr<ExportProcessor>(processor.release())]
      (ExportProcessorDelegate& delegate)
      {
         auto result = ExportResult::Error;
         auto cleanup = finally( [&] {
            if(result == ExportResult::Success || result == ExportResult::Stopped)
            {
               if (actualFilename != targetFilename)
               {
                  //may fail...
                  ::wxRenameFile(actualFilename.GetFullPath(),
                     targetFilename.GetFullPath(),
                     true);
               }
            }
            else
               ::wxRemoveFile(actualFilename.GetFullPath());
         } );
         result = processor->Process(delegate);
         return result;
      });
}

void ShowDiskFullExportErrorDialog(const wxFileNameWrapper &fileName)
{
   BasicUI::ShowErrorDialog( {},
      XO("Warning"),
      FileException::WriteFailureMessage(fileName),
      "Error:_Disk_full_or_not_writable"
   );
}

void ShowExportErrorDialog(const TranslatableString& message,
      const TranslatableString& caption,
      bool allowReporting)
{
   ShowExportErrorDialog(message, caption, {}, allowReporting);
}

void ShowExportErrorDialog(const TranslatableString& message,
   const TranslatableString& caption,
   const ManualPageID& helpPageId,
   bool allowReporting)
{
   using namespace BasicUI;
   ShowErrorDialog( {},
      caption,
      message,
      helpPageId,
      ErrorDialogOptions { allowReporting ? ErrorDialogType::ModalErrorReport : ErrorDialogType::ModalError });
}