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