File: ImportOpus.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 (421 lines) | stat: -rw-r--r-- 11,553 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
/**********************************************************************

   SPDX-License-Identifier: GPL-2.0-or-later

   Audacity: A Digital Audio Editor

   ImportOpus.cpp

   Dmitry Vedenko

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


#include "Import.h"
#include "ImportPlugin.h"

#include <string_view>

#include<wx/string.h>
#include<wx/log.h>

#include<stdlib.h>

#include "Tags.h"
#include "WaveTrack.h"
#include "CodeConversions.h"
#include "ImportUtils.h"
#include "ImportProgressListener.h"
#include "CodeConversions.h"

#include <opus/opusfile.h>

#define DESC XO("Opus files")

static const auto exts = { L"opus", L"ogg" };

class OpusImportPlugin final : public ImportPlugin
{
public:
   OpusImportPlugin();
   ~OpusImportPlugin();

   wxString GetPluginStringID() override;
   TranslatableString GetPluginFormatDescription() override;
   std::unique_ptr<ImportFileHandle> Open(
     const FilePath &Filename, AudacityProject*) override;
};

class OpusImportFileHandle final : public ImportFileHandleEx
{
public:
   explicit OpusImportFileHandle(const FilePath& filename);
   ~OpusImportFileHandle();

   bool IsOpen() const;

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

   wxInt32 GetStreamCount() override;
   const TranslatableStrings &GetStreamInfo() override;
   void SetStreamUsage(wxInt32 StreamID, bool Use) override;

private:
   static int OpusReadCallback(void* stream, unsigned char* ptr, int nbytes);
   static int OpusSeekCallback(void* stream, opus_int64 offset, int whence);
   static opus_int64 OpusTellCallback(void* stream);
   static int OpusCloseCallback(void* stream);

   static TranslatableString GetOpusErrorString(int error);
   void LogOpusError(const char* method, int error);
   void NotifyImportFailed(ImportProgressListener& progressListener, int error);
   void NotifyImportFailed(ImportProgressListener& progressListener, const TranslatableString& error);

   wxFile mFile;

   OpusFileCallbacks mCallbacks;
   OggOpusFile* mOpusFile {};
   int mNumChannels {};
   int64_t mNumSamples {};

   // Opus internally uses 48kHz sample rate
   // The file header contains the sample rate of the original audio.
   // We ignore it and let Audacity resample the audio to the project sample rate.
   const double mSampleRate { 48000.0 };

   // Opus decodes to float samples internally, optionally converting them to int16.
   // We let Audacity to convert the stream to the project sample format.
   const sampleFormat mFormat { floatSample };
};

// ============================================================================
// OpusImportPlugin
// ============================================================================

OpusImportPlugin::OpusImportPlugin()
:  ImportPlugin(FileExtensions(exts.begin(), exts.end()))
{
}

OpusImportPlugin::~OpusImportPlugin()
{
}

wxString OpusImportPlugin::GetPluginStringID()
{
   return wxT("libopus");
}

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

std::unique_ptr<ImportFileHandle> OpusImportPlugin::Open(const FilePath &filename, AudacityProject*)
{
   auto handle = std::make_unique<OpusImportFileHandle>(filename);

   if (!handle->IsOpen())
      return {};

   return std::move(handle);
}

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

// ============================================================================
// OpusImportFileHandle
// ============================================================================

OpusImportFileHandle::OpusImportFileHandle(const FilePath& filename)
    : ImportFileHandleEx { filename }
{
   // Try to open the file for reading
   if (!mFile.Open(filename, wxFile::read))
      return;

   OpusFileCallbacks callbacks = {
      OpusReadCallback,
      OpusSeekCallback,
      OpusTellCallback,
      OpusCloseCallback
   };

   int error = 0;
   mOpusFile = op_open_callbacks(this, &callbacks, nullptr, 0, &error);

   if (mOpusFile == nullptr)
   {
      LogOpusError("Error while opening Opus file", error);
      return;
   }

   mNumChannels = op_channel_count(mOpusFile, -1);
   mNumSamples = op_pcm_total(mOpusFile, -1);
}

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

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

void OpusImportFileHandle::Import(
   ImportProgressListener& progressListener, WaveTrackFactory* trackFactory,
   TrackHolders& outTracks, Tags* tags,
   std::optional<LibFileFormats::AcidizerTags>&)
{
   BeginImport();

   outTracks.clear();

   auto track = ImportUtils::NewWaveTrack(
      *trackFactory,
      mNumChannels,
      mFormat,
      mSampleRate);

   /* The number of samples to read in each loop */
   const size_t SAMPLES_TO_READ = track->GetMaxBlockSize();
   uint64_t totalSamplesRead = 0;

   const auto bufferSize = mNumChannels * SAMPLES_TO_READ;

   ArrayOf<float> floatBuffer { bufferSize };

   uint64_t samplesRead = 0;

   do
   {
      int linkIndex { -1 };
      auto samplesPerChannelRead = op_read_float(mOpusFile, floatBuffer.get(), SAMPLES_TO_READ, &linkIndex);

      if (samplesPerChannelRead < 0 && samplesPerChannelRead != OP_HOLE)
      {
         NotifyImportFailed(progressListener, samplesPerChannelRead);
         return;
      }

      auto linkChannels = op_head(mOpusFile, linkIndex)->channel_count;

      if (linkChannels != mNumChannels)
      {
         NotifyImportFailed(progressListener, XO("File has changed the number of channels in the middle."));
         return;
      }

      unsigned chn = 0;
      ImportUtils::ForEachChannel(*track, [&](auto& channel)
      {
         channel.AppendBuffer(
            reinterpret_cast<constSamplePtr>(floatBuffer.get() +
            chn), mFormat, samplesPerChannelRead, mNumChannels, mFormat
         );
         ++chn;
      });

      samplesRead = samplesPerChannelRead;
      totalSamplesRead += samplesRead;

      progressListener.OnImportProgress(double(totalSamplesRead) / mNumSamples);
   } while (!IsCancelled() && !IsStopped() && samplesRead != 0);

   if (IsCancelled())
   {
      progressListener.OnImportResult(
         ImportProgressListener::ImportResult::Cancelled);
      return;
   }

   if (totalSamplesRead < mNumSamples && !IsStopped())
   {
      progressListener.OnImportResult(ImportProgressListener::ImportResult::Error);
      return;
   }

   ImportUtils::FinalizeImport(outTracks, *track);

   auto opusTags = op_tags(mOpusFile, -1);

   if (opusTags != nullptr)
   {
         for (int i = 0; i < opusTags->comments; ++i)
         {
            const auto comment = opusTags->user_comments[i];
            const auto commentLength = opusTags->comment_lengths[i];

            std::string_view tag { comment,
                                   std::string_view::size_type(commentLength) };

            const auto separator = tag.find('=');

            if (separator != std::string_view::npos)
            {
               auto name = audacity::ToWXString(tag.substr(0, separator));
               const auto value = audacity::ToWXString(tag.substr(separator + 1));

               // See: ImportOGG.cpp tags parsing
               if (name.Upper() == wxT("DATE") && !tags->HasTag(TAG_YEAR))
               {
                  long val;

                  if (value.length() == 4 && value.ToLong(&val))
                     name = TAG_YEAR;
               }

               tags->SetTag(name, value);
            }
         }
   }

   progressListener.OnImportResult(IsStopped()
                                   ? ImportProgressListener::ImportResult::Stopped
                                   : ImportProgressListener::ImportResult::Success);
}

