diff --git a/CMakeLists.txt b/CMakeLists.txt index b22f0e959613054056b526bf1cbdf35431c87802..fc836b79504207541b1188216fc1f9249d93e15c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,22 +51,24 @@ if( NOT WIN32 ) set( SHARED_LIBRARY_EXTENSION "so" ) endif( ) -# VA shared libs -install( FILES "${VABASE_LIBRARY_DIRS}/VABase.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) -install( FILES "${VANET_LIBRARY_DIRS}/VANet.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) - -# VistaCoreLibs shared libs -foreach( VISTACORELIBS_LIBRARY_DIR ${VISTACORELIBS_LIBRARY_DIRS} ) - if( EXISTS "${VISTACORELIBS_LIBRARY_DIR}/VistaBase.${SHARED_LIBRARY_EXTENSION}" ) - install( FILES "${VISTACORELIBS_LIBRARY_DIR}/VistaBase.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) - endif( ) - if( EXISTS "${VISTACORELIBS_LIBRARY_DIR}/VistaAspects.${SHARED_LIBRARY_EXTENSION}" ) - install( FILES "${VISTACORELIBS_LIBRARY_DIR}/VistaAspects.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) - endif( ) - if( EXISTS "${VISTACORELIBS_LIBRARY_DIR}/VistaInterProcComm.${SHARED_LIBRARY_EXTENSION}" ) - install( FILES "${VISTACORELIBS_LIBRARY_DIR}/VistaInterProcComm.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) - endif( ) -endforeach( ) +if( BUILD_SHARED_LIBS ) + # VA shared libs + install( FILES "${VABASE_LIBRARY_DIRS}/VABase.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) + install( FILES "${VANET_LIBRARY_DIRS}/VANet.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) + + # VistaCoreLibs shared libs + foreach( VISTACORELIBS_LIBRARY_DIR ${VISTACORELIBS_LIBRARY_DIRS} ) + if( EXISTS "${VISTACORELIBS_LIBRARY_DIR}/VistaBase.${SHARED_LIBRARY_EXTENSION}" ) + install( FILES "${VISTACORELIBS_LIBRARY_DIR}/VistaBase.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) + endif( ) + if( EXISTS "${VISTACORELIBS_LIBRARY_DIR}/VistaAspects.${SHARED_LIBRARY_EXTENSION}" ) + install( FILES "${VISTACORELIBS_LIBRARY_DIR}/VistaAspects.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) + endif( ) + if( EXISTS "${VISTACORELIBS_LIBRARY_DIR}/VistaInterProcComm.${SHARED_LIBRARY_EXTENSION}" ) + install( FILES "${VISTACORELIBS_LIBRARY_DIR}/VistaInterProcComm.${SHARED_LIBRARY_EXTENSION}" DESTINATION "python/Lib/site-packages" ) + endif( ) + endforeach( ) +endif( ) # Tests install( DIRECTORY "tests" DESTINATION "python" ) diff --git a/examples/jupyter/va_notebook_example.ipynb b/examples/jupyter/va_notebook_example.ipynb index 41c2c990985bdb8568099312d202138a3547a50d..eb49ae1f9a30faf3a6981be89b5e922078f41471 100644 --- a/examples/jupyter/va_notebook_example.ipynb +++ b/examples/jupyter/va_notebook_example.ipynb @@ -7,28 +7,37 @@ "# VA notebook test\n", "\n", "This is a simple test program that demonstrates the use of the VA Python binding within a jupyter notebook environment.\n", - "Before we start scripting, let's make VA available for us. If it is not available system (or user) wide, this is how you can add the `va` module from the distribution folder structure." + "\n", + "## Before we start\n", + "Before we start scripting, let's make VA available for us. If it is not installed and available from everywhere, this is how you can add the `va` module folder:" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "scrolled": false }, "outputs": [], "source": [ "import sys\n", - "sys.path.append( \"../Lib/site-packages\" )\n", - "sys.path" + "sys.path.append( \"../../dist/Lib/site-packages\" ) # build\n", + "sys.path.append( \"../Lib/site-packages\" ) # deploy" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Start\n", + "\n", + "We start by making va available for our script" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 2, + "metadata": {}, "outputs": [], "source": [ "import va" @@ -38,7 +47,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Ok. Now let's try to connect to a VA server running on the local machine. Remark: this local machine will be the one where this server jupyter notebook is running, not necessarily your PC." + "Ok. Now let's try to connect to the VA server that should be running on the same computer where this jupyter notebook is running. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We start by finding out where we are currently working and list the files available, i.e. to identify files that can be used as HRIR, audio file or directivity." ] }, { @@ -47,41 +63,84 @@ "metadata": {}, "outputs": [], "source": [ - "va.connect( \"localhost\" ) # default port is: 12340; can be given as second parameter" + "connection_status = va.connect()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "We start by finding out where we are currently working and list the files available, i.e. to identify files that can be used as HRIR, audio file or directivity." + "We can check the connection by the following line" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "VA connection ready!\n" + ] + } + ], + "source": [ + "connected = va.is_connected()\n", + "if connected :\n", + " print( \"VA connection ready!\" )\n", + "else :\n", + " print( \"Something went wrong.\" )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "... and also use different server names and ports" + ] + }, + { + "cell_type": "code", + "execution_count": 5, "metadata": {}, "outputs": [], + "source": [ + "if not connected :\n", + " va.connect( \"localhost\", 12340 ) # these are the default arguments" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "working directory: D:\\Users\\stienen\\dev\\VA\\VAPython\\examples\\jupyter\n" + ] + } + ], "source": [ "import os\n", "current_working_dir = os.getcwd()\n", - "print( \"working directory: \" + current_working_dir )\n", - "os.listdir( current_working_dir )" + "print( \"working directory: \" + current_working_dir )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Now lets add this folder to VA. This means that VA can find files that reside in this location, all you have to use is the file name (or a relative path from the base path you have added as search path)." + "Now lets add this folder to VA. This means that VA can find files that reside in this location. All you have to do is use a file name or a relative path from this base path. You can add as much folders as you like." ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "va.add_search_path( current_working_dir )" diff --git a/setup_static.py b/setup_static.py new file mode 100644 index 0000000000000000000000000000000000000000..ead84fc152f46fcb81572bcb701c8d1b6eee02e9 --- /dev/null +++ b/setup_static.py @@ -0,0 +1,20 @@ +from distutils.core import setup, Extension + +module1 = Extension('va', + define_macros = [('VA_PYTHON_VERSION_MAJOR', '2017'), ('VA_PYTHON_VERSION_MINOR', 'c'), ('VABASE_STATIC', 1), ('VANET_STATIC', 1), ('VISTABASE_STATIC', 1), ('VISTAINTERPROCCOMM_STATIC', 1), ('VISTAASPECTS_STATIC',1)], + include_dirs = ['../VABase/include','../VANet/include'], + libraries = ['VABase', 'VANet', 'VistaBase', 'VistaAspects', 'VistaInterProcComm', 'winmm', 'ws2_32' ], + library_dirs = ['../build_win32-x64.vc14_static/lib','../../ViSTA/build_static_win32-x64.vc14/lib'], + sources = ['src/vasingleton.cpp'] ) + +setup (name = 'va', + version = '2017.c', + description = 'Virtual Acoustics singleton interface', + author = 'Institute of Technical Acoustics (ITA), RWTH Aachen University', + author_email = 'post@akustik.rwth-aachen.de', + url = 'https://virtualacoustics.org', + long_description = ''' +Virtual Acoustics is a real-time auralization framework for Virtual Reality. This module is an interface to interact with a VA server. +''', + license = "Apache License Version 2.0", + ext_modules = [module1]) \ No newline at end of file diff --git a/src/vasingletonmethods.hpp b/src/vasingletonmethods.hpp index dedd6d3f13701af2dcec0fe4542eb3764b257b65..7726a09c742f68a0310d669b700778643901e2aa 100644 --- a/src/vasingletonmethods.hpp +++ b/src/vasingletonmethods.hpp @@ -39,16 +39,16 @@ static PyObject* va_connect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs if (g_pVANetClient->IsConnected()) { - PyErr_Warn(pSelf, "Was still connected, forced disconnect."); + PyErr_WarnEx(NULL, "Was still connected, forced disconnect.", 1 ); g_pVANetClient->Disconnect(); } static const char * const _keywords[] = { "server", "port", NULL }; static _PyArg_Parser _parser = { "|si:connect", _keywords, 0 }; - char* pcServerIP; + char* pcServerIP = nullptr; int iServerPort = 12340; - if (!_PyArg_ParseStack(ppArgs, nArgs, pKeywordNames, &_parser, pcServerIP, &iServerPort)) + if (!_PyArg_ParseStack(ppArgs, nArgs, pKeywordNames, &_parser, &pcServerIP, &iServerPort)) return NULL; std::string sServerIP = pcServerIP ? std::string(pcServerIP) : "localhost"; @@ -63,10 +63,15 @@ static PyObject* va_connect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs static PyObject* va_disconnect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) { if (g_pVANetClient) - g_pVANetClient->Disconnect(); - else - PyErr_Warn(pSelf, "Was not connected, doing nothing. Use is_connected to avoid this message."); + { + if (g_pVANetClient->IsConnected()) + { + g_pVANetClient->Disconnect(); + return PyBool_FromLong(1); + } + } + PyErr_WarnEx(NULL, "Was not connected, doing nothing. Use is_connected to avoid this message.", 1); return PyBool_FromLong(1); }; @@ -78,7 +83,7 @@ static PyObject* va_is_connected(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t return PyBool_FromLong(g_pVANetClient->IsConnected()); }; -static PyObject* va_reset(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) +static PyObject* va_reset(PyObject*, PyObject*) { VAPY_REQUIRE_CONN_TRY; @@ -91,20 +96,14 @@ static PyObject* va_reset(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, static PyObject* va_enumerate_modules(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) { VAPY_REQUIRE_CONN_TRY; - VA_EXCEPT_NOT_IMPLEMENTED; - return NULL; - VAPY_CATCH_RETURN; }; static PyObject* va_call_module(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) { VAPY_REQUIRE_CONN_TRY; - VA_EXCEPT_NOT_IMPLEMENTED; - return NULL; - VAPY_CATCH_RETURN; }; @@ -112,8 +111,13 @@ static PyObject* va_add_search_path(PyObject* pSelf, PyObject** ppArgs, Py_ssize { VAPY_REQUIRE_CONN_TRY; - VA_EXCEPT_NOT_IMPLEMENTED; - return NULL; + static const char * const _keywords[] = { "directory_path", NULL }; + static _PyArg_Parser _parser = { "s:add_search_path", _keywords, 0 }; + char* pcPath = nullptr; + if (!_PyArg_ParseStack(ppArgs, nArgs, pKeywordNames, &_parser, &pcPath)) + return NULL; + + return PyBool_FromLong(g_pVANetClient->GetCoreInstance()->AddSearchPath(std::string(pcPath))); VAPY_CATCH_RETURN; }; diff --git a/tests/va_test_connection.py b/tests/va_test_connection.py index c4e6f27dfc7d9e5e700787f9023e84072227e156..a388e519f81d6d2dd14b1405eb99343179cc9ffb 100644 --- a/tests/va_test_connection.py +++ b/tests/va_test_connection.py @@ -7,14 +7,47 @@ sys.path.append( '../Lib/site-packages' ) # deploy structure import va -print( "Testing va extension connection." ) +print( "Testing va extension connection methods." ) -if va.connect( "pc-jst" ) : - print( "Successfully connected" ) +if va.connect() : + print( "Successfully connected to local server without arguments" ) + va.disconnect() # direct disconnect else : print( "Connection failed" ) +if va.connect( "localhost" ) : + print( "Successfully connected to local server with localhost argument" ) +else : + print( "Connection failed" ) + +# sensitive disconnect if va.is_connected() : va.disconnect() + +if va.connect( "localhost", 12340 ) : + print( "Successfully connected to local server with localhost and port 12340 argument" ) +else : + print( "Connection failed" ) + +print( "Disconnect." ) +va.disconnect() + +import time + +import warnings + +with warnings.catch_warnings() : + warnings.simplefilter( "always" ) + time.sleep( 1 ) + print( "Double disconnect:" ) + va.disconnect() # double disconnect should raise warning + + va.connect() + + time.sleep( 1 ) + print( "Double connect:" ) + va.connect() # double connect should raise forced disconnection warning + +va.disconnect() print( "Test done." )