forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileFormats.cpp
More file actions
304 lines (230 loc) · 6.71 KB
/
FileFormats.cpp
File metadata and controls
304 lines (230 loc) · 6.71 KB
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
/**********************************************************************
Audacity: A Digital Audio Editor
FileFormats.cpp
Dominic Mazzoni
*******************************************************************//*!
\file FileFormats.cpp
\brief Works with libsndfile to provide encoding and other file
information.
*//*******************************************************************/
#include <wx/arrstr.h>
#include <wx/intl.h>
#include "sndfile.h"
#ifndef SNDFILE_1
#error Requires libsndfile 1.0 or higher
#endif
#include "FileFormats.h"
#include "Internat.h"
//
// 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;
char *tmp;
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));
tmp = (char *)malloc(strlen(format_info.name)+1);
strcpy(tmp, format_info.name);
i = 0;
while(tmp[i]) {
if (tmp[i]==' ')
tmp[i] = 0;
else
i++;
}
s = LAT1CTOWX(tmp);
free(tmp);
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);
}
wxArrayString sf_get_all_extensions()
{
wxArrayString 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.Add(LAT1CTOWX(format_info.extension));
}
// Some other extensions that are often sound files
// but aren't included by libsndfile
exts.Add(wxT("aif")); // AIFF file with a DOS-style extension
exts.Add(wxT("ircam"));
exts.Add(wxT("snd"));
exts.Add(wxT("svx"));
exts.Add(wxT("svx8"));
exts.Add(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
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
};
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__
#include <wx/msgdlg.h>
ODLock libSndFileMutex;
void SFFileCloser::operator() (SNDFILE *sf) const
{
auto err = SFCall<int>(sf_close, sf);
if (err) {
char buffer[1000];
sf_error_str(sf, buffer, 1000);
wxMessageBox(wxString::Format
/* i18n-hint: %s will be the error message from libsndfile */
(_("Error (file may not have been written): %s"),
buffer));
}
}