我正在使用matlab GUI,在我的列表框中有wav声音。我想选择一个声音并在播放后绘制它。
答案 0 :(得分:0)
正如@Arpi所指出的那样,你的问题相当广泛,可以通过你尝试过的和不起作用的例子更清楚地表达出来。
话虽如此,我感觉很慷慨,所以这里有一些示例代码可以帮助您入门。有一个带按钮和轴的列表框。你基本上选择一个声音,然后按下按钮绘制信号并播放声音。我使用Matlab的demo handel.mat
声音文件。您可以使用自己的数据填充其余部分并查看其工作原理。例如,我将声音文件中的数据存储在单元格数组中,因为您的数据可能大小不同。
您可以将此代码复制到新的.m文件中并运行它。我对此进行了评论,但如果您有任何疑问,请询问。
function PlaySound_GUI
clc
clear
close all
%// Create GUI components
hfigure = figure('Position',[100 100 500 500],'Units','Pixels');
handles.axes1 = axes('Units','Pixels','Position',[60,90,400,300]);
handles.SoundString = {'Handel' ;'Sound 1'; 'Sound 2'};
handles.LB = uicontrol('Style','Listbox','Position',[60 420 100 60],'String',handles.SoundString,'Value',1,'Callback',@(s,e) ListBoxCallback);
handles.PlayButton = uicontrol('Style','Push','Position',[200 420 60 30],'String','Play sound','Value',1,'BackgroundColor',[.9 .9 .9],'Callback',@(s,e) PlayCallback);
%// Load sounds and place their data in a cell array. Seems complicated but
%// you can simplify by storing everything into the handles structure.
handles.SoundCellArray = cell(3,2);
HandelSound = load('handel.mat');
handles.SoundCellArray{1,1} = HandelSound.y;
handles.SoundCellArray{1,2} = HandelSound.Fs;
guidata(hfigure,handles);
%// Listbox callback. Check what sound is selected
function ListBoxCallback()
handles = guidata(hfigure);
handles.SoundSelected = handles.SoundString{get(handles.LB,'value')};
guidata(hfigure,handles);
end
function PlayCallback()
handles = guidata(hfigure);
%// Plot and play selected sound.
switch handles.SoundSelected
case 'Handel'
N = length(handles.SoundCellArray{1,1});
t = linspace(0, N/handles.SoundCellArray{1,2}, N);
plot(t,handles.SoundCellArray{1,1})
%// Play sound
sound(handles.SoundCellArray{1,1},handles.SoundCellArray{1,2});
case 'Sound 1'
%// For the demo there is nothing here...
case 'Sound 2'
%// For the demo there is nothing here...
end
guidata(hfigure,handles);
end
end
输出截图:
与其他人玩得开心!