Audacity 3.2.0
Public Member Functions | Private Attributes | List of all members
OggImportFileHandle Class Referencefinal
Inheritance diagram for OggImportFileHandle:
[legend]
Collaboration diagram for OggImportFileHandle:
[legend]

Public Member Functions

 OggImportFileHandle (const FilePath &filename, std::unique_ptr< wxFFile > &&file, std::unique_ptr< OggVorbis_File > &&vorbisFile)
 
 ~OggImportFileHandle ()
 
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 TranslatableStringsGetStreamInfo () override
 
void SetStreamUsage (wxInt32 StreamID, bool Use) override
 
- Public Member Functions inherited from ImportFileHandleEx
 ImportFileHandleEx (const FilePath &filename)
 
FilePath GetFilename () const override
 
void Cancel () override
 
void Stop () override
 
- Public Member Functions inherited from ImportFileHandle
virtual ~ImportFileHandle ()
 
virtual FilePath GetFilename () const =0
 
virtual TranslatableString GetErrorMessage () const
 
virtual TranslatableString GetFileDescription ()=0
 
virtual ByteCount GetFileUncompressedBytes ()=0
 
virtual wxInt32 GetStreamCount ()=0
 
virtual const TranslatableStringsGetStreamInfo ()=0
 
virtual void SetStreamUsage (wxInt32 StreamID, bool Use)=0
 
virtual void Import (ImportProgressListener &progressListener, WaveTrackFactory *trackFactory, TrackHolders &outTracks, Tags *tags, std::optional< LibFileFormats::AcidizerTags > &acidTags)=0
 
virtual void Cancel ()=0
 
virtual void Stop ()=0
 

Private Attributes

std::unique_ptr< wxFFile > mFile
 
std::unique_ptr< OggVorbis_File > mVorbisFile
 
ArrayOf< int > mStreamUsage
 
TranslatableStrings mStreamInfo
 
std::vector< WaveTrack::HoldermStreams
 

Additional Inherited Members

- Public Types inherited from ImportFileHandle
using ByteCount = unsigned long long
 
- Protected Member Functions inherited from ImportFileHandleEx
void BeginImport ()
 
bool IsCancelled () const noexcept
 
bool IsStopped () const noexcept
 

Detailed Description

Definition at line 73 of file ImportOGG.cpp.

Constructor & Destructor Documentation

◆ OggImportFileHandle()

OggImportFileHandle::OggImportFileHandle ( const FilePath filename,
std::unique_ptr< wxFFile > &&  file,
std::unique_ptr< OggVorbis_File > &&  vorbisFile 
)
inline

Definition at line 76 of file ImportOGG.cpp.

79 : ImportFileHandleEx(filename),
80 mFile(std::move(file)),
81 mVorbisFile(std::move(vorbisFile))
82 , mStreamUsage{ static_cast<size_t>(mVorbisFile->links) }
83 {
84 for (int i = 0; i < mVorbisFile->links; i++)
85 {
86 auto strinfo = XO("Index[%02x] Version[%d], Channels[%d], Rate[%ld]")
87 .Format(
88 (unsigned int) i,
89 mVorbisFile->vi[i].version,
90 mVorbisFile->vi[i].channels,
91 mVorbisFile->vi[i].rate);
92 mStreamInfo.push_back(strinfo);
93 mStreamUsage[i] = 0;
94 }
95
96 }
XO("Cut/Copy/Paste")
ImportFileHandleEx(const FilePath &filename)
std::unique_ptr< wxFFile > mFile
Definition: ImportOGG.cpp:129
ArrayOf< int > mStreamUsage
Definition: ImportOGG.cpp:132
TranslatableStrings mStreamInfo
Definition: ImportOGG.cpp:133
std::unique_ptr< OggVorbis_File > mVorbisFile
Definition: ImportOGG.cpp:130

References mStreamInfo, mStreamUsage, mVorbisFile, and XO().

Here is the call graph for this function:

◆ ~OggImportFileHandle()

OggImportFileHandle::~OggImportFileHandle ( )

Definition at line 364 of file ImportOGG.cpp.

365{
366 ov_clear(mVorbisFile.get());
367 mFile->Detach(); // so that it doesn't try to close the file (ov_clear()
368 // did that already)
369}

References mFile, and mVorbisFile.

Member Function Documentation

◆ GetFileDescription()

TranslatableString OggImportFileHandle::GetFileDescription ( )
overridevirtual

Implements ImportFileHandle.

Definition at line 194 of file ImportOGG.cpp.

195{
196 return DESC;
197}
#define DESC
Definition: ImportOGG.cpp:35

References DESC.

◆ GetFileUncompressedBytes()

auto OggImportFileHandle::GetFileUncompressedBytes ( )
overridevirtual

Implements ImportFileHandle.

Definition at line 199 of file ImportOGG.cpp.

200{
201 // TODO:
202 return 0;
203}

◆ GetStreamCount()

wxInt32 OggImportFileHandle::GetStreamCount ( )
inlineoverridevirtual

