Saving .wav file
조회 수: 6 (최근 30일)
이전 댓글 표시
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)
댓글 수: 0
채택된 답변
Rik
2021년 5월 9일
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
Rik
2021년 5월 10일
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?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!