summaryrefslogtreecommitdiff
path: root/src/c_asndfile.c
blob: 116fa0c6b66a8b7ecf2dd441b9dd1fcb02db8052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189


//  Programmed by Jedidiah Barber
//  Released into the public domain


#include <sndfile.h>
#include "c_asndfile.h"



const int sf_false = SF_FALSE;
const int sf_true = SF_TRUE;

const int sfm_read = SFM_READ;
const int sfm_write = SFM_WRITE;
const int sfm_rdwr = SFM_RDWR;

const int sf_seek_set = SF_SEEK_SET;
const int sf_seek_cur = SF_SEEK_CUR;
const int sf_seek_end = SF_SEEK_END;

const int sf_str_title = SF_STR_TITLE;
const int sf_str_copyright = SF_STR_COPYRIGHT;
const int sf_str_software = SF_STR_SOFTWARE;
const int sf_str_artist = SF_STR_ARTIST;
const int sf_str_comment = SF_STR_COMMENT;
const int sf_str_date = SF_STR_DATE;
const int sf_str_album = SF_STR_ALBUM;
const int sf_str_license = SF_STR_LICENSE;
const int sf_str_tracknumber = SF_STR_TRACKNUMBER;
const int sf_str_genre = SF_STR_GENRE;

const int format_wav = SF_FORMAT_WAV;
const int format_aiff = SF_FORMAT_AIFF;
const int format_au = SF_FORMAT_AU;
const int format_raw = SF_FORMAT_RAW;
const int format_paf = SF_FORMAT_PAF;
const int format_svx = SF_FORMAT_SVX;
const int format_nist = SF_FORMAT_NIST;
const int format_voc = SF_FORMAT_VOC;
const int format_ircam = SF_FORMAT_IRCAM;
const int format_w64 = SF_FORMAT_W64;
const int format_mat4 = SF_FORMAT_MAT4;
const int format_mat5 = SF_FORMAT_MAT5;
const int format_pvf = SF_FORMAT_PVF;
const int format_xi = SF_FORMAT_XI;
const int format_htk = SF_FORMAT_HTK;
const int format_sds = SF_FORMAT_SDS;
const int format_avr = SF_FORMAT_AVR;
const int format_wavex = SF_FORMAT_WAVEX;
const int format_sd2 = SF_FORMAT_SD2;
const int format_flac = SF_FORMAT_FLAC;
const int format_caf = SF_FORMAT_CAF;
const int format_wve = SF_FORMAT_WVE;
const int format_ogg = SF_FORMAT_OGG;
const int format_mpc2k = SF_FORMAT_MPC2K;
const int format_rf64 = SF_FORMAT_RF64;
const int format_mpeg = SF_FORMAT_MPEG;

const int format_pcm_s8 = SF_FORMAT_PCM_S8;
const int format_pcm_16 = SF_FORMAT_PCM_16;
const int format_pcm_24 = SF_FORMAT_PCM_24;
const int format_pcm_32 = SF_FORMAT_PCM_32;
const int format_pcm_u8 = SF_FORMAT_PCM_U8;
const int format_float = SF_FORMAT_FLOAT;
const int format_double = SF_FORMAT_DOUBLE;
const int format_ulaw = SF_FORMAT_ULAW;
const int format_alaw = SF_FORMAT_ALAW;
const int format_ima_adpcm = SF_FORMAT_IMA_ADPCM;
const int format_ms_adpcm = SF_FORMAT_MS_ADPCM;
const int format_gsm610 = SF_FORMAT_GSM610;
const int format_vox_adpcm = SF_FORMAT_VOX_ADPCM;
const int format_nms_adpcm_16 = SF_FORMAT_NMS_ADPCM_16;
const int format_nms_adpcm_24 = SF_FORMAT_NMS_ADPCM_24;
const int format_nms_adpcm_32 = SF_FORMAT_NMS_ADPCM_32;
const int format_g721_32 = SF_FORMAT_G721_32;
const int format_g723_24 = SF_FORMAT_G723_24;
const int format_g723_40 = SF_FORMAT_G723_40;
const int format_dwvw_12 = SF_FORMAT_DWVW_12;
const int format_dwvw_16 = SF_FORMAT_DWVW_16;
const int format_dwvw_24 = SF_FORMAT_DWVW_24;
const int format_dwvw_n = SF_FORMAT_DWVW_N;
const int format_dpcm_8 = SF_FORMAT_DPCM_8;
const int format_dpcm_16 = SF_FORMAT_DPCM_16;
const int format_vorbis = SF_FORMAT_VORBIS;
const int format_opus = SF_FORMAT_OPUS;
const int format_alac_16 = SF_FORMAT_ALAC_16;
const int format_alac_20 = SF_FORMAT_ALAC_20;
const int format_alac_24 = SF_FORMAT_ALAC_24;
const int format_alac_32 = SF_FORMAT_ALAC_32;
const int format_mpeg_layer_i = SF_FORMAT_MPEG_LAYER_I;
const int format_mpeg_layer_ii = SF_FORMAT_MPEG_LAYER_II;
const int format_mpeg_layer_iii = SF_FORMAT_MPEG_LAYER_III;

