forked from fcannizzaro/win-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio-napi.cc
124 lines (93 loc) · 3.19 KB
/
audio-napi.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <windows.h>
#include <math.h>
#include <stdlib.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <node_api.h>
IAudioEndpointVolume* getVolume(int mic){
HRESULT hr;
IMMDeviceEnumerator *enumerator = NULL;
IAudioEndpointVolume *volume = NULL;
IMMDevice *defaultDevice = NULL;
CoInitialize(NULL);
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *) &enumerator);
hr = enumerator->GetDefaultAudioEndpoint(mic ? eCapture : eRender, eConsole, &defaultDevice);
if (hr != 0) {
return volume;
}
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *) &volume);
enumerator->Release();
defaultDevice->Release();
CoUninitialize();
return volume;
}
int *getArgs(napi_env env, napi_callback_info info){
napi_value argv[2];
size_t argc = 2;
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
int *out = (int*) malloc(sizeof(int) * argc);
for(int i = 0; i < (int)argc; i++){
napi_get_value_int32(env, argv[i], &out[i]);
}
return out;
}
napi_value toValue(napi_env env, int value){
napi_value nvalue = 0;
napi_create_int32(env, value, &nvalue);
return nvalue;
}
napi_value get(napi_env env, napi_callback_info info) {
int *argv = getArgs(env, info);
float volume = 0;
IAudioEndpointVolume *tmp_volume = getVolume(argv[0]);
if (tmp_volume == NULL) {
return toValue(env, -1);
}
tmp_volume->GetMasterVolumeLevelScalar(&volume);
return toValue(env, (int) round(volume*100));
}
napi_value isMuted(napi_env env, napi_callback_info info) {
int *argv = getArgs(env, info);
int mute = 0;
IAudioEndpointVolume *tmp_volume = getVolume(argv[0]);
if (tmp_volume == NULL) {
return toValue(env, -999);
}
tmp_volume->GetMute(&mute);
return toValue(env, mute);
}
napi_value mute(napi_env env, napi_callback_info info) {
int *argv = getArgs(env, info);
IAudioEndpointVolume *tmp_volume = getVolume(argv[0]);
if (tmp_volume == NULL) {
return toValue(env, -1);
}
tmp_volume->SetMute(argv[1], NULL);
return toValue(env, 1);
}
napi_value set(napi_env env, napi_callback_info info) {
int *argv = getArgs(env, info);
float newVolume = ((float)argv[0])/100.0f;
int mic = argv[1];
IAudioEndpointVolume *tmp_volume = getVolume(mic);
if (tmp_volume == NULL) {
return toValue(env, -1);
}
tmp_volume->SetMasterVolumeLevelScalar(newVolume, NULL);
return toValue(env, 1);
}
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_value get_fn, set_fn, mute_fn, is_mute_fn;
status = napi_create_function(env, NULL, 0, get, NULL, &get_fn);
status = napi_create_function(env, NULL, 0, set, NULL, &set_fn);
status = napi_create_function(env, NULL, 0, mute, NULL, &mute_fn);
status = napi_create_function(env, NULL, 0, isMuted, NULL, &is_mute_fn);
status = napi_set_named_property(env, exports, "get", get_fn);
status = napi_set_named_property(env, exports, "set", set_fn);
status = napi_set_named_property(env, exports, "mute", mute_fn);
status = napi_set_named_property(env, exports, "isMuted", is_mute_fn);
return exports;
}
NAPI_MODULE(addon, Init)