1 // sndfile helper functions
3 // Copyright (c) 2002 James McCartney. All rights reserved.
4 // Copyright (C) 2012 Tim Blechmann
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; see the file COPYING. If not, write to
18 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, USA.
21 #ifndef SC_SNDFILEHELPERS_HPP_INCLUDED
22 #define SC_SNDFILEHELPERS_HPP_INCLUDED
24 #include "SC_Errors.h"
29 #include "SC_DirUtils.h"
31 static inline int headerFormatFromString(const char *name
)
33 if (!name
) return SF_FORMAT_AIFF
;
34 if (stringCaseCompare(name
, "AIFF")) return SF_FORMAT_AIFF
;
35 if (stringCaseCompare(name
, "AIFC")) return SF_FORMAT_AIFF
;
36 if (stringCaseCompare(name
, "RIFF")) return SF_FORMAT_WAV
;
37 if (stringCaseCompare(name
, "WAVEX")) return SF_FORMAT_WAVEX
;
38 if (stringCaseCompare(name
, "WAVE")) return SF_FORMAT_WAV
;
39 if (stringCaseCompare(name
, "WAV" )) return SF_FORMAT_WAV
;
40 if (stringCaseCompare(name
, "Sun" )) return SF_FORMAT_AU
;
41 if (stringCaseCompare(name
, "IRCAM")) return SF_FORMAT_IRCAM
;
42 if (stringCaseCompare(name
, "NeXT")) return SF_FORMAT_AU
;
43 if (stringCaseCompare(name
, "raw")) return SF_FORMAT_RAW
;
44 if (stringCaseCompare(name
, "MAT4")) return SF_FORMAT_MAT4
;
45 if (stringCaseCompare(name
, "MAT5")) return SF_FORMAT_MAT5
;
46 if (stringCaseCompare(name
, "PAF")) return SF_FORMAT_PAF
;
47 if (stringCaseCompare(name
, "SVX")) return SF_FORMAT_SVX
;
48 if (stringCaseCompare(name
, "NIST")) return SF_FORMAT_NIST
;
49 if (stringCaseCompare(name
, "VOC")) return SF_FORMAT_VOC
;
50 if (stringCaseCompare(name
, "W64")) return SF_FORMAT_W64
;
51 if (stringCaseCompare(name
, "PVF")) return SF_FORMAT_PVF
;
52 if (stringCaseCompare(name
, "XI")) return SF_FORMAT_XI
;
53 if (stringCaseCompare(name
, "HTK")) return SF_FORMAT_HTK
;
54 if (stringCaseCompare(name
, "SDS")) return SF_FORMAT_SDS
;
55 if (stringCaseCompare(name
, "AVR")) return SF_FORMAT_AVR
;
56 if (stringCaseCompare(name
, "SD2")) return SF_FORMAT_SD2
;
57 if (stringCaseCompare(name
, "FLAC")) return SF_FORMAT_FLAC
;
58 // TODO allow other platforms to know vorbis once libsndfile 1.0.18 is established
59 #if defined(__APPLE__) || defined(_WIN32) || LIBSNDFILE_1018
60 if (stringCaseCompare(name
, "vorbis")) return SF_FORMAT_VORBIS
;
62 if (stringCaseCompare(name
, "CAF")) return SF_FORMAT_CAF
;
63 if (stringCaseCompare(name
, "RF64")) return SF_FORMAT_RF64
;
67 static inline int sampleFormatFromString(const char* name
)
69 if (!name
) return SF_FORMAT_PCM_16
;
71 size_t len
= strlen(name
);
72 if (len
< 1) return 0;
75 if (len
< 5) return 0;
76 if (name
[4] == '8') return SF_FORMAT_PCM_U8
; // uint8
78 } else if (name
[0] == 'i') {
79 if (len
< 4) return 0;
80 if (name
[3] == '8') return SF_FORMAT_PCM_S8
; // int8
81 else if (name
[3] == '1') return SF_FORMAT_PCM_16
; // int16
82 else if (name
[3] == '2') return SF_FORMAT_PCM_24
; // int24
83 else if (name
[3] == '3') return SF_FORMAT_PCM_32
; // int32
84 } else if (name
[0] == 'f') {
85 return SF_FORMAT_FLOAT
; // float
86 } else if (name
[0] == 'd') {
87 return SF_FORMAT_DOUBLE
; // double
88 } else if (name
[0] == 'm' || name
[0] == 'u') {
89 return SF_FORMAT_ULAW
; // mulaw ulaw
90 } else if (name
[0] == 'a') {
91 return SF_FORMAT_ALAW
; // alaw
96 static inline int sndfileFormatInfoFromStrings(struct SF_INFO
*info
, const char *headerFormatString
, const char *sampleFormatString
)
98 int headerFormat
= headerFormatFromString(headerFormatString
);
99 if (!headerFormat
) return kSCErr_Failed
;
101 int sampleFormat
= sampleFormatFromString(sampleFormatString
);
102 if (!sampleFormat
) return kSCErr_Failed
;
104 info
->format
= (unsigned int)(headerFormat
| sampleFormat
);
110 static inline int sndfileFormatInfoFromStrings(struct SF_INFO
*info
, const char *headerFormatString
, const char *sampleFormatString
)
112 return kSCErr_Failed
;
117 #endif /* SC_SNDFILEHELPERS_HPP_INCLUDED */