Audacity  2.2.2
Functions
Legacy.cpp File Reference

Converts old Audacity file types. Implements AutoRollbackRenamer. More...

#include "Audacity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wx/defs.h>
#include <wx/ffile.h>
#include <wx/filefn.h>
#include <wx/intl.h>
#include <wx/string.h>
#include <wx/textfile.h>
#include "Internat.h"
#include "Legacy.h"
#include "widgets/ErrorDialog.h"
#include "xml/XMLWriter.h"

Go to the source code of this file.

Functions

static bool ConvertLegacyTrack (wxTextFile *f, XMLFileWriter &xmlFile)
 
bool ConvertLegacyProjectFile (const wxFileName &filename)
 

Detailed Description

Converts old Audacity file types. Implements AutoRollbackRenamer.

These routines convert Audacity project files from the 0.98...1.0 format into an XML format that's compatible with Audacity 1.2.0 and newer.

Definition in file Legacy.cpp.

Function Documentation

bool ConvertLegacyProjectFile ( const wxFileName &  filename)

Definition at line 253 of file Legacy.cpp.

References _(), AUDACITY_VERSION_STRING, AudacityMessageBox(), ConvertLegacyTrack(), label, and name.

Referenced by AudacityProject::OpenFile().

254 {
255  wxTextFile f;
256 
257  const wxString name = filename.GetFullPath();
258  f.Open( name );
259  if (!f.IsOpened())
260  return false;
261 
262  return GuardedCall< bool >( [&] {
263  XMLFileWriter xmlFile{ name, _("Error Converting Legacy Project File") };
264 
265  xmlFile.Write(wxT("<?xml version=\"1.0\"?>\n"));
266 
267  wxString label;
268  wxString value;
269 
270  if (f.GetFirstLine() != wxT("AudacityProject"))
271  return false;
272  if (f.GetNextLine() != wxT("Version"))
273  return false;
274  if (f.GetNextLine() != wxT("0.95"))
275  return false;
276  if (f.GetNextLine() != wxT("projName"))
277  return false;
278 
279  xmlFile.StartTag(wxT("audacityproject"));
280  xmlFile.WriteAttr(wxT("projname"), f.GetNextLine());
281  xmlFile.WriteAttr(wxT("version"), wxT("1.1.0"));
282  xmlFile.WriteAttr(wxT("audacityversion"),AUDACITY_VERSION_STRING);
283 
284  label = f.GetNextLine();
285  while (label != wxT("BeginTracks")) {
286  xmlFile.WriteAttr(label, f.GetNextLine());
287  label = f.GetNextLine();
288  }
289 
290  label = f.GetNextLine();
291  while (label != wxT("EndTracks")) {
292  bool success = ConvertLegacyTrack(&f, xmlFile);
293  if (!success)
294  return false;
295  label = f.GetNextLine();
296  }
297 
298  // Close original before Commit() tries to overwrite it.
299  f.Close();
300 
301  xmlFile.EndTag(wxT("audacityproject"));
302  xmlFile.Commit();
303 
304  ::AudacityMessageBox(wxString::Format(_("Converted a 1.0 project file to the new format.\nThe old file has been saved as '%s'"), xmlFile.GetBackupName()),
305  _("Opening Audacity Project"));
306 
307  return true;
308  } );
309 }
#define AUDACITY_VERSION_STRING
Definition: Audacity.h:81
wxString label
Definition: Tags.cpp:727
int AudacityMessageBox(const wxString &message, const wxString &caption=AudacityMessageBoxCaptionStr(), long style=wxOK|wxCENTRE, wxWindow *parent=NULL, int x=wxDefaultCoord, int y=wxDefaultCoord)
Definition: ErrorDialog.h:92
Wrapper to output XML data to files.
Definition: XMLWriter.h:74
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
const wxChar * name
Definition: Distortion.cpp:94
static bool ConvertLegacyTrack(wxTextFile *f, XMLFileWriter &xmlFile)
Definition: Legacy.cpp:47
static bool ConvertLegacyTrack ( wxTextFile *  f,
XMLFileWriter xmlFile 
)
static

