Audacity 3.2.0
ImportPCM.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 ImportPCM.cpp
6
7 Dominic Mazzoni
8 Leland Lucius
9
10*//****************************************************************//****************************************************************//*******************************************************************/
21
22
23
24#include "Import.h"
25#include "Tags.h"
26
27#include <wx/wx.h>
28#include <wx/ffile.h>
29
30#include "sndfile.h"
31
32#ifndef SNDFILE_1
33#error Requires libsndfile 1.0 or higher
34#endif
35
36#include "FileFormats.h"
37#include "GetAcidizerTags.h"
38#include "ImportPlugin.h"
40#include "ImportUtils.h"
41#include "WaveTrack.h"
42
43#include <algorithm>
44
45#ifdef USE_LIBID3TAG
46 #include <id3tag.h>
47 // DM: the following functions were supposed to have been
48 // included in id3tag.h - should be fixed in the next release
49 // of mad.
50 extern "C" {
51 struct id3_frame *id3_frame_new(char const *);
52 id3_length_t id3_latin1_length(id3_latin1_t const *);
53 void id3_latin1_decode(id3_latin1_t const *, id3_ucs4_t *);
54 }
55#endif
56
57#define DESC XO("WAV, AIFF, and other uncompressed types")
58
59class PCMImportPlugin final : public ImportPlugin
60{
61public:
64 {
65 }
66
68
69 wxString GetPluginStringID() override { return wxT("libsndfile"); }
71 std::unique_ptr<ImportFileHandle> Open(
72 const FilePath &Filename, AudacityProject*) override;
73};
74
75
77{
78public:
79 PCMImportFileHandle(const FilePath &name, SFFile &&file, SF_INFO info);
81
84 void Import(
85 ImportProgressListener& progressListener, WaveTrackFactory* trackFactory,
86 TrackHolders& outTracks, Tags* tags,
87 std::optional<LibFileFormats::AcidizerTags>& outAcidTags) override;
88
89 wxInt32 GetStreamCount() override { return 1; }
90
92 {
93 static TranslatableStrings empty;
94 return empty;
95 }
96
97 void SetStreamUsage(wxInt32 WXUNUSED(StreamID), bool WXUNUSED(Use)) override
98 {}
99
100private:
102 const SF_INFO mInfo;
105};
106
108{
109 return DESC;
110}
111
112std::unique_ptr<ImportFileHandle> PCMImportPlugin::Open(
113 const FilePath &filename, AudacityProject*)
114{
115 SF_INFO info;
116 wxFile f; // will be closed when it goes out of scope
117 SFFile file;
118
119 memset(&info, 0, sizeof(info));
120
121
122#ifdef __WXGTK__
123 if (filename.Lower().EndsWith(wxT("mp3"))) {
124 // There is a bug in libsndfile where mp3s with duplicated metadata tags
125 // will crash libsndfile and thus audacity.
126 // We have patched the lib-src version of libsndfile, but
127 // for linux the user can build against the system libsndfile which
128 // still has this bug.
129 // This happens in sf_open_fd, which is the very first point of
130 // interaction with libsndfile, so the only workaround is to hardcode
131 // ImportPCM to not handle .mp3. Of course, this will still fail for mp3s
132 // that are mislabeled with a .wav or other extension.
133 // So, in the future we may want to write a simple parser to detect mp3s here.
134 return NULL;
135 }
136#endif
137
138
139 if (f.Open(filename)) {
140 // Even though there is an sf_open() that takes a filename, use the one that
141 // takes a file descriptor since wxWidgets can open a file with a Unicode name and
142 // libsndfile can't (under Windows).
143 file.reset(SFCall<SNDFILE*>(sf_open_fd, f.fd(), SFM_READ, &info, TRUE));
144 }
145
146 // The file descriptor is now owned by "file", so we must tell "f" to leave
147 // it alone. The file descriptor is closed by the destructor of file even if an error
148 // occurs.
149 f.Detach();
150
151 if (!file) {
152 // TODO: Handle error
153 //char str[1000];
154 //sf_error_str((SNDFILE *)NULL, str, 1000);
155
156 return nullptr;
157 } else if (file &&
158 (info.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_OGG) {
159 // mchinen 15.1.2012 - disallowing libsndfile to handle
160 // ogg files because seeking is broken at this date (very slow,
161 // seeks from beginning of file each seek).
162 // This was said by Erik (libsndfile maintainer).
163 // Note that this won't apply to our local libsndfile, so only
164 // linux builds that use --with-libsndfile=system are affected,
165 // as our local libsndfile doesn't do OGG.
166 // In particular ubuntu 10.10 and 11.04 are known to be affected
167 // When the bug is fixed, we can check version to avoid only
168 // the broken builds.
169
170 return nullptr;
171 }
172
173 // Success, so now transfer the duty to close the file from "file".
174 return std::make_unique<PCMImportFileHandle>(filename, std::move(file), info);
175}
176
178 std::make_unique< PCMImportPlugin >()
179};
180
182 SFFile &&file, SF_INFO info)
184 mFile(std::move(file)),
185 mInfo(info)
186{
187 wxASSERT(info.channels >= 0);
188
189 //
190 // Figure out the format to use.
191 //
192 // In general, go with the user's preferences. However, if
193 // the file is higher-quality, go with a format which preserves
194 // the quality of the original file.
195 //
196
197 // Effective format
199 // But maybe different storage format
201}
202
204{
205 // Library strings
206 // See the major_formats and subtype_formats tables in command.c in
207 // libsndfile for this list of possibilities
208
209using Unevaluated = decltype(
210 /* major_formats */
211 XO("AIFF (Apple/SGI)")
212 , XO("AU (Sun/NeXT)")
213 , XO("AVR (Audio Visual Research)")
214 , XO("CAF (Apple Core Audio File)")
215 /* i18n-hint: "codec" is short for a "coder-decoder" algorithm */
216 , XO("FLAC (FLAC Lossless Audio Codec)")
217 , XO("HTK (HMM Tool Kit)")
218 , XO("IFF (Amiga IFF/SVX8/SV16)")
219 , XO("MAT4 (GNU Octave 2.0 / Matlab 4.2)")
220 , XO("MAT5 (GNU Octave 2.1 / Matlab 5.0)")
221 , XO("MPC (Akai MPC 2k)")
222 , XO("OGG (OGG Container format)")
223 , XO("PAF (Ensoniq PARIS)")
224 , XO("PVF (Portable Voice Format)")
225 , XO("RAW (header-less)")
226 , XO("RF64 (RIFF 64)")
227 , XO("SD2 (Sound Designer II)")
228 , XO("SDS (Midi Sample Dump Standard)")
229 , XO("SF (Berkeley/IRCAM/CARL)")
230 , XO("VOC (Creative Labs)")
231 , XO("W64 (SoundFoundry WAVE 64)")
232 , XO("WAV (Microsoft)")
233 , XO("WAV (NIST Sphere)")
234 , XO("WAVEX (Microsoft)")
235 , XO("WVE (Psion Series 3)")
236 , XO("XI (FastTracker 2)")
237);
238
239using Unevaluated2 = decltype(
240 /* subtype_formats */
241 XO("Signed 8 bit PCM")
242 , XO("Signed 16 bit PCM")
243 , XO("Signed 24 bit PCM")
244 , XO("Signed 32 bit PCM")
245 , XO("Unsigned 8 bit PCM")
246 , XO("32 bit float")
247 , XO("64 bit float")
248 , XO("U-Law")
249 , XO("A-Law")
250 , XO("IMA ADPCM")
251 , XO("Microsoft ADPCM")
252 , XO("GSM 6.10")
253 , XO("32kbs G721 ADPCM")
254 , XO("24kbs G723 ADPCM")
255 , XO("12 bit DWVW")
256 , XO("16 bit DWVW")
257 , XO("24 bit DWVW")
258 , XO("VOX ADPCM")
259 , XO("16 bit DPCM")
260 , XO("8 bit DPCM")
261 , XO("Vorbis")
262);
263
264 auto untranslated = SFCall<wxString>(sf_header_name, mInfo.format);
265 return TranslatableString{
266 untranslated, {} };
267}
268
270{
271 return mInfo.frames * mInfo.channels * SAMPLE_SIZE(mFormat);
272}
273
274#ifdef USE_LIBID3TAG
275struct id3_tag_deleter {
276 void operator () (id3_tag *p) const { if (p) id3_tag_delete(p); }
277};
278using id3_tag_holder = std::unique_ptr<id3_tag, id3_tag_deleter>;
279#endif
280
282 ImportProgressListener& progressListener, WaveTrackFactory* trackFactory,
283 TrackHolders& outTracks, Tags* tags,
284 std::optional<LibFileFormats::AcidizerTags>& outAcidTags)
285{
286 BeginImport();
287
288 outTracks.clear();
289
290 wxASSERT(mFile.get());
291
293 auto trackList = trackFactory->CreateMany(mInfo.channels, format, mInfo.samplerate);
294
295 auto fileTotalFrames =
296 (sampleCount)mInfo.frames; // convert from sf_count_t
297 auto maxBlockSize = (*trackList->Any<WaveTrack>().begin())->GetMaxBlockSize();
298
299 {
300 // Otherwise, we're in the "copy" mode, where we read in the actual
301 // samples from the file and store our own local copy of the
302 // samples in the tracks.
303
304 // PRL: guard against excessive memory buffer allocation in case of many channels
305 using type = decltype(maxBlockSize);
306 if (mInfo.channels < 1)
307 {
309 return;
310 }
311 auto maxBlock = std::min(maxBlockSize,
312 std::numeric_limits<type>::max() /
313 (mInfo.channels * SAMPLE_SIZE(mFormat))
314 );
315 if (maxBlock < 1)
316 {
318 return;
319 }
320
321 SampleBuffer srcbuffer, buffer;
322 wxASSERT(mInfo.channels >= 0);
323 while (NULL == srcbuffer.Allocate(maxBlock * mInfo.channels, mFormat).ptr() ||
324 NULL == buffer.Allocate(maxBlock, mFormat).ptr())
325 {
326 maxBlock /= 2;
327 if (maxBlock < 1)
328 {
330 return;
331 }
332 }
333
334 decltype(fileTotalFrames) framescompleted = 0;
335
336 long block;
337 do {
338 block = maxBlock;
339
340 if (mFormat == int16Sample)
341 block = SFCall<sf_count_t>(sf_readf_short, mFile.get(), (short *)srcbuffer.ptr(), block);
342 //import 24 bit int as float and have the append function convert it. This is how PCMAliasBlockFile worked too.
343 else
344 block = SFCall<sf_count_t>(sf_readf_float, mFile.get(), (float *)srcbuffer.ptr(), block);
345
346 if(block < 0 || block > (long)maxBlock) {
347 wxASSERT(false);
348 block = maxBlock;
349 }
350
351 if (block) {
352 unsigned c = 0;
353 ImportUtils::ForEachChannel(*trackList, [&](auto& channel)
354 {
355 if (mFormat==int16Sample) {
356 for(int j=0; j<block; j++)
357 ((short *)buffer.ptr())[j] =
358 ((short *)srcbuffer.ptr())[mInfo.channels*j+c];
359 }
360 else {
361 for(int j=0; j<block; j++)
362 ((float *)buffer.ptr())[j] =
363 ((float *)srcbuffer.ptr())[mInfo.channels*j+c];
364 }
365
366 channel.AppendBuffer(
367 buffer.ptr(),
369 block, 1, mEffectiveFormat
370 );
371 ++c;
372 });
373 framescompleted += block;
374 }
375 if(fileTotalFrames > 0)
376 progressListener.OnImportProgress(framescompleted.as_double() / fileTotalFrames.as_double());
377 } while (block > 0 && !IsCancelled() && !IsStopped());
378 }
379
380 if(IsCancelled())
381 {
383 return;
384 }
385
386 ImportUtils::FinalizeImport(outTracks, std::move(*trackList));
387
388 const char *str;
389
390 str = sf_get_string(mFile.get(), SF_STR_TITLE);
391 if (str) {
392 tags->SetTag(TAG_TITLE, UTF8CTOWX(str));
393 }
394
395 str = sf_get_string(mFile.get(), SF_STR_ALBUM);
396 if (str) {
397 tags->SetTag(TAG_ALBUM, UTF8CTOWX(str));
398 }
399
400 str = sf_get_string(mFile.get(), SF_STR_ARTIST);
401 if (str) {
403 }
404
405 str = sf_get_string(mFile.get(), SF_STR_COMMENT);
406 if (str) {
408 }
409
410 str = sf_get_string(mFile.get(), SF_STR_DATE);
411 if (str) {
412 tags->SetTag(TAG_YEAR, UTF8CTOWX(str));
413 }
414
415 str = sf_get_string(mFile.get(), SF_STR_COPYRIGHT);
416 if (str) {
418 }
419
420 str = sf_get_string(mFile.get(), SF_STR_SOFTWARE);
421 if (str) {
423 }
424
425 str = sf_get_string(mFile.get(), SF_STR_TRACKNUMBER);
426 if (str) {
427 tags->SetTag(TAG_TRACK, UTF8CTOWX(str));
428 }
429
430 str = sf_get_string(mFile.get(), SF_STR_GENRE);
431 if (str) {
432 tags->SetTag(TAG_GENRE, UTF8CTOWX(str));
433 }
434
435 // To begin with, only trust the Muse Hub, with whom we collaborate and can
436 // ensure they comply. In the future we may extend this list.
437 const std::vector<std::string> trustedDistributors { "Muse Hub" };
438 if (const auto acidTags = LibImportExport::GetAcidizerTags(*mFile, trustedDistributors))
439 outAcidTags.emplace(*acidTags);
440
441#if defined(USE_LIBID3TAG)
442 if (((mInfo.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AIFF) ||
443 ((mInfo.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV)) {
444 wxFFile f(GetFilename(), wxT("rb"));
445 if (f.IsOpened()) {
446 char id[5];
447 wxUint32 len;
448
449 id[4] = '\0';
450
451 f.Seek(12); // Skip filetype, length, and formtype
452
453 while (!f.Error()) {
454 f.Read(id, 4); // Get chunk type
455 if (f.Eof()) {
456 break;
457 }
458 f.Read(&len, 4);
459 if((mInfo.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AIFF)
460 len = wxUINT32_SWAP_ON_LE(len);
461
462 if (wxStricmp(id, "ID3 ") != 0) { // must be case insensitive
463 f.Seek(len + (len & 0x01), wxFromCurrent);
464 continue;
465 }
466
467
468 id3_tag_holder tp;
469 {
470 ArrayOf<id3_byte_t> buffer{ len };
471 if (!buffer) {
472 break;
473 }
474
475 f.Read(buffer.get(), len);
476 tp.reset( id3_tag_parse(buffer.get(), len) );
477 }
478
479 if (!tp) {
480 break;
481 }
482
483 // Loop through all frames
484 bool have_year = false;
485 for (int i = 0; i < (int) tp->nframes; i++) {
486 struct id3_frame *frame = tp->frames[i];
487
488 // wxPrintf("ID: %08x '%4s'\n", (int) *(int *)frame->id, frame->id);
489 // wxPrintf("Desc: %s\n", frame->description);
490 // wxPrintf("Num fields: %d\n", frame->nfields);
491
492 // for (int j = 0; j < (int) frame->nfields; j++) {
493 // wxPrintf("field %d type %d\n", j, frame->fields[j].type );
494 // if (frame->fields[j].type == ID3_FIELD_TYPE_STRINGLIST) {
495 // wxPrintf("num strings %d\n", frame->fields[j].stringlist.nstrings);
496 // }
497 // }
498
499 wxString n, v;
500
501 // Determine the tag name
502 if (strcmp(frame->id, ID3_FRAME_TITLE) == 0) {
503 n = TAG_TITLE;
504 }
505 else if (strcmp(frame->id, ID3_FRAME_ARTIST) == 0) {
506 n = TAG_ARTIST;
507 }
508 else if (strcmp(frame->id, ID3_FRAME_ALBUM) == 0) {
509 n = TAG_ALBUM;
510 }
511 else if (strcmp(frame->id, ID3_FRAME_TRACK) == 0) {
512 n = TAG_TRACK;
513 }
514 else if (strcmp(frame->id, ID3_FRAME_YEAR) == 0) {
515 // LLL: When libid3tag encounters the "TYER" tag, it converts it to a
516 // "ZOBS" (obsolete) tag and adds a "TDRC" tag at the end of the
517 // list of tags using the first 4 characters of the "TYER" tag.
518 // Since we write both the "TDRC" and "TYER" tags, the "TDRC" tag
519 // will always be encountered first in the list. We want use it
520 // since the converted "TYER" tag may have been truncated.
521 if (have_year) {
522 continue;
523 }
524 n = TAG_YEAR;
525 have_year = true;
526 }
527 else if (strcmp(frame->id, ID3_FRAME_COMMENT) == 0) {
528 n = TAG_COMMENTS;
529 }
530 else if (strcmp(frame->id, ID3_FRAME_GENRE) == 0) {
531 n = TAG_GENRE;
532 }
533 else {
534 // Use frame description as default tag name. The descriptions
535 // may include several "meanings" separated by "/" characters, so
536 // we just use the first meaning
537 n = UTF8CTOWX(frame->description).BeforeFirst(wxT('/'));
538 }
539
540 const id3_ucs4_t *ustr = NULL;
541
542 if (n == TAG_COMMENTS) {
543 ustr = id3_field_getfullstring(&frame->fields[3]);
544 }
545 else if (frame->nfields == 3) {
546 ustr = id3_field_getstring(&frame->fields[1]);
547 if (ustr) {
548 // Is this duplication really needed?
549 MallocString<> convStr{ (char *)id3_ucs4_utf8duplicate(ustr) };
550 n = UTF8CTOWX(convStr.get());
551 }
552
553 ustr = id3_field_getstring(&frame->fields[2]);
554 }
555 else if (frame->nfields >= 2) {
556 ustr = id3_field_getstrings(&frame->fields[1], 0);
557 }
558
559 if (ustr) {
560 // Is this duplication really needed?
561 MallocString<> convStr{ (char *)id3_ucs4_utf8duplicate(ustr) };
562 v = UTF8CTOWX(convStr.get());
563 }
564
565 if (!n.empty() && !v.empty()) {
566 tags->SetTag(n, v);
567 }
568 }
569
570 // Convert v1 genre to name
571 if (tags->HasTag(TAG_GENRE)) {
572 long g = -1;
573 if (tags->GetTag(TAG_GENRE).ToLong(&g)) {
574 tags->SetTag(TAG_GENRE, tags->GetGenre(g));
575 }
576 }
577
578 break;
579 }
580 }
581 }
582#endif
583
584 progressListener.OnImportResult(IsStopped()
587}
588
590{
591}
wxT("CloseDown"))
int min(int a, int b)
#define str(a)
FileExtensions sf_get_all_extensions()
wxString sf_header_name(int format)
Get the string name of the specified container format.
sampleFormat sf_subtype_to_effective_format(unsigned int format)
Choose the narrowest value in the sampleFormat enumeration for a given libsndfile format.
XO("Cut/Copy/Paste")
#define DESC
Definition: ImportPCM.cpp:57
static Importer::RegisteredImportPlugin registered
Definition: ImportPCM.cpp:177
The interface that all file import "plugins" (if you want to call them that) must implement....
std::vector< std::shared_ptr< Track > > TrackHolders
Definition: ImportRaw.h:24
#define UTF8CTOWX(X)
Definition: Internat.h:157
std::unique_ptr< Character[], freer > MallocString
Definition: MemoryX.h:148
wxString FilePath
Definition: Project.h:21
sampleFormat
The ordering of these values with operator < agrees with the order of increasing bit width.
Definition: SampleFormat.h:30
#define SAMPLE_SIZE(SampleFormat)
Definition: SampleFormat.h:52
#define TAG_TRACK
Definition: Tags.h:61
#define TAG_COMMENTS
Definition: Tags.h:64
#define TAG_GENRE
Definition: Tags.h:63
#define TAG_SOFTWARE
Definition: Tags.h:65
#define TAG_COPYRIGHT
Definition: Tags.h:66
#define TAG_ALBUM
Definition: Tags.h:60
#define TAG_YEAR
Definition: Tags.h:62
#define TAG_TITLE
Definition: Tags.h:58
#define TAG_ARTIST
Definition: Tags.h:59
wxString name
Definition: TagsEditor.cpp:166
std::vector< TranslatableString > TranslatableStrings
This simplifies arrays of arrays, each array separately allocated with NEW[] But it might be better t...
Definition: MemoryX.h:29
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
bool IsStopped() const noexcept
FilePath GetFilename() const override
bool IsCancelled() const noexcept
unsigned long long ByteCount
Definition: ImportPlugin.h:114
Base class for FlacImportPlugin, LOFImportPlugin, MP3ImportPlugin, OggImportPlugin and PCMImportPlugi...
Definition: ImportPlugin.h:67
Interface used to report on import state and progress.
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 sampleFormat ChooseFormat(sampleFormat effectiveFormat)
Choose appropriate format, which will not be narrower than the specified one.
Definition: ImportUtils.cpp:19
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
An ImportFileHandle for PCM data.
Definition: ImportPCM.cpp:77
const TranslatableStrings & GetStreamInfo() override
Definition: ImportPCM.cpp:91
void Import(ImportProgressListener &progressListener, WaveTrackFactory *trackFactory, TrackHolders &outTracks, Tags *tags, std::optional< LibFileFormats::AcidizerTags > &outAcidTags) override
Definition: ImportPCM.cpp:281
void SetStreamUsage(wxInt32 WXUNUSED(StreamID), bool WXUNUSED(Use)) override
Definition: ImportPCM.cpp:97
wxInt32 GetStreamCount() override
Definition: ImportPCM.cpp:89
TranslatableString GetFileDescription() override
Definition: ImportPCM.cpp:203
ByteCount GetFileUncompressedBytes() override
Definition: ImportPCM.cpp:269
PCMImportFileHandle(const FilePath &name, SFFile &&file, SF_INFO info)
Definition: ImportPCM.cpp:181
sampleFormat mFormat
Definition: ImportPCM.cpp:104
const SF_INFO mInfo
Definition: ImportPCM.cpp:102
sampleFormat mEffectiveFormat
Definition: ImportPCM.cpp:103
An ImportPlugin for PCM data.
Definition: ImportPCM.cpp:60
wxString GetPluginStringID() override
Definition: ImportPCM.cpp:69
TranslatableString GetPluginFormatDescription() override
Definition: ImportPCM.cpp:107
std::unique_ptr< ImportFileHandle > Open(const FilePath &Filename, AudacityProject *) override
Definition: ImportPCM.cpp:112
SampleBuffer & Allocate(size_t count, sampleFormat format)
Definition: SampleFormat.h:151
samplePtr ptr() const
Definition: SampleFormat.h:165
ID3 Tags (for MP3)
Definition: Tags.h:73
bool HasTag(const wxString &name) const
Definition: Tags.cpp:397
wxString GetGenre(int value)
Definition: Tags.cpp:373
void SetTag(const wxString &name, const wxString &value, const bool bSpecialTag=false)
Definition: Tags.cpp:431
wxString GetTag(const wxString &name) const
Definition: Tags.cpp:406
Holds a msgid for the translation catalog; may also bind format arguments.
Used to create or clone a WaveTrack, with appropriate context from the project that will own the trac...
Definition: WaveTrack.h:871
TrackListHolder CreateMany(size_t nChannels)
Creates tracks with project's default rate and format and the given number of channels.
Definition: WaveTrack.cpp:423
A Track that contains audio waveform data.
Definition: WaveTrack.h:203
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
std::optional< LibFileFormats::AcidizerTags > GetAcidizerTags(SNDFILE &file, const std::vector< std::string > &trustedDistributors)
Get the Acidizer tags from a file if from a trusted distributor.
STL namespace.