cubeb_devid cubeb_client::select_device()

in tools/cubeb-test.cpp [525:573]


cubeb_devid cubeb_client::select_device(cubeb_device_type type) const
{
  assert(type == CUBEB_DEVICE_TYPE_INPUT || type == CUBEB_DEVICE_TYPE_OUTPUT);

  cubeb_device_collection collection;
  if (cubeb_enumerate_devices(context, type, &collection) ==
      CUBEB_ERROR_NOT_SUPPORTED) {
    fprintf(stderr,
            "Not support %s device selection. Force to use default device\n",
            device_type_to_string(type));
    return nullptr;
  }

  assert(collection.count);
  fprintf(stderr, "Found %zu %s devices. Choose one:\n", collection.count,
          device_type_to_string(type));

  std::vector<cubeb_devid> devices;
  devices.emplace_back(nullptr);
  fprintf(stderr, "# 0\n\tname: system default device\n");
  for (size_t i = 0; i < collection.count; i++) {
    assert(collection.device[i].type == type);
    fprintf(stderr,
            "# %zu %s\n"
            "\tname: %s\n"
            "\tdevice id: %s\n"
            "\tmax channels: %u\n"
            "\tstate: %s\n",
            devices.size(),
            collection.device[i].preferred ? " (PREFERRED)" : "",
            collection.device[i].friendly_name, collection.device[i].device_id,
            collection.device[i].max_channels,
            device_state_to_string(collection.device[i].state));
    devices.emplace_back(collection.device[i].devid);
  }

  cubeb_device_collection_destroy(context, &collection);

  size_t number;
  std::cout << "Enter device number: ";
  std::cin >> number;
  while (!std::cin || number >= devices.size()) {
    std::cin.clear();
    std::cin.ignore(100, '\n');
    std::cout << "Error: Please enter a valid numeric input. Enter again: ";
    std::cin >> number;
  }
  return devices[number];
}