const int endian_file = SF_ENDIAN_FILE;
const int endian_little = SF_ENDIAN_LITTLE;
const int endian_big = SF_ENDIAN_BIG;
const int endian_cpu = SF_ENDIAN_CPU;

const int err_no_error = SF_ERR_NO_ERROR;
const int err_unrecognised_format = SF_ERR_UNRECOGNISED_FORMAT;
const int err_system = SF_ERR_SYSTEM;
const int err_malformed_file = SF_ERR_MALFORMED_FILE;
const int err_unsupported_encoding = SF_ERR_UNSUPPORTED_ENCODING;



SNDFILE * asf_open(const char * path, int mode, Asf_Info * sfinfo) {
    SF_INFO actual;

    actual.frames = (sf_count_t)sfinfo->frames;
    actual.samplerate = sfinfo->samplerate;
    actual.channels = sfinfo->channels;
    actual.format = sfinfo->major & sfinfo->minor & sfinfo->endian;
    actual.sections = sfinfo->sections;
    actual.seekable = sfinfo->seekable;

    SNDFILE * result = sf_open(path, mode, &actual);
    if (result == NULL) { return NULL; }

    sfinfo->frames = (long long)actual.frames;
    sfinfo->samplerate = actual.samplerate;
    sfinfo->channels = actual.channels;
    sfinfo->major = actual.format & SF_FORMAT_TYPEMASK;
    sfinfo->minor = actual.format & SF_FORMAT_SUBMASK;
    sfinfo->endian = actual.format & SF_FORMAT_ENDMASK;
    sfinfo->sections = actual.sections;
    sfinfo->seekable = actual.seekable;

    return result;
}


int asf_format_check(Asf_Info * sfinfo) {
    SF_INFO actual;

    actual.frames = (sf_count_t)sfinfo->frames;
    actual.samplerate = sfinfo->samplerate;
    actual.channels = sfinfo->channels;
    actual.format = sfinfo->major & sfinfo->minor & sfinfo->endian;
    actual.sections = sfinfo->sections;
    actual.seekable = sfinfo->seekable;

    return sf_format_check (&actual);
}


int64_t asf_seek(SNDFILE * sndfile, int64_t frames, int whence) {
    return (int64_t) sf_seek(sndfile, (sf_count_t) frames, whence);
}


int64_t asf_readf_short(SNDFILE * sndfile, short *ptr, int64_t items) {
    return (int64_t) sf_readf_short(sndfile, ptr, (sf_count_t) items);
}
int64_t asf_readf_int(SNDFILE * sndfile, int * ptr, int64_t items) {
    return (int64_t) sf_readf_int(sndfile, ptr, (sf_count_t) items);
}
int64_t asf_readf_float(SNDFILE * sndfile, float * ptr, int64_t items) {
    return (int64_t) sf_readf_float(sndfile, ptr, (sf_count_t) items);
}
int64_t asf_readf_double(SNDFILE * sndfile, double * ptr, int64_t items) {
    return (int64_t) sf_readf_double(sndfile, ptr, (sf_count_t) items);
}


int64_t asf_writef_short(SNDFILE * sndfile, short * ptr, int64_t items) {
    return (int64_t) sf_writef_short(sndfile, ptr, (sf_count_t) items);
}
int64_t asf_writef_int(SNDFILE * sndfile, int * ptr, int64_t items) {
    return (int64_t) sf_writef_int(sndfile, ptr, (sf_count_t) items);
}
int64_t asf_writef_float(SNDFILE * sndfile, float * ptr, int64_t items) {
    return (int64_t) sf_writef_float(sndfile, ptr, (sf_count_t) items);
}
int64_t asf_writef_double(SNDFILE * sndfile, double * ptr, int64_t items) {
    return (int64_t) sf_writef_double(sndfile, ptr, (sf_count_t) items);
}


int64_t asf_read_raw(SNDFILE * sndfile, void * ptr, int64_t bytes) {
    return (int64_t) sf_read_raw(sndfile, ptr, (sf_count_t) bytes);
}
int64_t asf_write_raw(SNDFILE * sndfile, void * ptr, int64_t bytes) {
    return (int64_t) sf_write_raw(sndfile, ptr, (sf_count_t) bytes);
}