@@ -15,40 +15,16 @@ JsPaStreamCallbackBridge::JsPaStreamCallbackBridge(Callback *callback_,
1515    , UVCallback
1616  );
1717  async->data  = this ;
18-   uv_mutex_init (&async_lock );
18+   uv_barrier_init (&async_barrier,  2 );
1919
2020  //  Save userData to persistent object
2121  SaveToPersistent (ToLocString (" userData" 
2222}
2323
2424JsPaStreamCallbackBridge::~JsPaStreamCallbackBridge () {
25-   uv_mutex_destroy (&async_lock );
25+   uv_barrier_destroy (&async_barrier );
2626  uv_close ((uv_handle_t *)async, NULL );
2727
28-   // free buffer memory
29-   if (m_inputBuffer != nullptr )
30-       free (m_inputBuffer);
31-   if (m_outputBuffer != nullptr )
32-       free (m_outputBuffer);
33- }
34- 
35- int  JsPaStreamCallbackBridge::sendToCallback (const  void * input, unsigned  long  frameCount) {
36-   uv_mutex_lock (&async_lock);
37-     m_frameCount = frameCount;
38-   
39-     if (m_inputBuffer != nullptr )
40-       free (m_inputBuffer);
41-     m_inputBuffer = malloc (sizeof (float ) * frameCount * 2 );
42-   
43-     memmove (
44-       m_inputBuffer,
45-       input,
46-       m_bytesPerFrameIn * frameCount
47-     );
48-   uv_mutex_unlock (&async_lock);
49-   
50-   uv_async_send (async);
51-   return  0 ;
5228}
5329
5430void  JsPaStreamCallbackBridge::dispatchJSCallback () {
@@ -58,74 +34,47 @@ void JsPaStreamCallbackBridge::dispatchJSCallback() {
5834  v8::Local<v8::ArrayBuffer> output;
5935  v8::Local<v8::Value> callbackReturn;
6036
61-   uv_mutex_lock (&async_lock);
62-   
63-     frameCount = m_frameCount;
64-     
65-     //  Setup ArrayBuffer for input audio data
66-     input = v8::ArrayBuffer::New (
67-       v8::Isolate::GetCurrent (),
68-       m_bytesPerFrameIn * frameCount
69-     );
70-     //  Copy input audio data from bridge buffer to ArrayBuffer
71-     memmove (
72-       input->GetContents ().Data (),
73-       m_inputBuffer,
74-       input->ByteLength ()
75-     );
7637
77-     //  Setup ArrayBuffer for output audio data
78-     output = v8::ArrayBuffer::New (
79-       v8::Isolate::GetCurrent (),
80-       m_bytesPerFrameOut * frameCount
81-     );
82-     
83-     //  Create array of arguments and call the javascript callback
84-     LocalValue argv[] = {
85-       input,
86-       output,
87-       New<Number>(frameCount),
88-       GetFromPersistent (ToLocString (" userData" 
89-     };
90-     callbackReturn = callback->Call (4 , argv);
91-     
92-     if (m_outputBuffer != nullptr )
93-       free (m_outputBuffer);
94-     m_outputBuffer = malloc (output->ByteLength ());
95-     //  Copy output audio data from bridge buffer to ArrayBuffer
96-     memmove (
97-       m_outputBuffer,
98-       output->GetContents ().Data (),
99-       output->ByteLength ()
100-     );
101-     
102-     //  Store the return result of the javascript callback 
103-     //  so it be sent to the PaStreamCallback function
104-     m_callbackResult = LocalizeInt (callbackReturn);
105-   
106-   uv_mutex_unlock (&async_lock);
38+   frameCount = m_frameCount;
39+ 
40+   //  Setup ArrayBuffer for input audio data
41+   input = v8::ArrayBuffer::New (
42+     v8::Isolate::GetCurrent (),
43+     const_cast <void *>(m_inputBuffer),
44+     m_bytesPerFrameIn * frameCount
45+   );
46+ 
47+   //  Setup ArrayBuffer for output audio data
48+   output = v8::ArrayBuffer::New (
49+     v8::Isolate::GetCurrent (),
50+     m_outputBuffer,
51+     m_bytesPerFrameOut * frameCount
52+   );
53+ 
54+   //  Create array of arguments and call the javascript callback
55+   LocalValue argv[] = {
56+     input,
57+     output,
58+     New<Number>(frameCount),
59+     GetFromPersistent (ToLocString (" userData" 
60+   };
61+   m_callbackResult = LocalizeInt (callback->Call (4 , argv));
10762
63+   uv_barrier_wait (&async_barrier);
10864}
65+ 
66+ int  JsPaStreamCallbackBridge::Execute (const  void * input, void * output, unsigned  long  frameCount) {
67+   m_frameCount = frameCount;
68+ 
69+   m_inputBuffer = input;
70+   m_outputBuffer = output;
71+ 
72+   //  Dispatch the asyncronous callback
73+   uv_async_send (async);
10974
110- void  JsPaStreamCallbackBridge::consumeAudioData (void * output, unsigned  long  frameCount) {
75+   //  Wait for the asyncronous callback
76+   uv_barrier_wait (&async_barrier);
11177
112-   if (m_outputBuffer != nullptr ) {
113-     memmove (
114-       output,
115-       m_outputBuffer,
116-       m_bytesPerFrameOut * frameCount
117-     );
118-     
119-     //  Free the output buffer and set it to nullptr to prevent it from sending the same output data twice
120-     free (m_outputBuffer);
121-     m_outputBuffer = nullptr ;
122-   }
78+   return  m_callbackResult;
12379}
12480
125- int  JsPaStreamCallbackBridge::getCallbackResult () {
126-   int  ret;
127-   uv_mutex_lock (&async_lock);
128-     ret = m_callbackResult;
129-   uv_mutex_unlock (&async_lock);
130-   return  ret;
131- }
0 commit comments