Implements ImportFileHandle.

Definition at line 106 of file ImportOGG.cpp.

107 {
108 if (mVorbisFile)
109 return mVorbisFile->links;
110 else
111 return 0;
112 }

References mVorbisFile.

◆ GetStreamInfo()

const TranslatableStrings & OggImportFileHandle::GetStreamInfo ( )
inlineoverridevirtual

Implements ImportFileHandle.

Definition at line 114 of file ImportOGG.cpp.

115 {
116 return mStreamInfo;
117 }

References mStreamInfo.

◆ Import()

void OggImportFileHandle::Import ( ImportProgressListener progressListener,
WaveTrackFactory trackFactory,
TrackHolders outTracks,
Tags tags,
std::optional< LibFileFormats::AcidizerTags > &  outAcidTags 
)
overridevirtual

Implements ImportFileHandle.

Definition at line 205 of file ImportOGG.cpp.

209{
210 BeginImport();
211
212 outTracks.clear();
213
214 wxASSERT(mFile->IsOpened());
215
216 //Number of streams used may be less than mVorbisFile->links,
217 //but this way bitstream matches array index.
218 mStreams.reserve(mVorbisFile->links);
219
220 for(int i = 0; i < mVorbisFile->links; ++i)
221 {
222 //Stream is not used
223 if (mStreamUsage[i] == 0)
224 {
225 //This is just a padding to keep bitstream number and
226 //array indices matched.
227 mStreams.push_back({});
228 continue;
229 }
230
231 vorbis_info *vi = ov_info(mVorbisFile.get(), i);
232
233 // The format agrees with what is always passed to Append() below
235 *trackFactory,
236 vi->channels,
238 vi->rate));
239 }
240
241 /* The number of bytes to get from the codec in each run */
242#define CODEC_TRANSFER_SIZE 4096u
243
244 /* The number of samples to read between calls to the callback.
245 * Balance between responsiveness of the GUI and throughput of import. */
246#define SAMPLES_PER_CALLBACK 100000
247
248 long bytesRead = 0;
249 {
251
252 /* determine endianness (clever trick courtesy of Nicholas Devillard,
253 * (http://www.eso.org/~ndevilla/endian/) */
254 int testvar = 1, endian;
255 if (*(char *)&testvar)
256 endian = 0; // little endian
257 else
258 endian = 1; // big endian
259
260 /* number of samples currently in each channel's buffer */
261 long samplesRead = 0;
262 int bitstream = 0;
263 int samplesSinceLastCallback = 0;
264
265 // You would think that the stream would already be seeked to 0, and
266 // indeed it is if the file is legit. But I had several ogg files on
267 // my hard drive that have malformed headers, and this added call
268 // causes them to be read correctly. Otherwise they have lots of
269 // zeros inserted at the beginning
270 ov_pcm_seek(mVorbisFile.get(), 0);
271
272 do {
273 /* get data from the decoder */
274 bytesRead = ov_read(mVorbisFile.get(), (char *)mainBuffer.get(),
276 endian,
277 2, // word length (2 for 16 bit samples)
278 1, // signed
279 &bitstream);
280
281 if (bytesRead == OV_HOLE) {
282 wxFileName ff(GetFilename());
283 wxLogError(wxT("Ogg Vorbis importer: file %s is malformed, ov_read() reported a hole"),
284 ff.GetFullName());
285 /* http://lists.xiph.org/pipermail/vorbis-dev/2001-February/003223.html
286 * is the justification for doing this - best effort for malformed file,
287 * hence the message.
288 */
289 continue;
290 }
291 else if (bytesRead < 0) {
292 /* Malformed Ogg Vorbis file. */
293 /* TODO: Return some sort of meaningful error. */
294 wxLogError(wxT("Ogg Vorbis importer: ov_read() returned error %i"),
295 bytesRead);
296 break;
297 }
298
299 samplesRead = bytesRead / mVorbisFile->vi[bitstream].channels / sizeof(short);
300
301 if (mStreamUsage[bitstream] != 0)
302 {
303 /* give the data to the wavetracks */
304 unsigned chn = 0;
305 ImportUtils::ForEachChannel(**std::next(mStreams.begin(), bitstream), [&](auto& channel)
306 {
307 channel.AppendBuffer(
308 (char *)(mainBuffer.get() + chn),
309 int16Sample,
310 samplesRead,
311 mVorbisFile->vi[bitstream].channels,
312 int16Sample
313 );
314 ++chn;
315 });
316 }
317
318 samplesSinceLastCallback += samplesRead;
319 if (samplesSinceLastCallback > SAMPLES_PER_CALLBACK) {
320 const auto timeTotal = ov_time_total(mVorbisFile.get(), bitstream);
321 if(timeTotal > 0)
322 progressListener.OnImportProgress(ov_time_tell(mVorbisFile.get()) / timeTotal);
323 samplesSinceLastCallback -= SAMPLES_PER_CALLBACK;
324 }
325 } while (!IsCancelled() && !IsStopped() && bytesRead != 0);
326 }
327
328 if (bytesRead < 0)
329 {
331 return;
332 }
333
334 if(IsCancelled())
335 {
337 return;
338 }
339
341
342 //\todo { Extract comments from each stream? }
343 if (mVorbisFile->vc[0].comments > 0) {
344 tags->Clear();
345 for (int c = 0; c < mVorbisFile->vc[0].comments; c++) {
346 wxString comment = UTF8CTOWX(mVorbisFile->vc[0].user_comments[c]);
347 wxString name = comment.BeforeFirst(wxT('='));
348 wxString value = comment.AfterFirst(wxT('='));
349 if (name.Upper() == wxT("DATE") && !tags->HasTag(TAG_YEAR)) {
350 long val;
351 if (value.length() == 4 && value.ToLong(&val)) {
352 name = TAG_YEAR;
353 }
354 }
355 tags->SetTag(name, value);
356 }
357 }
358
359 progressListener.OnImportResult(IsStopped()
362}
wxT("CloseDown"))
const TranslatableString name
Definition: Distortion.cpp:76
#define SAMPLES_PER_CALLBACK
#define CODEC_TRANSFER_SIZE
#define UTF8CTOWX(X)
Definition: Internat.h:157
#define TAG_YEAR
Definition: Tags.h:62
This simplifies arrays of arrays, each array separately allocated with NEW[] But it might be better t...
Definition: MemoryX.h:28
bool IsStopped() const noexcept
FilePath GetFilename() const override
bool IsCancelled() const noexcept
virtual void OnImportResult(ImportResult result)=0
Used to report on import result for file handle passed as argument to OnImportFileOpened.
virtual void OnImportProgress(double progress)=0
static std::shared_ptr< WaveTrack > NewWaveTrack(WaveTrackFactory &trackFactory, unsigned nChannels, sampleFormat effectiveFormat, double rate)
Definition: ImportUtils.cpp:35
static void ForEachChannel(TrackList &trackList, const std::function< void(WaveChannel &)> &op)
Iterates over channels in each wave track from the list.
Definition: ImportUtils.cpp:73
static void FinalizeImport(TrackHolders &outTracks, const std::vector< std::shared_ptr< WaveTrack > > &importedStreams)
Flushes the given channels and moves them to outTracks.
Definition: ImportUtils.cpp:49
std::vector< WaveTrack::Holder > mStreams
Definition: ImportOGG.cpp:134
void Clear()
Definition: Tags.cpp:293
bool HasTag(const wxString &name) const
Definition: Tags.cpp:397
void SetTag(const wxString &name, const wxString &value, const bool bSpecialTag=false)
Definition: Tags.cpp:431

