diff --git a/NodeCoreAudio/AudioEngine.cpp b/NodeCoreAudio/AudioEngine.cpp index cd162d9..fe59cc7 100644 --- a/NodeCoreAudio/AudioEngine.cpp +++ b/NodeCoreAudio/AudioEngine.cpp @@ -10,7 +10,7 @@ #include "portaudio.h" #include #include -#include +#include #include #include @@ -62,25 +62,25 @@ Audio::AudioEngine::AudioEngine( Local options ) : m_uCurrentReadBuffer = 0; // Create V8 objects to hold our buffers - m_hInputBuffer = NanNew( m_uInputChannels ); + m_hInputBuffer = Nan::New( m_uInputChannels ); for( int iChannel=0; iChannelSet( iChannel, NanNew(m_uSamplesPerBuffer) ); + m_hInputBuffer->Set( iChannel, Nan::New(m_uSamplesPerBuffer) ); // Initialize our audio core PaError initErr = Pa_Initialize(); m_uInputDevice = Pa_GetDefaultInputDevice(); if( m_uInputDevice == paNoDevice ) { - NanThrowTypeError("Error: No default input device"); + Nan::ThrowTypeError("Error: No default input device"); }; m_uOutputDevice = Pa_GetDefaultOutputDevice(); if( m_uOutputDevice == paNoDevice ) { - NanThrowTypeError("Error: No default output device"); + Nan::ThrowTypeError("Error: No default output device"); } if( initErr != paNoError ) - NanThrowTypeError("Failed to initialize audio engine"); + Nan::ThrowTypeError("Failed to initialize audio engine"); applyOptions( options ); @@ -105,13 +105,13 @@ Audio::AudioEngine::AudioEngine( Local options ) : NULL ); if( openStreamErr != paNoError ) - NanThrowTypeError("Failed to open audio stream"); + Nan::ThrowTypeError("Failed to open audio stream"); // Start the audio stream PaError startStreamErr = Pa_StartStream( m_pPaStream ); if( startStreamErr != paNoError ) - NanThrowTypeError("Failed to start audio stream"); + Nan::ThrowTypeError("Failed to start audio stream"); uv_mutex_init( &m_mutex ); uv_thread_create( &ptStreamThread, do_work, (void*)this ); @@ -121,55 +121,49 @@ Audio::AudioEngine::AudioEngine( Local options ) : ////////////////////////////////////////////////////////////////////////////// /*! Gets options */ -//v8::Handle Audio::AudioEngine::getOptions(const v8::Arguments& args){ -NAN_METHOD(Audio::AudioEngine::getOptions){ - NanScope(); - //HandleScope scope; - NanLocker(); - - Local options = NanNew(); +void Audio::AudioEngine::getOptions(const Nan::FunctionCallbackInfo& info) { + Local options = Nan::New(); - AudioEngine* pEngine = AudioEngine::Unwrap( args.This() ); + AudioEngine* pEngine = AudioEngine::Unwrap( info.This() ); - options->Set( NanNew("inputChannels"), NanNew(pEngine->m_uInputChannels) ); - options->Set( NanNew("outputChannels"), NanNew(pEngine->m_uOutputChannels) ); + Nan::Set( options, Nan::New("inputChannels").ToLocalChecked(), Nan::New(pEngine->m_uInputChannels) ); + Nan::Set( options, Nan::New("outputChannels").ToLocalChecked(), Nan::New(pEngine->m_uOutputChannels) ); - options->Set( NanNew("inputDevice"), NanNew(pEngine->m_uInputDevice) ); - options->Set( NanNew("outputDevice"), NanNew(pEngine->m_uOutputDevice) ); + Nan::Set( options, Nan::New("inputDevice").ToLocalChecked(), Nan::New(pEngine->m_uInputDevice) ); + Nan::Set( options, Nan::New("outputDevice").ToLocalChecked(), Nan::New(pEngine->m_uOutputDevice) ); - options->Set( NanNew("sampleRate"), NanNew(pEngine->m_uSampleRate) ); - options->Set( NanNew("sampleFormat"), NanNew(pEngine->m_uSampleFormat) ); - options->Set( NanNew("framesPerBuffer"), NanNew(pEngine->m_uSamplesPerBuffer) ); - options->Set( NanNew("numBuffers"), NanNew(pEngine->m_uNumBuffers) ); - options->Set( NanNew("interleaved"), NanNew(pEngine->m_bInterleaved) ); - options->Set( NanNew("useMicrophone"), NanNew(pEngine->m_bReadMicrophone) ); + Nan::Set( options, Nan::New("sampleRate").ToLocalChecked(), Nan::New(pEngine->m_uSampleRate) ); + Nan::Set( options, Nan::New("sampleFormat").ToLocalChecked(), Nan::New(pEngine->m_uSampleFormat) ); + Nan::Set( options, Nan::New("framesPerBuffer").ToLocalChecked(), Nan::New(pEngine->m_uSamplesPerBuffer) ); + Nan::Set( options, Nan::New("numBuffers").ToLocalChecked(), Nan::New(pEngine->m_uNumBuffers) ); + Nan::Set( options, Nan::New("interleaved").ToLocalChecked(), Nan::New(pEngine->m_bInterleaved) ); + Nan::Set( options, Nan::New("useMicrophone").ToLocalChecked(), Nan::New(pEngine->m_bReadMicrophone) ); - NanReturnValue(options); + info.GetReturnValue().Set(options); } // end GetOptions ////////////////////////////////////////////////////////////////////////////// /*! Set options, restarts audio stream */ -//v8::Handle Audio::AudioEngine::setOptions( const v8::Arguments& args ) { -NAN_METHOD(Audio::AudioEngine::setOptions){ - NanScope(); +void Audio::AudioEngine::setOptions(const Nan::FunctionCallbackInfo& info) { + Nan::HandleScope scope; //HandleScope scope; Local options; - if( args.Length() > 0 ) { - if( !args[0]->IsObject() ) { - return NanThrowTypeError("First argument should be an object."); + if( info.Length() > 0 ) { + if( !info[0]->IsObject() ) { + return Nan::ThrowTypeError("First argument should be an object."); } - options = Local::Cast( args[0] ); + options = Local::Cast( info[0] ); } else { - return NanThrowTypeError("First argument does not exist."); + return Nan::ThrowTypeError("First argument does not exist."); } - AudioEngine* pEngine = AudioEngine::Unwrap( args.This() ); + AudioEngine* pEngine = AudioEngine::Unwrap( info.This() ); pEngine->applyOptions( options ); - NanReturnUndefined(); + info.GetReturnValue().SetUndefined(); } // end AudioEngine::SetOptions()x @@ -177,26 +171,27 @@ NAN_METHOD(Audio::AudioEngine::setOptions){ /*! Sets the given options and restarts the audio stream if necessary */ void Audio::AudioEngine::applyOptions( Local options ) { unsigned int oldBufferCount = m_uNumBuffers; - if( options->HasOwnProperty(NanNew("inputDevice")) ) - m_uInputDevice = (int)options->Get(NanNew("inputDevice"))->ToInteger()->Value(); - if( options->HasOwnProperty(NanNew("outputDevice")) ) - m_uOutputDevice = (int)options->Get(NanNew("outputDevice"))->ToInteger()->Value(); - if( options->HasOwnProperty(NanNew("inputChannels")) ) - m_uInputChannels = (int)options->Get(NanNew("inputChannels"))->ToInteger()->Value(); - if( options->HasOwnProperty(NanNew("outputChannels")) ) - m_uOutputChannels = (int)options->Get(NanNew("outputChannels"))->ToInteger()->Value(); - if( options->HasOwnProperty(NanNew("framesPerBuffer")) ) - m_uSamplesPerBuffer = (int)options->Get(NanNew("framesPerBuffer"))->ToInteger()->Value(); - if ( options->HasOwnProperty(NanNew("numBuffers")) ) - m_uNumBuffers = (int)options->Get(NanNew("numBuffers"))->ToInteger()->Value(); - - if( options->HasOwnProperty(NanNew("interleaved")) ) - m_bInterleaved = options->Get(NanNew("interleaved"))->ToBoolean()->Value(); - if ( options->HasOwnProperty(NanNew("useMicrophone")) ) - m_bReadMicrophone = options->Get(NanNew("useMicrophone"))->ToBoolean()->Value(); - - if( options->HasOwnProperty(NanNew("sampleFormat")) ) { - switch( options->Get(NanNew("sampleFormat"))->ToInteger()->Value() ){ + if(Nan::HasOwnProperty(options, Nan::New("inputDevice").ToLocalChecked()).FromMaybe(false) ) + m_uInputDevice = Nan::To(Nan::Get(options, Nan::New("inputDevice").ToLocalChecked()).ToLocalChecked()).FromJust(); + if(Nan::HasOwnProperty(options, Nan::New("outputDevice").ToLocalChecked()).FromMaybe(false) ) + m_uOutputDevice = Nan::To(Nan::Get(options, Nan::New("outputDevice").ToLocalChecked()).ToLocalChecked()).FromJust(); + if(Nan::HasOwnProperty(options, Nan::New("inputChannels").ToLocalChecked()).FromMaybe(false) ) + m_uInputChannels = Nan::To(Nan::Get(options, Nan::New("inputChannels").ToLocalChecked()).ToLocalChecked()).FromJust(); + if(Nan::HasOwnProperty(options, Nan::New("outputChannels").ToLocalChecked()).FromMaybe(false) ) + m_uOutputChannels = + Nan::To(Nan::Get(options, Nan::New("outputChannels").ToLocalChecked()).ToLocalChecked()).FromJust(); + if(Nan::HasOwnProperty(options, Nan::New("framesPerBuffer").ToLocalChecked()).FromMaybe(false) ) + m_uSamplesPerBuffer = Nan::To(Nan::Get(options, Nan::New("framesPerBuffer").ToLocalChecked()).ToLocalChecked()).FromJust(); + if (Nan::HasOwnProperty(options, Nan::New("numBuffers").ToLocalChecked()).FromMaybe(false) ) + m_uNumBuffers = Nan::To(Nan::Get(options, Nan::New("numBuffers").ToLocalChecked()).ToLocalChecked()).FromJust(); + + if(Nan::HasOwnProperty(options, Nan::New("interleaved").ToLocalChecked()).FromMaybe(false) ) + m_bInterleaved = Nan::To(Nan::Get(options, Nan::New("interleaved").ToLocalChecked()).ToLocalChecked()).FromJust(); + if (Nan::HasOwnProperty(options, Nan::New("useMicrophone").ToLocalChecked()).FromMaybe(false) ) + m_bReadMicrophone = Nan::To(Nan::Get(options, Nan::New("useMicrophone").ToLocalChecked()).ToLocalChecked()).FromJust();; + + if(Nan::HasOwnProperty(options, Nan::New("sampleFormat").ToLocalChecked()).FromMaybe(false) ) { + switch(Nan::To(Nan::Get(options, Nan::New("sampleFormat").ToLocalChecked()).ToLocalChecked()).FromJust()){ case 0x01: m_uSampleFormat = paFloat32; m_uSampleSize = 4; break; case 0x02: m_uSampleFormat = paInt32; m_uSampleSize = 4; break; case 0x04: m_uSampleFormat = paInt24; m_uSampleSize = 3; break; @@ -249,23 +244,19 @@ void Audio::AudioEngine::applyOptions( Local options ) { ////////////////////////////////////////////////////////////////////////////// /*! Returns a v8 array filled with input samples */ -Handle Audio::AudioEngine::getInputBuffer() { -//NAN_METHOD(Audio::AudioEngine::getInputBuffer){ - NanEscapableScope(); - //NanScope(); - NanLocker(); - //HandleScope scope; +Local Audio::AudioEngine::getInputBuffer() { + Nan::EscapableHandleScope scope; if( m_bInterleaved ) { - m_hInputBuffer = NanNew( m_uInputChannels * m_uSamplesPerBuffer ); + m_hInputBuffer = Nan::New( m_uInputChannels * m_uSamplesPerBuffer ); for( int iSample=0; iSampleSet( iSample, getSample(iSample) ); } } else { - m_hInputBuffer = NanNew( m_uInputChannels ); + m_hInputBuffer = Nan::New( m_uInputChannels ); for( int iChannel=0; iChannel( NanNew(m_uSamplesPerBuffer) ); + auto tempBuffer = Local( Nan::New(m_uSamplesPerBuffer) ); for( int iSample=0; iSampleSet( iSample, getSample(iSample) ); @@ -275,48 +266,45 @@ Handle Audio::AudioEngine::getInputBuffer() { } } - return NanEscapeScope( m_hInputBuffer ); + return scope.Escape( m_hInputBuffer ); } // end AudioEngine::getInputBuffer() ////////////////////////////////////////////////////////////////////////////// /*! Returns a sound card sample converted to a v8 Number */ Handle Audio::AudioEngine::getSample( int position ) { -//NAN_METHOD(Audio::AudioEngine::getSample){ - NanEscapableScope(); - NanLocker(); - //HandleScope scope; + Nan::EscapableHandleScope scope; - Handle sample; + Local sample; switch( m_uSampleFormat ) { case paFloat32: { float fValue = ((float*)m_cachedInputSampleBlock)[position]; - sample = NanNew( fValue ); + sample = Nan::New( fValue ); break; } case paInt32: - sample = NanNew( ((int*)m_cachedInputSampleBlock)[position] ); + sample = Nan::New( ((int*)m_cachedInputSampleBlock)[position] ); break; case paInt24: - sample = NanNew( + sample = Nan::New( (m_cachedInputSampleBlock[3*position + 0] << 16) + (m_cachedInputSampleBlock[3*position + 1] << 8) + (m_cachedInputSampleBlock[3*position + 2]) ); break; case paInt16: - sample = NanNew( ((int16_t*)m_cachedInputSampleBlock)[position] ); + sample = Nan::New( ((int16_t*)m_cachedInputSampleBlock)[position] ); break; default: - sample = NanNew( m_cachedInputSampleBlock[position]*-1 ); + sample = Nan::New( m_cachedInputSampleBlock[position]*-1 ); break; } - return NanEscapeScope(sample); + return scope.Escape(sample); } // end AudioEngine::getSample() @@ -366,16 +354,16 @@ void Audio::AudioEngine::queueOutputBuffer( Handle result ) { } else { // Validate the structure of the output buffer array if( !result->Get(0)->IsArray() ) { - NanThrowTypeError("Output buffer not properly setup, 0th channel is not an array"); + Nan::ThrowTypeError("Output buffer not properly setup, 0th channel is not an array"); return; } - Handle item; + Local item; for( int iChannel=0; iChannel::Cast( result->Get(iChannel) ); + item = Local::Cast( result->Get(iChannel) ); if( item->IsArray() ) { if( item->Length() > m_uNumCachedOutputSamples[m_uCurrentWriteBuffer] ) m_uNumCachedOutputSamples[m_uCurrentWriteBuffer] = item->Length(); @@ -427,194 +415,186 @@ void Audio::AudioEngine::RunAudioLoop(){ ////////////////////////////////////////////////////////////////////////////// /*! Initialize our node object */ -void Audio::AudioEngine::Init( v8::Handle target ) { +NAN_MODULE_INIT(Audio::AudioEngine::Init) { + // Prepare constructor template - Local functionTemplate = NanNew (Audio::AudioEngine::New ); - functionTemplate->SetClassName( NanNew("AudioEngine") ); + Local functionTemplate = Nan::New (Audio::AudioEngine::New ); + functionTemplate->SetClassName( Nan::New("AudioEngine").ToLocalChecked() ); functionTemplate->InstanceTemplate()->SetInternalFieldCount( 1 ); - //Local constructorHandle = NanNew(constructor); - //target->Set(NanNew("AudioEngine"), functionTemplate->GetFunction()); + //Local constructorHandle = Nan::New(constructor); + //target->Set(Nan::New("AudioEngine"), functionTemplate->GetFunction()); // Get - //functionTemplate->PrototypeTemplate()->Set( NanNew("isActive"), NanNew(Audio::AudioEngine::isActive)->GetFunction() ); - //functionTemplate->PrototypeTemplate()->Set( NanNew("getDeviceName"), NanNew(Audio::AudioEngine::getDeviceName)->GetFunction() ); - //functionTemplate->PrototypeTemplate()->Set( NanNew("getNumDevices"), NanNew(Audio::AudioEngine::getNumDevices)->GetFunction() ); - NODE_SET_PROTOTYPE_METHOD(functionTemplate, "isActive", Audio::AudioEngine::isActive); - NODE_SET_PROTOTYPE_METHOD(functionTemplate, "getDeviceName", Audio::AudioEngine::getDeviceName); - NODE_SET_PROTOTYPE_METHOD(functionTemplate, "getNumDevices", Audio::AudioEngine::getNumDevices); + //functionTemplate->PrototypeTemplate()->Set( Nan::New("isActive"), Nan::New(Audio::AudioEngine::isActive)->GetFunction() ); + //functionTemplate->PrototypeTemplate()->Set( Nan::New("getDeviceName"), Nan::New(Audio::AudioEngine::getDeviceName)->GetFunction() ); + //functionTemplate->PrototypeTemplate()->Set( Nan::New("getNumDevices"), Nan::New(Audio::AudioEngine::getNumDevices)->GetFunction() ); + Nan::SetPrototypeMethod(functionTemplate, "isActive", Audio::AudioEngine::isActive); + Nan::SetPrototypeMethod(functionTemplate, "getDeviceName", Audio::AudioEngine::getDeviceName); + Nan::SetPrototypeMethod(functionTemplate, "getNumDevices", Audio::AudioEngine::getNumDevices); // Set - //functionTemplate->PrototypeTemplate()->Set( NanNew("setOptions"), NanNew(Audio::AudioEngine::setOptions)->GetFunction() ); - //functionTemplate->PrototypeTemplate()->Set( NanNew("getOptions"), NanNew(Audio::AudioEngine::getOptions)->GetFunction() ); - //functionTemplate->PrototypeTemplate()->Set( NanNew("write"), NanNew(Audio::AudioEngine::write)->GetFunction() ); - //functionTemplate->PrototypeTemplate()->Set( NanNew("read"), NanNew(Audio::AudioEngine::read)->GetFunction() ); - //functionTemplate->PrototypeTemplate()->Set( NanNew("isBufferEmpty"), NanNew(Audio::AudioEngine::isBufferEmpty)->GetFunction() ); - NODE_SET_PROTOTYPE_METHOD(functionTemplate, "setOptions", Audio::AudioEngine::setOptions); - NODE_SET_PROTOTYPE_METHOD(functionTemplate, "getOptions", Audio::AudioEngine::getOptions); - NODE_SET_PROTOTYPE_METHOD(functionTemplate, "write", Audio::AudioEngine::write); - NODE_SET_PROTOTYPE_METHOD(functionTemplate, "read", Audio::AudioEngine::read); - NODE_SET_PROTOTYPE_METHOD(functionTemplate, "isBufferEmpty", Audio::AudioEngine::isBufferEmpty); + //functionTemplate->PrototypeTemplate()->Set( Nan::New("setOptions"), Nan::New(Audio::AudioEngine::setOptions)->GetFunction() ); + //functionTemplate->PrototypeTemplate()->Set( Nan::New("getOptions"), Nan::New(Audio::AudioEngine::getOptions)->GetFunction() ); + //functionTemplate->PrototypeTemplate()->Set( Nan::New("write"), Nan::New(Audio::AudioEngine::write)->GetFunction() ); + //functionTemplate->PrototypeTemplate()->Set( Nan::New("read"), Nan::New(Audio::AudioEngine::read)->GetFunction() ); + //functionTemplate->PrototypeTemplate()->Set( Nan::New("isBufferEmpty"), Nan::New(Audio::AudioEngine::isBufferEmpty)->GetFunction() ); + Nan::SetPrototypeMethod(functionTemplate, "setOptions", Audio::AudioEngine::setOptions); + Nan::SetPrototypeMethod(functionTemplate, "getOptions", Audio::AudioEngine::getOptions); + Nan::SetPrototypeMethod(functionTemplate, "write", Audio::AudioEngine::write); + Nan::SetPrototypeMethod(functionTemplate, "read", Audio::AudioEngine::read); + Nan::SetPrototypeMethod(functionTemplate, "isBufferEmpty", Audio::AudioEngine::isBufferEmpty); //constructor = Persistent::New( functionTemplate->GetFunction() ); - //Local tpl = NanNew(EOLFinder::New); - NanAssignPersistent(constructor, functionTemplate->GetFunction()); + //Local tpl = Nan::New(EOLFinder::New); +// NanAssignPersistent(constructor, functionTemplate->GetFunction()); + constructor.Reset(Isolate::GetCurrent(), functionTemplate->GetFunction()); } // end AudioEngine::Init() ////////////////////////////////////////////////////////////////////////////// /*! Create a new instance of the audio engine */ -//v8::Handle Audio::AudioEngine::NewInstance(const v8::Arguments& args) { - -NAN_METHOD(Audio::AudioEngine::NewInstance){ - NanScope(); +//v8::Handle Audio::AudioEngine::NewInstance(const v8::Arguments& info) { +void Audio::AudioEngine::NewInstance(const Nan::FunctionCallbackInfo& info) { + Nan::HandleScope scope; //HandleScope scope; - unsigned argc = args.Length(); + unsigned argc = info.Length(); if( argc > 2 ) argc = 2; Handle* argv = new Handle[argc]; - argv[0] = args[0]; + argv[0] = info[0]; if( argc > 1 ) - argv[1] = args[1]; + argv[1] = info[1]; //Local instance = constructor->NewInstance( argc, argv ); - Local instance = NanNew(constructor)->NewInstance(argc, argv); + Local instance = Nan::New(constructor)->NewInstance(argc, argv); //Local instance = constructor->NewInstance(argc, argv); - NanReturnValue( instance ); + info.GetReturnValue().Set( instance ); } // end AudioEngine::NewInstance() ////////////////////////////////////////////////////////////////////////////// /*! Create a v8 object */ -//v8::Handle Audio::AudioEngine::New( const v8::Arguments& args ) { -NAN_METHOD(Audio::AudioEngine::New){ - NanScope(); +void Audio::AudioEngine::New(const Nan::FunctionCallbackInfo& info) { + Nan::HandleScope scope; //HandleScope scope; Local options; - if( args.Length() > 0 ) { - if( !args[0]->IsObject() ) - return NanThrowTypeError("First argument must be an object."); + if( info.Length() > 0 ) { + if( !info[0]->IsObject() ) + return Nan::ThrowTypeError("First argument must be an object."); else - options = Local::Cast( args[0] ); + options = Local::Cast( info[0] ); } else { - options = NanNew(); + options = Nan::New(); } AudioEngine* pEngine = new AudioEngine( options ); - pEngine->Wrap( args.This() ); + pEngine->Wrap( info.This() ); - NanReturnValue( args.This() ); + info.GetReturnValue().Set( info.This() ); } // end AudioEngine::New() ////////////////////////////////////////////////////////////////////////////// /*! Write samples to the current audio device */ -//v8::Handle Audio::AudioEngine::write( const v8::Arguments& args ) { -NAN_METHOD(Audio::AudioEngine::write){ - NanScope(); +void Audio::AudioEngine::write(const Nan::FunctionCallbackInfo& info) { + Nan::HandleScope scope; //HandleScope scope; - AudioEngine* pEngine = AudioEngine::Unwrap( args.This() ); + AudioEngine* pEngine = AudioEngine::Unwrap( info.This() ); - if (args.Length() > 1 || !args[0]->IsArray()){ - return NanThrowTypeError("First argument should be an array."); + if (info.Length() > 1 || !info[0]->IsArray()){ + return Nan::ThrowTypeError("First argument should be an array."); } uv_mutex_lock( &pEngine->m_mutex ); - pEngine->queueOutputBuffer( Local::Cast(args[0]) ); + pEngine->queueOutputBuffer( Local::Cast(info[0]) ); uv_mutex_unlock( &pEngine->m_mutex ); - Handle result = NanNew( false ); - - NanReturnValue( result ); + info.GetReturnValue().Set( false ); } // end AudioEngine::Write() ////////////////////////////////////////////////////////////////////////////// /*! Checks if the current audio buffer has been fed to Port Audio */ -//v8::Handle Audio::AudioEngine::isBufferEmpty( const v8::Arguments& args ) { -NAN_METHOD(Audio::AudioEngine::isBufferEmpty){ - NanScope(); +void Audio::AudioEngine::isBufferEmpty(const Nan::FunctionCallbackInfo& info) { + Nan::HandleScope scope; //HandleScope scope; - AudioEngine* pEngine = AudioEngine::Unwrap( args.This() ); + AudioEngine* pEngine = AudioEngine::Unwrap( info.This() ); uv_mutex_lock( &pEngine->m_mutex ); - Handle isEmpty = NanNew(pEngine->m_uNumCachedOutputSamples[pEngine->m_uCurrentWriteBuffer] == 0); + Local isEmpty = Nan::New(pEngine->m_uNumCachedOutputSamples[pEngine->m_uCurrentWriteBuffer] == 0); uv_mutex_unlock( &pEngine->m_mutex ); - NanReturnValue( isEmpty ); + info.GetReturnValue().Set( isEmpty ); } // end AudioEngine::isBufferEmpty() ////////////////////////////////////////////////////////////////////////////// /*! Read samples from the current audio device */ -//v8::Handle Audio::AudioEngine::read( const v8::Arguments& args ) { -NAN_METHOD(Audio::AudioEngine::read){ - NanScope(); +void Audio::AudioEngine::read(const Nan::FunctionCallbackInfo& info) { + Nan::HandleScope scope; //HandleScope scope; - AudioEngine* pEngine = AudioEngine::Unwrap( args.This() ); + AudioEngine* pEngine = AudioEngine::Unwrap( info.This() ); if (pEngine->m_bReadMicrophone) { Pa_ReadStream( pEngine->m_pPaStream, pEngine->m_cachedInputSampleBlock, pEngine->m_uSamplesPerBuffer ); } - Handle input = pEngine->getInputBuffer(); + Local input = pEngine->getInputBuffer(); - NanReturnValue( input ); + info.GetReturnValue().Set( input ); } // end AudioEngine::Read() ////////////////////////////////////////////////////////////////////////////// /*! Returns whether the PortAudio stream is active */ -//v8::Handle Audio::AudioEngine::isActive( const v8::Arguments& args ) { -NAN_METHOD(Audio::AudioEngine::isActive){ - NanScope(); +void Audio::AudioEngine::isActive(const Nan::FunctionCallbackInfo& info) { + Nan::HandleScope scope; //HandleScope scope; - AudioEngine* pEngine = AudioEngine::Unwrap( args.This() ); + AudioEngine* pEngine = AudioEngine::Unwrap( info.This() ); if( Pa_IsStreamActive(pEngine->m_pPaStream) ) - NanReturnValue( NanNew(true) ); + info.GetReturnValue().Set( Nan::New(true) ); else - NanReturnValue( NanNew(false) ); + info.GetReturnValue().Set( Nan::New(false) ); } // end AudioEngine::IsActive() ////////////////////////////////////////////////////////////////////////////// /*! Get the name of an audio device with a given ID number */ -//v8::Handle Audio::AudioEngine::getDeviceName( const v8::Arguments& args ) { -NAN_METHOD(Audio::AudioEngine::getDeviceName){ - NanScope(); +void Audio::AudioEngine::getDeviceName(const Nan::FunctionCallbackInfo& info) { + Nan::HandleScope scope; //HandleScope scope; - if( !args[0]->IsNumber() ) { - return NanThrowTypeError("getDeviceName() requires a device index"); + if( !info[0]->IsNumber() ) { + return Nan::ThrowTypeError("getDeviceName() requires a device index"); } - Local deviceIndex = Local::Cast( args[0] ); + Local deviceIndex = Local::Cast( info[0] ); const PaDeviceInfo* pDeviceInfo = Pa_GetDeviceInfo( (PaDeviceIndex)deviceIndex->NumberValue() ); - NanReturnValue( NanNew(pDeviceInfo->name) ); + info.GetReturnValue().Set( Nan::New(pDeviceInfo->name).ToLocalChecked() ); } // end AudioEngine::GetDeviceName() ////////////////////////////////////////////////////////////////////////////// /*! Get the number of available devices */ -//v8::Handle Audio::AudioEngine::getNumDevices( const v8::Arguments& args ) { -NAN_METHOD(Audio::AudioEngine::getNumDevices){ - NanScope(); +void Audio::AudioEngine::getNumDevices(const Nan::FunctionCallbackInfo& info) { + Nan::HandleScope scope; //HandleScope scope; int deviceCount = Pa_GetDeviceCount(); - NanReturnValue( NanNew(deviceCount) ); + info.GetReturnValue().Set( Nan::New(deviceCount) ); } // end AudioEngine::GetNumDevices() @@ -627,15 +607,15 @@ void Audio::AudioEngine::restartStream() { error = Pa_StopStream( m_pPaStream ); if( error != paNoError ) - NanThrowTypeError("Failed to stop audio stream"); + Nan::ThrowTypeError("Failed to stop audio stream"); // Close the audio stream error = Pa_CloseStream( m_pPaStream ); if( error != paNoError ) - NanThrowTypeError("Failed to close audio stream"); + Nan::ThrowTypeError("Failed to close audio stream"); - // Open an audio stream. + // Open an audio stream.`` error = Pa_OpenStream( &m_pPaStream, &m_inputParams, &m_outputParams, @@ -646,14 +626,14 @@ void Audio::AudioEngine::restartStream() { NULL ); if( error != paNoError ) { - NanThrowTypeError("Failed to open audio stream :("); + Nan::ThrowTypeError("Failed to open audio stream :("); } // Start the audio stream error = Pa_StartStream( m_pPaStream ); if( error != paNoError ) - NanThrowTypeError("Failed to start audio stream"); + Nan::ThrowTypeError("Failed to start audio stream"); } // end AudioEngine::restartStream() diff --git a/NodeCoreAudio/AudioEngine.h b/NodeCoreAudio/AudioEngine.h index 11f42d4..ca09ae9 100644 --- a/NodeCoreAudio/AudioEngine.h +++ b/NodeCoreAudio/AudioEngine.h @@ -8,7 +8,6 @@ #include "portaudio.h" #include #include -#include #include #include using namespace v8; using namespace std; @@ -79,14 +78,14 @@ namespace Audio { void queueOutputBuffer( Handle result ); //!< Queues up an array to be sent to the sound card void setSample( int position, Handle sample ); //!< Sets a sample in the queued output buffer - Handle getInputBuffer(); //!< Returns a v8 array filled with input samples + Local getInputBuffer(); //!< Returns a v8 array filled with input samples Handle getSample( int position ); //!< Returns a sound card sample converted to a v8 Number PaStream *m_pPaStream; //!< The PortAudio stream object PaStreamParameters m_inputParams, //!< PortAudio stream parameters m_outputParams; - Handle m_hInputBuffer; //!< Our pre-allocated input buffer + Local m_hInputBuffer; //!< Our pre-allocated input buffer uv_thread_t ptStreamThread, //!< Our stream thread jsAudioThread; //!< Our JavaScript Audio thread diff --git a/NodeCoreAudio/NodeCoreAudio.cpp b/NodeCoreAudio/NodeCoreAudio.cpp index f370ebb..4246a6a 100644 --- a/NodeCoreAudio/NodeCoreAudio.cpp +++ b/NodeCoreAudio/NodeCoreAudio.cpp @@ -27,18 +27,18 @@ NAN_METHOD(CreateEngine) void InitAll(Handle target) { Audio::AudioEngine::Init( target ); - NODE_SET_METHOD(target, "createAudioEngine", Audio::AudioEngine::NewInstance); + Nan::SetMethod(target, "createAudioEngine", Audio::AudioEngine::NewInstance); //NODE_SET_METHOD(target, "createAudioEngine", CreateEngine); //target->Set( NanNew("createAudioEngine"), CreateEngine); //target->Set( NanNew("createAudioEngine"), NanNew(CreateEngine)->GetFunction() ); //target->Set( NanNew("createAudioEngine"), NanNew(Audio::AudioEngine::NewInstance)->GetFunction() ); - target->Set( NanNew("sampleFormatFloat32"), NanNew(1) ); - target->Set( NanNew("sampleFormatInt32"), NanNew(2) ); - target->Set( NanNew("sampleFormatInt24"), NanNew(4) ); - target->Set( NanNew("sampleFormatInt16"), NanNew(8) ); - target->Set( NanNew("sampleFormatInt8"), NanNew(10) ); - target->Set( NanNew("sampleFormatUInt8"), NanNew(20) ); + target->Set( Nan::New("sampleFormatFloat32").ToLocalChecked(), Nan::New(1)); + target->Set( Nan::New("sampleFormatInt32").ToLocalChecked(), Nan::New(2) ); + target->Set( Nan::New("sampleFormatInt24").ToLocalChecked(), Nan::New(4) ); + target->Set( Nan::New("sampleFormatInt16").ToLocalChecked(), Nan::New(8) ); + target->Set( Nan::New("sampleFormatInt8").ToLocalChecked(), Nan::New(10) ); + target->Set( Nan::New("sampleFormatUInt8").ToLocalChecked(), Nan::New(20) ); } NODE_MODULE( NodeCoreAudio, InitAll ); diff --git a/package.json b/package.json index 219870d..8af3f7a 100644 --- a/package.json +++ b/package.json @@ -10,14 +10,17 @@ "dependencies": { "audio-streamer": ">= 0.1.0", "fft": "~0.2.1", - "nan": "^1.8.4", + "nan": "^2.1.0", "node-waveheader": "git://github.com/mrose17/node-waveheader.git", "portfinder": "0.2.1" }, "description": "Core native node.js audio functionality, including sound card access and audio streaming", "main": "./node-core-audio", + "scripts": { + "test": "node test/test.js" + }, "engines": { - "node": ">=0.8.0" + "node": ">=0.12.0" }, "keywords": [ "audio",