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< TrackListHoldermStreams
 

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 366 of file ImportOGG.cpp.

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

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
234 auto tracks = trackFactory->CreateMany(vi->channels, int16Sample, vi->rate);
235
236 mStreams.push_back(tracks);
237 }
238
239 /* The number of bytes to get from the codec in each run */
240#define CODEC_TRANSFER_SIZE 4096u
241
242 /* The number of samples to read between calls to the callback.
243 * Balance between responsiveness of the GUI and throughput of import. */
244#define SAMPLES_PER_CALLBACK 100000
245
246 long bytesRead = 0;
247 {
249
250 /* determine endianness (clever trick courtesy of Nicholas Devillard,
251 * (http://www.eso.org/~ndevilla/endian/) */
252 int testvar = 1, endian;
253 if (*(char *)&testvar)
254 endian = 0; // little endian
255 else
256 endian = 1; // big endian
257
258 /* number of samples currently in each channel's buffer */
259 long samplesRead = 0;
260 int bitstream = 0;
261 int samplesSinceLastCallback = 0;
262
263 // You would think that the stream would already be seeked to 0, and
264 // indeed it is if the file is legit. But I had several ogg files on
265 // my hard drive that have malformed headers, and this added call
266 // causes them to be read correctly. Otherwise they have lots of
267 // zeros inserted at the beginning
268 ov_pcm_seek(mVorbisFile.get(), 0);
269
270 do {
271 /* get data from the decoder */
272 bytesRead = ov_read(mVorbisFile.get(), (char *)mainBuffer.get(),
274 endian,
275 2, // word length (2 for 16 bit samples)
276 1, // signed
277 &bitstream);
278
279 if (bytesRead == OV_HOLE) {
280 wxFileName ff(GetFilename());
281 wxLogError(wxT("Ogg Vorbis importer: file %s is malformed, ov_read() reported a hole"),
282 ff.GetFullName());
283 /* http://lists.xiph.org/pipermail/vorbis-dev/2001-February/003223.html
284 * is the justification for doing this - best effort for malformed file,
285 * hence the message.
286 */
287 continue;
288 }
289 else if (bytesRead < 0) {
290 /* Malformed Ogg Vorbis file. */
291 /* TODO: Return some sort of meaningful error. */
292 wxLogError(wxT("Ogg Vorbis importer: ov_read() returned error %i"),
293 bytesRead);
294 break;
295 }
296
297 samplesRead = bytesRead / mVorbisFile->vi[bitstream].channels / sizeof(short);
298
299 if (mStreamUsage[bitstream] != 0)
300 {
301 /* give the data to the wavetracks */
302 unsigned chn = 0;
303 ImportUtils::ForEachChannel(**std::next(mStreams.begin(), bitstream), [&](auto& channel)
304 {
305 channel.AppendBuffer(
306 (char *)(mainBuffer.get() + chn),
307 int16Sample,
308 samplesRead,
309 mVorbisFile->vi[bitstream].channels,
310 int16Sample
311 );
312 ++chn;
313 });
314 }
315
316 samplesSinceLastCallback += samplesRead;
317 if (samplesSinceLastCallback > SAMPLES_PER_CALLBACK) {
318 const auto timeTotal = ov_time_total(mVorbisFile.get(), bitstream);
319 if(timeTotal > 0)
320 progressListener.OnImportProgress(ov_time_tell(mVorbisFile.get()) / timeTotal);
321 samplesSinceLastCallback -= SAMPLES_PER_CALLBACK;
322 }
323 } while (!IsCancelled() && !IsStopped() && bytesRead != 0);
324 }
325
326 if (bytesRead < 0)
327 {
329 return;
330 }
331
332 if(IsCancelled())
333 {
335 return;
336 }
337
338 for (auto& stream : mStreams)
339 {
340 ImportUtils::FinalizeImport(outTracks, std::move(*stream));
341 }
342 mStreams.clear();
343
344 //\todo { Extract comments from each stream? }
345 if (mVorbisFile->vc[0].comments > 0) {
346 tags->Clear();
347 for (int c = 0; c < mVorbisFile->vc[0].comments; c++) {
348 wxString comment = UTF8CTOWX(mVorbisFile->vc[0].user_comments[c]);
349 wxString name = comment.BeforeFirst(wxT('='));
350 wxString value = comment.AfterFirst(wxT('='));
351 if (name.Upper() == wxT("DATE") && !tags->HasTag(TAG_YEAR)) {
352 long val;
353 if (value.length() == 4 && value.ToLong(&val)) {
354 name = TAG_YEAR;
355 }
356 }
357 tags->SetTag(name, value);
358 }
359 }
360
361 progressListener.OnImportResult(IsStopped()
364}
wxT("CloseDown"))
#define SAMPLES_PER_CALLBACK
#define CODEC_TRANSFER_SIZE
#define UTF8CTOWX(X)
Definition: Internat.h:157
#define TAG_YEAR
Definition: Tags.h:62
wxString name
Definition: TagsEditor.cpp:166
const auto tracks
This simplifies arrays of arrays, each array separately allocated with NEW[] But it might be better t...
Definition: MemoryX.h:29
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 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< TrackListHolder > 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
TrackListHolder CreateMany(size_t nChannels)
Creates tracks with project's default rate and format and the given number of channels.
Definition: WaveTrack.cpp:423

References ImportFileHandleEx::BeginImport(), ImportProgressListener::Cancelled, Tags::Clear(), CODEC_TRANSFER_SIZE, WaveTrackFactory::CreateMany(), ImportProgressListener::Error, ImportUtils::FinalizeImport(), ImportUtils::ForEachChannel(), ImportFileHandleEx::GetFilename(), Tags::HasTag(), int16Sample, ImportFileHandleEx::IsCancelled(), ImportFileHandleEx::IsStopped(), mFile, mStreams, mStreamUsage, mVorbisFile, name, ImportProgressListener::OnImportProgress(), ImportProgressListener::OnImportResult(), SAMPLES_PER_CALLBACK, Tags::SetTag(), ImportProgressListener::Stopped, ImportProgressListener::Success, TAG_YEAR, tracks, 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<TrackListHolder> 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: