Audacity 3.2.0
Public Member Functions | Private Attributes | List of all members
WavPackImportFileHandle Class Referencefinal

An ImportFileHandle for WavPack data. More...

Inheritance diagram for WavPackImportFileHandle:
[legend]
Collaboration diagram for WavPackImportFileHandle:
[legend]

Public Member Functions

 WavPackImportFileHandle (const FilePath &filename, WavpackContext *wavpackContext)
 
 ~WavPackImportFileHandle ()
 
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

WavpackContext * mWavPackContext
 
int mNumChannels
 
uint32_t mSampleRate
 
int mBitsPerSample
 
int mBytesPerSample
 
int64_t mNumSamples
 
sampleFormat mFormat
 

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

An ImportFileHandle for WavPack data.

Definition at line 55 of file ImportWavPack.cpp.

Constructor & Destructor Documentation

◆ WavPackImportFileHandle()

WavPackImportFileHandle::WavPackImportFileHandle ( const FilePath filename,
WavpackContext *  wavpackContext 
)

Definition at line 131 of file ImportWavPack.cpp.

133: ImportFileHandleEx(filename),
134 mWavPackContext(wavpackContext),
135 mNumChannels(WavpackGetNumChannels(mWavPackContext)),
136 mSampleRate(WavpackGetSampleRate(mWavPackContext)),
137 mBitsPerSample(WavpackGetBitsPerSample(mWavPackContext)),
138 mBytesPerSample(WavpackGetBytesPerSample(mWavPackContext)),
139 mNumSamples(WavpackGetNumSamples64(mWavPackContext))
140{
141 if (mBitsPerSample <= 16) {
143 } else if (mBitsPerSample <= 24) {
145 } else {
147 }
148}
ImportFileHandleEx(const FilePath &filename)
WavpackContext * mWavPackContext

References floatSample, int16Sample, int24Sample, mBitsPerSample, and mFormat.

◆ ~WavPackImportFileHandle()

WavPackImportFileHandle::~WavPackImportFileHandle ( )

Definition at line 337 of file ImportWavPack.cpp.

338{
339 WavpackCloseFile(mWavPackContext);
340}

References mWavPackContext.

Member Function Documentation

◆ GetFileDescription()

TranslatableString WavPackImportFileHandle::GetFileDescription ( )
overridevirtual

Implements ImportFileHandle.

Definition at line 150 of file ImportWavPack.cpp.

151{
152 return DESC;
153}
#define DESC

References DESC.

◆ GetFileUncompressedBytes()

auto WavPackImportFileHandle::GetFileUncompressedBytes ( )
overridevirtual

Implements ImportFileHandle.

Definition at line 155 of file ImportWavPack.cpp.

156{
157 return 0;
158}

◆ GetStreamCount()

wxInt32 WavPackImportFileHandle::GetStreamCount ( )
overridevirtual

Implements ImportFileHandle.

Definition at line 322 of file ImportWavPack.cpp.

323{
324 return 1;
325}

◆ GetStreamInfo()

const TranslatableStrings & WavPackImportFileHandle::GetStreamInfo ( )
overridevirtual

Implements ImportFileHandle.

Definition at line 327 of file ImportWavPack.cpp.

328{
329 static TranslatableStrings empty;
330 return empty;
331}
std::vector< TranslatableString > TranslatableStrings

◆ Import()

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

Implements ImportFileHandle.

Definition at line 160 of file ImportWavPack.cpp.

