保存录制的音频

时间:2016-11-09 21:47:07

标签: matlab matlab-guide

我试图保存我录制的声音。当我按下'保存'按钮录制的音频应保存为.wav文件。但这不会发生。这是我到目前为止的代码......

录制按钮......

function recordbutton_Callback(hObject, eventdata, handles)
% hObject    handle to recordbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
rec = audiorecorder
disp('Recording for 5 Seconds...')
recordblocking (rec,5); %5 Seconds
disp('Finished Recording.')

global myrecording
myrecording = getaudiodata(rec);

axes(handles.axes3);
plot(myrecording)

保存按钮...

% --- Executes on button press in saverecording.
function saverecording_Callback(hObject, eventdata, handles)
global myrecording
audiowrite(myrecording, 'RecordedSound.wav');

1 个答案:

答案 0 :(得分:1)

看起来你有一个指南变量范围问题。 我认为最佳实践是使用guidata而不是尝试使用全局变量。 mathworks Q&A here

其次,您的audiowrite代码以另一种方式调用(至少在我的系统上) 从mathworks help here开始,audiowrite(filename,y,Fs)将文件名,声音和频率作为输入。

将所有这些放在一起,您的代码会有一些变化,但这将记录带有两个按钮的wav文件。

function recordbutton_Callback(hObject, eventdata, handles)
rec = audiorecorder
disp('Recording for 5 Seconds...')
recordblocking (rec,5); %5 Seconds
disp('Finished Recording.')
handles.myrecording = getaudiodata(rec);
guidata(hObject, handles); %writes the handles structure back to the guiframe.

您的保存功能应该像这样写

function Save_Callback(hObject, eventdata, handles)
% hObject    handle to Save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.myrecording % check to make sure handles.myrecording actually has data
audiowrite('RecordedSound.wav',handles.myrecording, 44100);

除非你想删除录音以释放内存,否则无需在保存中写回guidata。