Make undo/redo work for:
[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 #ifndef COMPILER_MSVC
21 #include <strings.h>
22 #endif
23 #include <map>
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_("CAF"),
37         N_("W64 (64 bit WAV)"),
38         N_("FLAC"),
39         N_("Ogg/Vorbis"),
40         N_("raw (no header)"),
41         0
42 };
43
44 const char* const sndfile_file_endings_strings[SNDFILE_HEADER_FORMATS+1] = {
45         N_(".wav"),
46         N_(".aiff"),
47         N_(".caf"),
48         N_(".w64"),
49         N_(".flac"),
50         N_(".ogg"),
51         N_(".raw"),
52         0
53 };
54
55 int sndfile_header_formats[SNDFILE_HEADER_FORMATS] = {
56         SF_FORMAT_WAV,
57         SF_FORMAT_AIFF,
58         SF_FORMAT_CAF,
59         SF_FORMAT_W64,
60         SF_FORMAT_FLAC,
61         SF_FORMAT_OGG,
62         SF_FORMAT_RAW
63 };
64
65 const char * const sndfile_bitdepth_formats_strings[SNDFILE_BITDEPTH_FORMATS+1] = {
66         N_("Signed 16 bit PCM"),
67         N_("Signed 24 bit PCM"),
68         N_("Signed 32 bit PCM"),
69         N_("Signed 8 bit PCM"),
70         N_("32 bit 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 (PowerPC)"),
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_by_index (int index)
95 {
96         if (index >= 0 && index < SNDFILE_HEADER_FORMATS) {
97                 return sndfile_header_formats[index];
98         }
99         return -1;
100 }
101
102 int
103 sndfile_bitdepth_format_by_index (int index)
104 {
105         if (index >= 0 && index < SNDFILE_BITDEPTH_FORMATS) {
106                 return sndfile_bitdepth_formats[index];
107         }
108         return -1;
109 }
110
111 int
112 sndfile_endian_format_by_index (int index)
113 {
114         if (index >= 0 && index < SNDFILE_ENDIAN_FORMATS) {
115                 return sndfile_endian_formats[index];
116         }
117         return -1;
118 }
119
120 int
121 sndfile_data_width (int format)
122 {
123         int tval = format & 0xf;
124
125         switch (tval) {
126         case SF_FORMAT_PCM_S8:
127         case SF_FORMAT_PCM_U8:
128                 return 8;
129         case SF_FORMAT_PCM_16:
130                 return 16;
131         case SF_FORMAT_PCM_24:
132                 return 24;
133         case SF_FORMAT_PCM_32:
134                 return 32;
135         case SF_FORMAT_FLOAT:
136                 return 32;
137         default:
138                 // we don't handle anything else within ardour
139                 return 0;
140         }
141 }
142
143 int
144 sndfile_data_width (ARDOUR::SampleFormat format)
145 {
146         switch (format) {
147         case ARDOUR::FormatInt16:
148                 return sndfile_data_width (SF_FORMAT_PCM_16);
149         case ARDOUR::FormatInt24:
150                 return sndfile_data_width (SF_FORMAT_PCM_24);
151         default:
152                 return sndfile_data_width (SF_FORMAT_FLOAT);
153         }
154 }
155
156 string
157 sndfile_major_format(int format)
158 {
159         static map<int, string> m;
160
161         if(m.empty()){
162                 SF_FORMAT_INFO format_info;
163                 int count;
164                 sf_command(0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int));
165                 for (int i = 0; i < count; ++i){
166                         format_info.format = i;
167                         sf_command (0, SFC_GET_FORMAT_MAJOR,
168                                         &format_info, sizeof (format_info));
169                         m[format_info.format & SF_FORMAT_TYPEMASK] = format_info.name;
170
171                         /* normalize a couple of names rather than use what libsndfile gives us */
172
173                         if (strncasecmp (format_info.name, "OGG", 3) == 0) {
174                                 m[format_info.format & SF_FORMAT_TYPEMASK] = "Ogg";
175                         } else if (strncasecmp (format_info.name, "WAV", 3) == 0) {
176                                 m[format_info.format & SF_FORMAT_TYPEMASK] = "WAV";
177                         } else {
178                                 m[format_info.format & SF_FORMAT_TYPEMASK] = format_info.name;
179                         }
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