summaryrefslogtreecommitdiff
path: root/src/pulsecore/sndfile-util.c
blob: a951c0157d475c972402066212e8afa237d23910 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/***
  This file is part of PulseAudio.

  Copyright 2009 Lennart Poettering

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License,
  or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/* Shared between pacat/parec/paplay and the server */

#include <pulse/xmalloc.h>
#include <pulse/utf8.h>

#include <pulsecore/macro.h>

#include "sndfile-util.h"

int pa_sndfile_read_sample_spec(SNDFILE *sf, pa_sample_spec *ss) {
    SF_INFO sfi;
    int sf_errno;

    pa_assert(sf);
    pa_assert(ss);

    pa_zero(sfi);
    if ((sf_errno = sf_command(sf, SFC_GET_CURRENT_SF_INFO, &sfi, sizeof(sfi)))) {
        pa_log_error("sndfile: %s", sf_error_number(sf_errno));
        return -1;
    }

    switch (sfi.format & SF_FORMAT_SUBMASK) {

        case SF_FORMAT_PCM_16:
        case SF_FORMAT_PCM_U8:
        case SF_FORMAT_PCM_S8:
            ss->format = PA_SAMPLE_S16NE;
            break;

        case SF_FORMAT_PCM_24:
            ss->format = PA_SAMPLE_S24NE;
            break;

        case SF_FORMAT_PCM_32:
            ss->format = PA_SAMPLE_S32NE;
            break;

        case SF_FORMAT_ULAW:
            ss->format = PA_SAMPLE_ULAW;
            break;

        case SF_FORMAT_ALAW:
            ss->format = PA_SAMPLE_ALAW;
            break;

        case SF_FORMAT_FLOAT:
        case SF_FORMAT_DOUBLE:
        default:
            ss->format = PA_SAMPLE_FLOAT32NE;
            break;
    }

    ss->rate = (uint32_t) sfi.samplerate;
    ss->channels = (uint8_t) sfi.channels;

    if (!pa_sample_spec_valid(ss))
        return -1;

    return 0;
}

int pa_sndfile_write_sample_spec(SF_INFO *sfi, pa_sample_spec *ss) {
    pa_assert(sfi);
    pa_assert(ss);

    sfi->samplerate = (int) ss->rate;
    sfi->channels = (int) ss->channels;

    if (pa_sample_format_is_le(ss->format) > 0)
        sfi->format = SF_ENDIAN_LITTLE;
    else if (pa_sample_format_is_be(ss->format) > 0)
        sfi->format = SF_ENDIAN_BIG;

    switch (ss->format) {

        case PA_SAMPLE_U8:
            ss->format = PA_SAMPLE_S16NE;
            sfi->format = SF_FORMAT_PCM_U8;
            break;

        case PA_SAMPLE_S16LE:
        case PA_SAMPLE_S16BE:
            ss->format = PA_SAMPLE_S16NE;
            sfi->format |= SF_FORMAT_PCM_16;
            break;

        case PA_SAMPLE_S24LE:
        case PA_SAMPLE_S24BE:
            ss->format = PA_SAMPLE_S24NE;
            sfi->format |= SF_FORMAT_PCM_24;
            break;

        case PA_SAMPLE_S24_32LE:
        case PA_SAMPLE_S24_32BE:
            ss->format = PA_SAMPLE_S24_32NE;
            sfi->format |= SF_FORMAT_PCM_32;
            break;

        case PA_SAMPLE_S32LE:
        case PA_SAMPLE_S32BE:
            ss->format = PA_SAMPLE_S32NE;
            sfi->format |= SF_FORMAT_PCM_32;
            break;

        case PA_SAMPLE_ULAW:
            sfi->format = SF_FORMAT_ULAW;
            break;

        case PA_SAMPLE_ALAW:
            sfi->format = SF_FORMAT_ALAW;
            break;

        case PA_SAMPLE_FLOAT32LE:
        case PA_SAMPLE_FLOAT32BE:
        default:
            ss->format = PA_SAMPLE_FLOAT32NE;
            sfi->format |= SF_FORMAT_FLOAT;
            break;
    }

    if (!pa_sample_spec_valid(ss))
        return -1;

    return 0;
}

