Skip to content
Snippets Groups Projects
Commit 5aeff7ed authored by Dipl.-Ing. Jonas Stienen's avatar Dipl.-Ing. Jonas Stienen
Browse files

Updates in sample scripts

parent 3233e7fa
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# VA simple acoustic scene
This is a simple example how to create a simple acoustic scene in VA using Python.
# VA core controller
This is a simple example how to control global VA core functionality
%% Cell type:markdown id: tags:
### Prerequisites
You can ignore this part, it is for preparation purposes only.
%% Cell type:code id: tags:
``` python
import sys
sys.path.append( '../../Lib/site-packages' )
```
%% Cell type:code id: tags:
``` python
import ipywidgets as widgets
```
%% Cell type:code id: tags:
``` python
import va
if not va.connect() :
raise 'Could not connect to local VA server'
```
%% Cell type:markdown id: tags:
### Connect
%% Cell type:code id: tags:
``` python
va.connect();
```
## Control
%% Cell type:markdown id: tags:
## Control
### Output
%% Cell type:markdown id: tags:
#### Mute / unmute
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 id: tags:
``` python
mute_output_button = widgets.ToggleButton( description = 'Output muted', value = va.is_output_muted() )
def on_mute_button_clicked( b ) :
if b.name == 'value' :
va.set_output_muted( b.new ) # True if toggle button appears 'active'
mute_output_button.observe( on_mute_button_clicked )
display( mute_output_button )
```
%% Output
%% Cell type:markdown id: tags:
### Gain
#### Gain
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 id: tags:
``` python
output_gain_slider = widgets.FloatSlider(
value = va.get_output_gain(),
description = 'Output gain:',
min = 0.0,
max = 1.0,
step = 0.1,
readout = True,
readout_format = '.1f' )
def on_output_gain_changed( s ) :
if s.name == 'value' :
va.set_output_gain( s.new )
output_gain_slider.observe( on_output_gain_changed )
display( output_gain_slider )
```
%% Output
%% Cell type:markdown id: tags:
#### Global auralization mode
%% Cell type:code id: tags:
``` python
global_am = va.get_global_auralization_mode()
va.get_auralization_mode_str()
```
%% Cell type:markdown id: tags:
### Macros
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.
%% Cell type:code id: tags:
``` python
print( '$(DefaultHRIR): ' + va.substitute_macro( '$(DefaultHRIR)' ) )
print( '$(ProjectName): ' + va.substitute_macro( '$(ProjectName)' ) )
print( '$(data): ' + va.substitute_macro( '$(data)' ) )
print( '$(big_data_dir): ' + va.substitute_macro( '$(big_data_dir)' ) )
print( '$(conf): ' + va.substitute_macro( '$(conf)' ) )
```
%% Output
$(DefaultHRIR): HRIR\\ITA-Kunstkopf_HRIR_AP11_Pressure_Equalized_3x3_256.v17.ir.daff
$(ProjectName): MyVirtualAcousticsProject
$(data): $(data)
$(big_data_dir): C:\\data
$(conf): $(conf)
%% Cell type:markdown id: tags:
### Reset
Use `reset` to reset the entire scene.
%% Cell type:code id: tags:
``` python
reset_button = widgets.Button( description = 'Reset VA server' )
def on_reset_button_clicked( b ) :
va.reset()
reset_button.on_click( on_reset_button_clicked )
display( reset_button )
```
%% Output
%% Cell type:markdown id: tags:
## Modules
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.
You can interact with any registered VA module 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 receive information in return. It is used to change any setting in a registered module. This way, no new interface methods for prototyping have to be added during development and testing.
%% Cell type:code id: tags:
``` python
allmods = va.enumerate_modules()
modnames = list()
for mod in allmods[:] :
modnames.append( mod["name"] )
mods_dropdown_menu = widgets.Dropdown( options=modnames )
mod_call_button = widgets.Button( description = 'Call' )
mod_widget_box = widgets.HBox( [ mods_dropdown_menu, mod_call_button ] )
def on_mod_call( b ) :
mod = allmods[ mods_dropdown_menu.index ]
print( 'Calling ' + mod["name"] )
modnames.append( mod["name"] )
try :
va.call_module( mod["name"], { 'help': True } )
except :
print( 'Module returned an exception, could not get help' )
mod_call_button.on_click( on_mod_call )
display( mod_widget_box )
```
%% Output
Calling VACore
Calling BinauralFreeField:MyBinauralFreeField
Module returned an exception, could not get help
%% Cell type:markdown id: tags:
### Rendering modules
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:
Rendering modules are special modules that can also be listed using `get_rendering_modules`. They are called through the module interface just as described above, however the module name has to be assembled by the type of renderer followed by a semicolon `:` and the name of the rendering module.
%% Cell type:code id: tags:
``` python
allrmods = va.get_rendering_modules()
print( allrmods )
for rmod in allrmods :
print( rmod )
rmod_id = str( rmod.class + ':' + rmod.name )
try :
magic_return = va.call_module( rmod_id, { 'help': True } )
print( magic_return )
except:
print( 'Module call failed, could not get help' )
# todo implement in VAPy
```
%% Output
[{'enabled': 1, 'class': 'BinauralFreeField', 'description': ''}]
......
%% Cell type:markdown id: tags:
# VA simple acoustic scene
This is a simple example how to create a simple acoustic scene in VA using Python.
%% Cell type:markdown id: tags:
#### Prerequisites
You can ignore this part, it is for preparation purposes only.
%% Cell type:code id: tags:
``` python
import sys
sys.path.append( '../../Lib/site-packages' )
import os
print( 'Current working directory:', os.getcwd() )
import va
print( 'Successfully loaded VA Python extension')
```
%% Output
Current working directory: C:\dev\VA\dist\win32-x64.vc14\python\examples\jupyter
Successfully loaded VA Python extension
%% Cell type:markdown id: tags:
### Connect
%% Cell type:code id: tags:
``` python
va.connect( "localhost" );
if not va.connect( 'localhost' )
raise 'Could not connect to server on localhost, not running?'
```
%% Output
File "<ipython-input-28-20070eb91885>", line 1
if not va.connect( 'localhost' )
^
SyntaxError: invalid syntax
%% Cell type:markdown id: tags:
Reset VA to clear the scene
%% Cell type:code id: tags:
``` python
va.reset()
```
%% Cell type:markdown id: tags:
Control output gain
%% Cell type:code id: tags:
``` python
va.set_output_gain( 0.25 )
```
%% Cell type:markdown id: tags:
Add the current working directory or any other relative or absolute directory as a search path to your VA application. From now on, only use relative paths or macros to paths.
> Pathes are relevant on server side, not on a remote client. The files must be available on the computer **where the VA application is running**!
%% Cell type:code id: tags:
``` python
va.add_search_path( os.getcwd() );
```
%% Cell type:markdown id: tags:
### Signal source
Create a signal source from a file and start playback with looping mode
%% Cell type:code id: tags:
``` python
signal_source_id = va.create_audio_file_signal_source( 'Audiofiles/Bauer.wav' )
va.set_audio_file_signal_source_playback_action_str( signal_source_id, 'play' )
va.set_audio_file_signal_source_playback_is_looping( signal_source_id, True )
```
%% Cell type:markdown id: tags:
### Virtual sound source
Create a virtual sound source with any name and set a position
%% Cell type:code id: tags:
``` python
sound_source_id = va.create_sound_source( 'PySoundSource' )
va.set_sound_source_position( sound_source_id, ( 1.5, 1.7, -1.1 ) )
```
%% Cell type:markdown id: tags:
### Connect signal and source
Connect the signal source to the virtual sound source
%% Cell type:code id: tags:
``` python
va.set_sound_source_signal_source( sound_source_id, signal_source_id )
```
%% Cell type:markdown id: tags:
### Head-related transfer function / Head-related impulse response
Load an HRIR (time domain representation of an HRTF). See [OpenDAFF](http://www.opendaff.org) for more information.
> We use a macro `DefaultHRIR` here, that is usually available for a VA core.
%% Cell type:code id: tags:
``` python
hrir_id = va.load_hrir( '$(DefaultHRIR)' )
```
%% Cell type:markdown id: tags:
### Virtual listener
Create a listener with arbitrary name, assign the HRTF/HRIR and set a position.
%% Cell type:code id: tags:
``` python
listener_id = va.create_listener( 'PyListener' )
va.set_listener_position( listener_id, ( 0, 1.7, 0 ) )
va.set_listener_orientation_vu( listener_id, ( 0, 0, -1, ), ( 0, 1, 0 ) ) # Default view is to -Z (OpenGL)
va.set_listener_hrir( listener_id, hrir_id )
```
%% Output
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-25-f668a5e697e7> in <module>()
----> 1 listener_id = va.create_listener( 'PyListener' )
2 va.set_listener_position( listener_id, ( 0, 1.7, 0 ) )
3 va.set_listener_orientation_vu( listener_id, ( 0, 0, -1, ), ( 0, 1, 0 ) ) # Default view is to -Z (OpenGL)
4 va.set_listener_hrir( listener_id, hrir_id )
Exception: Modal error: Not connected. (error code 3)
%% Cell type:markdown id: tags:
### Active listener
Set an active listener. This concept is deprecated, but should be used for compatibility until it is removed.
%% Cell type:code id: tags:
``` python
va.set_active_listener( listener_id )
```
%% Cell type:markdown id: tags:
### Disconnect
%% Cell type:code id: tags:
``` python
va.disconnect();
```
......
# Add va module if it was not installed
import sys
sys.path.append( '../Lib/site-packages' )
sys.path.append( '../dist/Lib/site-packages' )
import os
current_exec_dir = os.getcwd()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment