Commit 49dbbd54 authored by ramoa's avatar ramoa
Browse files

Adding MASA SRIR examples. Includes SRIR analysis, SRIR normalization and SRIR...

Adding MASA SRIR examples. Includes SRIR analysis, SRIR normalization and SRIR MASA (FoA + Ext) usage examples.
parent d1205641
Loading
Loading
Loading
Loading
+132 −0
Original line number Diff line number Diff line
% Example script on analysis and normalization of SRIR (Spatial Room
% Impulse Responses) from recordings.
% This script also shows how IVAS MASA format SRIR can be created and used for listening test processing.
%
% In order to fully run the script you need also
% IVAS_rend         IVAS renderer 
% masaAnalyzer      IVAS MASA analyzer tool
%
% Windows binaries are included, but for Linux/Mac you need to add your own
%

%
% Author: Anssi Rämö (Nokia Techonologies)
% Date: 10.4.2023
%
%
%

clear
fs=48000;         % Sampling frequency
ms = fs/1000;     % millisecond in samples
DC=1;             % Preprocessing (DC-Block filtering may be useful, if lot of low frequency brum and recorded without high-pass filter)
PeakPos=2;        % in ms (SRIR peak position, for the closest channel)
IR_Len=200;       % in ms
FadeInOut=1;      % Post-processing of SRIR
FadeInLength=24;  % in samples. Sinusoidal FadeIn -length
FadeOutLength=60; % in samples. Cosine FadeOut -length


% Test signal that has been used for recordings
target=audioread('ExampleInput\TargetMLS.wav');target=target(:);

% The recording(s) made in a room using the test signal
AmbeoFoA=audioread('ExampleInput\SennheiserAmbeo_FoA_Pos3.wav');
ExtM5=audioread('ExampleInput\RodeExtM5_Cardioid_Pos3.wav');

% IVAS MASA format may utilise SRIR responses of form FoA + Ext transport (mono/stereo)
% FoA signal is created by VST-plugin while recording. If processing delay is not
% compensated during recording it has to be conmpensated in analysis

% We have found out following delays for different ambisonic microphones
% These depend on the DAW used and their settings.
RodeNTSF1_DelayComp=1312+1+1024;      % Delay compensation for Rode NT-SF1 VST plugin FoA analysis
SennheiserAmbeo_DelayComp=1312+1+128; % Delay compensation for Sennheiser Ambeo VST plugin FoA analysis
VoyageAudio_DelayComp=1312+1+512; %   % Delay compensation for Voyage Audio plugin HoA2 analysis

% Aling FoA + Ext audio signals for SRIR calculation
measurement=[AmbeoFoA(SennheiserAmbeo_DelayComp+1:end,:) ExtM5(1:end-SennheiserAmbeo_DelayComp,:)];

% Pre-process (DC-block, highpass filtering 30Hz) recorded signal, if needed
if (DC)
    measurement=DCBlock(measurement);
end

% Do the SRIR analysis
chn=min(size(measurement)); % Number of channels to analyze
len = length(target);
for i = 1:chn
    Y=fft(measurement(:,i),(length(measurement)+size(target,2)-1));
    H=fft(target,(length(measurement)+size(target,2)-1));
    G=Y./(H);
    SRIR_long(:,i)=ifft(G,'symmetric');
    [null,latencies(i)] = max(SRIR_long(1:fs,i)); % Peak has to be within first second, if not adjust your measurement signal range
end

latency = min(latencies); % Put the closest microphone signal to PeakPos
SRIR_short = SRIR_long(latency-PeakPos*ms+1:latency+(IR_Len-PeakPos)*ms,:);

