使用語音 SDK 選取音訊輸入裝置
本文說明如何取得連線至系統之音訊裝置的識別碼。 然後便可以在語音 SDK 中使用這些識別碼來選取音訊輸入。 您可以透過 AudioConfig
物件設定音訊裝置:
audioConfig = AudioConfig.FromMicrophoneInput("<device id>");
audioConfig = AudioConfig.FromMicrophoneInput("<device id>");
audio_config = AudioConfig(device_name="<device id>");
audioConfig = AudioConfiguration.FromMicrophoneInput("<device id>");
audioConfig = AudioConfiguration.fromMicrophoneInput("<device id>");
audioConfig = AudioConfiguration.fromMicrophoneInput("<device id>");
注意
麥克風無法供在 Node.js 中執行的 JavaScript 使用。
Windows 桌面應用程式上的音訊裝置識別碼
您可以從 Windows 桌面應用程式中的 IMMDevice
物件擷取音訊裝置端點識別碼字串。
下列程式碼範例說明如何用它在 C++ 中列舉音訊裝置:
#include <cstdio>
#include <mmdeviceapi.h>
#include <Functiondiscoverykeys_devpkey.h>
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
constexpr auto REFTIMES_PER_SEC = (10000000 * 25);
constexpr auto REFTIMES_PER_MILLISEC = 10000;
#define EXIT_ON_ERROR(hres) \
if (FAILED(hres)) { goto Exit; }
#define SAFE_RELEASE(punk) \
if ((punk) != NULL) \
{ (punk)->Release(); (punk) = NULL; }
void ListEndpoints();
int main()
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
ListEndpoints();
}
//-----------------------------------------------------------
// This function enumerates all active (plugged in) audio
// rendering endpoint devices. It prints the friendly name
// and endpoint ID string of each endpoint device.
//-----------------------------------------------------------
void ListEndpoints()
{
HRESULT hr = S_OK;
IMMDeviceEnumerator *pEnumerator = NULL;
IMMDeviceCollection *pCollection = NULL;
IMMDevice *pEndpoint = NULL;
IPropertyStore *pProps = NULL;
LPWSTR pwszID = NULL;
hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pEnumerator);
EXIT_ON_ERROR(hr);
hr = pEnumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &pCollection);
EXIT_ON_ERROR(hr);
UINT count;
hr = pCollection->GetCount(&count);
EXIT_ON_ERROR(hr);
if (count == 0)
{
printf("No endpoints found.\n");
}
// Each iteration prints the name of an endpoint device.
PROPVARIANT varName;
for (ULONG i = 0; i < count; i++)
{
// Get the pointer to endpoint number i.
hr = pCollection->Item(i, &pEndpoint);
EXIT_ON_ERROR(hr);
// Get the endpoint ID string.
hr = pEndpoint->GetId(&pwszID);
EXIT_ON_ERROR(hr);
hr = pEndpoint->OpenPropertyStore(
STGM_READ, &pProps);
EXIT_ON_ERROR(hr);
// Initialize the container for property value.
PropVariantInit(&varName);
// Get the endpoint's friendly-name property.
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
EXIT_ON_ERROR(hr);
// Print the endpoint friendly name and endpoint ID.
printf("Endpoint %d: \"%S\" (%S)\n", i, varName.pwszVal, pwszID);
CoTaskMemFree(pwszID);
pwszID = NULL;
PropVariantClear(&varName);
}
Exit:
CoTaskMemFree(pwszID);
pwszID = NULL;
PropVariantClear(&varName);
SAFE_RELEASE(pEnumerator);
SAFE_RELEASE(pCollection);
SAFE_RELEASE(pEndpoint);
SAFE_RELEASE(pProps);
}
在 C# 中,您可使用 NAudio 程式庫來存取 CoreAudio API 及列舉裝置,如下所示:
using System;
using NAudio.CoreAudioApi;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var enumerator = new MMDeviceEnumerator();
foreach (var endpoint in
enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active))
{
Console.WriteLine("{0} ({1})", endpoint.FriendlyName, endpoint.ID);
}
}
}
}
範例裝置識別碼為 {0.0.1.00000000}.{5f23ab69-6181-4f4a-81a4-45414013aac8}
。
UWP 上的音訊裝置識別碼
在通用 Windows 平台 (UWP) 上,您可以使用對應 DeviceInformation
物件的 Id()
屬性來取得音訊輸入裝置。
下列程式碼範例說明如何在 C++ 和 C# 中執行此步驟:
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Devices.Enumeration.h>
using namespace winrt::Windows::Devices::Enumeration;
void enumerateDeviceIds()
{
auto promise = DeviceInformation::FindAllAsync(DeviceClass::AudioCapture);
promise.Completed(
[](winrt::Windows::Foundation::IAsyncOperation<DeviceInformationCollection> const& sender,
winrt::Windows::Foundation::AsyncStatus /* asyncStatus */) {
auto info = sender.GetResults();
auto num_devices = info.Size();
for (const auto &device : info)
{
std::wstringstream ss{};
ss << "looking at device (of " << num_devices << "): " << device.Id().c_str() << "\n";
OutputDebugString(ss.str().c_str());
}
});
}
using Windows.Devices.Enumeration;
using System.Linq;
namespace helloworld {
private async void EnumerateDevices()
{
var devices = await DeviceInformation.FindAllAsync(DeviceClass.AudioCapture);
foreach (var device in devices)
{
Console.WriteLine($"{device.Name}, {device.Id}\n");
}
}
}
範例裝置識別碼為 \\\\?\\SWD#MMDEVAPI#{0.0.1.00000000}.{5f23ab69-6181-4f4a-81a4-45414013aac8}#{2eef81be-33fa-4800-9670-1cd474972c3f}
。
Linux 上的音訊裝置識別碼
使用標準 ALSA 裝置識別碼來選取裝置識別碼。
連結至系統的輸入識別碼,會包含在命令 arecord -L
的輸出中。
或者,可以使用 ALSA C 程式庫加以取得。
範例識別碼為 hw:1,0
和 hw:CARD=CC,DEV=0
。
macOS 上的音訊裝置識別碼
下列在 Objective-C 中實作的函式,會針對連結至 Mac 的音訊裝置建立其名稱與識別碼的清單。
deviceUID
字串用來識別「適用於 macOS 的語音 SDK」中的裝置。
#import <Foundation/Foundation.h>
#import <CoreAudio/CoreAudio.h>
CFArrayRef CreateInputDeviceArray()
{
AudioObjectPropertyAddress propertyAddress = {
kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
UInt32 dataSize = 0;
OSStatus status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize);
if (kAudioHardwareNoError != status) {
fprintf(stderr, "AudioObjectGetPropertyDataSize (kAudioHardwarePropertyDevices) failed: %i\n", status);
return NULL;
}
UInt32 deviceCount = (uint32)(dataSize / sizeof(AudioDeviceID));
AudioDeviceID *audioDevices = (AudioDeviceID *)(malloc(dataSize));
if (NULL == audioDevices) {
fputs("Unable to allocate memory", stderr);
return NULL;
}
status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize, audioDevices);
if (kAudioHardwareNoError != status) {
fprintf(stderr, "AudioObjectGetPropertyData (kAudioHardwarePropertyDevices) failed: %i\n", status);
free(audioDevices);
audioDevices = NULL;
return NULL;
}
CFMutableArrayRef inputDeviceArray = CFArrayCreateMutable(kCFAllocatorDefault, deviceCount, &kCFTypeArrayCallBacks);
if (NULL == inputDeviceArray) {
fputs("CFArrayCreateMutable failed", stderr);
free(audioDevices);
audioDevices = NULL;
return NULL;
}
// Iterate through all the devices and determine which are input-capable
propertyAddress.mScope = kAudioDevicePropertyScopeInput;
for (UInt32 i = 0; i < deviceCount; ++i) {
// Query device UID
CFStringRef deviceUID = NULL;
dataSize = sizeof(deviceUID);
propertyAddress.mSelector = kAudioDevicePropertyDeviceUID;
status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, &deviceUID);
if (kAudioHardwareNoError != status) {
fprintf(stderr, "AudioObjectGetPropertyData (kAudioDevicePropertyDeviceUID) failed: %i\n", status);
continue;
}
// Query device name
CFStringRef deviceName = NULL;
dataSize = sizeof(deviceName);
propertyAddress.mSelector = kAudioDevicePropertyDeviceNameCFString;
status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, &deviceName);
if (kAudioHardwareNoError != status) {
fprintf(stderr, "AudioObjectGetPropertyData (kAudioDevicePropertyDeviceNameCFString) failed: %i\n", status);
continue;
}
// Determine if the device is an input device (it is an input device if it has input channels)
dataSize = 0;
propertyAddress.mSelector = kAudioDevicePropertyStreamConfiguration;
status = AudioObjectGetPropertyDataSize(audioDevices[i], &propertyAddress, 0, NULL, &dataSize);
if (kAudioHardwareNoError != status) {
fprintf(stderr, "AudioObjectGetPropertyDataSize (kAudioDevicePropertyStreamConfiguration) failed: %i\n", status);
continue;
}
AudioBufferList *bufferList = (AudioBufferList *)(malloc(dataSize));
if (NULL == bufferList) {
fputs("Unable to allocate memory", stderr);
break;
}
status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, bufferList);
if (kAudioHardwareNoError != status || 0 == bufferList->mNumberBuffers) {
if (kAudioHardwareNoError != status)
fprintf(stderr, "AudioObjectGetPropertyData (kAudioDevicePropertyStreamConfiguration) failed: %i\n", status);
free(bufferList);
bufferList = NULL;
continue;
}
free(bufferList);
bufferList = NULL;
// Add a dictionary for this device to the array of input devices
CFStringRef keys [] = { CFSTR("deviceUID"), CFSTR("deviceName")};
CFStringRef values [] = { deviceUID, deviceName};
CFDictionaryRef deviceDictionary = CFDictionaryCreate(kCFAllocatorDefault,
(const void **)(keys),
(const void **)(values),
2,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFArrayAppendValue(inputDeviceArray, deviceDictionary);
CFRelease(deviceDictionary);
deviceDictionary = NULL;
}
free(audioDevices);
audioDevices = NULL;
// Return a non-mutable copy of the array
CFArrayRef immutableInputDeviceArray = CFArrayCreateCopy(kCFAllocatorDefault, inputDeviceArray);
CFRelease(inputDeviceArray);
inputDeviceArray = NULL;
return immutableInputDeviceArray;
}
例如,內建麥克風的 UID 為 BuiltInMicrophoneDevice
。
iOS 上的音訊裝置識別碼
iOS 上不支援使用語音 SDK 選取音訊裝置。 使用 SDK 的應用程式可透過 AVAudioSession
Framework 來影響音訊路由。
例如,指令
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord
withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:NULL];
可讓您將藍牙耳機用於具備語音功能的應用程式。
JavaScript 中的音訊裝置識別碼
在 JavaScript 中,MediaDevices.enumerateDevices() 方法可用來列舉媒體裝置及尋找要傳至 fromMicrophone(...)
的裝置識別碼。