This requires some thought and might just take some time to write down before everything is working.
Personally, I'd split the task up into multiple functions which in my mind would involve:
1. Semitone to frequency
2. Time to sample number
3. Frequency and sample number to output waveform
And if possible you should add another array which involves level for further progress later on/ helps simplify the problem into stages.
As im not sure which semitone scaling you are using you will have to confirm this for yourself but you will need some sort of converter/look up table that does something like (formulas according to Physics of Music website):
function [frequency] = semitone2frequency(semitone)
%Your input is how many semitones ABOVE middle C your note is
frequency = 440*((2^(1/12))^semitone); % where 440 Hz is middle C
end
This is based on an equal tempered scale. Now you know the frequency of your notes you could generate their sound, but wait theres more....
Now you can calculate the time of each sound in samples by writing another function...
function [nSamples] = time2samples(time, Fs)
dt = 1/Fs;
nSamples = time*dt;
end
Now that you have both of these values you can generate your audio signal!
frequency = semitone2frequency(55); %55 semitones above middle C
nSamples = time2samples(2,16000); %How many samples does it need to play 2 seconds
%Generate time array from 0 to how long you want the sound to be
time = 0:1:nSamples; %Starts at 0 increases by 1 sample each time up until 2 seconds worth of samples
%Create your output waveform!
outputAudio = sin(2*pi*frequency.*time);
This will create an array which you could play that will sound a note 55 semitones greater than middle C (not sure what note that actually is) for 2 seconds. Listen to it by using:
sound(outputAudio, 16000);
You can build on this to create multiple sounds one after the other (I would recommend creating one master function that lets you pass all arrays in and outputs one audio waveform), but this should be enough to create certain semitones for given durations!
P.s. To make the level 0 at any time simply multiply your outputAudio variable by 0
outputAudio = outputAudio.*0; %The . in .* is important!
Or for further control multiply it by any level between 0 and 1 for complete volume control.
I hope this is enough! good luck.