function record_vocoder()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Task: To apply a vocoder on a recorded signal.
%
% Inputs: -
%
% Outputs: -
%	
%
% Author: Improved using 
%         vocoder.m
%         code by :         Guillaume Gibert  - guillaume.gibert@ecam.fr
%         
%         Modified by :     Charles Stelandre - charles.stelandre@ecam.fr
%
% Date: 04/05/2025
%
% Notes : This code records an audio and applies a vocoder on this
% recording. Also plots the temporal plots and spectrograms. Problems
% occured to use the record() functions. An alternative involved using the
% recorder object.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all
close all
clc

duration = 3; % Recording duration in seconds
sr = 22050; % Sample rate (Hz)
nb_bands = 16;
overlap = 0.1;
numChannels = 1; % Mono recording

disp('Press any key to start recording for 3 seconds...');
pause;

% Create an audiorecorder object
recorder = audiorecorder(sr, nb_bands, numChannels);

disp('Recording...');
record(recorder, duration); % Start recording for the specified duration
pause(duration + 0.5);     % Wait for the recording to finish (with a buffer)
disp('Recording finished!');

% Get the recorded audio data
modulator = getaudiodata(recorder);
sound(modulator,sr)
pause(duration);
% Remove the first tenth of a second
%modulator(1:floor(sr/10)) = [];


% Load carrier signal
carfile = 'sound/white_periodic.wav';
[carrier, sr2] = audioread(carfile);
if sr~=sr2, disp('your sampling rates dont match'); end


% Apply channel vocoder
nb_channels = floor(sr/15);
disp('Computing channel vocoder...');
robotic_voice = chanvocoder(carrier, modulator, nb_channels, nb_bands, overlap);

% Display time domain plots
figure;
subplot(2,1,1);
plot(robotic_voice);
title('Vocoded Sound');
xlabel('Time (samples)');
ylabel('Amplitude');

subplot(2,1,2);
plot(modulator);
title('Recorded Sound (Modulator)');
xlabel('Time (samples)');
ylabel('Amplitude');

% Play the robotic voice
disp('Playing vocoder...');
sound(robotic_voice,sr)
%pause(duration*2);


% temporal plots
figure;
subplot(3,1,1);
plot(modulator);
title("modulator")
ylabel('Amplitude (a.u.)');
subplot(3,1,2);
plot(carrier);
title("carrier")
ylabel('Amplitude (a.u.)');
subplot(3,1,3);
plot(robotic_voice);
title("Robotic voice")
ylabel('Amplitude (a.u.)');
xlabel('Time (sample)');

% spectrogram plots
step_size = 5; % In ms
window_size = 30; %In ms
spectrogram(modulator, sr, step_size, window_size);
spectrogram(carrier, sr, step_size, window_size);
spectrogram(robotic_voice, sr, step_size, window_size);