int pa_sndfile_read_channel_map(SNDFILE *sf, pa_channel_map *cm) {

    static const pa_channel_position_t table[] = {
        [SF_CHANNEL_MAP_MONO] =                  PA_CHANNEL_POSITION_MONO,
        [SF_CHANNEL_MAP_LEFT] =                  PA_CHANNEL_POSITION_FRONT_LEFT, /* libsndfile distinguishes left and front-left, which we don't */
        [SF_CHANNEL_MAP_RIGHT] =                 PA_CHANNEL_POSITION_FRONT_RIGHT,
        [SF_CHANNEL_MAP_CENTER] =                PA_CHANNEL_POSITION_FRONT_CENTER,
        [SF_CHANNEL_MAP_FRONT_LEFT] =            PA_CHANNEL_POSITION_FRONT_LEFT,
        [SF_CHANNEL_MAP_FRONT_RIGHT] =           PA_CHANNEL_POSITION_FRONT_RIGHT,
        [SF_CHANNEL_MAP_FRONT_CENTER] =          PA_CHANNEL_POSITION_FRONT_CENTER,
        [SF_CHANNEL_MAP_REAR_CENTER] =           PA_CHANNEL_POSITION_REAR_CENTER,
        [SF_CHANNEL_MAP_REAR_LEFT] =             PA_CHANNEL_POSITION_REAR_LEFT,
        [SF_CHANNEL_MAP_REAR_RIGHT] =            PA_CHANNEL_POSITION_REAR_RIGHT,
        [SF_CHANNEL_MAP_LFE] =                   PA_CHANNEL_POSITION_LFE,
        [SF_CHANNEL_MAP_FRONT_LEFT_OF_CENTER] =  PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER,
        [SF_CHANNEL_MAP_FRONT_RIGHT_OF_CENTER] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER,
        [SF_CHANNEL_MAP_SIDE_LEFT] =             PA_CHANNEL_POSITION_SIDE_LEFT,
        [SF_CHANNEL_MAP_SIDE_RIGHT] =            PA_CHANNEL_POSITION_SIDE_RIGHT,
        [SF_CHANNEL_MAP_TOP_CENTER] =            PA_CHANNEL_POSITION_TOP_CENTER,
        [SF_CHANNEL_MAP_TOP_FRONT_LEFT] =        PA_CHANNEL_POSITION_TOP_FRONT_LEFT,
        [SF_CHANNEL_MAP_TOP_FRONT_RIGHT] =       PA_CHANNEL_POSITION_TOP_FRONT_RIGHT,
        [SF_CHANNEL_MAP_TOP_FRONT_CENTER] =      PA_CHANNEL_POSITION_TOP_FRONT_CENTER,
        [SF_CHANNEL_MAP_TOP_REAR_LEFT] =         PA_CHANNEL_POSITION_TOP_REAR_LEFT,
        [SF_CHANNEL_MAP_TOP_REAR_RIGHT] =        PA_CHANNEL_POSITION_TOP_REAR_RIGHT,
        [SF_CHANNEL_MAP_TOP_REAR_CENTER] =       PA_CHANNEL_POSITION_TOP_REAR_CENTER
    };

    SF_INFO sfi;
    int sf_errno;
    int *channels;
    unsigned c;

    pa_assert(sf);
    pa_assert(cm);

    pa_zero(sfi);
    if ((sf_errno = sf_command(sf, SFC_GET_CURRENT_SF_INFO, &sfi, sizeof(sfi)))) {
        pa_log_error("sndfile: %s", sf_error_number(sf_errno));
        return -1;
    }

    channels = pa_xnew(int, sfi.channels);
    if (!sf_command(sf, SFC_GET_CHANNEL_MAP_INFO, channels, sizeof(channels[0]) * sfi.channels)) {
        pa_xfree(channels);
        return -1;
    }

    cm->channels = (uint8_t) sfi.channels;
    for (c = 0; c < cm->channels; c++) {
        if (channels[c] <= SF_CHANNEL_MAP_INVALID ||
            (unsigned) channels[c] >= PA_ELEMENTSOF(table)) {
            pa_xfree(channels);
            return -1;
        }

        cm->map[c] = table[channels[c]];
    }

    pa_xfree(channels);

    if (!pa_channel_map_valid(cm))
        return -1;

    return 0;
}

