2.3) Develop a random music generator. You can use the following Matlab code to
ID: 3598427 • Letter: 2
Question
2.3) Develop a random music generator. You can use the following Matlab code to generate a tone (for example middle C). Fs-1000; % Samples per second toneFreq 261.63; % Tone frequency, in Hertz nSeconds-2; % Duration of the sound yl sin(linspace(0, nSeconds*toneFreq*2*pi, round(nSeconds*Fs))); sound(y, Fs); % Play sound at sampling rate Fs You should email me a code called music.m which produces a short (i.e. less than 10 seconds) string of random music. It should produce a different piece each time it runs. The music should be as melodicExplanation / Answer
% Copy this code and save it to file music.m
% Initialize the number of samples per second
Fs=1000;
% Initialize the tone frequency
toneFreq=261.63;
% Generate two random numbers to produce random Music
% Generate a random number between 1 and 10. 1,1 indicates that we need to
% generate 1 number only
randomNumber1=randi([1 10],1,1);
% Generate a random number between 10 and 20.
randomNumber2=randi([10 20],1,1);
% Set the duration of the music
nseconds=2;
% Use the random Numbers and create a new random music generator
% There are two random numbers and use of both helps to generate random
% music
% sin(randomNumber2) is multiplied with the slight modification of previously given music
y=sin(randomNumber1)*sin(randomNumber2*linspace(0,nseconds*toneFreq*2*pi, round(nseconds*Fs)));
% Play the music
sound(y,Fs)
I cannot attach the output file here because it is actually a sound and there is no visual output here. You can simply run this code in MATLAB and check it.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.