summaryrefslogtreecommitdiff
path: root/src/pulse/sample.c
blob: 4b13a3370705b028f2d0ae018b28f819fd6cdc44 (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
/***
  This file is part of PulseAudio.

  Copyright 2004-2006 Lennart Poettering
  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB

  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 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

#include <stdio.h>
#include <math.h>
#include <string.h>

#include <pulse/timeval.h>
#include <pulse/i18n.h>

#include <pulsecore/core-util.h>
#include <pulsecore/macro.h>

#include "sample.h"

size_t pa_sample_size_of_format(pa_sample_format_t f) {

    static const size_t table[] = {
        [PA_SAMPLE_U8] = 1,
        [PA_SAMPLE_ULAW] = 1,
        [PA_SAMPLE_ALAW] = 1,
        [PA_SAMPLE_S16LE] = 2,
        [PA_SAMPLE_S16BE] = 2,
        [PA_SAMPLE_FLOAT32LE] = 4,
        [PA_SAMPLE_FLOAT32BE] = 4,
        [PA_SAMPLE_S32LE] = 4,
        [PA_SAMPLE_S32BE] = 4,
        [PA_SAMPLE_S24LE] = 3,
        [PA_SAMPLE_S24BE] = 3,
        [PA_SAMPLE_S24_32LE] = 4,
        [PA_SAMPLE_S24_32BE] = 4
    };

    pa_assert(f >= 0);
    pa_assert(f < PA_SAMPLE_MAX);

    return table[f];
}

size_t pa_sample_size(const pa_sample_spec *spec) {

    pa_assert(spec);
    pa_return_val_if_fail(pa_sample_spec_valid(spec), 0);

    return pa_sample_size_of_format(spec->format);
}

size_t pa_frame_size(const pa_sample_spec *spec) {
    pa_assert(spec);
    pa_return_val_if_fail(pa_sample_spec_valid(spec), 0);

    return pa_sample_size(spec) * spec->channels;
}

size_t pa_bytes_per_second(const pa_sample_spec *spec) {
    pa_assert(spec);
    pa_return_val_if_fail(pa_sample_spec_valid(spec), 0);

    return spec->rate*pa_frame_size(spec);
}

pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) {
    pa_assert(spec);
    pa_return_val_if_fail(pa_sample_spec_valid(spec), 0);

    return (((pa_usec_t) (length / pa_frame_size(spec)) * PA_USEC_PER_SEC) / spec->rate);
}

size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) {
    pa_assert(spec);
    pa_return_val_if_fail(pa_sample_spec_valid(spec), 0);

    return (size_t) (((t * spec->rate) / PA_USEC_PER_SEC)) * pa_frame_size(spec);
}

pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec) {
    pa_assert(spec);

    spec->format = PA_SAMPLE_INVALID;
    spec->rate = 0;
    spec->channels = 0;

    return spec;
}

int pa_sample_spec_valid(const pa_sample_spec *spec) {
    pa_assert(spec);

    if (spec->rate <= 0 ||
        spec->rate > PA_RATE_MAX ||
        spec->channels <= 0 ||
        spec->channels > PA_CHANNELS_MAX ||
        spec->format >= PA_SAMPLE_MAX ||
        spec->format < 0)
        return 0;

    return 1;
}

int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) {
    pa_assert(a);
    pa_assert(b);

    pa_return_val_if_fail(pa_sample_spec_valid(a), 0);
    pa_return_val_if_fail(pa_sample_spec_valid(b), 0);

    return
        (a->format == b->format) &&
        (a->rate == b->rate) &&
        (a->channels == b->channels);
}

const char *pa_sample_format_to_string(pa_sample_format_t f) {
    static const char* const table[]= {
        [PA_SAMPLE_U8] = "u8",
        [PA_SAMPLE_ALAW] = "aLaw",
        [PA_SAMPLE_ULAW] = "uLaw",
        [PA_SAMPLE_S16LE] = "s16le",
        [PA_SAMPLE_S16BE] = "s16be",
        [PA_SAMPLE_FLOAT32LE] = "float32le",
        [PA_SAMPLE_FLOAT32BE] = "float32be",
        [PA_SAMPLE_S32LE] = "s32le",
        [PA_SAMPLE_S32BE] = "s32be",
        [PA_SAMPLE_S24LE] = "s24le",
        [PA_SAMPLE_S24BE] = "s24be",
        [PA_SAMPLE_S24_32LE] = "s24-32le",
        [PA_SAMPLE_S24_32BE] = "s24-32be",
    };

    if (f < 0 || f >= PA_SAMPLE_MAX)
        return NULL;

    return table[f];
}

char *pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec) {
    pa_assert(s);
    pa_assert(l > 0);
    pa_assert(spec);

    pa_init_i18n();

    if (!pa_sample_spec_valid(spec))
        pa_snprintf(s, l, _("(invalid)"));
    else
        pa_snprintf(s, l, _("%s %uch %uHz"), pa_sample_format_to_string(spec->format), spec->channels, spec->rate);

    return s;
}

char* pa_bytes_snprint(char *s, size_t l, unsigned v) {
    pa_assert(s);
    pa_assert(l > 0);

    pa_init_i18n();

    if (v >= ((unsigned) 1024)*1024*1024)
        pa_snprintf(s, l, _("%0.1f GiB"), ((double) v)/1024/1024/1024);
    else if (v >= ((unsigned) 1024)*1024)
        pa_snprintf(s, l, _("%0.1f MiB"), ((double) v)/1024/1024);
    else if (v >= (unsigned) 1024)
        pa_snprintf(s, l, _("%0.1f KiB"), ((double) v)/1024);
    else
        pa_snprintf(s, l, _("%u B"), (unsigned) v);

    return s;
}

pa_sample_format_t pa_parse_sample_format(const char *format) {
    pa_assert(format);

    if (strcasecmp(format, "s16le") == 0)
        return PA_SAMPLE_S16LE;
    else if (strcasecmp(format, "s16be") == 0)
        return PA_SAMPLE_S16BE;
    else if (strcasecmp(format, "s16ne") == 0 || strcasecmp(format, "s16") == 0 || strcasecmp(format, "16") == 0)
        return PA_SAMPLE_S16NE;
    else if (strcasecmp(format, "s16re") == 0)
        return PA_SAMPLE_S16RE;
    else if (strcasecmp(format, "u8") == 0 || strcasecmp(format, "8") == 0)
        return PA_SAMPLE_U8;
    else if (strcasecmp(format, "float32") == 0 || strcasecmp(format, "float32ne") == 0 || strcasecmp(format, "float") == 0)
        return PA_SAMPLE_FLOAT32NE;
    else if (strcasecmp(format, "float32re") == 0)
        return PA_SAMPLE_FLOAT32RE;
    else if (strcasecmp(format, "float32le") == 0)
        return PA_SAMPLE_FLOAT32LE;
    else if (strcasecmp(format, "float32be") == 0)
        return PA_SAMPLE_FLOAT32BE;
    else if (strcasecmp(format, "ulaw") == 0 || strcasecmp(format, "mulaw") == 0)
        return PA_SAMPLE_ULAW;
    else if (strcasecmp(format, "alaw") == 0)
        return PA_SAMPLE_ALAW;
    else if (strcasecmp(format, "s32le") == 0)
        return PA_SAMPLE_S32LE;
    else if (strcasecmp(format, "s32be") == 0)
        return PA_SAMPLE_S32BE;
    else if (strcasecmp(format, "s32ne") == 0 || strcasecmp(format, "s32") == 0 || strcasecmp(format, "32") == 0)
        return PA_SAMPLE_S32NE;
    else if (strcasecmp(format, "s32re") == 0)
        return PA_SAMPLE_S24RE;
    else if (strcasecmp(format, "s24le") == 0)
        return PA_SAMPLE_S24LE;
    else if (strcasecmp(format, "s24be") == 0)
        return PA_SAMPLE_S24BE;
    else if (strcasecmp(format, "s24ne") == 0 || strcasecmp(format, "s24") == 0 || strcasecmp(format, "24") == 0)
        return PA_SAMPLE_S24NE;
    else if (strcasecmp(format, "s24re") == 0)
        return PA_SAMPLE_S24RE;
    else if (strcasecmp(format, "s24-32le") == 0)
        return PA_SAMPLE_S24LE;
    else if (strcasecmp(format, "s24-32be") == 0)
        return PA_SAMPLE_S24BE;
    else if (strcasecmp(format, "s24-32ne") == 0 || strcasecmp(format, "s24-32") == 0)
        return PA_SAMPLE_S24NE;
    else if (strcasecmp(format, "s24-32re") == 0)
        return PA_SAMPLE_S24RE;

    return -1;
}