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#include "FileFormats.h"
18
19#include "wxArrayStringEx.h"
20#include "Internat.h"
21#include "MemoryX.h"
22#include "BasicUI.h"
23
24#ifndef SNDFILE_1
25#error Requires libsndfile 1.0 or higher
26#endif
27
28//
29// enumerating headers
30//
31
33{
34 int count;
35
36 sf_command(NULL, SFC_GET_FORMAT_MAJOR_COUNT,
37 &count, sizeof(count));
38
39 return count;
40}
41
43{
44 SF_FORMAT_INFO format_info;
45
46 memset(&format_info, 0, sizeof(format_info));
47 format_info.format = format;
48 sf_command(NULL, SFC_GET_FORMAT_MAJOR,
49 &format_info, sizeof (format_info)) ;
50
51 return LAT1CTOWX(format_info.name);
52}
53
54unsigned int sf_header_index_to_type(int i)
55{
56 SF_FORMAT_INFO format_info ;
57
58 memset(&format_info, 0, sizeof(format_info));
59 format_info.format = i;
60 sf_command (NULL, SFC_GET_FORMAT_MAJOR,
61 &format_info, sizeof (format_info));
62
63 return format_info.format & SF_FORMAT_TYPEMASK;
64}
65
66//
67// enumerating encodings
68//
69
71{
72 int count ;
73
74 sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int)) ;
75
76 return count;
77}
78
80{
81 SF_FORMAT_INFO format_info ;
82
83 memset(&format_info, 0, sizeof(format_info));
84 format_info.format = i;
85 sf_command (NULL, SFC_GET_FORMAT_SUBTYPE,
86 &format_info, sizeof (format_info));
87 return sf_normalize_name(format_info.name);
88}
89
91{
92 SF_FORMAT_INFO format_info ;
93
94 memset(&format_info, 0, sizeof(format_info));
95 format_info.format = i;
96 sf_command (NULL, SFC_GET_FORMAT_SUBTYPE,
97 &format_info, sizeof (format_info));
98
99 return format_info.format & SF_FORMAT_SUBMASK;
100}
101
102//
103// getting info about an actual SF format
104//
105
107{
108 SF_FORMAT_INFO format_info;
109
110 memset(&format_info, 0, sizeof(format_info));
111 format_info.format = (format & SF_FORMAT_TYPEMASK);
112 sf_command(NULL, SFC_GET_FORMAT_INFO, &format_info, sizeof(format_info));
113
114 return LAT1CTOWX(format_info.name);
115}
116
118{
119 SF_FORMAT_INFO format_info;
120 int i;
121 wxString s;
122
123 memset(&format_info, 0, sizeof(format_info));
124 format_info.format = (format & SF_FORMAT_TYPEMASK);
125 sf_command(NULL, SFC_GET_FORMAT_INFO, &format_info, sizeof(format_info));
126
127 MallocString<> tmp { strdup( format_info.name ) };
128 i = 0;
129 while(tmp[i]) {
130 if (tmp[i]==' ')
131 tmp[i] = 0;
132 else
133 i++;
134 }
135
136 s = LAT1CTOWX(tmp.get());
137
138 return s;
139}
140
142{
143 SF_FORMAT_INFO format_info;
144
145 memset(&format_info, 0, sizeof(format_info));
146 format_info.format = (format & SF_FORMAT_TYPEMASK);
147 sf_command(NULL, SFC_GET_FORMAT_INFO, &format_info, sizeof(format_info));
148
149 return LAT1CTOWX(format_info.extension);
150}
151
152wxString sf_encoding_name(int encoding)
153{
154 SF_FORMAT_INFO format_info;
155
156 memset(&format_info, 0, sizeof(format_info));
157 format_info.format = (encoding & SF_FORMAT_SUBMASK);
158 sf_command(NULL, SFC_GET_FORMAT_INFO, &format_info, sizeof(format_info));
159
160 return sf_normalize_name(format_info.name);
161}
162
164{
165 int count ;
166
167 sf_command (NULL, SFC_GET_SIMPLE_FORMAT_COUNT, &count, sizeof (int)) ;
168
169 return count;
170}
171
172static SF_FORMAT_INFO g_format_info;
173
174SF_FORMAT_INFO *sf_simple_format(int i)
175{
176 memset(&g_format_info, 0, sizeof(g_format_info));
177
178 g_format_info.format = i;
179 sf_command (NULL, SFC_GET_SIMPLE_FORMAT,
180 &g_format_info, sizeof(g_format_info));
181
182 return &g_format_info;
183}
184
186{
187 unsigned int subtype = format & SF_FORMAT_SUBMASK;
188 return (subtype == SF_FORMAT_FLOAT ||
189 subtype == SF_FORMAT_DOUBLE ||
190 subtype == SF_FORMAT_PCM_24 ||
191 subtype == SF_FORMAT_PCM_32);
192}
193
195{
196 unsigned int subtype = format & SF_FORMAT_SUBMASK;
197 return (subtype == SF_FORMAT_PCM_16 ||
198 subtype == SF_FORMAT_PCM_24 ||
199 subtype == SF_FORMAT_PCM_32);
200}
201
203 unsigned int subtype = format & SF_FORMAT_SUBMASK;
204 if( subtype == SF_FORMAT_PCM_S8 )
205 return 1;
206 if( subtype == SF_FORMAT_PCM_U8 )
207 return 1;
208 if( subtype == SF_FORMAT_PCM_16 )
209 return 2;
210 if( subtype == SF_FORMAT_PCM_24 )
211 return 3;
212 if( subtype == SF_FORMAT_PCM_32 )
213 return 4;
214 if( subtype == SF_FORMAT_FLOAT )
215 return 4;
216 if( subtype == SF_FORMAT_DOUBLE )
217 return 8;
218
219 // might be different to 2, but this is good enough for
220 // WAV and AIFF file size error trapping.
221 return 2;
222}
223
225{
226 unsigned int subtype = format & SF_FORMAT_SUBMASK;
227 if (subtype == SF_FORMAT_PCM_24)
228 return int24Sample;
230 return widestSampleFormat;
231 else
232 return int16Sample;
233}
234
235
237{
239 SF_FORMAT_INFO format_info;
240 int count, k;
241
242 memset(&format_info, 0, sizeof(format_info));
243
244 sf_command(NULL, SFC_GET_FORMAT_MAJOR_COUNT,
245 &count, sizeof(count));
246
247 for(k=0; k<count; k++) {
248 format_info.format = k;
249 sf_command(NULL, SFC_GET_FORMAT_MAJOR,
250 &format_info, sizeof (format_info)) ;
251
252 exts.push_back(LAT1CTOWX(format_info.extension));
253 }
254
255 // Some other extensions that are often sound files
256 // but aren't included by libsndfile
257
258 exts.insert( exts.end(), {
259 wxT("aif") , // AIFF file with a DOS-style extension
260 wxT("ircam") ,
261 wxT("snd") ,
262 wxT("svx") ,
263 wxT("svx8") ,
264 wxT("sv16") ,
265 } );
266
267 return exts;
268}
269
270wxString sf_normalize_name(const char *name)
271{
272 wxString n = LAT1CTOWX(name);
273
274 n.Replace(wxT("8 bit"), wxT("8-bit"));
275 n.Replace(wxT("16 bit"), wxT("16-bit"));
276 n.Replace(wxT("24 bit"), wxT("24-bit"));
277 n.Replace(wxT("32 bit"), wxT("32-bit"));
278 n.Replace(wxT("64 bit"), wxT("64-bit"));
279
280 return n;
281}
282
283#ifdef __WXMAC__
284
285// TODO: find out the appropriate OSType
286// for the ones with an '????'. The others
287// are at least the same type used by
288// SoundApp.
289
290#define NUM_HEADERS 13
291
292//
293// Mac OS 4-char type
294//
295
296# ifdef __UNIX__
297# include <CoreServices/CoreServices.h>
298# else
299# include <Types.h>
300# endif
301
303 'WAVE', // WAVE
304 'AIFF', // AIFF
305 'NeXT', // Sun/NeXT AU
306 'BINA', // RAW i.e. binary
307 'PAR ', // ??? Ensoniq PARIS
308 '8SVX', // Amiga IFF / SVX8
309 'NIST', // ??? NIST/Sphere
310 'VOC ', // VOC
311 '\?\?\?\?', // ?? Propellorheads Rex
312 'SF ', // ?? IRCAM
313 'W64 ', // ?? Wave64
314 'MAT4', // ?? Matlab 4
315 'MAT5', // ?? Matlab 5
316};
317
318static OSType sf_header_mactype(int format)
319{
320 if (format >= 0x10000)
321 return MacNames[(format/0x10000)-1];
322 else if (format>=0 && format<NUM_HEADERS)
323 return MacNames[format];
324 else
325 return '\?\?\?\?';
326}
327
328#endif // __WXMAC__
329
330//std::mutex libSndFileMutex;
331
332int SFFileCloser::operator() (SNDFILE *sf) const
333{
334 auto err = SFCall<int>(sf_close, sf);
335 if (err) {
336 char buffer[1000];
337 sf_error_str(sf, buffer, 1000);
339 /* i18n-hint: %s will be the error message from the libsndfile software library */
340 XO( "Error (file may not have been written): %s" )
341 // Not attempting to localize error messages
342 // from the library
343 .Format( buffer ));
344 }
345 return err;
346}
wxT("CloseDown"))
Toolkit-neutral facade for basic user interface services.
const TranslatableString name
Definition: Distortion.cpp:76
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:42
unsigned int sf_header_index_to_type(int i)
Definition: FileFormats.cpp:54
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:90
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:79
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
int sf_num_headers()
Get the number of container formats supported by libsndfile.
Definition: FileFormats.cpp:32
int sf_num_encodings()
Get the number of data encodings libsndfile supports (in any container or none.
Definition: FileFormats.cpp:70
static OSType sf_header_mactype(int format)
XO("Cut/Copy/Paste")
static const auto exts
Definition: ImportAUP.cpp:56
#define LAT1CTOWX(X)
Definition: Internat.h:158
std::unique_ptr< Character[], freer > MallocString
Definition: MemoryX.h:145
constexpr sampleFormat int16Sample
Definition: SampleFormat.h:43
constexpr sampleFormat int24Sample
Definition: SampleFormat.h:44
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:47
Abstract base class used in importing a file.
Extend wxArrayString with move operations and construction and insertion fromstd::initializer_list.
MessageBoxResult ShowMessageBox(const TranslatableString &message, MessageBoxOptions options={})
Show a modal message box with either Ok or Yes and No, and optionally Cancel.
Definition: BasicUI.h:277
int operator()(SNDFILE *) const