Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to send data continually to javascript from windows Wave API callback function? #406

Closed
CTianXing opened this issue Dec 6, 2018 · 4 comments

Comments

@CTianXing
Copy link

sorry for my poor english, hope you understand what I mean.
I take the windows Wave API to record sound and send it to speech recognize. There is a callback in the waveInOpen(), i can take the buffer recorded to recognize and get the result continually,but how do i send the result to javascript in the waveInProc().
MMRESULT waveInOpen(
 LPHWAVEIN       phwi,
 UINT            uDeviceID,
 LPCWAVEFORMATEX pwfx,
 DWORD_PTR       dwCallback,
 DWORD_PTR       dwCallbackInstance,
 DWORD           fdwOpen
);
void CALLBACK waveInProc(
HWAVEIN hwi,
UINT uMsg,
DWORD_PTR dwInstance,
DWORD_PTR dwParam1,
DWORD_PTR dwParam2
);
I try to emit events from addon, it has error"Entering the V8 API without proper locking in place“. I don't know how to use Asynchronous Thread-safe Function Calls and if it works. So I'm here for help. Thank you.
record2recognize.txt

@NickNaso
Copy link
Member

Hi @CTianXing,
sorry for the late in the answer. I taken a look at your code and I think that you are able to emit the first event that you named "start" but the problem is that you cannot able to emit the "data".
Is is right? You need to preserve the instance of the emit function and store it on a Napi::FunctionReference see the documentation here: https://github.com/nodejs/node-addon-api/blob/master/doc/function_reference.md.
In alternative you can extend the EventEmitter like in this example:
https://github.com/nodejs/node-addon-examples/tree/master/inherits_from_event_emitter/node-addon-api
I hope that the informations that I provided is good for you otherwise don't esitate to reply.

@gabrielschulhof
Copy link
Contributor

@CTianXing @romandev is working on providing a C++ wrapper for napi_threadsafe_function. In the meantime, you can use the C API directly.

At any rate, you should replace

Napi::Function cbemit;

with

napi_threadsafe_function cbemit;

which you then create using napi_create_threadsafe_function(). If you know ahead of time how many threads will be calling the function, you can set the initial_thread_count at creation time. Then, in the thread that is supposed to emit the "data" event you call napi_call_threadsafe_function().

Realize though that this calls a JavaScript function rather than emitting a JavaScript event. However, in your call_js_cb you have a chance to run any kind of code you like, including emitting an event.

@CTianXing
Copy link
Author

@NickNaso @gabrielschulhof Thank you very much!
I implemented it with "async_work_thread_safe_function". Wave API usually deal data in the CALLBACK waveInProc,but napi_call_threadsafe_function must be used in the ExecuteWork,is that right? so I preserve the result to global in waveInProc ,then I call napi_call_threadsafe_function in the ExecuteWork once a second. It works,I can get the result continually in javaScript.
I'm a new programmer, and I appreciate your help.

@gabrielschulhof
Copy link
Contributor

@CTianXing sounds like you're using napi_threadsafe_function correctly👍 napi_call_threadsafe_function can be called from any thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants