Audacity 3.2.0
FileFormats.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 FileFormats.cpp
6
7 Dominic Mazzoni
8
9*******************************************************************//*******************************************************************/
16
17
18
19#include "FileFormats.h"
20
21#include <wx/arrstr.h>
22#include "sndfile.h"
23#include "Internat.h"
24#include "MemoryX.h"
25#include "AudacityMessageBox.h"
26#include "Prefs.h"
27
28#ifndef SNDFILE_1
29#error Requires libsndfile 1.0 or higher
30#endif
31
32//
33// enumerating headers
34//
35
37{
38 int count;
39
40 sf_command(NULL, SFC_GET_FORMAT_MAJOR_COUNT,
41 &count, sizeof(count));
42
43 return count;
44}
45
47{
48 SF_FORMAT_INFO format_info;
49
50 memset(&format_info, 0, sizeof(format_info));
51 format_info.format = format;
52 sf_command(NULL, SFC_GET_FORMAT_MAJOR,
53 &format_info, sizeof (format_info)) ;
54
55 return LAT1CTOWX(format_info.name);
56}
57
58unsigned int sf_header_index_to_type(int i)
59{
60 SF_FORMAT_INFO format_info ;
61
62 memset(&format_info, 0, sizeof(format_info));
63 format_info.format = i;
64 sf_command (NULL, SFC_GET_FORMAT_MAJOR,
65 &format_info, sizeof (format_info));
66
67 return format_info.format & SF_FORMAT_TYPEMASK;
68}
69
70//
71// enumerating encodings
72//
73
75{
76 int count ;
77
78 sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int)) ;
79
80 return count;
81}
82
84{
85 SF_FORMAT_INFO format_info ;
86
87 memset(&format_info, 0, sizeof(format_info));
88 format_info.format = i;
89 sf_command (NULL, SFC_GET_FORMAT_SUBTYPE,
90 &format_info, sizeof (format_info));
91 return sf_normalize_name(format_info.name);
92}
93
95{
96 SF_FORMAT_INFO format_info ;
97
98 memset(&format_info, 0, sizeof(format_info));
99 format_info.format = i;
100 sf_command (NULL, SFC_GET_FORMAT_SUBTYPE,
101 &format_info, sizeof (format_info));
102
103 return format_info.format & SF_FORMAT_SUBMASK;
104}
105
106//
107// getting info about an actual SF format
108//
109
111{
112 SF_FORMAT_INFO format_info;
113
114 memset(&format_info, 0, sizeof(format_info));
115 format_info.format = (format & SF_FORMAT_TYPEMASK);
116 sf_command(NULL, SFC_GET_FORMAT_INFO, &format_info, sizeof(format_info));
117
118 return LAT1CTOWX(format_info.name);
119}
120
122{
123 SF_FORMAT_INFO format_info;
124 int i;
125 wxString s;
126
127 memset(&format_info, 0, sizeof(format_info));
128 format_info.format = (format & SF_FORMAT_TYPEMASK);
129 sf_command(NULL, SFC_GET_FORMAT_INFO, &format_info, sizeof(format_info));
130
131 MallocString<> tmp { strdup( format_info.name ) };
132 i = 0;
133 while(tmp[i]) {
134 if (tmp[i]==' ')
135 tmp[i] = 0;
136 else
137 i++;
138 }
139
140 s = LAT1CTOWX(tmp.get());
141
142 return s;
143}
144
146{
147 SF_FORMAT_INFO format_info;
148
149 memset(&format_info, 0, sizeof(format_info));
150 format_info.format = (format & SF_FORMAT_TYPEMASK);
151 sf_command(NULL, SFC_GET_FORMAT_INFO, &format_info, sizeof(format_info));
152
153 return LAT1CTOWX(format_info.extension);
154}
155
156wxString sf_encoding_name(int encoding)
157{
158 SF_FORMAT_INFO format_info;
159
160 memset(&format_info, 0, sizeof(format_info));
161 format_info.format = (encoding & SF_FORMAT_SUBMASK);
162 sf_command(NULL, SFC_GET_FORMAT_INFO, &format_info, sizeof(format_info));
163
164 return sf_normalize_name(format_info.name);
165}
166
168{
169 int count ;
170
171 sf_command (NULL, SFC_GET_SIMPLE_FORMAT_COUNT, &count, sizeof (int)) ;
172
173 return count;
174}
175
176static SF_FORMAT_INFO g_format_info;
177
178SF_FORMAT_INFO *sf_simple_format(int i)
179{
180 memset(&g_format_info, 0, sizeof(g_format_info));
181
182 g_format_info.format = i;
183 sf_command (NULL, SFC_GET_SIMPLE_FORMAT,
184 &g_format_info, sizeof(g_format_info));
185
186 return &g_format_info;
187}
188
190{
191 unsigned int subtype = format & SF_FORMAT_SUBMASK;
192 return (subtype == SF_FORMAT_FLOAT ||
193 subtype == SF_FORMAT_DOUBLE ||
194 subtype == SF_FORMAT_PCM_24 ||
195 subtype == SF_FORMAT_PCM_32);
196}
197
199{
200 unsigned int subtype = format & SF_FORMAT_SUBMASK;
201 return (subtype == SF_FORMAT_PCM_16 ||
202 subtype == SF_FORMAT_PCM_24 ||
203 subtype == SF_FORMAT_PCM_32);
204}
205
207 unsigned int subtype = format & SF_FORMAT_SUBMASK;
208 if( subtype == SF_FORMAT_PCM_S8 )
209 return 1;
210 if( subtype == SF_FORMAT_PCM_U8 )
211 return 1;
212 if( subtype == SF_FORMAT_PCM_16 )
213 return 2;
214 if( subtype == SF_FORMAT_PCM_24 )
215 return 3;
216 if( subtype == SF_FORMAT_PCM_32 )
217 return 4;
218 if( subtype == SF_FORMAT_FLOAT )
219 return 4;
220 if( subtype == SF_FORMAT_DOUBLE )
221 return 8;
222
223 // might be different to 2, but this is good enough for
224 // WAV and AIFF file size error trapping.
225 return 2;
226}
227
229{
230 unsigned int subtype = format & SF_FORMAT_SUBMASK;
231 if (subtype == SF_FORMAT_PCM_24)
232 return int24Sample;
234 return widestSampleFormat;
235 else
236 return int16Sample;
237}
238
239
241{
243 SF_FORMAT_INFO format_info;
244 int count, k;
245
246 memset(&format_info, 0, sizeof(format_info));
247
248 sf_command(NULL, SFC_GET_FORMAT_MAJOR_COUNT,
249 &count, sizeof(count));
250
251 for(k=0; k<count; k++) {
252 format_info.format = k;
253 sf_command(NULL, SFC_GET_FORMAT_MAJOR,
254 &format_info, sizeof (format_info)) ;
255
256 exts.push_back(LAT1CTOWX(format_info.extension));
257 }
258
259 // Some other extensions that are often sound files
260 // but aren't included by libsndfile
261
262 exts.insert( exts.end(), {
263 wxT("aif") , // AIFF file with a DOS-style extension
264 wxT("ircam") ,
265 wxT("snd") ,
266 wxT("svx") ,
267 wxT("svx8") ,
268 wxT("sv16") ,
269 } );
270
271 return exts;
272}
273
274wxString sf_normalize_name(const char *name)
275{
276 wxString n = LAT1CTOWX(name);
277
278 n.Replace(wxT("8 bit"), wxT("8-bit"));
279 n.Replace(wxT("16 bit"), wxT("16-bit"));
280 n.Replace(wxT("24 bit"), wxT("24-bit"));
281 n.Replace(wxT("32 bit"), wxT("32-bit"));
282 n.Replace(wxT("64 bit"), wxT("64-bit"));
283
284 return n;
285}
286
287#ifdef __WXMAC__
288
289// TODO: find out the appropriate OSType
290// for the ones with an '????'. The others
291// are at least the same type used by
292// SoundApp.
293
294#define NUM_HEADERS 13
295
296//
297// Mac OS 4-char type
298//
299
300# ifdef __UNIX__
301# include <CoreServices/CoreServices.h>
302# else
303# include <Types.h>
304# endif
305
307 'WAVE', // WAVE
308 'AIFF', // AIFF
309 'NeXT', // Sun/NeXT AU
310 'BINA', // RAW i.e. binary
311 'PAR ', // ??? Ensoniq PARIS
312 '8SVX', // Amiga IFF / SVX8
313 'NIST', // ??? NIST/Sphere
314 'VOC ', // VOC
315 '\?\?\?\?', // ?? Propellorheads Rex
316 'SF ', // ?? IRCAM
317 'W64 ', // ?? Wave64
318 'MAT4', // ?? Matlab 4
319 'MAT5', // ?? Matlab 5
320};
321
322static OSType sf_header_mactype(int format)
323{
324 if (format >= 0x10000)
325 return MacNames[(format/0x10000)-1];
326 else if (format>=0 && format<NUM_HEADERS)
327 return MacNames[format];
328 else
329 return '\?\?\?\?';
330}
331
332#endif // __WXMAC__
333
334//std::mutex libSndFileMutex;
335
336int SFFileCloser::operator() (SNDFILE *sf) const
337{
338 auto err = SFCall<int>(sf_close, sf);
339 if (err) {
340 char buffer[1000];
341 sf_error_str(sf, buffer, 1000);
343 /* i18n-hint: %s will be the error message from the libsndfile software library */
344 XO( "Error (file may not have been written): %s" )
345 // Not attempting to localize error messages
346 // from the library
347 .Format( buffer ));
348 }
349 return err;
350}
351
353 wxT("/FileFormats/CopyOrEditUncompressedData"),
354 {
356 wxT("copy"),
357 XXO("&Copy uncompressed files into the project (safer)")
358 },
360 wxT("edit"),
361 XXO("&Read uncompressed files from original location (faster)")
362 },
363 },
364 0 // copy
365};
366
368 wxT("/FileFormats/SaveProjectWithDependencies"),
369 {
370 { wxT("copy"), XXO("&Copy all audio into project (safest)") },
371 { wxT("never"), XXO("Do &not copy any audio") },
372 { wxT("ask"), XXO("As&k") },
373 },
374 2 // ask
375};
wxT("CloseDown"))
int AudacityMessageBox(const TranslatableString &message, const TranslatableString &caption, long style, wxWindow *parent, int x, int y)
const TranslatableString name
Definition: Distortion.cpp:76
int format
Definition: ExportPCM.cpp:53
SF_FORMAT_INFO * sf_simple_format(int i)
OSType MacNames[NUM_HEADERS]
wxString sf_header_extension(int format)
Get the most common file extension for the given format.
wxString sf_header_index_name(int format)
Get the name of a container format from libsndfile.
Definition: FileFormats.cpp:46
ChoiceSetting FileFormatsSaveWithDependenciesSetting
unsigned int sf_header_index_to_type(int i)
Definition: FileFormats.cpp:58
FileExtensions sf_get_all_extensions()
bool sf_subtype_is_integer(unsigned int format)
bool sf_subtype_more_than_16_bits(unsigned int format)
wxString sf_header_shortname(int format)
Get an abbreviated form of the string name of the specified format.
unsigned int sf_encoding_index_to_subtype(int i)
Definition: FileFormats.cpp:94
wxString sf_header_name(int format)
Get the string name of the specified container format.
wxString sf_encoding_name(int encoding)
Get the string name of the specified data encoding.
wxString sf_normalize_name(const char *name)
#define NUM_HEADERS
int sf_subtype_bytes_per_sample(unsigned int format)
wxString sf_encoding_index_name(int i)
Get the string name of the data encoding of the requested format.
Definition: FileFormats.cpp:83
int sf_num_simple_formats()
sampleFormat sf_subtype_to_effective_format(unsigned int format)
Choose the narrowest value in the sampleFormat enumeration for a given libsndfile format.
static SF_FORMAT_INFO g_format_info
ChoiceSetting FileFormatsCopyOrEditSetting
int sf_num_headers()
Get the number of container formats supported by libsndfile.
Definition: FileFormats.cpp:36
int sf_num_encodings()
Get the number of data encodings libsndfile supports (in any container or none.
Definition: FileFormats.cpp:74
static OSType sf_header_mactype(int format)
XO("Cut/Copy/Paste")
XXO("&Cut/Copy/Paste Toolbar")
static const auto exts
Definition: ImportAUP.cpp:56
#define LAT1CTOWX(X)
Definition: Internat.h:158
std::unique_ptr< Character[], freer > MallocString
Definition: MemoryX.h:146
constexpr sampleFormat int16Sample
Definition: SampleFormat.h:41
constexpr sampleFormat int24Sample
Definition: SampleFormat.h:42
sampleFormat
The ordering of these values with operator < agrees with the order of increasing bit width.
Definition: SampleFormat.h:30
constexpr sampleFormat widestSampleFormat
Definition: SampleFormat.h:45
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Abstract base class used in importing a file.
Extend wxArrayString with move operations and construction and insertion fromstd::initializer_list.
int operator()(SNDFILE *) const