a couple of modernizations; parse RE sample types properly

git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/lennart@1710 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Lennart Poettering 2007-08-23 22:34:10 +00:00
parent c72d4c6b9e
commit c1cdcfde7c

View file

@ -27,58 +27,58 @@
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <assert.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <pulsecore/core-util.h> #include <pulsecore/core-util.h>
#include <pulsecore/macro.h>
#include "sample.h" #include "sample.h"
size_t pa_sample_size(const pa_sample_spec *spec) { size_t pa_sample_size(const pa_sample_spec *spec) {
assert(spec);
switch (spec->format) { static const size_t table[] = {
case PA_SAMPLE_U8: [PA_SAMPLE_U8] = 1,
case PA_SAMPLE_ULAW: [PA_SAMPLE_ULAW] = 1,
case PA_SAMPLE_ALAW: [PA_SAMPLE_ALAW] = 1,
return 1; [PA_SAMPLE_S16LE] = 2,
case PA_SAMPLE_S16LE: [PA_SAMPLE_S16BE] = 2,
case PA_SAMPLE_S16BE: [PA_SAMPLE_FLOAT32LE] = 4,
return 2; [PA_SAMPLE_FLOAT32BE] = 4
case PA_SAMPLE_FLOAT32LE: };
case PA_SAMPLE_FLOAT32BE:
return 4; pa_assert(spec);
default: pa_assert(spec->format >= 0);
assert(0); pa_assert(spec->format < PA_SAMPLE_MAX);
return 0;
} return table[spec->format];
} }
size_t pa_frame_size(const pa_sample_spec *spec) { size_t pa_frame_size(const pa_sample_spec *spec) {
assert(spec); pa_assert(spec);
return pa_sample_size(spec) * spec->channels; return pa_sample_size(spec) * spec->channels;
} }
size_t pa_bytes_per_second(const pa_sample_spec *spec) { size_t pa_bytes_per_second(const pa_sample_spec *spec) {
assert(spec); pa_assert(spec);
return spec->rate*pa_frame_size(spec); return spec->rate*pa_frame_size(spec);
} }
pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) { pa_usec_t pa_bytes_to_usec(uint64_t length, const pa_sample_spec *spec) {
assert(spec); pa_assert(spec);
return (pa_usec_t) (((double) length/pa_frame_size(spec)*1000000)/spec->rate); return (pa_usec_t) (((double) length/pa_frame_size(spec)*1000000)/spec->rate);
} }
size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) { size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) {
assert(spec); pa_assert(spec);
return (size_t) (((double) t * spec->rate / 1000000))*pa_frame_size(spec); return (size_t) (((double) t * spec->rate / 1000000))*pa_frame_size(spec);
} }
int pa_sample_spec_valid(const pa_sample_spec *spec) { int pa_sample_spec_valid(const pa_sample_spec *spec) {
assert(spec); pa_assert(spec);
if (spec->rate <= 0 || if (spec->rate <= 0 ||
spec->rate > PA_RATE_MAX || spec->rate > PA_RATE_MAX ||
@ -92,7 +92,8 @@ int pa_sample_spec_valid(const pa_sample_spec *spec) {
} }
int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) { int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) {
assert(a && b); pa_assert(a);
pa_assert(b);
return (a->format == b->format) && (a->rate == b->rate) && (a->channels == b->channels); return (a->format == b->format) && (a->rate == b->rate) && (a->channels == b->channels);
} }
@ -108,14 +109,16 @@ const char *pa_sample_format_to_string(pa_sample_format_t f) {
[PA_SAMPLE_FLOAT32BE] = "float32be", [PA_SAMPLE_FLOAT32BE] = "float32be",
}; };
if (f >= PA_SAMPLE_MAX) if (f < 0 || f >= PA_SAMPLE_MAX)
return NULL; return NULL;
return table[f]; return table[f];
} }
char *pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec) { char *pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec) {
assert(s && l && spec); pa_assert(s);
pa_assert(l);
pa_assert(spec);
if (!pa_sample_spec_valid(spec)) if (!pa_sample_spec_valid(spec))
pa_snprintf(s, l, "Invalid"); pa_snprintf(s, l, "Invalid");
@ -126,6 +129,8 @@ char *pa_sample_spec_snprint(char *s, size_t l, const pa_sample_spec *spec) {
} }
char* pa_bytes_snprint(char *s, size_t l, unsigned v) { char* pa_bytes_snprint(char *s, size_t l, unsigned v) {
pa_assert(s);
if (v >= ((unsigned) 1024)*1024*1024) if (v >= ((unsigned) 1024)*1024*1024)
pa_snprintf(s, l, "%0.1f GiB", ((double) v)/1024/1024/1024); pa_snprintf(s, l, "%0.1f GiB", ((double) v)/1024/1024/1024);
else if (v >= ((unsigned) 1024)*1024) else if (v >= ((unsigned) 1024)*1024)
@ -139,6 +144,7 @@ char* pa_bytes_snprint(char *s, size_t l, unsigned v) {
} }
pa_sample_format_t pa_parse_sample_format(const char *format) { pa_sample_format_t pa_parse_sample_format(const char *format) {
pa_assert(format);
if (strcasecmp(format, "s16le") == 0) if (strcasecmp(format, "s16le") == 0)
return PA_SAMPLE_S16LE; return PA_SAMPLE_S16LE;
@ -146,15 +152,19 @@ pa_sample_format_t pa_parse_sample_format(const char *format) {
return PA_SAMPLE_S16BE; return PA_SAMPLE_S16BE;
else if (strcasecmp(format, "s16ne") == 0 || strcasecmp(format, "s16") == 0 || strcasecmp(format, "16") == 0) else if (strcasecmp(format, "s16ne") == 0 || strcasecmp(format, "s16") == 0 || strcasecmp(format, "16") == 0)
return PA_SAMPLE_S16NE; return PA_SAMPLE_S16NE;
else if (strcasecmp(format, "s16re") == 0)
return PA_SAMPLE_S16RE;
else if (strcasecmp(format, "u8") == 0 || strcasecmp(format, "8") == 0) else if (strcasecmp(format, "u8") == 0 || strcasecmp(format, "8") == 0)
return PA_SAMPLE_U8; return PA_SAMPLE_U8;
else if (strcasecmp(format, "float32") == 0 || strcasecmp(format, "float32ne") == 0) else if (strcasecmp(format, "float32") == 0 || strcasecmp(format, "float32ne") == 0)
return PA_SAMPLE_FLOAT32; return PA_SAMPLE_FLOAT32NE;
else if (strcasecmp(format, "float32re") == 0)
return PA_SAMPLE_FLOAT32RE;
else if (strcasecmp(format, "float32le") == 0) else if (strcasecmp(format, "float32le") == 0)
return PA_SAMPLE_FLOAT32LE; return PA_SAMPLE_FLOAT32LE;
else if (strcasecmp(format, "float32be") == 0) else if (strcasecmp(format, "float32be") == 0)
return PA_SAMPLE_FLOAT32BE; return PA_SAMPLE_FLOAT32BE;
else if (strcasecmp(format, "ulaw") == 0) else if (strcasecmp(format, "ulaw") == 0 || strcasecmp(format, "mulaw") == 0)
return PA_SAMPLE_ULAW; return PA_SAMPLE_ULAW;
else if (strcasecmp(format, "alaw") == 0) else if (strcasecmp(format, "alaw") == 0)
return PA_SAMPLE_ALAW; return PA_SAMPLE_ALAW;