Definition at line 47 of file Legacy.cpp.

References XMLWriter::EndTag(), name, XMLWriter::StartTag(), and XMLWriter::WriteAttr().

Referenced by ConvertLegacyProjectFile().

49 {
50  wxString line;
51  wxString kind;
52 
53  kind = (*f)[f->GetCurrentLine()];
54 
55  if (kind == wxT("WaveTrack")) {
56  xmlFile.StartTag(wxT("wavetrack"));
57  xmlFile.WriteAttr(wxT("name"), f->GetNextLine());
58 
59  wxString channel = f->GetNextLine();
60  if (channel == wxT("left")) {
61  xmlFile.WriteAttr(wxT("channel"), 0);
62  line = f->GetNextLine();
63  }
64  else if (channel == wxT("right")) {
65  xmlFile.WriteAttr(wxT("channel"), 1);
66  line = f->GetNextLine();
67  }
68  else if (channel == wxT("mono")) {
69  xmlFile.WriteAttr(wxT("channel"), 2);
70  line = f->GetNextLine();
71  }
72  else {
73  xmlFile.WriteAttr(wxT("channel"), 2);
74  line = channel;
75  }
76 
77  if (line == wxT("linked")) {
78  xmlFile.WriteAttr(wxT("linked"), 1);
79  line = f->GetNextLine();
80  }
81 
82  if (line != wxT("offset"))
83  return false;
84  xmlFile.WriteAttr(wxT("offset"), f->GetNextLine());
85 
86  long envLen;
87 
88  if (f->GetNextLine() != wxT("EnvNumPoints"))
89  return false;
90  line = f->GetNextLine();
91  line.ToLong(&envLen);
92  if (envLen < 0 || envLen > 10000)
93  return false;
94 
95  size_t envStart = f->GetCurrentLine();
96  if (f->GetLineCount() < envStart+(2*envLen)+1)
97  return false;
98 
99  f->GoToLine(envStart+(2*envLen));
100  if (f->GetNextLine() != wxT("EnvEnd"))
101  return false;
102  if (f->GetNextLine() != wxT("numSamples"))
103  return false;
104 
105  wxString numSamples = f->GetNextLine();
106 
107  if (f->GetNextLine() != wxT("rate"))
108  return false;
109 
110  xmlFile.WriteAttr(wxT("rate"), f->GetNextLine());
111 
112  if (envLen > 0) {
113  xmlFile.StartTag(wxT("envelope"));
114  xmlFile.WriteAttr(wxT("numpoints"), envLen);
115 
116  long i;
117  for(i=0; i<envLen; i++) {
118  xmlFile.StartTag(wxT("controlpoint"));
119  xmlFile.WriteAttr(wxT("t"), f->GetLine(envStart + 2*i + 1));
120  xmlFile.WriteAttr(wxT("val"), f->GetLine(envStart + 2*i + 2));
121  xmlFile.EndTag(wxT("controlpoint"));
122  }
123 
124  xmlFile.EndTag(wxT("envelope"));
125  }
126 
127  if (f->GetNextLine() != wxT("numBlocks"))
128  return false;
129  long numBlocks;
130  line = f->GetNextLine();
131  line.ToLong(&numBlocks);
132 
133  if (numBlocks < 0 || numBlocks > 131072)
134  return false;
135 
136  xmlFile.StartTag(wxT("sequence"));
137  xmlFile.WriteAttr(wxT("maxsamples"), 524288);
138  xmlFile.WriteAttr(wxT("sampleformat"), 131073);
139  xmlFile.WriteAttr(wxT("numsamples"), numSamples);
140 
141  long b;
142  for(b=0; b<numBlocks; b++) {
143  wxString start;
144  wxString len;
145  wxString name;
146 
147  if (f->GetNextLine() != wxT("Block start"))
148  return false;
149  start = f->GetNextLine();
150  if (f->GetNextLine() != wxT("Block len"))
151  return false;
152  len = f->GetNextLine();
153  if (f->GetNextLine() != wxT("Block info"))
154  return false;
155  name = f->GetNextLine();
156 
157  xmlFile.StartTag(wxT("waveblock"));
158  xmlFile.WriteAttr(wxT("start"), start);
159 
160  xmlFile.StartTag(wxT("legacyblockfile"));
161  if (name == wxT("Alias")) {
162  wxString aliasPath = f->GetNextLine();
163  wxString localLen = f->GetNextLine();
164  wxString aliasStart = f->GetNextLine();
165  wxString aliasLen = f->GetNextLine();
166  wxString aliasChannel = f->GetNextLine();
167  wxString localName = f->GetNextLine();
168 
169  xmlFile.WriteAttr(wxT("name"), localName);
170  xmlFile.WriteAttr(wxT("alias"), 1);
171  xmlFile.WriteAttr(wxT("aliaspath"), aliasPath);
172 
173  // This was written but not read again?
174  xmlFile.WriteAttr(wxT("aliasstart"), aliasStart);
175 
176  xmlFile.WriteAttr(wxT("aliaslen"), aliasLen);
177  xmlFile.WriteAttr(wxT("aliaschannel"), aliasChannel);
178  xmlFile.WriteAttr(wxT("summarylen"), localLen);
179  xmlFile.WriteAttr(wxT("norms"), 1);
180  }
181  else {
182  xmlFile.WriteAttr(wxT("name"), name);
183  xmlFile.WriteAttr(wxT("len"), len);
184  xmlFile.WriteAttr(wxT("summarylen"), 8244);
185  xmlFile.WriteAttr(wxT("norms"), 1);
186  }
187  xmlFile.EndTag(wxT("legacyblockfile"));
188 
189  xmlFile.EndTag(wxT("waveblock"));
190  }
191 
192  xmlFile.EndTag(wxT("sequence"));
193  xmlFile.EndTag(wxT("wavetrack"));
194 
195  return true;
196  }
197  else if (kind == wxT("LabelTrack")) {
198  line = f->GetNextLine();
199  if (line != wxT("NumMLabels"))
200  return false;
201 
202  long numLabels, l;
203 
204  line = f->GetNextLine();
205  line.ToLong(&numLabels);
206  if (numLabels < 0 || numLabels > 1000000)
207  return false;
208 
209  xmlFile.StartTag(wxT("labeltrack"));
210  xmlFile.WriteAttr(wxT("name"), wxT("Labels"));
211  xmlFile.WriteAttr(wxT("numlabels"), numLabels);
212 
213  for(l=0; l<numLabels; l++) {
214  wxString t, title;
215 
216  t = f->GetNextLine();
217  title = f->GetNextLine();
218 
219  xmlFile.StartTag(wxT("label"));
220  xmlFile.WriteAttr(wxT("t"), t);
221  xmlFile.WriteAttr(wxT("title"), title);
222  xmlFile.EndTag(wxT("label"));
223  }
224 
225  xmlFile.EndTag(wxT("labeltrack"));
226 
227  line = f->GetNextLine();
228  if (line != wxT("MLabelsEnd"))
229  return false;
230 
231  return true;
232  }
233  else if (kind == wxT("NoteTrack")) {
234  // Just skip over it - they didn't even work in version 1.0!
235 
236  do {
237  line = f->GetNextLine();
238  if (line == wxT("WaveTrack") ||
239  line == wxT("NoteTrack") ||
240  line == wxT("LabelTrack") ||
241  line == wxT("EndTracks")) {
242  f->GoToLine(f->GetCurrentLine()-1);
243  return true;
244  }
245  } while (f->GetCurrentLine() < f->GetLineCount());
246 
247  return false;
248  }
249  else
250  return false;
251 }
virtual void StartTag(const wxString &name)
Definition: XMLWriter.cpp:78
virtual void WriteAttr(const wxString &name, const wxString &value)
Definition: XMLWriter.cpp:131
virtual void EndTag(const wxString &name)
Definition: XMLWriter.cpp:101
const wxChar * name
Definition: Distortion.cpp:94