File: ImportLOF.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, 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 (536 lines) | stat: -rw-r--r-- 16,753 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/**********************************************************************

  Audacity: A Digital Audio Editor

  ImportLOF.h

  David I. Murray
  Leland Lucius

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

\class LOFImportFileHandle
\brief An ImportFileHandle for LOF data

  Supports the opening of ".lof" files which are text files that contain
  a list of individual files to open in audacity in specific formats. Files may
  be file names (in the same directory as the LOF file), absolute paths or
  relative paths relative to the directory of the LOF file.

  (In BNF) The syntax for an LOF file, denoted by <lof>:

\verbatim
  <lof> ::= [<window> | <file> | <#>]*
  <window> ::= window [<window-parameter>]* <newline>
  <window-parameter> ::= offset <time> | duration <time>
  <time> ::= [<digit>]+ [ . [<digit>]* ]
  <file> ::= file [<file-parameter>]* <newline>
  <file-parameter> ::= offset <time>
  <#> ::= <comment> <newline>
\endverbatim

  EXAMPLE LOF file:

\verbatim
  # everything following the hash character is ignored
  window # an initial window command is implicit and optional
  file "C:\folder1\sample1.wav"    # sample1.wav is displayed
  file "C:\sample2.wav" offset 5   # sample2 is displayed with a 5s offset
  File "C:\sample3.wav"            # sample3 is displayed with no offset
  File "foo.aiff" # foo is loaded from the same directory as the LOF file
  window offset 5 duration 10      # open a NEW window, zoom to display
  # 10 seconds total starting at 5 (ending at 15) seconds
  file "C:\sample3.wav" offset 2.5
\endverbatim

  SEMANTICS:

  There are two commands: "window" creates a NEW window, and "file"
  appends a track to the current window and displays the file there. The
  first file is always placed in a NEW window, whether or not an initial
  "window" command is given.

  Commands have optional keyword parameters that may be listed in any
  order. A parameter should only occur once per command. The "offset"
  parameter specifies a time offset. For windows, this is the leftmost
  time displayed in the window. For files, the offset is an amount by
  which the file is shifted in time before display (only enabled for audio;
  not midi). The offset is specified as an integer or decimal number of
  seconds, and the default value is zero.

  Windows may also have a "duration" parameter, which specifies how much
  time should be displayed in the window. The default duration is equal
  to the duration of the longest track currently displayed.

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

\class LOFImportPlugin
\brief An ImportPlugin for LOF data

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

#include <wx/frame.h>
#include <wx/textfile.h>
#include <wx/tokenzr.h>

#include "FileNames.h"
#include "WaveTrack.h"
#include "ImportPlugin.h"
#include "ImportProgressListener.h"
#include "Import.h"
#include "Project.h"
#include "ProjectHistory.h"
#include "ProjectManager.h"
#include "Viewport.h"
#include "ProjectWindows.h"
#include "ImportUtils.h"

#define BINARY_FILE_CHECK_BUFFER_SIZE 1024

#define DESC XO("List of Files in basic text format")

static const auto exts = {
   wxT("lof")
};

class LOFImportPlugin final : public ImportPlugin
{
public:
   LOFImportPlugin()
   :  ImportPlugin( FileExtensions( exts.begin(), exts.end() ) )
   {
   }

   ~LOFImportPlugin() { }

   wxString GetPluginStringID() override { return wxT("lof"); }
   TranslatableString GetPluginFormatDescription() override;
   std::unique_ptr<ImportFileHandle> Open(
      const FilePath &Filename, AudacityProject *pProject) override;
};


class LOFImportFileHandle final : public ImportFileHandle
{
public:
   LOFImportFileHandle( AudacityProject *pProject,
      const FilePath & name, std::unique_ptr<wxTextFile> &&file);
   ~LOFImportFileHandle();

   TranslatableString GetFileDescription() override;
   ByteCount GetFileUncompressedBytes() override;
   void Import(
      ImportProgressListener& progressListener, WaveTrackFactory* trackFactory,
      TrackHolders& outTracks, Tags* tags,
      std::optional<LibFileFormats::AcidizerTags>& outAcidTags) override;

   FilePath GetFilename() const override;

   void Cancel() override;

   void Stop() override;

   wxInt32 GetStreamCount() override { return 1; }

   const TranslatableStrings &GetStreamInfo() override
   {
      static TranslatableStrings empty;
      return empty;
   }

   void SetStreamUsage(wxInt32 WXUNUSED(StreamID), bool WXUNUSED(Use)) override
   {}

private:
   // Takes a line of text in lof file and interprets it and opens files
   void lofOpenFiles(wxString* ln);
   void doDurationAndScrollOffset();

   std::unique_ptr<wxTextFile> mTextFile;
   const wxFileName mLOFFileName;  /**< The name of the LOF file, which is used to
                                interpret relative paths in it */
   AudacityProject *mProject{};

   // In order to know whether or not to create a NEW window
   int nFilesInGroup{ 0 };

   // In order to zoom in, it must be done after files are opened
   bool              callDurationFactor{ false };
   double            durationFactor{ 1 };

   // In order to offset scrollbar, it must be done after files are opened
   bool              callScrollOffset{ false };
   double            scrollOffset{ 0 };
};

