Examples

These examples are use cases of the Binaural simulator. They apply the configuration directly in Matlab. For larger setups it might be easier to use the Configuration using XML Scene Description, which will also be used in the overall Examples where the complete model is demonstrated. If you want to execute or modify them have a look into the examples/ folder of the Binaural simulator.

Simulate two dry sources

In this example, two dry sources will be simulated and the binaural output is written to the file out_two_sources.wav. One source is a cello placed at the front left of the listener and the other source is castanets placed at the front right of the listener. If you want to test it run the two_sources.m script.

 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
sim = simulator.SimulatorConvexRoom();
set(sim, ...
    'HRIRDataset', simulator.DirectionalIR( ...
        'impulse_responses/qu_kemar_anechoic/QU_KEMAR_anechoic_3m.sofa'), ...
    'Sources', {simulator.source.Point(), simulator.source.Point()}, ...
    'Sinks',   simulator.AudioSink(2) ...
    );
set(sim.Sources{1}, ...
    'Name', 'Cello', ...
    'Position', [1; 2; 0], ...
    'AudioBuffer', simulator.buffer.FIFO(1) ...
    );
set(sim.Sources{1}.AudioBuffer, ...
    'File', 'stimuli/anechoic/instruments/anechoic_cello.wav' ...
    );
set(sim.Sources{2}, ...
    'Name', 'Castanets', ...
    'Position', [1; -2; 0], ...
    'AudioBuffer', simulator.buffer.FIFO(1) ...
    );
set(sim.Sources{2}.AudioBuffer, ...
    'File', 'stimuli/anechoic/instruments/anechoic_castanets.wav' ...
    );
set(sim.Sinks, ...
    'Name', 'Head', ...
    'UnitX', [1; 0; 0], ...
    'Position', [0; 0; 0] ...
    );
sim.set('Init',true);
while ~sim.isFinished()
    sim.set('Refresh',true);  % refresh all objects
    sim.set('Process',true);
end
data = sim.Sinks.getData();
sim.Sinks.saveFile('out_two_sources.wav',sim.SampleRate);
sim.set('ShutDown',true);

Simulate a moving source

This example simulates a dry cello that moves from the left to the right of the listener and stores the resulting binaural signal as out_moving_source.wav. If you want to test it run the moving_source.m script.

 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
sim = simulator.SimulatorConvexRoom();
set(sim, ...
    'HRIRDataset', simulator.DirectionalIR( ...
        'impulse_responses/qu_kemar_anechoic/QU_KEMAR_anechoic_3m.sofa'), ...
    'Sources', {simulator.source.Point()}, ...
    'Sinks',   simulator.AudioSink(2) ...
    );
set(sim.Sources{1}, ...
    'Name', 'Cello', ...
    'Position', [1; 2; 0], ...
    'AudioBuffer', simulator.buffer.FIFO(1) ...
    );
set(sim.Sources{1}.AudioBuffer, ...
    'File', 'stimuli/anechoic/instruments/anechoic_cello.wav' ...
    );
sim.set('Init',true);
sim.Sources{1}.setDynamic( ...
    'Position', 'Velocity', 0.25); % move source with 0.25 m/s
set(sim.Sources{1}, ...
    'Position', [1; -2; 0] ... %final position
    );
while ~sim.isFinished()
    sim.set('Refresh',true);  % refresh all objects
    sim.set('Process',true);
end
data = sim.Sinks.getData();
sim.Sinks.saveFile('out_moving_source.wav',sim.SampleRate);
sim.set('ShutDown',true);

Simulate rooms using the Image Source Model

This example simulates a dry cello in a shoe box room and stores the resulting binaural signal in out_room.wav. The shoe box room is simulated with an image source model. If you want to test it run the room.m script.

 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
set(sim, ...
    'MaximumDelay', 0.5, ...
    'PreDelay', 0.0, ...
    'HRIRDataset', simulator.DirectionalIR( ...
        'impulse_responses/qu_kemar_anechoic/QU_KEMAR_anechoic_3m.sofa'), ...
    'Sources', {simulator.source.ISMGroup}, ...
    'Sinks', simulator.AudioSink(2), ...
    'Room', simulator.room.Shoebox ...
    );
set(sim.Sources{1}, ...
    'Name', 'Cello', ...
    'Position', [2.5; 2.5; 0], ...
    'AudioBuffer', simulator.buffer.FIFO(1), ...
    'Room', sim.Room ...
    );
set(sim.Sources{1}.AudioBuffer, ...
    'File', 'stimuli/anechoic/instruments/anechoic_cello.wav' ...
    );
% define the room
set(sim.Room, ...
    'Name', 'Room', ...
    'Position', [-3; -3; -1.75], ...
    'UnitX', [1; 0; 0], ...
    'UnitZ', [0; 0; 1], ...
    'LengthX', 6.0, ...
    'LengthY', 6.0, ...
    'LengthZ', 2.7, ...
    'ReverberationMaxOrder', 8, ...
    'RT60', 1.0 ...
    );
sim.set('Init',true);
while ~sim.isFinished()
    sim.set('Refresh',true);  % refresh all objects
    sim.set('Process',true);
end
data = sim.Sinks.getData();
sim.Sinks.saveFile('out_room.wav',sim.SampleRate);  % save file
sim.set('ShutDown',true);

Simulate rooms using Binaural Room Impulse Responses

This example simulates a cello in a reverberant environment using BRIR recordings. The resulting binaural signal as stored in out_brirs.wav. If you want to test it run the brirs.m script.

 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
sim = simulator.SimulatorConvexRoom();
set(sim, ...
    'Renderer', @ssr_brs, ...
    'Sources', {simulator.source.Point()}, ...
    'Sinks', simulator.AudioSink(2) ...
    );
set(sim.Sources{1}, ...
    'Name', 'Cello', ...
    'IRDataset', simulator.DirectionalIR( ...
      ['impulse_responses/qu_kemar_rooms/auditorium3/', ...
       'QU_KEMAR_Auditorium3_src3_xs+2.20_ys-1.94.sofa']), ...
    'AudioBuffer', simulator.buffer.FIFO(1) ...
    );
set(sim.Sources{1}.AudioBuffer, ...
    'File', 'stimuli/anechoic/instruments/anechoic_cello.wav' ...
    );
sim.set('Init',true);
%% static scene, dynamic head
% head should rotate about 170 degree to the right with 20 degrees per second
sim.Sinks.setDynamic('UnitX', 'Velocity', 20);
sim.Sinks.set('UnitX', [cosd(85); sind(85); 0]);
while ~sim.isFinished()
  sim.set('Refresh',true);  % refresh all objects
  sim.set('Process',true);
end
sim.Sinks.saveFile('out_brirs.wav',sim.SampleRate);  % save file
sim.set('ShutDown',true);