#include "sourceoutput.h"
#include "tokenizer.h"
#include "strbuf.h"
+#include "namereg.h"
struct cli {
struct core *core;
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 }
};
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;
+}
+
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);
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;
+}
#define memblock_assert_exclusive(b) assert((b)->ref == 1)
+void memchunk_make_writable(struct memchunk *c);
+
extern unsigned memblock_count, memblock_total;
#endif
+#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string.h>
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;
}
+#include <stdio.h>
#include <string.h>
#include <assert.h>
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);
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;
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;
+}
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
assert(s->monitor_source);
free(n);
- s->volume = 0xFF;
+ s->volume = VOLUME_NORM;
s->notify = NULL;
s->get_latency = NULL;
return -1;
if (n == 1) {
+ uint32_t volume = VOLUME_NORM;
struct sink_info *i = info[0].userdata;
assert(i);
*result = info[0].chunk;
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);
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);
return -1;
if (n == 1) {
+ uint32_t volume = VOLUME_NORM;
struct sink_info *i = info[0].userdata;
assert(i);
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);
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);
}
+
struct source *monitor_source;
- uint8_t volume;
+ uint32_t volume;
void (*notify)(struct sink*sink);
uint32_t (*get_latency)(struct sink *s);
#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;
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);
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,
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);
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