LOFImportFileHandle::LOFImportFileHandle( AudacityProject *pProject,
   const FilePath & name, std::unique_ptr<wxTextFile> &&file)
:  mTextFile(std::move(file))
   , mLOFFileName{name}
   , mProject{ pProject }
{
}

TranslatableString LOFImportPlugin::GetPluginFormatDescription()
{
    return DESC;
}

std::unique_ptr<ImportFileHandle> LOFImportPlugin::Open(
   const FilePath &filename, AudacityProject *pProject)
{
   // Check if it is a text file.
   {
      wxFile binaryFile;
      if (!binaryFile.Open(filename))
         return nullptr; // File not found

      char buf[BINARY_FILE_CHECK_BUFFER_SIZE];
      int count = binaryFile.Read(buf, BINARY_FILE_CHECK_BUFFER_SIZE);

      bool isTextFile = false;
      const std::string lofToken("file");

      // At least we should get a size more than: <token> + <space> + <filename>.
      if (count > (lofToken.length() + sizeof(' ') + 1))
      {
          // Audacity can import list from LOF only with BOM or ASCII,
          // each other text (like unicode without BOM, bin, etc) can't be recognized.

          // UTF-16 BOM checker.
          auto IsUtf16_BE = [](const char* str) -> bool
          {
              return str[0] == static_cast<char>(0xFE) && str[1] == static_cast<char>(0xFF);
          };
          auto IsUtf16_LE = [](const char* str) -> bool
          {
              return str[0] == static_cast<char>(0xFF) && str[1] == static_cast<char>(0xFE);
          };

          // UTF-32 BOM checker.
          auto IsUtf32_BE = [](const char* str) -> bool
          {
              return str[0] == static_cast<char>(0x00) &&
                     str[1] == static_cast<char>(0x00) &&
                     str[2] == static_cast<char>(0xFE) &&
                     str[3] == static_cast<char>(0xFF);
          };
          auto IsUtf32_LE = [](const char* str) -> bool
          {
              return str[0] == static_cast<char>(0xFF) &&
                     str[1] == static_cast<char>(0xFE) &&
                     str[2] == static_cast<char>(0x00) &&
                     str[3] == static_cast<char>(0x00);
          };

          // Is unicode text file.
          if (IsUtf16_BE(buf) || IsUtf16_LE(buf) || IsUtf32_BE(buf) || IsUtf32_LE(buf))
          {
              isTextFile = true;
          }
          // Try parse as ASCII and UTF-8 text as ASCII too.
          else
          {
              buf[sizeof(buf) - 1] = '\0';

              std::string importedText(buf);

              if (importedText.find(lofToken) != std::string::npos)
                  isTextFile = true;
          }
      }

      if (!isTextFile)
      {
          binaryFile.Close();
          return nullptr;
      }
   }

   // Now open the file again as text file
   auto file = std::make_unique<wxTextFile>(filename);
   file->Open();

   if (!file->IsOpened())
      return nullptr;

   return std::make_unique<LOFImportFileHandle>(
      pProject, filename, std::move(file));
}

