Measure sample width in bytes

This commit is contained in:
Alexander Kojevnikov 2013-02-10 20:48:53 -08:00
parent b1b102044e
commit 389fed7a14
2 changed files with 7 additions and 8 deletions

@ -116,7 +116,7 @@ struct spek_audio_context * spek_audio_open(const char *path)
return cx;
}
cx->is_planar = av_sample_fmt_is_planar(cx->codec_context->sample_fmt);
cx->properties.width = 8 * av_get_bytes_per_sample(cx->codec_context->sample_fmt);
cx->properties.width = av_get_bytes_per_sample(cx->codec_context->sample_fmt);
switch (cx->codec_context->sample_fmt) {
case AV_SAMPLE_FMT_S16:
case AV_SAMPLE_FMT_S16P:
@ -176,8 +176,7 @@ int spek_audio_read(struct spek_audio_context *cx) {
}
// We have data, return it and come back for more later.
int buffer_size =
cx->frame->nb_samples * cx->properties.channels *
(cx->properties.width / 8);
cx->frame->nb_samples * cx->properties.channels * cx->properties.width;
if (buffer_size > cx->buffer_size) {
cx->properties.buffer = (uint8_t*)av_realloc(cx->properties.buffer, buffer_size);
cx->buffer_size = buffer_size;

@ -200,7 +200,7 @@ static void * reader_func(void *pp)
}
int pos = 0, prev_pos = 0;
int block_size = p->properties->width * p->properties->channels / 8;
int block_size = p->properties->width * p->properties->channels;
int size;
while ((size = spek_audio_read(p->cx)) > 0) {
if (p->quit) break;
@ -342,26 +342,26 @@ static float average_input(const struct spek_pipeline *p, void *buffer)
int channels = p->properties->channels;
float res = 0.0f;
if (p->properties->fp) {
if (p->properties->width == 32) {
if (p->properties->width == 4) {
float *b = (float*)buffer;
for (int i = 0; i < channels; i++) {
res += b[i];
}
} else {
assert(p->properties->width == 64);
assert(p->properties->width == 8);
double *b = (double*)buffer;
for (int i = 0; i < channels; i++) {
res += (float) b[i];
}
}
} else {
if (p->properties->width == 16) {
if (p->properties->width == 2) {
int16_t *b = (int16_t*)buffer;
for (int i = 0; i < channels; i++) {
res += b[i] / (float) INT16_MAX;
}
} else {
assert (p->properties->width == 32);
assert (p->properties->width == 4);
int32_t *b = (int32_t*)buffer;
for (int i = 0; i < channels; i++) {
res += b[i] / (float) INT32_MAX;