wxInt32 OpusImportFileHandle::GetStreamCount()
{
   return 1;
}

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

void OpusImportFileHandle::SetStreamUsage(wxInt32, bool)
{
}

int OpusImportFileHandle::OpusReadCallback(
   void* pstream, unsigned char* ptr, int nbytes)
{
   auto stream = static_cast<OpusImportFileHandle*>(pstream);

   if (!stream->mFile.IsOpened())
      return EOF;

   // OpusFile never reads more than 2^31 bytes at a time,
   // so we can safely cast ssize_t to int.
   return int(stream->mFile.Read(ptr, nbytes));
}

int OpusImportFileHandle::OpusSeekCallback(
   void* pstream, opus_int64 offset, int whence)
{
   auto stream = static_cast<OpusImportFileHandle*>(pstream);

   if (!stream->mFile.IsOpened())
      return -1;

   wxSeekMode wxWhence = whence == SEEK_SET ? wxFromStart :
                         whence == SEEK_CUR ? wxFromCurrent :
                         whence == SEEK_END ? wxFromEnd : wxFromStart;

   return stream->mFile.Seek(offset, wxWhence) != wxInvalidOffset ? 0 : -1;
}

opus_int64 OpusImportFileHandle::OpusTellCallback(void* pstream)
{
   auto stream = static_cast<OpusImportFileHandle*>(pstream);

   return static_cast<opus_int64>(stream->mFile.Tell());
}

int OpusImportFileHandle::OpusCloseCallback(void* pstream)
{
   auto stream = static_cast<OpusImportFileHandle*>(pstream);

   if (stream->mFile.IsOpened())
      return stream->mFile.Close() ? 0 : EOF;

   return 0;
}

TranslatableString OpusImportFileHandle::GetOpusErrorString (int error)
{
   switch (error)
   {
   case OP_EREAD:
      return XO("IO error reading from file");
   case OP_EFAULT:
      return XO("internal error");
   case OP_EIMPL:
      return XO("not implemented");
   case OP_EINVAL:
      return XO("invalid argument");
   case OP_ENOTFORMAT:
      return XO("not an Opus file");
   case OP_EBADHEADER:
      return XO("invalid header");
   case OP_EVERSION:
      return XO("unsupported version");
   case OP_EBADPACKET:
      return XO("invalid packet");
   case OP_EBADLINK:
      return XO("invalid stream structure");
   case OP_ENOSEEK:
      return XO("stream is not seekable");
   case OP_EBADTIMESTAMP:
      return XO("invalid timestamp");
   default:
      return {};
   }
}

void OpusImportFileHandle::LogOpusError(const char* method, int error)
{
   if (error == 0)
      return;

   if (error == OP_ENOTFORMAT)
      wxLogDebug("%s: Not Opus format", GetOpusErrorString(error).Translation());
   else
      wxLogError("%s: %s", method, GetOpusErrorString(error).Translation());
}

void OpusImportFileHandle::NotifyImportFailed(
   ImportProgressListener& progressListener, int error)
{
   NotifyImportFailed(progressListener, GetOpusErrorString(error));
}

void OpusImportFileHandle::NotifyImportFailed(
   ImportProgressListener& progressListener, const TranslatableString& error)
{
   ImportUtils::ShowMessageBox(
      XO("Failed to decode Opus file: %s").Format(error));

   if (IsCancelled())
      progressListener.OnImportResult(
         ImportProgressListener::ImportResult::Cancelled);
   else if (!IsStopped())
      progressListener.OnImportResult(
         ImportProgressListener::ImportResult::Error);
   else
      progressListener.OnImportResult(
         ImportProgressListener::ImportResult::Stopped);
}

bool OpusImportFileHandle::IsOpen() const
{
   return mOpusFile != nullptr;
}

OpusImportFileHandle::~OpusImportFileHandle()
{
   if (mOpusFile != nullptr)
      op_free(mOpusFile);
}