TranslatableString LOFImportFileHandle::GetFileDescription()
{
   return DESC;
}

auto LOFImportFileHandle::GetFileUncompressedBytes() -> ByteCount
{
   return 0;
}

void LOFImportFileHandle::Import(
   ImportProgressListener& progressListener, WaveTrackFactory*,
   TrackHolders& outTracks, Tags*, std::optional<LibFileFormats::AcidizerTags>&)
{
   // Unlike other ImportFileHandle subclasses, this one never gives any tracks
   // back to the caller.
   // Instead, it recursively calls AudacityProject::Import for each file listed
   // in the .lof file.
   // Each importation creates a NEW undo state.
   // If there is an error or exception during one of them, only that one's
   // side effects are rolled back, and the rest of the import list is skipped.
   // The file may have "window" directives that cause NEW AudacityProjects
   // to be created, and the undo states are pushed onto the latest project.
   // If a project is created but the first file import into it fails, destroy
   // the project.

   outTracks.clear();

   wxASSERT(mTextFile->IsOpened());

   if(mTextFile->Eof())
   {
      mTextFile->Close();
      progressListener.OnImportResult(ImportProgressListener::ImportResult::Error);
      return;
   }

   wxString line = mTextFile->GetFirstLine();

   while (!mTextFile->Eof())
   {
      lofOpenFiles(&line);
      line = mTextFile->GetNextLine();
   }

   // for last line
   lofOpenFiles(&line);

   if(!mTextFile->Close())
   {
      progressListener.OnImportResult(ImportProgressListener::ImportResult::Error);
      return;
   }
   // set any duration/offset factors for last window, as all files were called
   doDurationAndScrollOffset();
   progressListener.OnImportResult(ImportProgressListener::ImportResult::Success);
}

FilePath LOFImportFileHandle::GetFilename() const
{
   return mLOFFileName.GetFullPath();
}

void LOFImportFileHandle::Cancel()
{
   //LOFImport delegates import to other plugins
}

void LOFImportFileHandle::Stop()
{
   //LOFImport delegates import to other plugins
}

static Importer::RegisteredImportPlugin registered{ "LOF",
   std::make_unique< LOFImportPlugin >()
};

/** @brief Processes a single line from a LOF text file, doing whatever is
 * indicated on the line.
 *
 * This function should just return for lines it cannot deal with, and the
 * caller will continue to the next line of the input file
 */
