Skip to content

Commit d77363a

Browse files
committed
デフォルトオーディオデバイスを最初にソートする機能を追加
1 parent c408ffb commit d77363a

3 files changed

Lines changed: 51 additions & 24 deletions

File tree

bin/vosk-cli.exe

2 KB
Binary file not shown.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vosk-cli",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "",
55
"main": "src/index.js",
66
"types": "src/index.d.ts",
@@ -17,4 +17,4 @@
1717
"build": "build.bat"
1818
},
1919
"license": "Apache-2.0"
20-
}
20+
}

vosk-cli/vosk-cli.cpp

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//-----------------------------------------------------------------------------
55

66
// バージョン情報
7-
#define VOSK_CLI_VERSION "1.0.1"
7+
#define VOSK_CLI_VERSION "1.0.2"
88
#define VOSK_CLI_BUILD_DATE __DATE__
99

1010
#include <windows.h>
@@ -16,7 +16,6 @@
1616
#include <stdio.h>
1717
#include <string.h>
1818
#include <stdlib.h>
19-
#include <wchar.h>
2019
#include <locale.h>
2120
//--
2221
#include <codecvt>
@@ -30,24 +29,28 @@
3029
// VOSKライブラリ
3130
#pragma comment(lib, "libvosk.lib")
3231

33-
3432
/**
3533
* @brief 文字列をJSON形式でエスケープする関数
3634
*/
37-
std::string escapeJson(const std::string& str) {
35+
std::string escapeJson(const std::string &str) {
3836
std::string result;
3937
for (char c : str) {
40-
if (c == '"') result += "\\\"";
41-
else if (c == '\\') result += "\\\\";
42-
else if (c == '\n') result += "\\n";
43-
else if (c == '\r') result += "\\r";
44-
else if (c == '\t') result += "\\t";
38+
if (c == '"')
39+
result += "\\\"";
40+
else if (c == '\\')
41+
result += "\\\\";
42+
else if (c == '\n')
43+
result += "\\n";
44+
else if (c == '\r')
45+
result += "\\r";
46+
else if (c == '\t')
47+
result += "\\t";
4548
else if (c >= 0 && c < 0x20) {
4649
char buf[7];
4750
sprintf_s(buf, "\\u%04x", (unsigned char)c);
4851
result += buf;
49-
}
50-
else result += c;
52+
} else
53+
result += c;
5154
}
5255
return result;
5356
}
@@ -70,9 +73,11 @@ struct AudioDeviceInfo {
7073
};
7174

7275
/**
73-
* @brief 利用可能な入力オーディオデバイスを列挙する関数
76+
* @brief
77+
* 利用可能な入力オーディオデバイスを列挙する関数(デフォルトデバイスを最初にソート)
7478
*
75-
* @return std::vector<AudioDeviceInfo> 利用可能なオーディオデバイスの一覧
79+
* @return std::vector<AudioDeviceInfo>
80+
* 利用可能なオーディオデバイスの一覧(デフォルトデバイスが先頭)
7681
*/
7782
std::vector<AudioDeviceInfo> EnumerateInputDevices() {
7883
std::vector<AudioDeviceInfo> devices;
@@ -84,6 +89,19 @@ std::vector<AudioDeviceInfo> EnumerateInputDevices() {
8489
CLSCTX_ALL, IID_PPV_ARGS(&enumerator));
8590
if (FAILED(hr)) return devices;
8691

92+
// デフォルトデバイスのIDを取得
93+
std::wstring defaultDeviceId;
94+
CComPtr<IMMDevice> defaultDevice;
95+
hr = enumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &defaultDevice);
96+
if (SUCCEEDED(hr)) {
97+
LPWSTR deviceId;
98+
hr = defaultDevice->GetId(&deviceId);
99+
if (SUCCEEDED(hr)) {
100+
defaultDeviceId = deviceId;
101+
CoTaskMemFree(deviceId);
102+
}
103+
}
104+
87105
hr = enumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE,
88106
&collection);
89107
if (FAILED(hr)) {
@@ -93,7 +111,9 @@ std::vector<AudioDeviceInfo> EnumerateInputDevices() {
93111

94112
UINT count = 0;
95113
collection->GetCount(&count);
96-
for (UINT i = 0; i < count; ++i) {
114+
115+
// デフォルトデバイスを最初に追加し、その他のデバイスを続けて追加
116+
for (UINT i = 0; i < count; i++) {
97117
CComPtr<IMMDevice> device;
98118
collection->Item(i, &device);
99119

@@ -109,9 +129,17 @@ std::vector<AudioDeviceInfo> EnumerateInputDevices() {
109129
if (varName.vt != VT_LPWSTR) {
110130
PropVariantClear(&varName);
111131
CoTaskMemFree(deviceId);
112-
continue; // 名前が取得できない場合はスキップ
132+
continue;
133+
}
134+
135+
AudioDeviceInfo deviceInfo = {deviceId, varName.pwszVal};
136+
137+
// デフォルトデバイスなら最初に追加
138+
if (deviceInfo.id == defaultDeviceId) {
139+
devices.insert(devices.begin(), deviceInfo);
140+
} else {
141+
devices.push_back(deviceInfo);
113142
}
114-
devices.push_back({deviceId, varName.pwszVal});
115143

116144
CoTaskMemFree(deviceId);
117145
PropVariantClear(&varName);
@@ -156,11 +184,9 @@ void OutputDevicesAsJson() {
156184
*/
157185
std::string RemoveSpaces(const char *input) {
158186
if (!input) return "";
159-
auto a = std::string(input);
160-
std::regex space_pattern("\\s+");
161-
162187
// スペースを空文字列に置換
163-
return std::regex_replace(a, space_pattern, "");
188+
std::regex space_pattern("\\s+");
189+
return std::regex_replace(std::string(input), space_pattern, "");
164190
}
165191

166192
/**
@@ -514,7 +540,7 @@ void StartAudioStream(int deviceIndex, const char *modelPath, bool isTest,
514540
resources.setDeviceFormat(deviceFormat); // 自動解放の対象に追加
515541

516542
// フォーマット情報を表示
517-
// PrintDeviceFormat(deviceFormat);
543+
// PrintDeviceFormat(deviceFormat);
518544

519545
int sample_rate = deviceFormat->nSamplesPerSec;
520546
int channels = deviceFormat->nChannels;
@@ -606,7 +632,8 @@ void StartAudioStream(int deviceIndex, const char *modelPath, bool isTest,
606632
std::string partialStr = RemoveSpaces(partial);
607633

608634
// 空または前回と同じ結果は出力しない
609-
if (!partialStr.empty() && partialStr != "{\"partial\":\"\"}" && partialStr != lastPartialStr) {
635+
if (!partialStr.empty() && partialStr != "{\"partial\":\"\"}" &&
636+
partialStr != lastPartialStr) {
610637
puts(partialStr.c_str());
611638
fflush(stdout);
612639

0 commit comments

Comments
 (0)