References ImportFileHandleEx::BeginImport(), ImportProgressListener::Cancelled, Tags::Clear(), CODEC_TRANSFER_SIZE, ImportProgressListener::Error, ImportUtils::FinalizeImport(), ImportUtils::ForEachChannel(), ImportFileHandleEx::GetFilename(), Tags::HasTag(), int16Sample, ImportFileHandleEx::IsCancelled(), ImportFileHandleEx::IsStopped(), mFile, mStreams, mStreamUsage, mVorbisFile, name, ImportUtils::NewWaveTrack(), ImportProgressListener::OnImportProgress(), ImportProgressListener::OnImportResult(), SAMPLES_PER_CALLBACK, Tags::SetTag(), ImportProgressListener::Stopped, ImportProgressListener::Success, TAG_YEAR, UTF8CTOWX, and wxT().

Here is the call graph for this function:

◆ SetStreamUsage()

void OggImportFileHandle::SetStreamUsage ( wxInt32  StreamID,
bool  Use 
)
inlineoverridevirtual

Implements ImportFileHandle.

Definition at line 119 of file ImportOGG.cpp.

120 {
121 if (mVorbisFile)
122 {
123 if (StreamID < mVorbisFile->links)
124 mStreamUsage[StreamID] = (Use ? 1 : 0);
125 }
126 }

References mStreamUsage, and mVorbisFile.

Member Data Documentation

◆ mFile

std::unique_ptr<wxFFile> OggImportFileHandle::mFile
private

Definition at line 129 of file ImportOGG.cpp.

Referenced by Import(), and ~OggImportFileHandle().

◆ mStreamInfo

TranslatableStrings OggImportFileHandle::mStreamInfo
private

Definition at line 133 of file ImportOGG.cpp.

Referenced by GetStreamInfo(), and OggImportFileHandle().

◆ mStreams

std::vector<WaveTrack::Holder> OggImportFileHandle::mStreams
private

Definition at line 134 of file ImportOGG.cpp.

Referenced by Import().

◆ mStreamUsage

ArrayOf<int> OggImportFileHandle::mStreamUsage
private

Definition at line 132 of file ImportOGG.cpp.

Referenced by Import(), OggImportFileHandle(), and SetStreamUsage().

◆ mVorbisFile

std::unique_ptr<OggVorbis_File> OggImportFileHandle::mVorbisFile
private

The documentation for this class was generated from the following file: