second (and hopefully) final part of changes to respond to header format changes...
[ArdourMidi.git] / libs / ardour / sndfile_helpers.cc
blob32ff48f48595e4bf83e29f8e9a963b0c379e4e35
1 /*
2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
20 #include <strings.h>
21 #include <map>
22 #include <vector>
24 #include "pbd/convert.h"
26 #include <sndfile.h>
27 #include "ardour/sndfile_helpers.h"
29 #include "i18n.h"
31 using std::map;
32 using namespace std;
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)"),
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"),
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
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"),
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
83 const char * const sndfile_endian_formats_strings[SNDFILE_ENDIAN_FORMATS+1] = {
84 N_("Little-endian (Intel)"),
85 N_("Big-endian (Mac)"),
89 int sndfile_endian_formats[SNDFILE_ENDIAN_FORMATS] = {
90 SF_ENDIAN_LITTLE,
91 SF_ENDIAN_BIG
94 int
95 sndfile_header_format_from_string (string str)
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];
102 return -1;
106 sndfile_bitdepth_format_from_string (string str)
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];
113 return -1;
117 sndfile_endian_format_from_string (string str)
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];
124 return -1;
127 string
128 sndfile_file_ending_from_string (string str)
130 static vector<string> file_endings;
132 if (file_endings.empty()) {
133 file_endings = I18N((const char **) sndfile_file_endings_strings);
136 for (int n = 0; sndfile_header_formats_strings[n]; ++n) {
137 if (str == sndfile_header_formats_strings[n]) {
138 return file_endings[n];
141 return 0;
145 sndfile_data_width (int format)
147 int tval = format & 0xf;
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;
167 string
168 sndfile_major_format(int format)
170 static map<int, string> m;
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;
182 /* normalize a couple of names rather than use what libsndfile gives us */
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;
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-";
202 string
203 sndfile_minor_format(int format)
205 static map<int, string> m;
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;
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-";