Use ISC license for RDFF (same idea, MIT style, just prettier).
[ardour.git] / libs / ardour / sndfile_helpers.cc
1 /*
2     Copyright (C) 2000-2007 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <strings.h>
21 #include <map>
22 #include <vector>
23
24 #include "pbd/convert.h"
25
26 #include <sndfile.h>
27 #include "ardour/sndfile_helpers.h"
28
29 #include "i18n.h"
30
31 using std::map;
32 using namespace std;
33
34 const char * const sndfile_header_formats_strings[SNDFILE_HEADER_FORMATS+1] = {
35         N_("WAV"),
36         N_("AIFF"),
37         N_("CAF"),
38         N_("W64 (64 bit WAV)"),
39         N_("FLAC"),
40         N_("Ogg/Vorbis"),
41         N_("raw (no header)"),
42         0
43 };
44
45 const char* const sndfile_file_endings_strings[SNDFILE_HEADER_FORMATS+1] = {
46         N_(".wav"),
47         N_(".aiff"),
48         N_(".caf"),
49         N_(".w64"),
50         N_(".flac"),
51         N_(".ogg"),
52         N_(".raw"),
53         0
54 };
55
56 int sndfile_header_formats[SNDFILE_HEADER_FORMATS] = {
57         SF_FORMAT_WAV,
58         SF_FORMAT_AIFF,
59         SF_FORMAT_CAF,
60         SF_FORMAT_W64,
61         SF_FORMAT_FLAC,
62         SF_FORMAT_OGG,
63         SF_FORMAT_RAW
64 };
65
66 const char * const sndfile_bitdepth_formats_strings[SNDFILE_BITDEPTH_FORMATS+1] = {
67         N_("Signed 16 bit PCM"),
68         N_("Signed 24 bit PCM"),
69         N_("Signed 32 bit PCM"),
70         N_("Signed 8 bit PCM"),
71         N_("32 bit float"),
72         0
73 };
74
75 int sndfile_bitdepth_formats[SNDFILE_BITDEPTH_FORMATS] = {
76         SF_FORMAT_PCM_16,
77         SF_FORMAT_PCM_24,
78         SF_FORMAT_PCM_32,
79         SF_FORMAT_PCM_S8,
80         SF_FORMAT_FLOAT
81 };
82
83 const char * const sndfile_endian_formats_strings[SNDFILE_ENDIAN_FORMATS+1] = {
84         N_("Little-endian (Intel)"),
85         N_("Big-endian (PowerPC)"),
86         0
87 };
88
89 int sndfile_endian_formats[SNDFILE_ENDIAN_FORMATS] = {
90         SF_ENDIAN_LITTLE,
91         SF_ENDIAN_BIG
92 };
93
94 int
95 sndfile_header_format_from_string (string str)
96 {
97         for (int n = 0; sndfile_header_formats_strings[n]; ++n) {
98                 if (str == sndfile_header_formats_strings[n]) {
99                         return sndfile_header_formats[n];
100                 }
101         }
102         return -1;
103 }
104
105 int
106 sndfile_bitdepth_format_from_string (string str)
107 {
108         for (int n = 0; sndfile_bitdepth_formats_strings[n]; ++n) {
109                 if (str == sndfile_bitdepth_formats_strings[n]) {
110                         return sndfile_bitdepth_formats[n];
111                 }
112         }
113         return -1;
114 }
115
116 int
117 sndfile_endian_format_from_string (string str)
118 {
119         for (int n = 0; sndfile_endian_formats_strings[n]; ++n) {
120                 if (str == sndfile_endian_formats_strings[n]) {
121                         return sndfile_endian_formats[n];
122                 }
123         }
124         return -1;
125 }
126
127 string
128 sndfile_file_ending_from_string (string str)
129 {
130         static vector<string> file_endings;
131
132         if (file_endings.empty()) {
133                 file_endings = I18N((const char **) sndfile_file_endings_strings);
134         }
135
136         for (int n = 0; sndfile_header_formats_strings[n]; ++n) {
137                 if (str == sndfile_header_formats_strings[n]) {
138                         return file_endings[n];
139                 }
140         }
141         return 0;
142 }
143
144 int
145 sndfile_data_width (int format)
146 {
147         int tval = format & 0xf;
148
149         switch (tval) {
150           case SF_FORMAT_PCM_S8:
151           case SF_FORMAT_PCM_U8:
152                 return 8;
153           case SF_FORMAT_PCM_16:
154                 return 16;
155           case SF_FORMAT_PCM_24:
156                 return 24;
157           case SF_FORMAT_PCM_32:
158                 return 32;
159           case SF_FORMAT_FLOAT:
160                 return 1; // heh, heh
161           default:
162             // we don't handle anything else within ardour
163                 return 0;
164         }
165 }
166
167 string
168 sndfile_major_format(int format)
169 {
170         static map<int, string> m;
171
172         if(m.empty()){
173                 SF_FORMAT_INFO format_info;
174                 int count;
175                 sf_command(0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int));
176                 for (int i = 0; i < count; ++i){
177                         format_info.format = i;
178                         sf_command (0, SFC_GET_FORMAT_MAJOR,
179                                         &format_info, sizeof (format_info));
180                         m[format_info.format & SF_FORMAT_TYPEMASK] = format_info.name;
181
182                         /* normalize a couple of names rather than use what libsndfile gives us */
183                         
184                         if (strncasecmp (format_info.name, "OGG", 3) == 0) {
185                                 m[format_info.format & SF_FORMAT_TYPEMASK] = "Ogg";
186                         } else if (strncasecmp (format_info.name, "WAV", 3) == 0) {
187                                 m[format_info.format & SF_FORMAT_TYPEMASK] = "WAV";
188                         } else {
189                                 m[format_info.format & SF_FORMAT_TYPEMASK] = format_info.name;
190                         }
191                 }
192         }
193
194         map<int, string>::iterator p = m.find(format & SF_FORMAT_TYPEMASK);
195         if(p != m.end()){
196                 return m[format & SF_FORMAT_TYPEMASK];
197         } else {
198                 return "-Unknown-";
199         }
200 }
201
202 string
203 sndfile_minor_format(int format)
204 {
205         static map<int, string> m;
206
207         if(m.empty()){
208                 SF_FORMAT_INFO format_info;
209                 int count;
210                 sf_command(0, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int));
211                 for (int i = 0; i < count; ++i){
212                         format_info.format = i;
213                         sf_command (0, SFC_GET_FORMAT_SUBTYPE,
214                                         &format_info, sizeof (format_info));
215                         m[format_info.format & SF_FORMAT_SUBMASK] = format_info.name;
216                 }
217         }
218
219         map<int, string>::iterator p = m.find(format & SF_FORMAT_SUBMASK);
220         if(p != m.end()){
221                 return m[format & SF_FORMAT_SUBMASK];
222         } else {
223                 return "-Unknown-";
224         }
225 }
226