void LOFImportFileHandle::lofOpenFiles(wxString* ln)
{
   wxStringTokenizer tok(*ln, wxT(" "));
   wxStringTokenizer temptok1(*ln, wxT("\""));
   wxStringTokenizer temptok2(*ln, wxT(" "));
   int tokenplace = 0;

   wxString targetfile;
   wxString tokenholder = tok.GetNextToken();


   if (tokenholder.IsSameAs(wxT("window"), false))
   {
      // set any duration/offset factors for last window, as all files were called
      doDurationAndScrollOffset();

      if (nFilesInGroup > 0 )
         // Cause a project to be created with the next import
         mProject = nullptr;

      nFilesInGroup = 0;

      while (tok.HasMoreTokens())
      {
         tokenholder = tok.GetNextToken();

         if (tokenholder.IsSameAs(wxT("offset"), false))
         {
            if (tok.HasMoreTokens())
               tokenholder = tok.GetNextToken();

            if (Internat::CompatibleToDouble(tokenholder, &scrollOffset))
            {
               callScrollOffset = true;
            }
            else
            {
               ImportUtils::ShowMessageBox(
                  /* i18n-hint: You do not need to translate "LOF" */
                  XO("Invalid window offset in LOF file."));
            }

            if (tok.HasMoreTokens())
               tokenholder = tok.GetNextToken();
         }

         if (tokenholder.IsSameAs(wxT("duration"), false))
         {
            if (tok.HasMoreTokens())
               tokenholder = tok.GetNextToken();

            if (Internat::CompatibleToDouble(tokenholder, &durationFactor))
            {
               callDurationFactor = true;
            }
            else
            {
               ImportUtils::ShowMessageBox(
                  /* i18n-hint: You do not need to translate "LOF" */
                  XO("Invalid duration in LOF file."));
            }
         }     // End if statement

         if (tokenholder == wxT("#"))
         {
            // # indicates comments; ignore line
            tok = wxStringTokenizer(wxT(""), wxT(" "));
         }
      }     // End while loop
   }        // End if statement handling "window" lines

   else if (tokenholder.IsSameAs(wxT("file"), false))
   {
      nFilesInGroup++;
      // To identify filename and open it
      tokenholder = temptok1.GetNextToken();
      wxString targettoken = temptok1.GetNextToken();
      targetfile = targettoken;

      // If path is relative, make absolute path from LOF path
      if(!wxIsAbsolutePath(targetfile)) {
         wxFileName fName(targetfile);
         fName.Normalize(wxPATH_NORM_ALL, mLOFFileName.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR));
         if(fName.FileExists()) {
            targetfile = fName.GetFullPath();
         }
      }

      // Do recursive call to import
      mProject = ProjectManager::OpenProject( mProject, targetfile,
         true /* addtohistory */, true /* reuseNonemptyProject */ );

      // Set tok to right after filename
      temptok2.SetString(targettoken);
      tokenplace = temptok2.CountTokens();

      for (int i = 0; i < tokenplace; i++)
         tokenholder = tok.GetNextToken();

      if (tok.HasMoreTokens())
      {
         tokenholder = tok.GetNextToken();

         if (tokenholder == wxT("#"))
         {
            // # indicates comments; ignore line
            tok = wxStringTokenizer(wxT(""), wxT(" "));
         }

         if (tokenholder.IsSameAs(wxT("offset"), false))
         {
            if (tok.HasMoreTokens())
               tokenholder = tok.GetNextToken();
            double offset;

            // handle an "offset" specifier
            if (!mProject)
               // there was an import error,
               // presumably with its own error message
               ;
            else if (Internat::CompatibleToDouble(tokenholder, &offset))
            {
               auto &tracks = TrackList::Get( *mProject );
               auto t = *tracks.rbegin();

               // t is now the last track in the project, unless the import of
               // all tracks failed, in which case it will be null. In that
               // case we return because we cannot offset a non-existent track.
               if (t == NULL)
                  return;
#ifdef USE_MIDI
               if (targetfile.AfterLast(wxT('.')).IsSameAs(wxT("mid"), false) ||
                   targetfile.AfterLast(wxT('.')).IsSameAs(wxT("midi"), false))
               {
                  ImportUtils::ShowMessageBox(XO("MIDI tracks cannot be offset individually, only audio files can be."));
               }
               else
#endif
               t->MoveTo(offset);

               // Amend the undo transaction made by import
               ProjectHistory::Get( *mProject ).ModifyState(false);
            } // end of converting "offset" argument
            else
            {
               ImportUtils::ShowMessageBox(
                  /* i18n-hint: You do not need to translate "LOF" */
                  XO("Invalid track offset in LOF file."));
            }
         }     // End if statement for "offset" parameters
      }     // End if statement (more tokens after file name)
   }     // End if statement "file" lines

   else if (tokenholder == wxT("#"))
   {
      // # indicates comments; ignore line
      tok = wxStringTokenizer(wxT(""), wxT(" "));
   }
   else
   {
      // Couldn't parse a line
   }
}

void LOFImportFileHandle::doDurationAndScrollOffset()
{
   if (!mProject)
      return;

   callScrollOffset = callScrollOffset && (scrollOffset != 0);
   bool doSomething = callDurationFactor || callScrollOffset;

   if (callDurationFactor)
   {
      double longestDuration = TrackList::Get(*mProject).GetEndTime();
      Viewport::Get(*mProject).ZoomBy(longestDuration / durationFactor);
      callDurationFactor = false;
   }

   if (callScrollOffset)
   {
      Viewport::Get(*mProject).SetHorizontalThumb(scrollOffset);
      callScrollOffset = false;
   }

   if (doSomething)
      // Amend last undo state
      ProjectHistory::Get( *mProject ).ModifyState(false);
}

LOFImportFileHandle::~LOFImportFileHandle()
{
}