其他分享
首页 > 其他分享> > 【游戏】音乐时钟Matlab设计

【游戏】音乐时钟Matlab设计

作者:互联网

1 简介

本文结合"数字信号处理"课程和"Matlab程序设计"课程的相关知识,给出了基于Matlab的音乐播放器的总体设计方案,介绍了播放器主要模块的功能,设计与实现方法.我们将该设计作为电子信息类专业数字信号处理课程设计项目,有助于激发学生的学习兴趣,提高其综合设计和应用创新能力.

2 部分代码

function varargout = untitled(varargin)

% UNTITLED M-file for untitled.fig

%      UNTITLED, by itself, creates a new UNTITLED or raises the existing

%      singleton*.

%

%      H = UNTITLED returns the handle to a new UNTITLED or the handle to

%      the existing singleton*.

%

%      UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local

%      function named CALLBACK in UNTITLED.M with the given input arguments.

%

%      UNTITLED('Property','Value',...) creates a new UNTITLED or raises the

%      existing singleton*.  Starting from the left, property value pairs ar

%      applied to the GUI before untitled_OpeningFcn gets called.  An

%      unrecognized property name or invalid value makes property application

%      stop.  All inputs are passed to untitled_OpeningFcn via varargin.

%

%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one

%      instance to run (singleton)".

%

% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help untitled

% Last Modified by GUIDE v2.5 02-Jun-2021 00:03:55

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name',       mfilename, ...

                   'gui_Singleton',  gui_Singleton, ...

                   'gui_OpeningFcn', @untitled_OpeningFcn, ...

                   'gui_OutputFcn',  @untitled_OutputFcn, ...

                   'gui_LayoutFcn',  [] , ...

                   'gui_Callback',   []);

if nargin && ischar(varargin{1})

    gui_State.gui_Callback = str2func(varargin{1});

end

if nargout

    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});

else

    gui_mainfcn(gui_State, varargin{:});

end

% End initialization code - DO NOT EDIT

% --- Executes just before untitled is made visible.

function untitled_OpeningFcn(hObject, eventdata, handles, varargin)

% This function has no output args, see OutputFcn.

% hObject    handle to figure

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% varargin   command line arguments to untitled (see VARARGIN)

if ~isempty(timerfindall), stop(timerfindall);  delete(timerfindall);end

ed until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end

set(hObject,'Value',handles.clockSaveData(2));

handles.popupmenu2=hObject;

guidata(hObject,handles);

function edit_musicFile_Callback(hObject, eventdata, handles)

