fix crash when copy'ing latent plugins
[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 "pbd/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 1; /* ridiculous but used as a magic value */
137         default:
138                 // we don't handle anything else within ardour
139                 return 0;
140         }
141 }
142
143 string
144 sndfile_major_format(int format)
145 {
146         static map<int, string> m;
147
148         if(m.empty()){
149                 SF_FORMAT_INFO format_info;
150                 int count;
151                 sf_command(0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof (int));
152                 for (int i = 0; i < count; ++i){
153                         format_info.format = i;
154                         sf_command (0, SFC_GET_FORMAT_MAJOR,
155                                         &format_info, sizeof (format_info));
156                         m[format_info.format & SF_FORMAT_TYPEMASK] = format_info.name;
157
158                         /* normalize a couple of names rather than use what libsndfile gives us */
159
160                         if (strncasecmp (format_info.name, "OGG", 3) == 0) {
161                                 m[format_info.format & SF_FORMAT_TYPEMASK] = "Ogg";
162                         } else if (strncasecmp (format_info.name, "WAV", 3) == 0) {
163                                 m[format_info.format & SF_FORMAT_TYPEMASK] = "WAV";
164                         } else {
165                                 m[format_info.format & SF_FORMAT_TYPEMASK] = format_info.name;
166                         }
167                 }
168         }
169
170         map<int, string>::iterator p = m.find(format & SF_FORMAT_TYPEMASK);
171         if(p != m.end()){
172                 return m[format & SF_FORMAT_TYPEMASK];
173         } else {
174                 return "-Unknown-";
175         }
176 }
177
178 string
179 sndfile_minor_format(int format)
180 {
181         static map<int, string> m;
182
183         if(m.empty()){
184                 SF_FORMAT_INFO format_info;
185                 int count;
186                 sf_command(0, SFC_GET_FORMAT_SUBTYPE_COUNT, &count, sizeof (int));
187                 for (int i = 0; i < count; ++i){
188                         format_info.format = i;
189                         sf_command (0, SFC_GET_FORMAT_SUBTYPE,
190                                         &format_info, sizeof (format_info));
191                         m[format_info.format & SF_FORMAT_SUBMASK] = format_info.name;
192                 }
193         }
194
195         map<int, string>::iterator p = m.find(format & SF_FORMAT_SUBMASK);
196         if(p != m.end()){
197                 return m[format & SF_FORMAT_SUBMASK];
198         } else {
199                 return "-Unknown-";
200         }
201 }
202