Merged with trunk R1141
[ardour.git] / libs / ardour / sndfile_helpers.cc
1 #include <map>
2 #include <vector>
3
4 #include <pbd/convert.h>
5
6 #include <sndfile.h>
7 #include <ardour/sndfile_helpers.h>
8
9 #include "i18n.h"
10
11 using std::map;
12 using namespace std;
13
14 const char * const sndfile_header_formats_strings[SNDFILE_HEADER_FORMATS+1] = {
15         N_("WAV"),
16         N_("AIFF"),
17         N_("raw (no header)"),
18         N_("PAF (Ensoniq Paris)"),
19         N_("AU (Sun/NeXT)"),
20         N_("IRCAM"),
21         N_("W64 (64 bit WAV)"),
22         0
23 };
24
25 const char* const sndfile_file_endings_strings[SNDFILE_HEADER_FORMATS+1] = {
26         N_(".wav"),
27         N_(".aiff"),
28         N_(".raw"),
29         N_(".paf"),
30         N_(".au"),
31         N_(".ircam"),
32         N_(".w64"),
33         0
34 };
35
36 int sndfile_header_formats[SNDFILE_HEADER_FORMATS] = {
37         SF_FORMAT_WAV,
38         SF_FORMAT_AIFF,
39         SF_FORMAT_RAW,
40         SF_FORMAT_PAF,
41         SF_FORMAT_AU,
42         SF_FORMAT_IRCAM,
43         SF_FORMAT_W64
44 };
45
46 const char * const sndfile_bitdepth_formats_strings[SNDFILE_BITDEPTH_FORMATS+1] = {
47         N_("16 bit"),
48         N_("24 bit"),
49         N_("32 bit"),
50         N_("8 bit"),
51         N_("float"),
52         0
53 };
54
55 int sndfile_bitdepth_formats[SNDFILE_BITDEPTH_FORMATS] = {
56         SF_FORMAT_PCM_16,
57         SF_FORMAT_PCM_24,
58         SF_FORMAT_PCM_32,
59         SF_FORMAT_PCM_S8,
60         SF_FORMAT_FLOAT
61 };
62
63 const char * const sndfile_endian_formats_strings[SNDFILE_ENDIAN_FORMATS+1] = {
64         N_("Little-endian (Intel)"),
65         N_("Big-endian (Mac)"),
66         0
67 };
68
69 int sndfile_endian_formats[SNDFILE_ENDIAN_FORMATS] = {
70         SF_ENDIAN_LITTLE,
71         SF_ENDIAN_BIG
72 };
73
74 int
75 sndfile_header_format_from_string (string str)
76 {
77         for (int n = 0; sndfile_header_formats_strings[n]; ++n) {
78                 if (str == sndfile_header_formats_strings[n]) {
79                         return sndfile_header_formats[n];
80                 }
81         }
82         return -1;
83 }
84
85 int
86 sndfile_bitdepth_format_from_string (string str)
87 {
88         for (int n = 0; sndfile_bitdepth_formats_strings[n]; ++n) {
89                 if (str == sndfile_bitdepth_formats_strings[n]) {
90                         return sndfile_bitdepth_formats[n];
91                 }
92         }
93         return -1;
94 }
95
96 int
97 sndfile_endian_format_from_string (string str)
98 {
99         for (int n = 0; sndfile_endian_formats_strings[n]; ++n) {
100                 if (str == sndfile_endian_formats_strings[n]) {
101                         return sndfile_endian_formats[n];
102                 }
103         }
104         return -1;
105 }
106
107 string
108 sndfile_file_ending_from_string (string str)
109 {       
110         static vector<string> file_endings;
111
112         if (file_endings.empty()) {
113                 file_endings = I18N((const char **) sndfile_file_endings_strings);
114         }
115
116         for (int n = 0; sndfile_header_formats_strings[n]; ++n) {
117                 if (str == sndfile_header_formats_strings[n]) {
118                         return file_endings[n];
119                 }
120         }
121         return 0;
122 }
123
124 int
125 sndfile_data_width (int format)
126 {
127         int tval = format & 0xf;
128
129         switch (tval) {
130           case SF_FORMAT_PCM_S8:
131           case SF_FORMAT_PCM_U8:
132                 return 8;
133           case SF_FORMAT_PCM_16:
134                 return 16;
135           case SF_FORMAT_PCM_24:
136                 return 24;
137           case SF_FORMAT_PCM_32:
138                 return 32;
139           case SF_FORMAT_FLOAT:
140                 return 1; // heh, heh
141           default:
142             // we don't handle anything else within ardour
143                 return 0;
144         }
145 }
146
147 string 
148 sndfile_major_format(int format)
149 {
150         static map<int, string> m;
151
152         if(m.empty()){
153                 SF_FORMAT_INFO format_info;
154                 int count;
155                 sf_command(0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int));
156                 for (int i = 0; i < count; ++i){
157                         format_info.format = i;
158                         sf_command (0, SFC_GET_FORMAT_MAJOR, 
159                                         &format_info, sizeof (format_info));
160                         m[format_info.format & SF_FORMAT_TYPEMASK] = format_info.name;
161                 }
162         }
163         
164         map<int, string>::iterator p = m.find(format & SF_FORMAT_TYPEMASK);
165         if(p != m.end()){
166                 return m[format & SF_FORMAT_TYPEMASK];
167         } else {
168                 return "-Unknown-";
169         }
170 }
171
172 string
173 sndfile_minor_format(int format)
174 {
175         static map<int, string> m;
176
177         if(m.empty()){
178                 SF_FORMAT_INFO format_info;
179                 int count;
180                 sf_command(0, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int));
181                 for (int i = 0; i < count; ++i){
182                         format_info.format = i;
183                         sf_command (0, SFC_GET_FORMAT_SUBTYPE, 
184                                         &format_info, sizeof (format_info));
185                         m[format_info.format & SF_FORMAT_SUBMASK] = format_info.name;
186                 }
187         }
188         
189         map<int, string>::iterator p = m.find(format & SF_FORMAT_SUBMASK);
190         if(p != m.end()){
191                 return m[format & SF_FORMAT_SUBMASK];
192         } else {
193                 return "-Unknown-";
194         }
195 }
196