% hObject    handle to edit_musicFile (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_musicFile as text

%        str2double(get(hObject,'String')) returns contents of edit_musicFile as a double

handles.musicSaveData=get(hObject,'String');

musicSaveData=handles.musicSaveData;

save('musicSaveData.txt','-ascii','musicSaveData');

guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.

function edit_musicFile_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit_musicFile (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

    set(hObject,'BackgroundColor','white');

end

set(hObject,'String',handles.musicSaveData); 

handles.edit_musicFile=hObject;

guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.

function pushbutton1_CreateFcn(hObject, eventdata, handles)

% hObject    handle to pushbutton1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

handles.pushbutton1=hObject;

guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.

function radiobutton1_CreateFcn(hObject, eventdata, handles)

% hObject    handle to radiobutton1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

handles.radiobutton1=hObject;

guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.

function radiobutton2_CreateFcn(hObject, eventdata, handles)

% hObject    handle to radiobutton2 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

handles.radiobutton2=hObject;

guidata(hObject,handles);

% --- Executes when selected object is changed in uipanel4.

function uipanel4_SelectionChangeFcn(hObject, eventdata, handles)

% hObject    handle to the selected object in uipanel4 

% eventdata  structure with the following fields (see UIBUTTONGROUP)

% EventName: string 'SelectionChanged' (read only)

% OldValue: handle of the previously selected object or empty if none was selected

% NewValue: handle of the currently selected object

% handles    structure with handles and user data (see GUIDATA)

clockSaveData=handles.clockSaveData;

if isequal(get(hObject,'Tag'),get(gethandles.radiobutton1,'Tag'))

    clockSavaData(3)=1-get(hObject,'Value');

else

    clockSaveData(3)=get(hObject,'Value');

end

save('clockSaveData.txt','-ascii','clockSaveData');

% --- Executes during object creation, after setting all properties.

function figure1_CreateFcn(hObject, eventdata, handles)

% hObject    handle to figure1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

set(hObject,'name','音乐闹钟');

if exist('clockSaveData.txt','file')

    clockSaveData=load('clockSaveData.txt');

    if numel(clockSaveData)~=3

        clockSaveData=[13,1,0];

        save('clockSaveData.txt','-ascii','clockSaveData')

    end

else

    clockSaveData=[13,1,0];

    save('clockSaveData.txt','-ascii','clockSaveData')

end

if exist('musicSaveData.txt','file')

    musicSaveData=char(load('musicSaveData.txt'));    

else

    musicSaveData='设定音乐文件';

    save('musicSaveData.txt','-ascii','musicSaveData')

end

handles.clockSaveData=clockSaveData;

handles.musicSaveData=musicSaveData;

handles.figure1=hObject;

guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.

function uipanel3_CreateFcn(hObject, eventdata, handles)

% hObject    handle to uipanel3 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

handles.uipanel3=hObject;

guidata(hObject,handles);

function start_timer(handles)

    contents=cellstr(get(handles.popupmenu1,'String'));

    hour=contents{get(handles.popupmenu1,'Value')};

    hour=str2double(hour);

    contents=cellstr(get(handles.popupmenu2,'String'));

    minute=contents{get(handles.popupmenu2,'Value')};

    minute=str2double(minute);

    startTime=datevec(now);%时间矢量为【年,月,日,时,分,秒】

    if hour>startTime(4)

        startTime(4:6)=[hour,minute,0];

    elseif hour==startTime(4)

        if minute>startTime(5)

            startTime(5:6)=[minute,0];

        else

            startTime(3:6)=[startTime(3)+1,hour,minute,0];

        end

    else

         startTime(3:6)=[startTime(3)+1,hour,minute,0];

    end

stop(handles.timer);

startat(handles.timer,startTime);

function start_timer_solid_hour(handles)

    startTime=datevec(now);

    startTime(4:6)=[startTime(4)+1,0,0];

    stop(handles.timer);

    startat(handles.timer,startTime);

function timer_action(timerObj,evendata,handles)

if (get(handles.radiobutton1,'Value')==1 && get(handles.checkbox1,'Value')==1)

% --- Executes on button press in checkbox1.

function checkbox1_Callback(hObject, eventdata, handles)

handles.checkbox1=hObject;

guidata(hObject,handles);

if (get(handles.checkbox1,'Value')==1)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    start_timer_solid_hour(handles);

else

    start_timer(handles);

end

    

% --- Executes during object creation, after setting all properties.

function checkbox1_CreateFcn(hObject, eventdata, handles)

% hObject    handle to checkbox1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

handles.checkbox1=hObject;

guidata(hObject,handles);

% --- Executes on button press in pushbutton3.

function pushbutton3_Callback(hObject, eventdata, handles)

% hObject    handle to pushbutton3 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

[filename,pathname]=uigetfile(...

    {'*.jpg;*.bmp;*.tif','可选择';},'选择背景图片','MultiSelect','off');

%handles.musicSaveData=fullfile(pathname,filename);

selected_figure=fullfile(pathname,filename);

ha=axes('units','normalized','position',[0 0 1 1]); 

uistack(ha,'down') 

II=imread(selected_figure); 

image(II) 

colormap gray 

set(ha,'handlevisibility','off','visible','off'); 

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@

%set(handles.edit_musicFile,'String',handles.musicSaveData);

%musicSaveData=handles.musicSaveData;

%save('musicSaveData.txt','-ascii','musicSaveData');

handles.pushbutton3=hObject;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.

function pushbutton3_CreateFcn(hObject, eventdata, handles)

% hObject    handle to pushbutton3 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

handles.pushbutton3=hObject;

guidata(hObject,handles);

3 仿真结果

4 参考文献

[1]韩萍, 周航, 金昌旺. 基于Matlab的音乐播放器设计[J]. 电气电子教学学报, 2012, 34(4):4.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。

标签:function,游戏,get,hObject,handles,Matlab,musicSaveData,eventdata,时钟
来源: https://blog.csdn.net/qq_59747472/article/details/122788657