%%The Script was designed by Samuel Selim based on the schroeder.m
%%reverberation function by Riitta Väänänen from the Helsinki University of
%%Technology, and the matpan.m script from DAFX:Digital Audio Effects, Udo
%%Zolzer. This script incorporates the use of reverberation and panning to
%%create a realistic sense of depth and presence to audio files in the
%%lateral axis (azimuth: sound moves from one ear to the other).
%This script is intended to be evaluated in two parts without the need to evaluate each line separately.
x=wavread('wavfile.wav'); %%x is the input wav file to be altered
delays=[2191 2971 3253  3307]; %%The amount of delay in the comb filters
rt60=0.5;       %%reverberation time (this can be higher or lower as desired, however this was my desired value for a realistic sound).
fs=44100;       %% The sample rate of the input file
revvv=schroeder(x,delays,rt60,fs);  %%revvv is the output
wavwrite(revvv, 'wavfilerevvv.wav'); %%this will create a new file with the output
%%
filename='wavfilerevvv.wav';  %% We will use the output from the first section of the script
[input,Fs,bits] = wavread(filename);
Fs=44100;            % The sampling rate at which the panned reverberant output will play. Make sure itstays the same as the reverberant sample rate in the first section of this script.
initial_angle = -45; % The starting position of the sound (right) and is measured in degrees
final_angle = 45;    % The limit of sound displacement (left)
segments = 40;       %% We break up the monoinput into segments. The more segments, the more gradual the panning effect, this is why I chose 40 as it sounded real. If you reduce the amount of segments, the sound doesn't travel past you but just appears in another ear without anticipation. This is not realistic panning.
angle_increment = (initial_angle - final_angle)/segments * pi / 180;
                %  in order to convert from degrees to radians, multiply by pi/180 (this is so matlab can read it)
lenseg = floor(length(input)/segments) - 1; %% this is the length of the segments of the input 
pointer = 1; %%this is the mobile unit that carries the sound
angle = initial_angle * pi / 180; % unit conversion (degrees to radians)
y=[[];[]]; 
for i=1:segments %%we use an indexing system in matlab to divide the segments equally in order to process them
 A =[cos(angle), sin(angle); -sin(angle), cos(angle)];  %%A is the rotation matrix. Right channel (cos?, sin? ) and left channel (-sin? , cos? )
 stereoinput = [input(pointer:pointer+lenseg)'; input(pointer:pointer+lenseg)']; %% this represents the monoinput going through two channels created in stereo
 y =[y, A * stereoinput]; 
 angle = angle + angle_increment; pointer = pointer + lenseg; %%the pointer is positioned based on the angle in the matrix column
end;
sound(y', Fs, bits);    %To hear the sound of the new output
wavwrite(y',Fs,bits,'wavfilerevvvpan.wav'); %%create new wav file with reverberant and panned qualities


