2004-06-08 23:54:24 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
#include "sample.h"
|
|
|
|
|
|
|
|
|
|
struct sample_spec default_sample_spec = {
|
|
|
|
|
.format = SAMPLE_S16NE,
|
|
|
|
|
.rate = 44100,
|
|
|
|
|
.channels = 2
|
|
|
|
|
};
|
|
|
|
|
|
2004-06-16 00:05:30 +00:00
|
|
|
struct memblock *silence_memblock(struct memblock* b, struct sample_spec *spec) {
|
|
|
|
|
assert(b && b->data && spec);
|
2004-06-08 23:54:24 +00:00
|
|
|
memblock_assert_exclusive(b);
|
2004-06-16 00:05:30 +00:00
|
|
|
silence_memory(b->data, b->length, spec);
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void silence_memchunk(struct memchunk *c, struct sample_spec *spec) {
|
|
|
|
|
assert(c && c->memblock && c->memblock->data && spec && c->length);
|
|
|
|
|
memblock_assert_exclusive(c->memblock);
|
|
|
|
|
silence_memory(c->memblock->data+c->index, c->length, spec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void silence_memory(void *p, size_t length, struct sample_spec *spec) {
|
|
|
|
|
char c = 0;
|
|
|
|
|
assert(p && length && spec);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
switch (spec->format) {
|
|
|
|
|
case SAMPLE_U8:
|
|
|
|
|
c = 127;
|
|
|
|
|
break;
|
|
|
|
|
case SAMPLE_S16LE:
|
|
|
|
|
case SAMPLE_S16BE:
|
|
|
|
|
case SAMPLE_FLOAT32:
|
|
|
|
|
c = 0;
|
|
|
|
|
break;
|
|
|
|
|
case SAMPLE_ALAW:
|
|
|
|
|
case SAMPLE_ULAW:
|
|
|
|
|
c = 80;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-16 00:05:30 +00:00
|
|
|
memset(p, c, length);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t sample_size(struct sample_spec *spec) {
|
|
|
|
|
assert(spec);
|
2004-06-11 00:33:43 +00:00
|
|
|
size_t b = 1;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
switch (spec->format) {
|
|
|
|
|
case SAMPLE_U8:
|
|
|
|
|
case SAMPLE_ULAW:
|
|
|
|
|
case SAMPLE_ALAW:
|
|
|
|
|
b = 1;
|
|
|
|
|
break;
|
|
|
|
|
case SAMPLE_S16LE:
|
|
|
|
|
case SAMPLE_S16BE:
|
|
|
|
|
b = 2;
|
|
|
|
|
break;
|
|
|
|
|
case SAMPLE_FLOAT32:
|
|
|
|
|
b = 4;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return b * spec->channels;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t bytes_per_second(struct sample_spec *spec) {
|
|
|
|
|
assert(spec);
|
|
|
|
|
return spec->rate*sample_size(spec);
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
size_t mix_chunks(struct mix_info channels[], unsigned nchannels, void *data, size_t length, struct sample_spec *spec, uint8_t volume) {
|
2004-06-15 15:18:33 +00:00
|
|
|
unsigned c, d;
|
2004-06-15 17:05:03 +00:00
|
|
|
assert(channels && data && length && spec);
|
2004-06-15 15:18:33 +00:00
|
|
|
assert(spec->format == SAMPLE_S16NE);
|
|
|
|
|
|
|
|
|
|
for (d = 0;; d += sizeof(int16_t)) {
|
|
|
|
|
int32_t sum = 0;
|
|
|
|
|
|
|
|
|
|
if (d >= length)
|
|
|
|
|
return d;
|
|
|
|
|
|
|
|
|
|
for (c = 0; c < nchannels; c++) {
|
|
|
|
|
int32_t v;
|
|
|
|
|
uint8_t volume = channels[c].volume;
|
|
|
|
|
|
|
|
|
|
if (d >= channels[c].chunk.length)
|
|
|
|
|
return d;
|
|
|
|
|
|
|
|
|
|
if (volume == 0)
|
|
|
|
|
v = 0;
|
|
|
|
|
else {
|
2004-06-15 17:05:03 +00:00
|
|
|
v = *((int16_t*) (channels[c].chunk.memblock->data + channels[c].chunk.index + d));
|
2004-06-15 15:18:33 +00:00
|
|
|
|
|
|
|
|
if (volume != 0xFF)
|
|
|
|
|
v = v*volume/0xFF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sum += v;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-15 17:05:03 +00:00
|
|
|
if (volume == 0)
|
|
|
|
|
sum = 0;
|
|
|
|
|
else if (volume != 0xFF)
|
|
|
|
|
sum = sum*volume/0xFF;
|
|
|
|
|
|
2004-06-15 15:18:33 +00:00
|
|
|
if (sum < -0x8000) sum = -0x8000;
|
|
|
|
|
if (sum > 0x7FFF) sum = 0x7FFF;
|
2004-06-15 17:05:03 +00:00
|
|
|
|
|
|
|
|
*((int16_t*) data) = sum;
|
|
|
|
|
data += sizeof(int16_t);
|
2004-06-15 15:18:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-06-18 17:12:50 +00:00
|
|
|
|
|
|
|
|
uint32_t samples_usec(size_t length, struct sample_spec *spec) {
|
|
|
|
|
assert(spec);
|
|
|
|
|
|
|
|
|
|
return (uint32_t) (((double) length /sample_size(spec))/spec->rate*1000000);
|
|
|
|
|
}
|