list_devices example: more detailed output

This commit is contained in:
Andrew Kelley 2015-07-10 03:00:29 -07:00
parent 7d45b15036
commit 6c2226a6d7
5 changed files with 30 additions and 39 deletions

View file

@ -31,27 +31,31 @@ static void print_channel_layout(const struct SoundIoChannelLayout *layout) {
}
static void print_device(struct SoundIoDevice *device, bool is_default) {
const char *purpose_str;
const char *default_str;
const char *raw_str;
if (soundio_device_purpose(device) == SoundIoDevicePurposeOutput) {
purpose_str = "playback";
default_str = is_default ? " (default)" : "";
} else {
purpose_str = "recording";
default_str = is_default ? " (default)" : "";
}
raw_str = device->is_raw ? "(raw) " : "";
const char *description = soundio_device_description(device);
int sample_rate = device->sample_rate_max;
fprintf(stderr, "%s%s device: ", raw_str, purpose_str);
const char *default_str = is_default ? " (default)" : "";
const char *raw_str = device->is_raw ? " (raw)" : "";
fprintf(stderr, "%s%s%s\n", device->description, default_str, raw_str);
fprintf(stderr, " name: %s\n", device->name);
if (device->probe_error) {
fprintf(stderr, "[%s] %s%s\n", soundio_strerror(device->probe_error),
description, default_str);
fprintf(stderr, " probe error: %s\n", soundio_strerror(device->probe_error));
} else {
print_channel_layout(soundio_device_channel_layout(device));
fprintf(stderr, " %d Hz %s%s\n", sample_rate, description, default_str);
fprintf(stderr, " channel layout: ");
print_channel_layout(&device->channel_layout);
fprintf(stderr, "\n");
fprintf(stderr, " min sample rate: %d\n", device->sample_rate_min);
fprintf(stderr, " max sample rate: %d\n", device->sample_rate_max);
if (device->sample_rate_current)
fprintf(stderr, " current sample rate: %d\n", device->sample_rate_current);
fprintf(stderr, " formats: ");
for (int i = 0; i < device->format_count; i += 1) {
const char *comma = (i == device->format_count - 1) ? "" : ", ";
fprintf(stderr, "%s%s", soundio_format_string(device->formats[i]), comma);
}
fprintf(stderr, "\n");
if (device->current_format != SoundIoFormatInvalid)
fprintf(stderr, " current format: %s\n", soundio_format_string(device->current_format));
}
fprintf(stderr, "\n");
}
static int list_devices(struct SoundIo *soundio) {
@ -61,18 +65,20 @@ static int list_devices(struct SoundIo *soundio) {
int default_output = soundio_get_default_output_device_index(soundio);
int default_input = soundio_get_default_input_device_index(soundio);
fprintf(stderr, "--------Input Devices--------\n\n");
for (int i = 0; i < input_count; i += 1) {
struct SoundIoDevice *device = soundio_get_input_device(soundio, i);
print_device(device, default_input == i);
soundio_device_unref(device);
}
fprintf(stderr, "\n--------Output Devices--------\n\n");
for (int i = 0; i < output_count; i += 1) {
struct SoundIoDevice *device = soundio_get_output_device(soundio, i);
print_device(device, default_output == i);
soundio_device_unref(device);
}
fprintf(stderr, "%d devices found\n", input_count + output_count);
fprintf(stderr, "\n%d devices found\n", input_count + output_count);
return 0;
}

View file

@ -64,13 +64,10 @@ int main(int argc, char **argv) {
if (!in_device)
panic("could not get input device: out of memory");
fprintf(stderr, "Input device: %s\n", soundio_device_description(in_device));
fprintf(stderr, "Output device: %s\n", soundio_device_description(out_device));
fprintf(stderr, "Input device: %s\n", in_device->description);
fprintf(stderr, "Output device: %s\n", out_device->description);
const struct SoundIoChannelLayout *in_layout = soundio_device_channel_layout(in_device);
const struct SoundIoChannelLayout *out_layout = soundio_device_channel_layout(out_device);
if (!soundio_channel_layout_equal(in_layout, out_layout))
if (!soundio_channel_layout_equal(&in_device->channel_layout, &out_device->channel_layout))
panic("channel layouts not compatible");
double latency = 0.1;

View file

@ -85,9 +85,7 @@ int main(int argc, char **argv) {
if (!device)
panic("could not get output device: out of memory");
fprintf(stderr, "Output device: %s: %s\n",
soundio_device_name(device),
soundio_device_description(device));
fprintf(stderr, "Output device: %s: %s\n", device->name, device->description);
struct SoundIoOutStream *out_stream;
soundio_out_stream_create(device, SoundIoFormatFloat32NE, 48000,

View file

@ -186,7 +186,7 @@ struct SoundIoDevice {
struct SoundIo *soundio;
// `name` uniquely identifies this device. `description` is user-friendly
// text to describe the device.
// text to describe the device. These fields are UTF-8 encoded.
char *name;
char *description;
@ -392,14 +392,6 @@ int soundio_get_default_output_device_index(struct SoundIo *soundio);
void soundio_device_ref(struct SoundIoDevice *device);
void soundio_device_unref(struct SoundIoDevice *device);
// the name is the identifier for the device. UTF-8 encoded
const char *soundio_device_name(const struct SoundIoDevice *device);
// UTF-8 encoded
const char *soundio_device_description(const struct SoundIoDevice *device);
const struct SoundIoChannelLayout *soundio_device_channel_layout(const struct SoundIoDevice *device);
bool soundio_device_equal(
const struct SoundIoDevice *a,
const struct SoundIoDevice *b);

View file

@ -34,8 +34,6 @@ static void test_create_out_stream(void) {
assert(default_out_device_index >= 0);
struct SoundIoDevice *device = soundio_get_output_device(soundio, default_out_device_index);
assert(device);
soundio_device_name(device);
soundio_device_description(device);
struct SoundIoOutStream *out_stream;
soundio_out_stream_create(device, SoundIoFormatFloat32NE, 48000, 0.1, NULL,
write_callback, underrun_callback, &out_stream);