VA_example_outdoor_acoustics.m 5.61 KB
Newer Older
1 2 3 4 5 6
%% VA offline simulation/auralization example for an outdoor noise scenario

% Requires VA to run with a virtual audio device that can be triggered by
% the user. Also the generic path prototype rendering module(s) has to record the output
% to hard drive.

7
free_field_only = false;
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
8 9
direct_field_only = false; % with shadowing of edge
diffracted_field_only = false;
10
renderer_id = 'MyBinauralOutdoorNoise';
11

Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
12 13 14 15
assert( ~( free_field_only && diffracted_field_only ) )
assert( ~( free_field_only && direct_field_only ) )
assert( ~( diffracted_field_only && direct_field_only ) )

16 17
%% Connect and set up simple scene
va = VA( 'localhost' );
18 19 20
va.add_search_path( 'D:/Users/andrew/dev/ITASuite/VA/VACore/data' );
va.add_search_path( 'D:/Users/andrew/dev/ITASuite/VA/VAMatlab/matlab' );
va.add_search_path( 'C:/dev/VA/VACore/data' );
21
va.add_search_path( 'D:/Users/stienen/dev/VA/VACore/data' );
22
va.add_search_path( 'C:/ITASoftware/Raven/RavenDatabase/SoundDatabase' );
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
23
va.add_search_path( 'C:\Users\jonas\sciebo\ITA\Lehre\Masterarbeiten\2019 Henry Andrew\car_pass-by_corner' );
24

25 26 27 28 29

c = va.get_homogeneous_medium_sound_speed();
f = ita_ANSI_center_frequencies;

L = va.create_sound_receiver( 'VA_Listener' );
30
receiver_pos = [ 1 1.7 2 ]; % OpenGL coordinates
31
va.set_sound_receiver_position( L, receiver_pos )
32
%H = va.create_directivity_from_file( '$(DefaultHRIR)' );
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
33
H = va.create_directivity_from_file( 'ITA-Kunstkopf_HRIR_AP11_Pressure_Equalized_3x3_256.v17.ir.daff' );
34

35 36 37
va.set_sound_receiver_directivity( L, H );

S = va.create_sound_source( 'VA_Source' );
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
38

39
%X = va.create_signal_source_buffer_from_file( 'WelcomeToVA.wav' );
40 41
X = va.create_signal_source_buffer_from_file( 'LKW Leerlauf 2.wav' );
%X = va.create_signal_source_buffer_from_file( 'Full song - Vulfpeck - Dean Town.wav' );
42 43 44 45 46 47
va.set_signal_source_buffer_playback_action( X, 'play' )
va.set_signal_source_buffer_looping( X, true );
va.set_sound_source_signal_source( S, X )

% House corner
w = itaFiniteWedge( [ 1 0 0 ], [ 0 0 -1 ], [ 0 -2 0 ], 4 ); % OpenGL coordinates
48
   
49 50 51 52 53 54 55 56 57 58 59 60
%% Example for a synchronized scene update & audio processing simulation/auralization

timestep = 128 / 44100; % here: depends on block size and sample rate
manual_clock = 0;
va.set_core_clock( 0 );

spatialstep = 0.1;
disp( [ 'Resulting sound source speed: ' num2str( spatialstep / timestep ) ' m/s' ] )

numsteps = 3400;
disp( [ 'Simulation result duration: ' num2str( numsteps * timestep ) ' s' ] )

61
x = linspace( -1, 1, numsteps ) * 50; % motion from x = -50m to x = 50m
62

63 64
n0_of_paths = 2;

Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
65 66 67 68
D_log = [];
A_log = [];
gain_log = [];

69
h = waitbar( 0, 'Hold on, running auralization' );
70
direct_deleted = false; %set to true when the direct path has been deleted
71 72 73
for n = 1:length( x )
    
    source_pos = [ x( n ) 1.1 -3 ]; % OpenGL coordinates
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
74 75
    va.set_sound_source_position( S, source_pos );    
        
76
    
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
77
    %% Direct sound path
78 79
    
    % Manually create direct sound path and diffracted sound path
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
80 81 82 83 84
    shadow_zone = ita_diffraction_shadow_zone( w, source_pos, receiver_pos ); %is receiver in shadow zone?
    if n > 1 && last_shadow_zone ~= shadow_zone
        fprintf( 'Shadow zone has been crossed on frame %i\n', n ) 
    end
    last_shadow_zone = shadow_zone;
85
    
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
86
    distance = norm( source_pos - receiver_pos );
87 88
    
    prop_path_direct = struct();
89
    prop_path_direct.identifier = 'direct_path'; 
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
90
    prop_path_direct.gain = 1 / distance;
91 92 93 94 95 96 97 98 99
    prop_path_direct.delay = distance / c; %delay from sound emitting from source to being received at listener
    prop_path_direct.frequencies = f; %Frequencies corresponding to the mags
    values = ones(1,31);
    prop_path_direct.frequency_magnitudes = values;
    prop_path_direct.source = S; %sound source ID
    prop_path_direct.receiver = L; %sound receiver ID
    prop_path_direct.delete = false; %set to true when an existing path should be deleted
    prop_path_direct.position = source_pos;
    if free_field_only
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
100
        prop_path_direct.audible = true; % Free field: always audible
101
    else
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
102
        prop_path_direct.audible = ~shadow_zone; % Direct field: may be occluded
103
    end
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
104 105 106
    
    
    %% Diffracted sound path
107
    
108
    apex = w.get_aperture_point( source_pos, receiver_pos );
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
109 110 111
    detour = norm( source_pos - apex ) + norm( apex - receiver_pos );
    [ ~, D, A ] = ita_diffraction_utd( w, source_pos, receiver_pos, f, c );    
    rho = norm( apex - source_pos );
112
    
113
    prop_path_diffracted = struct();    
114 115
    prop_path_diffracted.source = S;
    prop_path_diffracted.receiver = L;
116
    prop_path_diffracted.identifier = 'diffracted_path';
117
    prop_path_diffracted.delete = false;
118
    prop_path_diffracted.position = apex;
119
    prop_path_diffracted.audible = true;
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
120 121 122 123
    prop_path_diffracted.delay = detour / c;
    prop_path_diffracted.frequencies = f;
    prop_path_diffracted.gain = A / rho;
    prop_path_diffracted.frequency_magnitudes = abs( D );
124 125
    
    
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
126
    %% Update wave fronts in renderer
127
    paths_update = struct();
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
128 129 130 131 132
    if ~diffracted_field_only
        paths_update.direct_path = prop_path_direct;
    end
    if ~free_field_only && ~direct_field_only
        paths_update.diffracted_path = prop_path_diffracted;
133 134
    end
    
135
    va.set_rendering_module_parameters( renderer_id, paths_update );
136 137 138 139 140 141 142 143 144
    
    % Increment core clock
    manual_clock = manual_clock + timestep;
    va.call_module( 'manualclock', struct( 'time', manual_clock ) );
    
    % Process audio chain by incrementing one block
    va.call_module( 'virtualaudiodevice', struct( 'trigger', true ) );
    
    waitbar( n / numsteps )
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
145 146 147 148
    
    D_log = [ D_log; D ];
    A_log = [ A_log; A ];
    gain_log = [ gain_log; prop_path_diffracted.gain ];
149
     
150 151 152 153 154 155
end
close( h )

va.disconnect

disp( 'Stop VA to export simulation results from rendering module(s)' )