VA_example_outdoor_acoustics.m 3.39 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
%% 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.


%% Connect and set up simple scene
va = VA( 'localhost' );

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

L = va.create_sound_receiver( 'VA_Listener' );
receiver_pos = [ 2 1.7 0 ]; % OpenGL coordinates
va.set_sound_receiver_position( L, receiver_pos )
H = va.create_directivity_from_file( '$(DefaultHRIR)' );
va.set_sound_receiver_directivity( L, H );

S = va.create_sound_source( 'VA_Source' );
X = va.create_signal_source_buffer_from_file( '$(DemoSound)' );
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

%% 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' ] )

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

h = waitbar( 0, 'Hold on, running auralization' );
for n = 1:length( x )
    
    % Modify scene as you please (position has no real effect for prototype generic path renderer)
    source_pos = [ x( n ) 1.1 -3 ]; % OpenGL coordinates
    distance = sum( abs( source_pos - receiver_pos ) );
    va.set_sound_source_position( S, source_pos );    
    
    
    % Manually create direct sound path and diffracted sound path
    
    prop_path_direct = struct();
    prop_path_direct.distance = distance;
    prop_path_direct.delay = distance / c;
    prop_path_direct.frequencies = f;
    if ita_diffraction_shadow_zone( w, source_pos, receiver_pos )
        prop_path.values = zeros( 1, numel( f ) );
    else
        prop_path.values = ones( 1, numel( f ) );
    end
    
    apex = w.get_aperture_point( source_pos, receiver_pos );
    detour = norm( source_pos - apex ) + norm(apex - receiver_pos );
    
    prop_path_diffracted = struct();
    prop_path_diffracted.distance = detour;
    prop_path_diffracted.delay = detour / c;
    prop_path_diffracted.frequencies = f;
    prop_path.values = ita_diffraction_utd( w, source_pos, receiver_pos, f, c );
    
    % @todo at some point: load all paths from pre-calculated simulation -> ITAPropagationPathSim (C++) output
    
    
    % Update wave fronts in renderer
    path_update = struct();
    path_update.source = S;
    path_update.receiver = L;
    path_update.prop_path_1 = prop_path_direct;
    path_update.prop_path_2 = prop_path_diffracted;
    va.set_rendering_module_parameters( 'MyBinauralOutdoorNoise', path_update );
    
    % 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 )
    
end
close( h )

va.disconnect

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