va_core_controller.ipynb 8.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# VA simple acoustic scene\n",
    "This is a simple example how to create a simple acoustic scene in VA using Python."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prerequisites\n",
    "You can ignore this part, it is for preparation purposes only."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append( '../../Lib/site-packages' )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import ipywidgets as widgets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import va"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Connect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "va.connect();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Control"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can mute and unmute the entire audio output by using `set_output_muted` and receive the current setting by `is_output_muted`. The setter uses the optional argument `True` or `False` and will mute the output if no argument is passed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "07034f7c9d36416d9a094fd806b3fb22",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "A Jupyter Widget"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "mute_output_button = widgets.ToggleButton( description = 'Output muted', value = va.is_output_muted() )\n",
    "\n",
    "def on_mute_button_clicked( b ) :\n",
    "    if b.name == 'value' :\n",
    "        va.set_output_muted( b.new ) # True if toggle button appears 'active'\n",
    "    \n",
    "mute_output_button.observe( on_mute_button_clicked )\n",
    "display( mute_output_button )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Gain\n",
    "The output gain or output volume of VA can be controlled by `set_output_gain` and received by `get_output_gain`. Gains or volumes are defined as a factore between 0 and 1. The same functions also exist for the audio inputs of the sound device."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1b4fe929c18c409281211379ba390e52",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "A Jupyter Widget"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "output_gain_slider = widgets.FloatSlider( \n",
    "    value = va.get_output_gain(),\n",
    "    description = 'Output gain:',\n",
    "    min = 0.0,\n",
    "    max = 1.0,\n",
    "    step = 0.1,\n",
    "    readout = True,\n",
    "    readout_format = '.1f' )\n",
    "\n",
    "def on_output_gain_changed( s ) :\n",
    "    if s.name == 'value' :\n",
    "        va.set_output_gain( s.new )\n",
    "\n",
    "output_gain_slider.observe( on_output_gain_changed )\n",
    "display( output_gain_slider )"
   ]
  },
160 161 162 163
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
164
    "### Macros\n",
165 166 167 168
    "\n",
    "Macros can be defined to make your life easier. Don't mess around with file pathes too much, use macros. Don't rename output file names for recording and other exported information, use macros. You can test your macros using th method `substitute_macro` (see below), but you don't have to do it yourself. VA will always substitute macros where possible."
   ]
  },
169 170
  {
   "cell_type": "code",
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "$(DefaultHRIR): HRIR\\\\ITA-Kunstkopf_HRIR_AP11_Pressure_Equalized_3x3_256.v17.ir.daff\n",
      "$(ProjectName): MyVirtualAcousticsProject\n",
      "$(data): $(data)\n",
      "$(big_data_dir): C:\\\\data\n",
      "$(conf): $(conf)\n"
     ]
    }
   ],
   "source": [
    "print( '$(DefaultHRIR): ' + va.substitute_macro( '$(DefaultHRIR)' ) )\n",
    "print( '$(ProjectName): ' + va.substitute_macro( '$(ProjectName)' ) )\n",
    "print( '$(data): ' + va.substitute_macro( '$(data)' ) )\n",
    "print( '$(big_data_dir): ' + va.substitute_macro( '$(big_data_dir)' ) )\n",
    "print( '$(conf): ' + va.substitute_macro( '$(conf)' ) )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
198
    "### Reset\n",
199 200
    "Use `reset` to reset the entire scene."
   ]
201 202 203
  },
  {
   "cell_type": "code",
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d008b4bac6c14a21ad1956680f4b7645",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "A Jupyter Widget"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "reset_button = widgets.Button( description = 'Reset VA server' )\n",
    "\n",
    "def on_reset_button_clicked( b ) :\n",
    "    va.reset()\n",
    "    \n",
    "reset_button.on_click( on_reset_button_clicked )\n",
    "display( reset_button )"
   ]
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Modules\n",
    "You can interact with modules using a magic struct. In Python, this struct is represented by a (nested) dictionary, that is translated to a VAStruct. This struct can be used to call modules and receiver information on return. It is used to change any setting in a given module. This way, interfaces sta untouched but developers have all the possibilities required to try out new prototype functions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1d339c748fd44c82ab9b8d5f4a31610c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "A Jupyter Widget"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calling VACore\n",
      "Calling BinauralFreeField:MyBinauralFreeField\n",
      "Module returned an exception, could not get help\n"
     ]
    }
   ],
   "source": [
    "allmods = va.enumerate_modules()\n",
    "\n",
    "modnames = list()\n",
    "for mod in allmods[:] :\n",
    "    modnames.append( mod[\"name\"] )\n",
    "mods_dropdown_menu = widgets.Dropdown( options=modnames )\n",
    "\n",
    "mod_call_button = widgets.Button( description = 'Call' )\n",
    "mod_widget_box = widgets.HBox( [ mods_dropdown_menu, mod_call_button ] )\n",
    "\n",
    "def on_mod_call( b ) :\n",
    "    mod = allmods[ mods_dropdown_menu.index ]\n",
    "    print( 'Calling ' + mod[\"name\"] )\n",
    "    modnames.append( mod[\"name\"] )\n",
    "    try :\n",
    "        va.call_module( mod[\"name\"], { 'help': True } )\n",
    "    except :\n",
    "        print( 'Module returned an exception, could not get help' )\n",
    "        \n",
    "mod_call_button.on_click( on_mod_call )\n",
    "\n",
    "display( mod_widget_box )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Rendering modules\n",
    "Rendering modules are special modules and can also be called using the getter and setter methods `get_renderer_parameters` and `set_renderer_parameters`. They usually work like this: use the getter with an empty struct and receive every possible setting with its current value. Change the value or the returned struct and use the setter to modify the corresponding values in the renderinig module:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'enabled': 1, 'class': 'BinauralFreeField', 'description': ''}]\n"
     ]
    }
   ],
   "source": [
    "allrmods = va.get_rendering_modules()\n",
    "print( allrmods )\n",
    "# todo implement in VAPy"
   ]
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  }
 ],
 "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
}