% FadeIn, FadeOut
if (FadeInOut==1)
    inwin=sin(pi*(0:FadeInLength-1)/(2*FadeInLength-2)).^2;
    FadeIn=repmat(inwin',[1 chn]);
    SRIR_short(1:FadeInLength,:)=SRIR_short(1:FadeInLength,:).*FadeIn;
    outwin=cos(pi*(0:FadeOutLength-1)/(2*FadeOutLength-2)).^2;
    FadeOut=repmat(outwin',[1 chn]);
    SRIR_short(end-FadeOutLength+1:end,:)=SRIR_short(end-FadeOutLength+1:end,:).*FadeOut;
end
%plot(SRIR_short)


% Do SRIR normalization
% IVAS MASA input format FoA + Ext parts are best to be normalized separately
% for the FoA/HoA part and transport signals.

% Test signal with short sequences of male & female speech and white & pink noise. All normalized to -26 dbOV
test26=audioread('ExampleInput\Test-26dbOV.wav');


Tmp_Talker=zeros([length(test26) chn]);
% Make initial SRIR scaling something reasonable
SRIR_short=0.2*SRIR_short/max(max(abs(SRIR_short))); 

% Filter test signal with SRIR vectors
for t=1:chn
    Tmp_Talker(:,t)=filter(SRIR_short(:,t),1,test26);
end

% Normalize FoA part based on the binaural rendered signal.
% Render FoA to binaural with IVAS_rend-tool
audiowrite('FoA.wav',Tmp_Talker(:,1:4),48000,'BitsPerSample',16);
eval('!IVAS_rend.exe -i FoA.wav -if FOA -o BIN.wav -of BINAURAL');
bin=audioread('BIN.wav');
% Calculate BS.1770 loudness and scale to target level (-26 dBOv)
[level gain]=bs1770(bin,-26) 
SRIR_short(:,1:4)=gain*SRIR_short(:,1:4);

% Normalize transports, directly from the Ext-transport stereo signal
[leveltr gaintr]=bs1770(Tmp_Talker(:,5:6),-26)
SRIR_short(:,5:6)=gaintr*SRIR_short(:,5:6);

% Write normalized SRIR to a file
audiowrite('Nokia_LargeRoom_Pos3_FoA+Ext_6hcn.wav',SRIR_short,48000,'BitsPerSample',16);

% Render spatialized mono signal with stereo MASA FoA+Ext processing

if (1)
    SRIR=audioread('Nokia_LargeRoom_Pos3_FoA+Ext_6hcn.wav');
    speech=audioread('ExampleInput\FinnishMale_mono.wav');

    SRIR_speech=zeros([length(speech) 6]);
    for t=1:6
        SRIR_speech(:,t)=filter(SRIR(:,t),1,speech(:,1)); % Anssi
    end

    audiowrite('FoAExt.wav',SRIR_speech,48000,'BitsPerSample',16);
    WavToPCM('FoAExt.wav');

    % Analyze FoA + stereo Ext signal with masaAnalyzer and create stereo.pcm and MASA metadata.
    !masaAnalyzer -stereo -1dir -foaext FoAExt.pcm stereo.pcm 1dir.met
    eval(['!IVAS_rend.exe -fs 48 -if MASA2 -im 1Dir.met -i stereo.pcm -of BINAURAL -o FinMale_Pos3_BIN.wav']);

end
 No newline at end of file
+13 −0
Original line number Diff line number Diff line
function y = DCBlock ( x )
% DC-block filtering. Default is 30Hz (-3 dB)
% a = 1-2^-11 = 0.99951171875  -3dB cut-off 3.8Hz, 20Hz atten. -0.004dB
% a = 1-2^-9  = 0.998046875    -3dB cut-off 15Hz, 20Hz atten. -1.9 dB
% a = 1-2^-8  = 0.9961         -3dB cut-off 30Hz, 20Hz atten. -5.1 dB
% a = 1-2^-7  = 0.9922         -3dB cut-off 60Hz, 20Hz atten. -10 dB
% a = 1-2^-6  ~= 0.985         -3dB cut-off 120Hz, 20Hz atten. -15? dB (ITU-T STL p50mnru default)

c = 1-2^-8;         % c - coefficient
b = [1 -1];	 	 % filter coefficient b
a = [1 -c];	 	 % filter coefficient a
y = filter (b,a,x);
end
 No newline at end of file
+131 B

File added.

No diff preview for this file type.

+132 B

File added.

No diff preview for this file type.

+132 B

File added.

No diff preview for this file type.

Loading