(native) Linux VST support from LinuxDSP
[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_by_index (int index)
96 {
97         if (index >= 0 && index < SNDFILE_HEADER_FORMATS) {
98                 return sndfile_header_formats[index];
99         }
100         return -1;
101 }
102
103 int
104 sndfile_bitdepth_format_by_index (int index)
105 {
106         if (index >= 0 && index < SNDFILE_BITDEPTH_FORMATS) {
107                 return sndfile_bitdepth_formats[index];
108         }
109         return -1;
110 }
111
112 int
113 sndfile_endian_format_by_index (int index)
114 {
115         if (index >= 0 && index < SNDFILE_ENDIAN_FORMATS) {
116                 return sndfile_endian_formats[index];
117         }
118         return -1;
119 }
120
121 int
122 sndfile_data_width (int format)
123 {
124         int tval = format & 0xf;
125
126         switch (tval) {
127           case SF_FORMAT_PCM_S8:
128           case SF_FORMAT_PCM_U8:
129                 return 8;
130           case SF_FORMAT_PCM_16:
131                 return 16;
132           case SF_FORMAT_PCM_24:
133                 return 24;
134           case SF_FORMAT_PCM_32:
135                 return 32;
136           case SF_FORMAT_FLOAT:
137                 return 1; // heh, heh
138           default:
139             // we don't handle anything else within ardour
140                 return 0;
141         }
142 }
143
144 string
145 sndfile_major_format(int format)
146 {
147         static map<int, string> m;
148
149         if(m.empty()){
150                 SF_FORMAT_INFO format_info;
151                 int count;
152                 sf_command(0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int));
153                 for (int i = 0; i < count; ++i){
154                         format_info.format = i;
155                         sf_command (0, SFC_GET_FORMAT_MAJOR,
156                                         &format_info, sizeof (format_info));
157                         m[format_info.format & SF_FORMAT_TYPEMASK] = format_info.name;
158
159                         /* normalize a couple of names rather than use what libsndfile gives us */
160
161                         if (strncasecmp (format_info.name, "OGG", 3) == 0) {
162                                 m[format_info.format & SF_FORMAT_TYPEMASK] = "Ogg";
163                         } else if (strncasecmp (format_info.name, "WAV", 3) == 0) {
164                                 m[format_info.format & SF_FORMAT_TYPEMASK] = "WAV";
165                         } else {
166                                 m[format_info.format & SF_FORMAT_TYPEMASK] = format_info.name;
167                         }
168                 }
169         }
170
171         map<int, string>::iterator p = m.find(format & SF_FORMAT_TYPEMASK);
172         if(p != m.end()){
173                 return m[format & SF_FORMAT_TYPEMASK];
174         } else {
175                 return "-Unknown-";
176         }
177 }
178
179 string
180 sndfile_minor_format(int format)
181 {
182         static map<int, string> m;
183
184         if(m.empty()){
185                 SF_FORMAT_INFO format_info;
186                 int count;
187                 sf_command(0, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int));
188                 for (int i = 0; i < count; ++i){
189                         format_info.format = i;
190                         sf_command (0, SFC_GET_FORMAT_SUBTYPE,
191                                         &format_info, sizeof (format_info));
192                         m[format_info.format & SF_FORMAT_SUBMASK] = format_info.name;
193                 }
194         }
195
196         map<int, string>::iterator p = m.find(format & SF_FORMAT_SUBMASK);
197         if(p != m.end()){
198                 return m[format & SF_FORMAT_SUBMASK];
199         } else {
200                 return "-Unknown-";
201         }
202 }
203