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