How do I plot the sound that is stored in the variable complete_sound = [ ];

function songmaker(hObject, handles)
global player;
notestr = get(handles.edit1, 'String');
note_struct = regexp(notestr, '(?<note>[ABCDEFG]#?)', 'names');
note_list = {note_struct.note};
complete_sound = [];
for note_idx = 1 : length(note_list)
note_selected = note_list{note_idx};
switch note_selected
case 'A'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.5];
F_A=440; %Frequency of note A is 440 Hz
A=(sin(2*pi*F_A*t));
complete_sound = [complete_sound,A];
case 'C'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.5];
F_C=261.626; %Frequency of note A is 440 Hz
C=sin(2*pi*F_C*t);
complete_sound = [complete_sound,C];
case 'G'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.5];
F_G=391.995; %Frequency of note A is 440 Hz
G=sin(2*pi*F_G*t);
complete_sound = [complete_sound,G];
case'F'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.5];
F_F=349.228; %Frequency of note A is 440 Hz
F=sin(2*pi*F_F*t);
complete_sound = [complete_sound,F];
case 'D'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.5];
F_D=293.665; %Frequency of note A is 440 Hz
D=sin(2*pi*F_D*t);
complete_sound = [complete_sound,D];
case 'E'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.5];
F_E=329.628; %Frequency of note A is 440 Hz
E=sin(2*pi*F_E*t);
complete_sound = [complete_sound,E];
end
end
player=audioplayer(complete_sound,Fs);

Answers (1)

t = (0:length(complete_sound)-1)/Fs;
plot(t, complete_sound);

7 Comments

Is it possible for the graph to keep changing if the note being played gets changed?
I don't understand any of it. I am new to Matlab(just a week of programming).
Is there a simpler way so that I can understand.
complete_sound=[];
Is there a way to save the sound that is stored in the above array to a .wav format?
audiowrite('FileNameGoesHere.wav', complete_sound.', Fs);
Note: this expects that complete_sound is still a row vector of data. It would also work if complete_sound has somehow become a column vector of data, or if it has become multiple rows of sound data, one row per channel. It would, however, fail if complete_sound has become multiple columns of data, one column per channel (which is the format that audioread of stereo would give you.)

Sign in to comment.

Tags

Asked:

MAX
on 15 Apr 2018

Commented:

MAX
on 16 Apr 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!