int pa_sndfile_write_channel_map(SNDFILE *sf, pa_channel_map *cm) {
    static const int table[PA_CHANNEL_POSITION_MAX] = {
        [PA_CHANNEL_POSITION_MONO] = SF_CHANNEL_MAP_MONO,

        [PA_CHANNEL_POSITION_FRONT_LEFT] = SF_CHANNEL_MAP_FRONT_LEFT,
        [PA_CHANNEL_POSITION_FRONT_RIGHT] = SF_CHANNEL_MAP_FRONT_RIGHT,
        [PA_CHANNEL_POSITION_FRONT_CENTER] = SF_CHANNEL_MAP_FRONT_CENTER,

        [PA_CHANNEL_POSITION_REAR_CENTER] = SF_CHANNEL_MAP_REAR_CENTER,
        [PA_CHANNEL_POSITION_REAR_LEFT] = SF_CHANNEL_MAP_REAR_LEFT,
        [PA_CHANNEL_POSITION_REAR_RIGHT] = SF_CHANNEL_MAP_REAR_RIGHT,

        [PA_CHANNEL_POSITION_LFE] = SF_CHANNEL_MAP_LFE,

        [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = SF_CHANNEL_MAP_FRONT_LEFT_OF_CENTER,
        [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = SF_CHANNEL_MAP_FRONT_RIGHT_OF_CENTER,

        [PA_CHANNEL_POSITION_SIDE_LEFT] = SF_CHANNEL_MAP_SIDE_LEFT,
        [PA_CHANNEL_POSITION_SIDE_RIGHT] = SF_CHANNEL_MAP_SIDE_RIGHT,

        [PA_CHANNEL_POSITION_AUX0] = -1,
        [PA_CHANNEL_POSITION_AUX1] = -1,
        [PA_CHANNEL_POSITION_AUX2] = -1,
        [PA_CHANNEL_POSITION_AUX3] = -1,
        [PA_CHANNEL_POSITION_AUX4] = -1,
        [PA_CHANNEL_POSITION_AUX5] = -1,
        [PA_CHANNEL_POSITION_AUX6] = -1,
        [PA_CHANNEL_POSITION_AUX7] = -1,
        [PA_CHANNEL_POSITION_AUX8] = -1,
        [PA_CHANNEL_POSITION_AUX9] = -1,
        [PA_CHANNEL_POSITION_AUX10] = -1,
        [PA_CHANNEL_POSITION_AUX11] = -1,
        [PA_CHANNEL_POSITION_AUX12] = -1,
        [PA_CHANNEL_POSITION_AUX13] = -1,
        [PA_CHANNEL_POSITION_AUX14] = -1,
        [PA_CHANNEL_POSITION_AUX15] = -1,
        [PA_CHANNEL_POSITION_AUX16] = -1,
        [PA_CHANNEL_POSITION_AUX17] = -1,
        [PA_CHANNEL_POSITION_AUX18] = -1,
        [PA_CHANNEL_POSITION_AUX19] = -1,
        [PA_CHANNEL_POSITION_AUX20] = -1,
        [PA_CHANNEL_POSITION_AUX21] = -1,
        [PA_CHANNEL_POSITION_AUX22] = -1,
        [PA_CHANNEL_POSITION_AUX23] = -1,
        [PA_CHANNEL_POSITION_AUX24] = -1,
        [PA_CHANNEL_POSITION_AUX25] = -1,
        [PA_CHANNEL_POSITION_AUX26] = -1,
        [PA_CHANNEL_POSITION_AUX27] = -1,
        [PA_CHANNEL_POSITION_AUX28] = -1,
        [PA_CHANNEL_POSITION_AUX29] = -1,
        [PA_CHANNEL_POSITION_AUX30] = -1,
        [PA_CHANNEL_POSITION_AUX31] = -1,

        [PA_CHANNEL_POSITION_TOP_CENTER] = SF_CHANNEL_MAP_TOP_CENTER,

        [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = SF_CHANNEL_MAP_TOP_FRONT_LEFT,
        [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = SF_CHANNEL_MAP_TOP_FRONT_RIGHT,
        [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = SF_CHANNEL_MAP_TOP_FRONT_CENTER ,

        [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = SF_CHANNEL_MAP_TOP_REAR_LEFT,
        [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = SF_CHANNEL_MAP_TOP_REAR_RIGHT,
        [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = SF_CHANNEL_MAP_TOP_REAR_CENTER,
    };

    int *channels;
    unsigned c;

    pa_assert(sf);
    pa_assert(cm);

    /* Suppress channel mapping for the obvious cases */
    if (cm->channels == 1 && cm->map[0] == PA_CHANNEL_POSITION_MONO)
        return 0;

    if (cm->channels == 2 &&
        cm->map[0] == PA_CHANNEL_POSITION_FRONT_LEFT &&
        cm->map[1] == PA_CHANNEL_POSITION_FRONT_RIGHT)
        return 0;

    channels = pa_xnew(int, cm->channels);
    for (c = 0; c < cm->channels; c++) {

        if (cm->map[c] < 0 ||
            cm->map[c] >= PA_CHANNEL_POSITION_MAX ||
            table[cm->map[c]] < 0) {
            pa_xfree(channels);
            return -1;
        }

        channels[c] = table[cm->map[c]];
    }

    if (!sf_command(sf, SFC_SET_CHANNEL_MAP_INFO, channels, sizeof(channels[0]) * cm->channels)) {
        pa_xfree(channels);
        return -1;
    }

    pa_xfree(channels);
    return 0;
}

void pa_sndfile_init_proplist(SNDFILE *sf, pa_proplist *p) {

    static const char* table[] = {
        [SF_STR_TITLE] = PA_PROP_MEDIA_TITLE,
        [SF_STR_COPYRIGHT] = PA_PROP_MEDIA_COPYRIGHT,
        [SF_STR_SOFTWARE] = PA_PROP_MEDIA_SOFTWARE,
        [SF_STR_ARTIST] = PA_PROP_MEDIA_ARTIST,
        [SF_STR_COMMENT] = "media.comment",
        [SF_STR_DATE] = "media.date"
    };

    SF_INFO sfi;
    SF_FORMAT_INFO fi;
    int sf_errno;
    unsigned c;

    pa_assert(sf);
    pa_assert(p);

    for (c = 0; c < PA_ELEMENTSOF(table); c++) {
        const char *s;
        char *t;

        if (!table[c])
            continue;

        if (!(s = sf_get_string(sf, c)))
            continue;

        t = pa_utf8_filter(s);
        pa_proplist_sets(p, table[c], t);
        pa_xfree(t);
    }

    pa_zero(sfi);
    if ((sf_errno = sf_command(sf, SFC_GET_CURRENT_SF_INFO, &sfi, sizeof(sfi)))) {
        pa_log_error("sndfile: %s", sf_error_number(sf_errno));
        return;
    }

    pa_zero(fi);
    fi.format = sfi.format;
    if (sf_command(sf, SFC_GET_FORMAT_INFO, &fi, sizeof(fi)) == 0 && fi.name) {
        char *t;

        t = pa_utf8_filter(fi.name);
        pa_proplist_sets(p, "media.format", t);
        pa_xfree(t);
    }
}

pa_sndfile_readf_t pa_sndfile_readf_function(const pa_sample_spec *ss) {
    pa_assert(ss);

    switch (ss->format) {
        case PA_SAMPLE_S16NE:
            return (pa_sndfile_readf_t) sf_readf_short;

        case PA_SAMPLE_S32NE:
        case PA_SAMPLE_S24_32NE:
            return (pa_sndfile_readf_t) sf_readf_int;

        case PA_SAMPLE_FLOAT32NE:
            return (pa_sndfile_readf_t) sf_readf_float;

        case PA_SAMPLE_ULAW:
        case PA_SAMPLE_ALAW:
        case PA_SAMPLE_S24NE:
            return NULL;

        default:
            pa_assert_not_reached();
    }
}

pa_sndfile_writef_t pa_sndfile_writef_function(const pa_sample_spec *ss) {
    pa_assert(ss);

    switch (ss->format) {
        case PA_SAMPLE_S16NE:
            return (pa_sndfile_writef_t) sf_writef_short;

        case PA_SAMPLE_S32NE:
        case PA_SAMPLE_S24_32NE:
            return (pa_sndfile_writef_t) sf_writef_int;

        case PA_SAMPLE_FLOAT32NE:
            return (pa_sndfile_writef_t) sf_writef_float;

        case PA_SAMPLE_ULAW:
        case PA_SAMPLE_ALAW:
        case PA_SAMPLE_S24NE:
            return NULL;

        default:
            pa_assert_not_reached();
    }
}

int pa_sndfile_format_from_string(const char *name) {
    int i, count = 0;

    if (!name[0])
        return -1;

    pa_assert_se(sf_command(NULL, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof(int)) == 0);

    for (i = 0; i < count; i++) {
        SF_FORMAT_INFO fi;
        pa_zero(fi);
        fi.format = i;

        pa_assert_se(sf_command(NULL, SFC_GET_FORMAT_MAJOR, &fi, sizeof(fi)) == 0);

        /* First try to match via full type string */
        if (strcasecmp(name, fi.name) == 0)
            return fi.format;

        /* Then, try to match via the full extension */
        if (strcasecmp(name, fi.extension) == 0)
            return fi.format;

        /* Then, try to match via the start of the type string */
        if (strncasecmp(name, fi.name, strlen(name)) == 0)
            return fi.format;
    }

    return -1;
}

void pa_sndfile_dump_formats(void) {
    int i, count = 0;

    pa_assert_se(sf_command(NULL, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof(int)) == 0);

    for (i = 0; i < count; i++) {
        SF_FORMAT_INFO fi;
        pa_zero(fi);
        fi.format = i;

        pa_assert_se(sf_command(NULL, SFC_GET_FORMAT_MAJOR, &fi, sizeof(fi)) == 0);
        printf("%s\t%s\n", fi.extension, fi.name);
    }
}