Progress on va extension and adding tests including jupyter notebook example

parent b9c73f87
python.exe setup.py build
python.exe setup.py build --force
python.exe setup.py install --prefix dist
python.exe setup.py sdist --formats=zip
\ No newline at end of file
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 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."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['',\n",
" 'c:\\\\users\\\\jonas\\\\appdata\\\\local\\\\programs\\\\python\\\\python36\\\\python36.zip',\n",
" 'c:\\\\users\\\\jonas\\\\appdata\\\\local\\\\programs\\\\python\\\\python36\\\\DLLs',\n",
" 'c:\\\\users\\\\jonas\\\\appdata\\\\local\\\\programs\\\\python\\\\python36\\\\lib',\n",
" 'c:\\\\users\\\\jonas\\\\appdata\\\\local\\\\programs\\\\python\\\\python36',\n",
" 'c:\\\\users\\\\jonas\\\\appdata\\\\local\\\\programs\\\\python\\\\python36\\\\lib\\\\site-packages',\n",
" 'c:\\\\users\\\\jonas\\\\appdata\\\\local\\\\programs\\\\python\\\\python36\\\\lib\\\\site-packages\\\\IPython\\\\extensions',\n",
" 'C:\\\\Users\\\\jonas\\\\.ipython',\n",
" '../../../build_win32-x64.vc14/python/Lib/site-packages',\n",
" '../../../build_win32-x64.vc14/python/Lib/site-packages',\n",
" 'C:\\\\dev\\\\VA\\\\dist\\\\win32-x64.vc14\\\\python\\\\Lib\\\\site-packages',\n",
" '../../../build_win32-x64.vc14/python/Lib/site-packages',\n",
" 'C:\\\\dev\\\\VA\\\\dist\\\\win32-x64.vc14\\\\python\\\\Lib\\\\site-packages']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import sys\n",
"sys.path.append( \"../Lib/site-packages\" )\n",
"sys.path"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import va"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We start by adding finding out where we are currently working and list the files available, i.e. to load them as HRIR, audio file or directivity.\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"working directory: C:\\dev\\VA\\VAPython\\examples\\jupyter_notebook\n"
]
},
{
"data": {
"text/plain": [
"['.ipynb_checkpoints', 'VA notebook test.ipynb']"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"current_working_dir = os.getcwd()\n",
"print( \"working directory: \" + current_working_dir )\n",
"os.listdir( 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. We first have to import va, though:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"va.add_search_path( current_working_dir )"
]
},
{
"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."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'va' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-316881027ede>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mva\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconnect\u001b[0m\u001b[1;33m(\u001b[0m \u001b[1;34m\"localhost\"\u001b[0m \u001b[1;33m)\u001b[0m \u001b[1;31m# default port is: 12340; can be given as second parameter\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'va' is not defined"
]
}
],
"source": [
"va.connect( \"localhost\" ) # default port is: 12340; can be given as second parameter"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
import os
# Add va module if it was not installed
import sys
sys.path.append( '../Lib/site-packages' )
import os
current_exec_dir = os.getcwd()
import va
va.connect # localhost
va.connect() # localhost
va.add_search_path( current_exec_dir ) # add current working path to find any file lying around here
signal_source_id = va.create_audio_file_signal_source( "Bauer.wav" ) # Provide this file or any other file
......@@ -25,4 +28,4 @@ va.set_listener_position( listener_id, ( 0, 1.7, 0 ) ) # Ear height at 1.7m
va.set_audio_file_signal_source_is_looping( signal_source_id, true )
va.set_audio_file_signal_source_play_state( signal_source_id, "play" )
va.disconnect
va.disconnect()
......@@ -10,6 +10,7 @@ static struct PyMethodDef va_methods[] =
{
{ "connect", ( PyCFunction ) va_connect, METH_FASTCALL, va_connect_doc },
{ "disconnect", ( PyCFunction ) va_disconnect, METH_FASTCALL, va_no_doc },
{ "is_connected", ( PyCFunction ) va_is_connected, METH_FASTCALL, va_no_doc },
{ "reset", ( PyCFunction ) va_reset, METH_FASTCALL, va_no_doc },
{ "enumerate_modules", ( PyCFunction ) va_enumerate_modules, METH_FASTCALL, va_no_doc },
......
......@@ -27,28 +27,29 @@ static void RequireCoreAvailable()
VA_EXCEPT2( CVAException::NETWORK_ERROR, "VA client available, but access to VA interface failed. Please reconnect." );
}
static PyObject* va_connect( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
static PyObject* va_connect( PyObject* self, PyObject* args )
{
if( !g_pVANetClient )
g_pVANetClient = IVANetClient::Create();
if( g_pVANetClient->IsConnected() )
g_pVANetClient->Disconnect();
static const char * const _keywords[] = { "server", "port", NULL };
static _PyArg_Parser _parser = { "Oi:connect", _keywords, 0 };
PyObject* server;
PyObject* port;
if( !_PyArg_ParseStack( args, nargs, kwnames, &_parser, &server, &port ) )
return PyBool_FromLong( 0 );
std::string sServer = "localhost";
int iPort = 12340;
if( IVANetClient::VA_NO_ERROR == g_pVANetClient->Initialize( sServer, iPort ) )
return PyBool_FromLong( 1 );
else
const char* pcServerIP = nullptr;
std::string sServerIP = "localhost";
if( PyArg_ParseTuple( args, "s", &pcServerIP ) )
sServerIP = std::string( pcServerIP );
int iServerPort = -1;
if( !PyArg_ParseTuple( args, "i", &iServerPort ))
iServerPort = 12340;
if( IVANetClient::VA_NO_ERROR == g_pVANetClient->Initialize( sServerIP, iServerPort ) )
return PyBool_FromLong( 1 );
PyErr_SetString( PyExc_ConnectionError, std::string( "Could not connect to " + sServerIP + " on " + std::to_string( ( long ) iServerPort ) ).c_str() );
return NULL;
}
static PyObject* va_disconnect( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
......@@ -58,6 +59,13 @@ static PyObject* va_disconnect( PyObject *module, PyObject **args, Py_ssize_t na
return PyBool_FromLong( 1 );
}
static PyObject* va_is_connected( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
{
if( !g_pVANetClient )
return PyBool_FromLong( 0 );
else
return PyBool_FromLong( g_pVANetClient->IsConnected() );
}
static PyObject* va_reset( PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames )
{
......
......@@ -5,12 +5,12 @@ print( "Testing va extension connection." )
import va
if( va.connect( "localhost", 12340 ) )
if va.connect( "localhost", 12340 ) == 1 :
print( "Successfully connected" )
else
else :
print( "Connection failed" )
if( va.is_connected() )
if va.is_connected() == 1:
va.disconnect()
print( "Test done." )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment