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