目录

1.软件版本

2.理论知识

3.核心代码

4.操作步骤与仿真结论

5.参考文献

6.完整源码获得方式


1.软件版本

matlab2013b

2.理论知识

这个部分,不涉及什么原理,主要是对MIDI音频信号需要了解其主要的文件结构特点,MIDI文件的主要结构如下:

http://wenku.baidu.com/view/298f6f758e9951e79b8927fa.html

3.核心代码

function midi = readmidi(filename, rawbytes)if (nargin<2)rawbytes=0;
endfid = fopen(filename);
[A count] = fread(fid,'uint8');
fclose(fid);if (rawbytes) midi.rawbytes_all = A; endif ~isequal(A(1:4)',[77 84 104 100])  % double('MThd')error('File does not begin with header ID (MThd)');
endheader_len = decode_int(A(5:8));
if (header_len == 6)
elseerror('Header length != 6 bytes.');
endformat = decode_int(A(9:10));
if (format==0 || format==1 || format==2)midi.format = format;
else    error('Format does not equal 0,1,or 2');
endnum_tracks = decode_int(A(11:12));
if (format==0 && num_tracks~=1)error('File is format 0, but num_tracks != 1');
endtime_unit = decode_int(A(13:14));
if (bitand(time_unit,2^15)==0)midi.ticks_per_quarter_note = time_unit;
elseerror('Header: SMPTE time format found - not currently supported');
endif (rawbytes)midi.rawbytes_header = A(1:14);
endctr = 15;
for i=1:num_tracksif ~isequal(A(ctr:ctr+3)',[77 84 114 107])  % double('MTrk')error(['Track ' num2str(i) ' does not begin with track ID=MTrk']);endctr = ctr+4;track_len = decode_int(A(ctr:ctr+3));ctr = ctr+4;% have track.rawbytes hold initial 8B also...track_rawbytes{i} = A((ctr-8):(ctr+track_len-1));if (rawbytes)midi.track(i).rawbytes_header = A(ctr-8:ctr-1);endctr = ctr+track_len;
endfor i=1:num_trackstrack = track_rawbytes{i};if (rawbytes); midi.track(i).rawbytes = track; endmsgCtr = 1;ctr=9;  % first 8B were MTrk and lengthwhile (ctr < length(track_rawbytes{i}))clear currMsg;currMsg.used_running_mode = 0;% note:%  .used_running_mode is necessary only to %  be able to reconstruct a file _exactly_ from %  the 'midi' structure.  this is helpful for %  debugging since write(read(filename)) can be %  tested for exact replication...%ctr_start_msg = ctr;[deltatime,ctr] = decode_var_length(track, ctr);% ?%if (rawbytes)%  currMsg.rawbytes_deltatime = track(ctr_start_msg:ctr-1);%end% deltaime must be 1-4 bytes long.% could check here...% CHECK FOR META EVENTS ------------------------% 'FF'if track(ctr)==255type = track(ctr+1);ctr = ctr+2;% get variable length 'length' field[len,ctr] = decode_var_length(track, ctr);% note: some meta events have pre-determined lengths...%  we could try verifiying they are correct here.thedata = track(ctr:ctr+len-1);chan = [];ctr = ctr + len;      midimeta = 0;else midimeta = 1;% MIDI EVENT ---------------------------% check for running mode:if (track(ctr)<128)% make it re-do last command:%ctr = ctr - 1;%track(ctr) = last_byte;currMsg.used_running_mode = 1;B = last_byte;nB = track(ctr); % ?elseB  = track(ctr);nB = track(ctr+1);ctr = ctr + 1;end% nibbles:%B  = track(ctr);%nB = track(ctr+1);Hn = bitshift(B,-4);Ln = bitand(B,15);chan = [];msg_type = midi_msg_type(B,nB);% DEBUG:if (i==2)if (msgCtr==1)disp(msg_type);endendswitch msg_typecase 'channel_mode'% UNSURE: if all channel mode messages have 2 data byes (?)type = bitshift(Hn,4) + (nB-120+1);thedata = track(ctr:ctr+1);chan = Ln;ctr = ctr + 2;% ---- channel voice messages:case 'channel_voice'type = bitshift(Hn,4);len = channel_voice_msg_len(type); % var length data:thedata = track(ctr:ctr+len-1);chan = Ln;% DEBUG:if (i==2)if (msgCtr==1)disp([999  Hn type])endendctr = ctr + len;case 'sysex'% UNSURE: do sysex events (F0-F7) have %  variable length 'length' field?[len,ctr] = decode_var_length(track, ctr);type = B;thedata = track(ctr:ctr+len-1);chan = [];ctr = ctr + len;case 'sys_realtime'% UNSURE: I think these are all just one bytetype = B;thedata = [];chan = [];endlast_byte = Ln + bitshift(Hn,4);end % end midi event 'if'currMsg.deltatime = deltatime;currMsg.midimeta = midimeta;currMsg.type = type;currMsg.data = thedata;currMsg.chan = chan;if (rawbytes)currMsg.rawbytes = track(ctr_start_msg:ctr-1);endmidi.track(i).messages(msgCtr) = currMsg;msgCtr = msgCtr + 1;end % end loop over rawbytes
end % end loop over tracksfunction val=decode_int(A)val = 0;
for i=1:length(A)val = val + bitshift(A(length(A)-i+1), 8*(i-1));
endfunction len=channel_voice_msg_len(type)if     (type==128); len=2;
elseif (type==144); len=2;
elseif (type==160); len=2;
elseif (type==176); len=2;
elseif (type==192); len=1;
elseif (type==208); len=1;
elseif (type==224); len=2;
elsedisp(type); error('bad channel voice message type');
end%
% decode variable length field (often deltatime)
%
%  return value and new position of pointer into 'bytes'
%
function [val,ptr] = decode_var_length(bytes, ptr)keepgoing=1;
binarystring = '';
while (keepgoing)% check MSB:%  if MSB=1, then delta-time continues into next byte...if(~bitand(bytes(ptr),128))keepgoing=0;end% keep appending last 7 bits from each byte in the deltatime:binbyte = ['00000000' dec2base(bytes(ptr),2)];binarystring = [binarystring binbyte(end-6:end)];ptr=ptr+1;
end
val = base2dec(binarystring,2);%
% Read first 2 bytes of msg and
%  determine the type
%  (most require only 1st byte)
%
% str is one of:
%  'channel_mode'
%  'channel_voice'
%  'sysex'
%  'sys_realtime'
%
function str=midi_msg_type(B,nB)Hn = bitshift(B,-4);
Ln = bitand(B,7);% ---- channel mode messages:
%if (Hn==11 && nB>=120 && nB<=127)
if (Hn==11 && nB>=122 && nB<=127)str = 'channel_mode';% ---- channel voice messages:
elseif (Hn>=8 && Hn<=14)str = 'channel_voice';%  ---- sysex events:
elseif (Hn==15 && Ln>=0 && Ln<=7)str = 'sysex';% system real-time messages
elseif (Hn==15 && Ln>=8 && Ln<=15)% UNSURE: how can you tell between 0xFF system real-time%   message and 0xFF meta event?%   (now, it will always be processed by meta)str = 'sys_realtime';else% don't think it can get here...error('bad midi message');
end
function [PR,t,nn] = piano_roll(Notes,vel,ts)
%
% Inputs:
%  Notes: A 'notes' matrix as returned from midiInfo.m
%  vel:   (optional) if vel==1, set value to note velocity instead of 1. (default 0)
%  ts:    (optional) time step of one 'pixel' in seconds (default 0.01)
%
% Outputs:
%  PR:    PR(ni,ti): value at note index ni, time index ti
%  t:     t(ti):  time value in seconds at time index ti
%  nn:    nn(ni): note number at note index ti
%if nargin < 2vel = 0;
end
if nargin < 3ts = 0.01;
endNnotes = size(Notes,1);n1 = round(Notes(:,5)/ts)+1;
n2 = round(Notes(:,6)/ts)+1;if vel == 0vals = ones(Nnotes,1);
elsevals = Notes(:,4); % velocity
endfor i=1:NnotesPR(Notes(i,3), n1(i):n2(i)) = vals(i);
end% create quantized time axis:
t = linspace(0,max(Notes(:,6)),size(PR,2));
% note axis:
nn = min(Notes(:,3)):max(Notes(:,3));
% truncate to notes used:
PR = PR(nn,:);

4.操作步骤与仿真结论

5.参考文献

MIDI文件格式分析 - 百度文库

A03-11

6.完整源码获得方式

方式1:微信或者QQ联系博主

方式2:订阅MATLAB/FPGA教程,免费获得教程案例以及任意2份完整源码

【MID音频读取和分析】基于matlab的MID音频文件读取和分析相关推荐

  1. matlab降压启动,基于 Matlab 的笼形异步电动机降压启动分析

    原标题:基于 Matlab 的笼形异步电动机降压启动分析 基于 Matlab 的笼形异步电动机降压启动分析 陈滨掖 摘要:三相异步电动机全压启动时的瞬间大电流及启动转矩会对负载造成很大冲击.降压启动的 ...

  2. matlab 避雷针保护范围程序,基于MATLAB避雷针保护范围可视化设计与分析.doc

    基于MATLAB避雷针保护范围可视化设计与分析 基于MATLAB避雷针保护范围可视化设计与分析 摘要: 利用MATLAB设计避雷针保护范围可视化程序与界面,对避雷针保护范围采用折线法和滚球法进行对比分 ...

  3. Matlab语音采集与读写程序,基于MATLAB的语音信号录制采集和分析的程序设计

    理 论广 角 ● I 基于 MATLAB的语音信号录制采集和分析的程序设计 刘 晓炯 (西北民族大学电气工程学院 甘肃 兰州I 730030) [摘 要]语音信号处理技术是语音处理领域中新近发展起来的 ...

  4. 判断清浊音 matlab,基于MATLAB的语音信号的清浊音分析.doc

    基于MATLAB的语音信号的清浊音分析 目录 1 语音信号概述1 1.1 语音信号的基本组成1 1.2 语音信号的"短时谱"1 1.3 基音周期2 1.4 短时分析技术2 2 语音 ...

  5. 《对冲基金建模与分析基于MATLAB》简介及PDF下载

    转 <对冲基金建模与分析--基于MATLAB>简介及PDF下载 内容简介 本书是关于用MATLAB对对冲基金进行建模和分析的入门读物.在对对冲基金的基本概念.分类.相关工具和指标系统介绍的 ...

  6. 巴特列特窗的matlab代码,基于MATLAB的FIR数字滤波器设计与优化分析.doc

    您所在位置:网站首页 > 海量文档 &nbsp>&nbsp计算机&nbsp>&nbspmatlab 基于MATLAB的FIR数字滤波器设计与优化分析. ...

  7. 电路仿真matlab实验总结,基于matlab的boost电路仿真的实验报告分析.doc

    基于matlab的boost电路仿真的实验报告分析.doc Boost电路 1.实验名称:基于matlab的boost电路仿真的实验报告分析. 2.实验目的:学习matlab的基础知识和操作: 改变占 ...

  8. 频域参数 matlab,基于MATLAB的语音信号时频域参数分析

    22 科技广场 2007.9 基于MATLAB的语音信号时频域参数分析 the Character Analysis of Speech Signal with Time and Frequency ...

  9. matlab计算空间桁架,基于matlab的空间三维桁架结构受力分析通用程序设计

    第 26 卷第 3 期 喀什师范学院学报 Vol. 26 No. 3 2005 年 5 月 Journal of Kashgar Teachers College May 2005 基于 matlab ...

  10. 基于Matlab的二阶电路的动态电路分析!

    基于Matlab的二阶电路的动态电路分析! 算法思路 1.一阶电路 1.只有电感:先得到电感所在端口的戴维宁等效电路: 接着代入公式计算,得到 i-t图像: 2.只有电容:先得到电容所在端口的戴维宁等 ...

最新文章

  1. ASP.NET中Visio图形的控制与数据的动态显示
  2. oracle普通用户使用dbms函数,oracle使用DBMS_SCHEDULER调度作业
  3. 中国SaaS人力资源管理系统市场发展模式分析与前景深度研究报告2022年版
  4. 【java】动态绑定机制
  5. “保持耐心”,永远从用户角度出发— 专访阿里巴巴淘系技术内容中台负责人吴桂林(梁舒)...
  6. 写给前端工程师的 Flutter 详细教程
  7. 6月21 百度文本编辑器
  8. Python第三方库离线安装包制作(whl文件)(离线包)
  9. 提交表单的时候会出现提交两次的问题
  10. 计算机学会a类论文是sci吗,sci分区和ccf分区的区别
  11. 数据结构与算法之python
  12. 圣诞节计算机老师贺卡祝福语,[圣诞节贺卡教师祝福语]2020圣诞节贺卡祝福语
  13. 北京中医药大学计算机应用基础作业1,北京中医药大学远程教育“计算机应用基础”第5次作业.doc...
  14. 门店定位怎么在地图上显示_门店位置如何显示在地图上?
  15. 大数据学习——相关资源
  16. 百度医疗广告卷土重来_意见3d电视值得卷土重来的时间
  17. Creo 导入图片不显示
  18. SpringBoot控制台打印SQL
  19. Android初学习
  20. 使用 EasyPoi 完成复杂一对多 excel 表格导出功能

热门文章

  1. 华为畅享max支持鸿蒙,华为手机怎么升级鸿蒙?华为鸿蒙系统支持手机型号大全...
  2. Fiddler 4 模拟 服务端返回 json
  3. 单片机烧录文件的几种格式
  4. cx_Oracle安装教程
  5. MATLAB 学习笔记(5)MATLAB 数据的导入和导出
  6. GsonFormat的使用
  7. 依赖于 !important 标签是个危险的现象。奔驰车如何查4S店的保养记录
  8. Jupyter Notebook打开时报错的问题解决办法
  9. 6N137中文说明书 光耦资料 6N137资料
  10. matlab的默认字体_matlab画图字体 matlab默认的字体是什么