MATLAB Answers

Saving .wav file

4 views (last 30 days)
TAN KYE JYEN
TAN KYE JYEN on 9 May 2021
Commented: Rik on 10 May 2021
Hi everyone, recently I designed a song maker using GUIDE and was required to simple music notes into the song maker. I tried to save the notes into a .wav file but was having problem as the notes keep replacing existing notes, thus only one latest note was successfully saved. May I know how to save few notes into a single .wav file? Attached is the code I wrote for the note 'A' the audiowrite function was used. Thanks in advance :)
% --- Executes on button press in A1.
function A1_Callback(hObject, eventdata, handles)
Fs=8000;
Ts=1/Fs;
t=[0:Ts:3];
F_A=440;
A=sin(2*pi*F_A*t);
sound(A,Fs);
filename = ('C:\Users\Song.wav');
audiowrite(filename,A,Fs);
clear A Fs
% hObject handle to A1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

Accepted Answer

Rik
Rik on 9 May 2021
You're overwriting the song every time this callback is triggered. You should probably store each note to a field of your guidata struct to be able to write your sequence of notes to a wav file.
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
You should also consider moving most of this code to a separate function. The only thing specific to this note is the frequency, so you should probably use a separate function that accepts the frequency and the handles struct as inputs. That will also allow you to change the path to which the file is written more easily.
  2 Comments
Rik
Rik on 10 May 2021
Did my answer help you solve your problem? If so, please consider marking my answer as accepted answer.
What did you try? Do you understand how guidata works? Did you read the thread I linked?

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!