From d571be6f5109ff5b256e4c14f391c916264f0a8e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 29 Jun 2004 20:37:24 +0000 Subject: volume work git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@42 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/cli.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++------- src/main.c | 2 +- src/memblock.c | 14 ++++++++ src/memblock.h | 2 ++ src/namereg.c | 18 +++++++++-- src/sample-util.c | 54 ++++++++++++++++++++++++++----- src/sample-util.h | 11 +++++-- src/sink.c | 22 +++++++++++-- src/sink.h | 2 +- src/sinkinput.c | 5 +-- src/sinkinput.h | 2 +- src/todo | 9 +++--- 12 files changed, 203 insertions(+), 35 deletions(-) diff --git a/src/cli.c b/src/cli.c index 09162351..10e780cd 100644 --- a/src/cli.c +++ b/src/cli.c @@ -13,6 +13,7 @@ #include "sourceoutput.h" #include "tokenizer.h" #include "strbuf.h" +#include "namereg.h" struct cli { struct core *core; @@ -45,20 +46,24 @@ static void cli_command_stat(struct cli *c, struct tokenizer *t); static void cli_command_info(struct cli *c, struct tokenizer *t); static void cli_command_load(struct cli *c, struct tokenizer *t); static void cli_command_unload(struct cli *c, struct tokenizer *t); +static void cli_command_sink_volume(struct cli *c, struct tokenizer *t); +static void cli_command_sink_input_volume(struct cli *c, struct tokenizer *t); static const struct command commands[] = { - { "exit", cli_command_exit, "Terminate the daemon", 1 }, - { "help", cli_command_help, "Show this help", 1 }, - { "modules", cli_command_modules, "List loaded modules", 1 }, - { "sinks", cli_command_sinks, "List loaded sinks", 1 }, - { "sources", cli_command_sources, "List loaded sources", 1 }, - { "clients", cli_command_clients, "List loaded clients", 1 }, - { "sink_inputs", cli_command_sink_inputs, "List sink inputs", 1 }, - { "source_outputs", cli_command_source_outputs, "List source outputs", 1 }, - { "stat", cli_command_stat, "Show memory block statistics", 1 }, - { "info", cli_command_info, "Show comprehensive status", 1 }, - { "load", cli_command_load, "Load a module (given by name and arguments)", 3 }, - { "unload", cli_command_unload, "Unload a module (specified by index)", 2 }, + { "exit", cli_command_exit, "Terminate the daemon", 1 }, + { "help", cli_command_help, "Show this help", 1 }, + { "modules", cli_command_modules, "List loaded modules", 1 }, + { "sinks", cli_command_sinks, "List loaded sinks", 1 }, + { "sources", cli_command_sources, "List loaded sources", 1 }, + { "clients", cli_command_clients, "List loaded clients", 1 }, + { "sink_inputs", cli_command_sink_inputs, "List sink inputs", 1 }, + { "source_outputs", cli_command_source_outputs, "List source outputs", 1 }, + { "stat", cli_command_stat, "Show memory block statistics", 1 }, + { "info", cli_command_info, "Show comprehensive status", 1 }, + { "load", cli_command_load, "Load a module (args: name, arguments)", 3}, + { "unload", cli_command_unload, "Unload a module (args: index)", 2}, + { "sink_volume", cli_command_sink_volume, "Set the volume of a sink (args: sink, volume)", 3}, + { "sink_input_volume", cli_command_sink_input_volume, "Set the volume of a sink input (args: sink input, volume)", 3}, { NULL, NULL, NULL, 0 } }; @@ -277,3 +282,71 @@ static void cli_command_unload(struct cli *c, struct tokenizer *t) { module_unload_request(c->core, m); } + +static void cli_command_sink_volume(struct cli *c, struct tokenizer *t) { + const char *n, *v; + char *x = NULL; + struct sink *sink; + long volume; + + if (!(n = tokenizer_get(t, 1))) { + ioline_puts(c->line, "You need to specify a sink either by its name or its index.\n"); + return; + } + + if (!(v = tokenizer_get(t, 2))) { + ioline_puts(c->line, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n"); + return; + } + + volume = strtol(v, &x, 0); + if (!x || *x != 0 || volume < 0) { + ioline_puts(c->line, "Failed to parse volume.\n"); + return; + } + + if (!(sink = namereg_get(c->core, n, NAMEREG_SINK))) { + ioline_puts(c->line, "No sink found by this name or index.\n"); + return; + } + + sink->volume = (uint32_t) volume; +} + +static void cli_command_sink_input_volume(struct cli *c, struct tokenizer *t) { + const char *n, *v; + char *x = NULL; + struct sink_input *si; + long index, volume; + + if (!(n = tokenizer_get(t, 1))) { + ioline_puts(c->line, "You need to specify a sink input by its index.\n"); + return; + } + + index = strtol(n, &x, 0); + if (!x || *x != 0 || index < 0) { + ioline_puts(c->line, "Failed to parse index.\n"); + return; + } + + if (!(v = tokenizer_get(t, 2))) { + ioline_puts(c->line, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n"); + return; + } + + x = NULL; + volume = strtol(v, &x, 0); + if (!x || *x != 0 || volume < 0) { + ioline_puts(c->line, "Failed to parse volume.\n"); + return; + } + + if (!(si = idxset_get_by_index(c->core->sink_inputs, (uint32_t) index))) { + ioline_puts(c->line, "No sink input found with this index.\n"); + return; + } + + si->volume = (uint32_t) volume; +} + diff --git a/src/main.c b/src/main.c index e50321f8..3512c5ba 100644 --- a/src/main.c +++ b/src/main.c @@ -38,7 +38,7 @@ int main(int argc, char *argv[]) { c = core_new(pa_mainloop_get_api(mainloop)); assert(c); - module_load(c, "module-oss-mmap", "/dev/dsp1"); + module_load(c, "module-oss", "/dev/dsp1"); /* module_load(c, "module-pipe-sink", NULL); module_load(c, "module-simple-protocol-tcp", NULL); module_load(c, "module-simple-protocol-unix", NULL); diff --git a/src/memblock.c b/src/memblock.c index 067243c5..79fe2977 100644 --- a/src/memblock.c +++ b/src/memblock.c @@ -78,3 +78,17 @@ void memblock_unref_fixed(struct memblock *b) { b->type = MEMBLOCK_DYNAMIC; } +void memchunk_make_writable(struct memchunk *c) { + struct memblock *n; + assert(c && c->memblock && c->memblock->ref >= 1); + + if (c->memblock->ref == 1) + return; + + n = memblock_new(c->length); + assert(n); + memcpy(n->data, c->memblock->data+c->index, c->length); + memblock_unref(c->memblock); + c->memblock = n; + c->index = 0; +} diff --git a/src/memblock.h b/src/memblock.h index cba11101..0c215e85 100644 --- a/src/memblock.h +++ b/src/memblock.h @@ -29,6 +29,8 @@ void memblock_unref_fixed(struct memblock*b); #define memblock_assert_exclusive(b) assert((b)->ref == 1) +void memchunk_make_writable(struct memchunk *c); + extern unsigned memblock_count, memblock_total; #endif diff --git a/src/namereg.c b/src/namereg.c index b286171d..0af46189 100644 --- a/src/namereg.c +++ b/src/namereg.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -87,11 +88,24 @@ void namereg_unregister(struct core *c, const char *name) { void* namereg_get(struct core *c, const char *name, enum namereg_type type) { struct namereg_entry *e; + uint32_t index; + char *x = NULL; + void *d = NULL; assert(c && name); - if (!(e = hashset_get(c->namereg, name))) + if ((e = hashset_get(c->namereg, name))) if (e->type == e->type) return e->data; - return NULL; + index = (uint32_t) strtol(name, &x, 0); + + if (!x || *x != 0) + return NULL; + + if (type == NAMEREG_SINK) + d = idxset_get_by_index(c->sinks, index); + else if (type == NAMEREG_SOURCE) + d = idxset_get_by_index(c->sources, index); + + return d; } diff --git a/src/sample-util.c b/src/sample-util.c index ff14548c..09511a3c 100644 --- a/src/sample-util.c +++ b/src/sample-util.c @@ -1,3 +1,4 @@ +#include #include #include @@ -46,7 +47,7 @@ void silence_memory(void *p, size_t length, struct pa_sample_spec *spec) { memset(p, c, length); } -size_t mix_chunks(struct mix_info channels[], unsigned nchannels, void *data, size_t length, struct pa_sample_spec *spec, uint8_t volume) { +size_t mix_chunks(struct mix_info channels[], unsigned nchannels, void *data, size_t length, struct pa_sample_spec *spec, uint32_t volume) { unsigned c, d; assert(channels && data && length && spec); assert(spec->format == PA_SAMPLE_S16NE); @@ -59,27 +60,27 @@ size_t mix_chunks(struct mix_info channels[], unsigned nchannels, void *data, si for (c = 0; c < nchannels; c++) { int32_t v; - uint8_t volume = channels[c].volume; + uint32_t volume = channels[c].volume; if (d >= channels[c].chunk.length) return d; - if (volume == 0) + if (volume == VOLUME_MUTE) v = 0; else { v = *((int16_t*) (channels[c].chunk.memblock->data + channels[c].chunk.index + d)); - if (volume != 0xFF) - v = v*volume/0xFF; + if (volume != VOLUME_NORM) + v = (int32_t) ((float)v*volume/VOLUME_NORM); } sum += v; } - if (volume == 0) + if (volume == VOLUME_MUTE) sum = 0; - else if (volume != 0xFF) - sum = sum*volume/0xFF; + else if (volume != VOLUME_NORM) + sum = (int32_t) ((float) sum*volume/VOLUME_NORM); if (sum < -0x8000) sum = -0x8000; if (sum > 0x7FFF) sum = 0x7FFF; @@ -88,3 +89,40 @@ size_t mix_chunks(struct mix_info channels[], unsigned nchannels, void *data, si data += sizeof(int16_t); } } + + +void volume_memchunk(struct memchunk*c, struct pa_sample_spec *spec, uint32_t volume) { + int16_t *d; + size_t n; + assert(c && spec && (c->length % pa_sample_size(spec) == 0)); + assert(spec->format == PA_SAMPLE_S16NE); + memblock_assert_exclusive(c->memblock); + + if (volume == VOLUME_NORM) + return; + + if (volume == VOLUME_MUTE) { + silence_memchunk(c, spec); + return; + } + + for (d = (c->memblock->data+c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) { + int32_t t = (int32_t)(*d); + + t *= volume; + t /= VOLUME_NORM; + + if (t < -0x8000) t = -0x8000; + if (t > 0x7FFF) t = 0x7FFF; + + *d = (int16_t) t; + } +} + +uint32_t volume_multiply(uint32_t a, uint32_t b) { + uint64_t p = a; + p *= b; + p /= VOLUME_NORM; + + return (uint32_t) p; +} diff --git a/src/sample-util.h b/src/sample-util.h index 0a3f7c89..2f2539d0 100644 --- a/src/sample-util.h +++ b/src/sample-util.h @@ -8,16 +8,23 @@ extern struct pa_sample_spec default_sample_spec; +#define VOLUME_NORM (0x100) +#define VOLUME_MUTE (0) + struct memblock *silence_memblock(struct memblock* b, struct pa_sample_spec *spec); void silence_memchunk(struct memchunk *c, struct pa_sample_spec *spec); void silence_memory(void *p, size_t length, struct pa_sample_spec *spec); struct mix_info { struct memchunk chunk; - uint8_t volume; + uint32_t volume; void *userdata; }; -size_t mix_chunks(struct mix_info channels[], unsigned nchannels, void *data, size_t length, struct pa_sample_spec *spec, uint8_t volume); +size_t mix_chunks(struct mix_info channels[], unsigned nchannels, void *data, size_t length, struct pa_sample_spec *spec, uint32_t volume); + +void volume_memchunk(struct memchunk*c, struct pa_sample_spec *spec, uint32_t volume); + +uint32_t volume_multiply(uint32_t a, uint32_t b); #endif diff --git a/src/sink.c b/src/sink.c index 5ab1a7a7..8a510f1b 100644 --- a/src/sink.c +++ b/src/sink.c @@ -39,7 +39,7 @@ struct sink* sink_new(struct core *core, const char *name, int fail, const struc assert(s->monitor_source); free(n); - s->volume = 0xFF; + s->volume = VOLUME_NORM; s->notify = NULL; s->get_latency = NULL; @@ -132,6 +132,7 @@ int sink_render(struct sink*s, size_t length, struct memchunk *result) { return -1; if (n == 1) { + uint32_t volume = VOLUME_NORM; struct sink_info *i = info[0].userdata; assert(i); *result = info[0].chunk; @@ -141,6 +142,14 @@ int sink_render(struct sink*s, size_t length, struct memchunk *result) { result->length = length; l = result->length; + + if (s->volume != VOLUME_NORM || info[0].volume != VOLUME_NORM) + volume = volume_multiply(s->volume, info[0].volume); + + if (volume != VOLUME_NORM) { + memchunk_make_writable(result); + volume_memchunk(result, &s->sample_spec, volume); + } } else { result->memblock = memblock_new(length); assert(result->memblock); @@ -164,6 +173,7 @@ int sink_render_into(struct sink*s, struct memchunk *target) { unsigned n; size_t l; assert(s && target && target->length && target->memblock && target->memblock->data); + memblock_assert_exclusive(target->memblock); n = fill_mix_info(s, info, MAX_MIX_CHANNELS); @@ -171,6 +181,7 @@ int sink_render_into(struct sink*s, struct memchunk *target) { return -1; if (n == 1) { + uint32_t volume = VOLUME_NORM; struct sink_info *i = info[0].userdata; assert(i); @@ -180,6 +191,12 @@ int sink_render_into(struct sink*s, struct memchunk *target) { memcpy(target->memblock->data+target->index, info[0].chunk.memblock->data + info[0].chunk.index, l); target->length = l; + + if (s->volume != VOLUME_NORM || info[0].volume != VOLUME_NORM) + volume = volume_multiply(s->volume, info[0].volume); + + if (volume != VOLUME_NORM) + volume_memchunk(target, &s->sample_spec, volume); } else target->length = l = mix_chunks(info, n, target->memblock->data+target->index, target->length, &s->sample_spec, s->volume); @@ -257,8 +274,9 @@ char *sink_list_to_string(struct core *c) { for (sink = idxset_first(c->sinks, &index); sink; sink = idxset_next(c->sinks, &index)) { assert(sink->monitor_source); - strbuf_printf(s, " %c index: %u, name: <%s>, volume: <0x%02x>, latency: <%u usec>, monitor_source: <%u>\n", sink == default_sink ? '*' : ' ', sink->index, sink->name, (unsigned) sink->volume, sink_get_latency(sink), sink->monitor_source->index); + strbuf_printf(s, " %c index: %u, name: <%s>, volume: <0x%04x>, latency: <%u usec>, monitor_source: <%u>\n", sink == default_sink ? '*' : ' ', sink->index, sink->name, (unsigned) sink->volume, sink_get_latency(sink), sink->monitor_source->index); } return strbuf_tostring_free(s); } + diff --git a/src/sink.h b/src/sink.h index a5b1ff68..f251c0b9 100644 --- a/src/sink.h +++ b/src/sink.h @@ -20,7 +20,7 @@ struct sink { struct source *monitor_source; - uint8_t volume; + uint32_t volume; void (*notify)(struct sink*sink); uint32_t (*get_latency)(struct sink *s); diff --git a/src/sinkinput.c b/src/sinkinput.c index b81c9c71..54bc98a6 100644 --- a/src/sinkinput.c +++ b/src/sinkinput.c @@ -4,6 +4,7 @@ #include "sinkinput.h" #include "strbuf.h" +#include "sample-util.h" struct sink_input* sink_input_new(struct sink *s, struct pa_sample_spec *spec, const char *name) { struct sink_input *i; @@ -22,7 +23,7 @@ struct sink_input* sink_input_new(struct sink *s, struct pa_sample_spec *spec, c i->get_latency = NULL; i->userdata = NULL; - i->volume = 0xFF; + i->volume = VOLUME_NORM; assert(s->core); r = idxset_put(s->core->sink_inputs, i, &i->index); @@ -64,7 +65,7 @@ char *sink_input_list_to_string(struct core *c) { for (i = idxset_first(c->sink_inputs, &index); i; i = idxset_next(c->sink_inputs, &index)) { assert(i->sink); - strbuf_printf(s, " index: %u, name: <%s>, sink: <%u>; volume: <0x%02x>, latency: <%u usec>\n", + strbuf_printf(s, " index: %u, name: <%s>, sink: <%u>; volume: <0x%04x>, latency: <%u usec>\n", i->index, i->name, i->sink->index, diff --git a/src/sinkinput.h b/src/sinkinput.h index f04ecb95..4fe39e2a 100644 --- a/src/sinkinput.h +++ b/src/sinkinput.h @@ -13,7 +13,7 @@ struct sink_input { char *name; struct sink *sink; struct pa_sample_spec sample_spec; - uint8_t volume; + uint32_t volume; int (*peek) (struct sink_input *i, struct memchunk *chunk); void (*drop) (struct sink_input *i, size_t length); diff --git a/src/todo b/src/todo index 78768be0..e1cdb9f1 100644 --- a/src/todo +++ b/src/todo @@ -2,16 +2,17 @@ recording sync() function more functions +- esound protocol: + recording +- split oss-dma? - simple library - simple control protocol: kill client/input/output - set_volume - resampling -- volume adjust on single sink input -- fp volume scaling (both < and > 1) -- esound protocol - config parser/cmdline - record testing +- mixing/volume +- kill() routines in all modules -- 0.1 - future cancellation -- cgit