File: Export.cpp

package info (click to toggle)
audacity 0.98-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,896 kB
  • ctags: 4,089
  • sloc: cpp: 26,099; ansic: 4,961; sh: 2,465; makefile: 156; perl: 23
file content (258 lines) | stat: -rw-r--r-- 7,949 bytes parent folder | download
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**********************************************************************

  Audacity: A Digital Audio Editor

  Export.cpp

  Dominic Mazzoni

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

#include <wx/button.h>
#include <wx/textctrl.h>
#include <wx/choice.h>
#include <wx/file.h>
#include <wx/thread.h>
#include <wx/radiobut.h>
#include <wx/sizer.h>
#include <wx/timer.h>
#include <wx/filedlg.h>
#include <wx/msgdlg.h>
#include <wx/progdlg.h>
#include <wx/textfile.h>

#include "Export.h"
#include "ExportPCM.h"
#include "ExportMP3.h"

#include "Audacity.h"
#include "DirManager.h"
#include "LabelTrack.h"
#include "Mix.h"
#include "Prefs.h"
#include "Project.h"
#include "Track.h"
#include "WaveTrack.h"

bool Export(AudacityProject *project,
            wxString format,
            bool selectionOnly, double t0, double t1)
{
   TrackList *tracks = project->GetTracks();

   /* Test to see if the format is supported. This could more easily be done
    * later when we dispatch to a specific function, but I think it's best to
    * let the user know up front if the format simply isn't supported */

   if (format != "WAV" &&
       format != "AIFF" && format != "IRCAM" && format != "AU" &&
#ifdef __WXMAC__
       format != "AIFF with track markers" &&
#endif
       format != "MP3") {
      wxMessageBox(wxString::Format("Sorry, cannot export %s data (yet)."
                                    "Change the default export format in your preferences",
                                    (const char *) format));
      return false;
   }

   /* First analyze the selected audio, perform sanity checks, and provide
    * information as appropriate. */

   /* make sure all selected tracks are at the same rate, tally how many are
    * right, left, mono, and make sure at least one track is selected */

   int numSelected = 0, numLeft = 0, numRight = 0, numMono = 0;
   double rate = 0;

   TrackListIterator iter1(tracks);
   VTrack *tr = iter1.First();

   while (tr) {
      if (tr->GetKind() == VTrack::Wave) {
         if (tr->selected || !selectionOnly) {

            if (rate == 0)
               rate = ((WaveTrack *) tr)->GetRate();

            if (rate != ((WaveTrack *) tr)->GetRate()) {
               wxMessageBox("Cannot export tracks with different rates.");
               return false;
            }

            numSelected++;

            if (tr->channel == VTrack::LeftChannel)
               numLeft++;
            if (tr->channel == VTrack::RightChannel)
               numRight++;
            if (tr->channel == VTrack::MonoChannel)
               numMono++;
         }
      }

      tr = iter1.Next();
   }

   if (numSelected == 0 && selectionOnly) {
      wxMessageBox("No tracks are selected!\n"
                   "Choose Export... to export all tracks.");
      return false;
   }

   /* Detemine if exported file will be stereo or mono, and if mixing will occur */

   bool stereo = false;
   if (numRight > 0 || numLeft > 0)
      stereo = true;

   numRight += numMono;
   numLeft += numMono;

   if (numLeft > 1 || numRight > 1)
      if (stereo)
         wxMessageBox
             ("Your tracks will be mixed down to two stereo channels "
              "in the exported file.");
      else
         wxMessageBox
             ("Your tracks will be mixed down to a single mono channel "
              "in the exported file.");

   /* Prepare and display the filename selection dialog */

   // account for "AIFF with track markers"
   wxString dlgExt = "." + format.BeforeFirst(' ').Lower();
   wxString defaultName;
   wxString projectName = project->GetName();
   if (projectName.Length() > 4 && projectName.Right(4).Upper()==".AUP")
      defaultName = projectName.Left(projectName.Length()-4)+dlgExt;
   else
      defaultName = projectName + dlgExt;

   wxString path = gPrefs->Read("/DefaultExportPath",::wxGetCwd());

   wxString fName;
   wxString nameOnly;
   wxString pathOnly;
   wxString extension;
   bool fileOkay;

   do {
      fileOkay = true;

      fName = wxFileSelector(wxString::Format("Save %s File As:",
                                              (const char *) format),
                             path,
                             defaultName,   // default file name
                             dlgExt,   // extension
                             "*.*",
                             wxSAVE | wxOVERWRITE_PROMPT);

      if (fName.Length() >= 256) {
         wxMessageBox
            ("Sorry, pathnames longer than 256 characters not supported.");
         return false;
      }
      
      if (fName == "")
         return false;

      ::wxSplitPath(fName, &pathOnly, &nameOnly, &extension);
      gPrefs->Write("/DefaultExportPath", pathOnly);

      if ((nameOnly.Left(1)=="." && extension=="") ||
          (nameOnly=="" && extension!="")) {
         wxString prompt =
            "Are you sure you want to save the file as \""+
            ::wxFileNameFromPath(fName)+"\"?\n";

         int action = wxMessageBox(prompt,
                                   "Warning",
                                   wxYES_NO | wxICON_EXCLAMATION,
                                   project);

         fileOkay = (action == wxYES);
         continue;
      }
      
      /*
       * Check the extension - add the default if it's not there,
       * and warn user if it's abnormal.
       */
      
      wxString defExt = format.BeforeFirst(' ');
      wxString defExt3 = defExt;
      if (defExt.Length() > 3)
         defExt3 = defExt.Left(3);
      
      if (extension == "") {
         #ifdef __WXMSW__
         // Windows prefers 3-char uppercase extensions
         extension = defExt3;
         #else
         // Linux and Mac prefer lowercase extensions
         extension = defExt.Lower();
         #endif
      }
      else if (extension.Upper() != defExt.Upper() &&
               extension.Upper() != defExt3.Upper()) {
         #ifdef __WXMSW__
         // Windows prefers 3-char extensions
         defExt = defExt3;
         #endif

         wxString prompt =
            "You are about to save a "+format+" file with the name \""+
            nameOnly+"."+extension+"\".\n"+
            "Normally these files end in \"."+defExt+"\", and\n"+
            "some programs will not open files with nonstandard "+
            "extensions.\n"
            "Are you sure you want to save the file under this name?";         

         int action = wxMessageBox(prompt,
                                   "Warning",
                                   wxYES_NO | wxICON_EXCLAMATION,
                                   project);

         if (action == wxYES)
            fileOkay = true;
         else {
            fileOkay = false;
            path = pathOnly;
            defaultName = nameOnly + "." + extension;
         }
      }

      fName = pathOnly + wxFILE_SEP_PATH + 
         nameOnly + "." + extension;

   } while (!fileOkay);

   /*
    * Ensure that exporting a file by this name doesn't overwrite
    * one of the existing files in the project.  (If it would
    * overwrite an existing file, DirManager tries to rename the
    * existing file.)
    */

   if (!project->GetDirManager()->EnsureSafeFilename(fName))
      return false;

   /* Finally, dispatch to the correct procedure... 
    * These functions take too many parameters, almost to the point where I
    * am tempted to create a structure to contain this data... */
   if (format == "WAV" ||
       format == "AIFF" ||
       format == "IRCAM" ||
       format == "AU" || format == "AIFF with track markers")
      return ExportPCM(project, format, stereo, fName,
                       selectionOnly, t0, t1);
   else if (format == "MP3")
      return ExportMP3(project, stereo, fName,
                       selectionOnly, t0, t1);

   /* Execution should never reach this point...!
      Return false only so we don't get a compiler warning */
   return false;
}