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"
30 static inline int headerFormatFromString(const char *name
)
32 if (!name
) return SF_FORMAT_AIFF
;
33 if (strcasecmp(name
, "AIFF")==0) return SF_FORMAT_AIFF
;
34 if (strcasecmp(name
, "AIFC")==0) return SF_FORMAT_AIFF
;
35 if (strcasecmp(name
, "RIFF")==0) return SF_FORMAT_WAV
;
36 if (strcasecmp(name
, "WAVEX")==0) return SF_FORMAT_WAVEX
;
37 if (strcasecmp(name
, "WAVE")==0) return SF_FORMAT_WAV
;
38 if (strcasecmp(name
, "WAV" )==0) return SF_FORMAT_WAV
;
39 if (strcasecmp(name
, "Sun" )==0) return SF_FORMAT_AU
;
40 if (strcasecmp(name
, "IRCAM")==0) return SF_FORMAT_IRCAM
;
41 if (strcasecmp(name
, "NeXT")==0) return SF_FORMAT_AU
;
42 if (strcasecmp(name
, "raw")==0) return SF_FORMAT_RAW
;
43 if (strcasecmp(name
, "MAT4")==0) return SF_FORMAT_MAT4
;
44 if (strcasecmp(name
, "MAT5")==0) return SF_FORMAT_MAT5
;
45 if (strcasecmp(name
, "PAF")==0) return SF_FORMAT_PAF
;
46 if (strcasecmp(name
, "SVX")==0) return SF_FORMAT_SVX
;
47 if (strcasecmp(name
, "NIST")==0) return SF_FORMAT_NIST
;
48 if (strcasecmp(name
, "VOC")==0) return SF_FORMAT_VOC
;
49 if (strcasecmp(name
, "W64")==0) return SF_FORMAT_W64
;
50 if (strcasecmp(name
, "PVF")==0) return SF_FORMAT_PVF
;
51 if (strcasecmp(name
, "XI")==0) return SF_FORMAT_XI
;
52 if (strcasecmp(name
, "HTK")==0) return SF_FORMAT_HTK
;
53 if (strcasecmp(name
, "SDS")==0) return SF_FORMAT_SDS
;
54 if (strcasecmp(name
, "AVR")==0) return SF_FORMAT_AVR
;
55 if (strcasecmp(name
, "SD2")==0) return SF_FORMAT_SD2
;
56 if (strcasecmp(name
, "FLAC")==0) return SF_FORMAT_FLAC
;
57 // TODO allow other platforms to know vorbis once libsndfile 1.0.18 is established
58 #if defined(__APPLE__) || defined(_WIN32) || LIBSNDFILE_1018
59 if (strcasecmp(name
, "vorbis")==0) return SF_FORMAT_VORBIS
;
61 if (strcasecmp(name
, "CAF")==0) return SF_FORMAT_CAF
;
62 if (strcasecmp(name
, "RF64")==0) return SF_FORMAT_RF64
;
66 static inline int sampleFormatFromString(const char* name
)
68 if (!name
) return SF_FORMAT_PCM_16
;
70 size_t len
= strlen(name
);
71 if (len
< 1) return 0;
74 if (len
< 5) return 0;
75 if (name
[4] == '8') return SF_FORMAT_PCM_U8
; // uint8
77 } else if (name
[0] == 'i') {
78 if (len
< 4) return 0;
79 if (name
[3] == '8') return SF_FORMAT_PCM_S8
; // int8
80 else if (name
[3] == '1') return SF_FORMAT_PCM_16
; // int16
81 else if (name
[3] == '2') return SF_FORMAT_PCM_24
; // int24
82 else if (name
[3] == '3') return SF_FORMAT_PCM_32
; // int32
83 } else if (name
[0] == 'f') {
84 return SF_FORMAT_FLOAT
; // float
85 } else if (name
[0] == 'd') {
86 return SF_FORMAT_DOUBLE
; // double
87 } else if (name
[0] == 'm' || name
[0] == 'u') {
88 return SF_FORMAT_ULAW
; // mulaw ulaw
89 } else if (name
[0] == 'a') {
90 return SF_FORMAT_ALAW
; // alaw
95 static inline int sndfileFormatInfoFromStrings(struct SF_INFO
*info
, const char *headerFormatString
, const char *sampleFormatString
)
97 int headerFormat
= headerFormatFromString(headerFormatString
);
98 if (!headerFormat
) return kSCErr_Failed
;
100 int sampleFormat
= sampleFormatFromString(sampleFormatString
);
101 if (!sampleFormat
) return kSCErr_Failed
;
103 info
->format
= (unsigned int)(headerFormat
| sampleFormat
);
109 static inline int sndfileFormatInfoFromStrings(struct SF_INFO
*info
, const char *headerFormatString
, const char *sampleFormatString
)
111 return kSCErr_Failed
;
116 #endif /* SC_SNDFILEHELPERS_HPP_INCLUDED */