164{
165 BeginImport();
166
167 const int wavpackMode = WavpackGetMode(mWavPackContext);
168
169 outTracks.clear();
170
171 auto trackList = ImportUtils::NewWaveTrack(
172 *trackFactory,
174 mFormat,
176
177 /* The number of samples to read in each loop */
178 const size_t SAMPLES_TO_READ = (*trackList->Any<WaveTrack>().begin())->GetMaxBlockSize();
179 uint32_t totalSamplesRead = 0;
180
181 {
182 const uint32_t bufferSize = mNumChannels * SAMPLES_TO_READ;
183 ArrayOf<int32_t> wavpackBuffer{ bufferSize };
184 ArrayOf<int16_t> int16Buffer;
185 ArrayOf<float> floatBuffer;
186 uint32_t samplesRead = 0;
187
188 if (mFormat == int16Sample) {
189 int16Buffer.reinit(bufferSize);
190 } else if (mFormat == floatSample && (wavpackMode & MODE_FLOAT) != MODE_FLOAT) {
191 floatBuffer.reinit(bufferSize);
192 }
193
194 do {
195 samplesRead = WavpackUnpackSamples(mWavPackContext, wavpackBuffer.get(), SAMPLES_TO_READ);
196
197 if (mFormat == int16Sample) {
198 if (mBytesPerSample == 1)
199 for (int64_t c = 0; c < samplesRead * mNumChannels; c++)
200 int16Buffer[c] = static_cast<int16_t>(wavpackBuffer[c] * 256);
201 else
202 for (int64_t c = 0; c < samplesRead * mNumChannels; c++)
203 int16Buffer[c] = static_cast<int16_t>(wavpackBuffer[c]);
204
205 unsigned chn = 0;
206 ImportUtils::ForEachChannel(*trackList, [&](auto& channel)
207 {
208 channel.AppendBuffer(
209 reinterpret_cast<constSamplePtr>(int16Buffer.get() + chn),
210 mFormat,
211 samplesRead,
213 mFormat
214 );
215 ++chn;
216 });
217 } else if (mFormat == int24Sample || (wavpackMode & MODE_FLOAT) == MODE_FLOAT) {
218 unsigned chn = 0;
219 ImportUtils::ForEachChannel(*trackList, [&](auto& channel)
220 {
221 channel.AppendBuffer(
222 reinterpret_cast<constSamplePtr>(wavpackBuffer.get() + chn),
223 mFormat,
224 samplesRead,
226 mFormat
227 );
228 ++chn;
229 });
230 } else {
231 for (int64_t c = 0; c < samplesRead * mNumChannels; c++)
232 floatBuffer[c] = static_cast<float>(wavpackBuffer[c] / static_cast<double>(std::numeric_limits<int32_t>::max()));
233
234 unsigned chn = 0;
235 ImportUtils::ForEachChannel(*trackList, [&](auto& channel)
236 {
237 channel.AppendBuffer(
238 reinterpret_cast<constSamplePtr>(floatBuffer.get() + chn),
239 mFormat,
240 samplesRead,
242 mFormat
243 );
244 ++chn;
245 });
246 }
247
248 totalSamplesRead += samplesRead;
249
250 progressListener.OnImportProgress(WavpackGetProgress(mWavPackContext));
251 } while (!IsCancelled() && !IsStopped() && samplesRead != 0);
252 }
253
254 if (WavpackGetNumErrors(mWavPackContext))
256 XO( "Encountered %d errors decoding WavPack file!" ).Format( WavpackGetNumErrors(mWavPackContext) ));
257
258 if(IsCancelled())
259 {
261 return;
262 }
263
264 if (totalSamplesRead < mNumSamples && !IsStopped())
265 {
267 return;
268 }
269
270 ImportUtils::FinalizeImport(outTracks, trackList);
271
272 if (wavpackMode & MODE_VALID_TAG) {
273 bool apeTag = wavpackMode & MODE_APETAG;
274 int numItems = WavpackGetNumTagItems(mWavPackContext);
275
276 if (numItems > 0) {
277 tags->Clear();
278 for (int i = 0; i < numItems; i++) {
279 int itemLen = 0, valueLen = 0;
280 wxString value, name;
281
282 // Get the actual length of the item key at this index i
283 itemLen = WavpackGetTagItemIndexed(mWavPackContext, i, NULL, 0);
284 std::string item (itemLen + 1, '\0');
285 WavpackGetTagItemIndexed(mWavPackContext, i, item.data(), itemLen + 1);
286 item.resize(itemLen); // remove terminating NULL from std::string
288
289 // Get the actual length of the value for this item key
290 valueLen = WavpackGetTagItem(mWavPackContext, item.data(), NULL, 0);
291 std::string itemValue (valueLen + 1, '\0');
292 WavpackGetTagItem(mWavPackContext, item.data(), itemValue.data(), valueLen + 1);
293 itemValue.resize(valueLen); // remove terminating NULL from std::string
294
295 if (apeTag) {
296 for (int j = 0; j < valueLen; j++) {
297 // APEv2 text tags can have multiple NULL separated string values
298 if (!itemValue[j]) {
299 itemValue[j] = '\n';
300 }
301 }
302 }
303 value = audacity::ToWXString(itemValue);
304
305 if (name.Upper() == wxT("DATE") && !tags->HasTag(TAG_YEAR)) {
306 long val;
307 if (value.length() == 4 && value.ToLong(&val)) {
308 name = TAG_YEAR;
309 }
310 }
311
312 tags->SetTag(name, value);
313 }
314 }
315 }
316
317 progressListener.OnImportResult(IsStopped()
320}
wxT("CloseDown"))
const TranslatableString name
Definition: Distortion.cpp:76
XO("Cut/Copy/Paste")
const char * constSamplePtr
Definition: SampleFormat.h:58
#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:26
void reinit(Integral count, bool initialize=false)
Definition: MemoryX.h:56
Abstract base class used in importing a file.
bool IsStopped() const noexcept
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 ShowMessageBox(const TranslatableString &message, const TranslatableString &caption=XO("Import Project"))
Definition: ImportUtils.cpp:43
static TrackListHolder 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:66
static void FinalizeImport(TrackHolders &outTracks, const std::vector< TrackListHolder > &importedStreams)
Flushes the given channels and moves them to outTracks.
Definition: ImportUtils.cpp:49
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
A Track that contains audio waveform data.
Definition: WaveTrack.h:227
auto begin(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:150
wxString ToWXString(const std::string &str)

References PackedArray::begin(), ImportFileHandleEx::BeginImport(), ImportProgressListener::Cancelled, Tags::Clear(), ImportProgressListener::Error, ImportUtils::FinalizeImport(), floatSample, ImportUtils::ForEachChannel(), Tags::HasTag(), int16Sample, int24Sample, ImportFileHandleEx::IsCancelled(), ImportFileHandleEx::IsStopped(), mBytesPerSample, mFormat, mNumChannels, mNumSamples, mSampleRate, mWavPackContext, name, ImportUtils::NewWaveTrack(), ImportProgressListener::OnImportProgress(), ImportProgressListener::OnImportResult(), ArrayOf< X >::reinit(), Tags::SetTag(), ImportUtils::ShowMessageBox(), ImportProgressListener::Stopped, ImportProgressListener::Success, TAG_YEAR, audacity::ToWXString(), wxT(), and XO().

Here is the call graph for this function:

◆ SetStreamUsage()

void WavPackImportFileHandle::SetStreamUsage ( wxInt32  StreamID,
bool  Use 
)
overridevirtual

Implements ImportFileHandle.

Definition at line 333 of file ImportWavPack.cpp.

334{
335}

Member Data Documentation

◆ mBitsPerSample

int WavPackImportFileHandle::mBitsPerSample
private

Definition at line 77 of file ImportWavPack.cpp.

Referenced by WavPackImportFileHandle().

◆ mBytesPerSample

int WavPackImportFileHandle::mBytesPerSample
private

Definition at line 78 of file ImportWavPack.cpp.

Referenced by Import().

◆ mFormat

sampleFormat WavPackImportFileHandle::mFormat
private

Definition at line 80 of file ImportWavPack.cpp.

Referenced by Import(), and WavPackImportFileHandle().

◆ mNumChannels

int WavPackImportFileHandle::mNumChannels
private

Definition at line 75 of file ImportWavPack.cpp.

Referenced by Import().

◆ mNumSamples

int64_t WavPackImportFileHandle::mNumSamples
private

Definition at line 79 of file ImportWavPack.cpp.

Referenced by Import().

◆ mSampleRate

uint32_t WavPackImportFileHandle::mSampleRate
private

Definition at line 76 of file ImportWavPack.cpp.

Referenced by Import().

◆ mWavPackContext

WavpackContext* WavPackImportFileHandle::mWavPackContext
private

Definition at line 74 of file ImportWavPack.cpp.

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


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