rhino/demo/c/pvrecorder/node/pv_recorder_napi.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <node_api.h>
3 #include <stdio.h>
4 
5 #include "pv_recorder.h"
6 
7 napi_value napi_pv_recorder_init(napi_env env, napi_callback_info info) {
8  size_t argc = 4;
9  napi_value args[4];
10  napi_status status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
11  assert(status == napi_ok);
12 
13  int32_t device_index;
14  status = napi_get_value_int32(env, args[0], &device_index);
15  assert(status == napi_ok);
16 
17  int32_t frame_length;
18  status = napi_get_value_int32(env, args[1], &frame_length);
19  assert(status == napi_ok);
20 
21  int32_t buffer_size_msec;
22  status = napi_get_value_int32(env, args[2], &buffer_size_msec);
23  assert(status == napi_ok);
24 
25  bool log_overflow;
26  status = napi_get_value_bool(env, args[3], &log_overflow);
27  assert(status == napi_ok);
28 
29  pv_recorder_t *handle = NULL;
30  pv_recorder_status_t pv_recorder_status = pv_recorder_init(device_index, frame_length, buffer_size_msec, log_overflow, &handle);
31  if (pv_recorder_status != PV_RECORDER_STATUS_SUCCESS) {
32  handle = NULL;
33  }
34 
35  napi_value result;
36  uint64_t object_id_and_status = (((uint64_t)(uintptr_t) handle) * 10) + pv_recorder_status;
37  status = napi_create_bigint_uint64(env, (uint64_t) object_id_and_status, &result);
38  assert(status == napi_ok);
39 
40  return result;
41 }
42 
43 napi_value napi_pv_recorder_delete(napi_env env, napi_callback_info info) {
44  size_t argc = 1;
45  napi_value args[1];
46  napi_status status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
47  assert(status == napi_ok);
48 
49  uint64_t object_id;
50  bool lossless;
51  status = napi_get_value_bigint_uint64(env, args[0], &object_id, &lossless);
52  assert(status == napi_ok);
53  assert(lossless);
54 
55  pv_recorder_delete((pv_recorder_t *)(uintptr_t) object_id);
56 
57  return NULL;
58 }
59 
60 napi_value napi_pv_recorder_start(napi_env env, napi_callback_info info) {
61  size_t argc = 1;
62  napi_value args[1];
63  napi_status status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
64  assert(status == napi_ok);
65 
66  uint64_t object_id;
67  bool lossless;
68  status = napi_get_value_bigint_uint64(env, args[0], &object_id, &lossless);
69  assert(status == napi_ok);
70  assert(lossless);
71 
72  pv_recorder_status_t pv_recorder_status = pv_recorder_start((pv_recorder_t *)(uintptr_t) object_id);
73 
74  napi_value result;
75  status = napi_create_int32(env, pv_recorder_status, &result);
76  assert(status == napi_ok);
77 
78  return result;
79 }
80 
81 napi_value napi_pv_recorder_stop(napi_env env, napi_callback_info info) {
82  size_t argc = 1;
83  napi_value args[1];
84  napi_status status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
85  assert(status == napi_ok);
86 
87  uint64_t object_id;
88  bool lossless;
89  status = napi_get_value_bigint_uint64(env, args[0], &object_id, &lossless);
90  assert(status == napi_ok);
91  assert(lossless);
92 
93  pv_recorder_status_t pv_recorder_status = pv_recorder_stop((pv_recorder_t *)(uintptr_t) object_id);
94 
95  napi_value result;
96  status = napi_create_int32(env, pv_recorder_status, &result);
97  assert(status == napi_ok);
98 
99  return result;
100 }
101 
102 napi_value napi_pv_recorder_read(napi_env env, napi_callback_info info) {
103  size_t argc = 2;
104  napi_value args[2];
105  napi_status status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
106  assert(status == napi_ok);
107 
108  uint64_t object_id;
109  bool lossless;
110  status = napi_get_value_bigint_uint64(env, args[0], &object_id, &lossless);
111  assert(status == napi_ok);
112  assert(lossless);
113 
114  napi_typedarray_type arr_type;
115  size_t length;
116  void *data;
117  napi_value arr_value;
118  size_t offset;
119  status = napi_get_typedarray_info(env, args[1], &arr_type, &length, &data, &arr_value, &offset);
120  assert(status == napi_ok);
121  assert(arr_type == napi_int16_array);
122  assert(length > 0);
123  assert(offset == 0);
124 
125  pv_recorder_status_t pv_recorder_status = pv_recorder_read((pv_recorder_t *)(uintptr_t) object_id, (int16_t *) data);
126 
127  napi_value result;
128  status = napi_create_int32(env, pv_recorder_status, &result);
129  assert(status == napi_ok);
130 
131  return result;
132 }
133 
134 napi_value napi_pv_recorder_get_selected_device(napi_env env, napi_callback_info info) {
135  size_t argc = 1;
136  napi_value args[1];
137  napi_status status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
138  assert(status == napi_ok);
139 
140  uint64_t object_id;
141  bool lossless;
142  status = napi_get_value_bigint_uint64(env, args[0], &object_id, &lossless);
143  assert(status == napi_ok);
144  assert(lossless);
145 
146  napi_value result;
147  status = napi_create_string_utf8(env, pv_recorder_get_selected_device((pv_recorder_t *)(uintptr_t) object_id), NAPI_AUTO_LENGTH, &result);
148  assert(status == napi_ok);
149 
150  return result;
151 }
152 
153 napi_value napi_pv_recorder_get_audio_devices(napi_env env, napi_callback_info info) {
154  size_t argc = 0;
155  napi_status status = napi_get_cb_info(env, info, &argc, NULL, NULL, NULL);
156  assert(status == napi_ok);
157 
158  int32_t count = 0;
159  char **devices = NULL;
160  pv_recorder_status_t pv_recorder_status = pv_recorder_get_audio_devices(&count, &devices);
161  assert(devices != NULL);
162 
163  if (pv_recorder_status != PV_RECORDER_STATUS_SUCCESS) {
164  return NULL;
165  }
166 
167  napi_value result;
168  status = napi_create_array_with_length(env, count, &result);
169  assert(status == napi_ok);
170 
171  for (int32_t i = 0; i < count; i++) {
172  napi_value device_name;
173  status = napi_create_string_utf8(env, devices[i], NAPI_AUTO_LENGTH, &device_name);
174  assert(status == napi_ok);
175 
176  status = napi_set_element(env, result, i, device_name);
177  assert(status == napi_ok);
178  }
179 
181 
182  return result;
183 }
184 
185 napi_value napi_pv_recorder_version(napi_env env, napi_callback_info info) {
186  (void)(info);
187 
188  napi_value result;
189  napi_status status = napi_create_string_utf8(env, pv_recorder_version(), NAPI_AUTO_LENGTH, &result);
190  assert(status == napi_ok);
191 
192  return result;
193 }
194 
195 #define DECLARE_NAPI_METHOD(name, func) (napi_property_descriptor){ name, 0, func, 0, 0, 0, napi_default, 0 }
196 
197 napi_value Init(napi_env env, napi_value exports) {
198  napi_property_descriptor desc = DECLARE_NAPI_METHOD("init", napi_pv_recorder_init);
199  napi_status status = napi_define_properties(env, exports, 1, &desc);
200  assert(status == napi_ok);
201 
203  status = napi_define_properties(env, exports, 1, &desc);
204  assert(status == napi_ok);
205 
207  status = napi_define_properties(env, exports, 1, &desc);
208  assert(status == napi_ok);
209 
211  status = napi_define_properties(env, exports, 1, &desc);
212  assert(status == napi_ok);
213 
215  status = napi_define_properties(env, exports, 1, &desc);
216  assert(status == napi_ok);
217 
218  desc = DECLARE_NAPI_METHOD("get_selected_device", napi_pv_recorder_get_selected_device);
219  status = napi_define_properties(env, exports, 1, &desc);
220  assert(status == napi_ok);
221 
222  desc = DECLARE_NAPI_METHOD("get_audio_devices", napi_pv_recorder_get_audio_devices);
223  status = napi_define_properties(env, exports, 1, &desc);
224  assert(status == napi_ok);
225 
227  status = napi_define_properties(env, exports, 1, &desc);
228  assert(status == napi_ok);
229 
230  return exports;
231 }
232 
233 NAPI_MODULE(NODE_GYB_MODULE_NAME, Init)
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
pv_recorder_delete
PV_API void pv_recorder_delete(pv_recorder_t *object)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:162
napi_pv_recorder_get_audio_devices
napi_value napi_pv_recorder_get_audio_devices(napi_env env, napi_callback_info info)
Definition: rhino/demo/c/pvrecorder/node/pv_recorder_napi.c:153
napi_pv_recorder_get_selected_device
napi_value napi_pv_recorder_get_selected_device(napi_env env, napi_callback_info info)
Definition: rhino/demo/c/pvrecorder/node/pv_recorder_napi.c:134
pv_recorder_init
PV_API pv_recorder_status_t pv_recorder_init(int32_t device_index, int32_t frame_length, int32_t buffer_size_msec, bool log_overflow, pv_recorder_t **object)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:51
pv_recorder::log_overflow
bool log_overflow
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:34
pv_recorder_free_device_list
PV_API void pv_recorder_free_device_list(int32_t count, char **devices)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:314
pv_recorder_read
PV_API pv_recorder_status_t pv_recorder_read(pv_recorder_t *object, int16_t *pcm)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:213
napi_pv_recorder_delete
napi_value napi_pv_recorder_delete(napi_env env, napi_callback_info info)
Definition: rhino/demo/c/pvrecorder/node/pv_recorder_napi.c:43
pv_recorder_stop
PV_API pv_recorder_status_t pv_recorder_stop(pv_recorder_t *object)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:192
napi_pv_recorder_init
napi_value napi_pv_recorder_init(napi_env env, napi_callback_info info)
Definition: rhino/demo/c/pvrecorder/node/pv_recorder_napi.c:7
pv_recorder_status_t
pv_recorder_status_t
Definition: porcupine/demo/c/pvrecorder/include/pv_recorder.h:31
napi_pv_recorder_start
napi_value napi_pv_recorder_start(napi_env env, napi_callback_info info)
Definition: rhino/demo/c/pvrecorder/node/pv_recorder_napi.c:60
pv_recorder_get_selected_device
const PV_API char * pv_recorder_get_selected_device(pv_recorder_t *object)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:249
count
size_t count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_common/ma_test_common.c:31
pv_recorder
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:28
pv_recorder_version
const PV_API char * pv_recorder_version(void)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:343
napi_pv_recorder_version
napi_value napi_pv_recorder_version(napi_env env, napi_callback_info info)
Definition: rhino/demo/c/pvrecorder/node/pv_recorder_napi.c:185
DECLARE_NAPI_METHOD
#define DECLARE_NAPI_METHOD(name, func)
Definition: rhino/demo/c/pvrecorder/node/pv_recorder_napi.c:195
napi_pv_recorder_read
napi_value napi_pv_recorder_read(napi_env env, napi_callback_info info)
Definition: rhino/demo/c/pvrecorder/node/pv_recorder_napi.c:102
pv_recorder::frame_length
int32_t frame_length
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:32
napi_pv_recorder_stop
napi_value napi_pv_recorder_stop(napi_env env, napi_callback_info info)
Definition: rhino/demo/c/pvrecorder/node/pv_recorder_napi.c:81
pv_recorder_start
PV_API pv_recorder_status_t pv_recorder_start(pv_recorder_t *object)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:172
Init
napi_value Init(napi_env env, napi_value exports)
Definition: rhino/demo/c/pvrecorder/node/pv_recorder_napi.c:197
args
assert.h
PV_RECORDER_STATUS_SUCCESS
@ PV_RECORDER_STATUS_SUCCESS
Definition: porcupine/demo/c/pvrecorder/include/pv_recorder.h:32
pv_recorder_get_audio_devices
PV_API pv_recorder_status_t pv_recorder_get_audio_devices(int32_t *count, char ***devices)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:256


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50