西乡做网站价格,自己做的网站如何让别人看到,免费网站排名优化在线,十大互联网装修平台排名在C语言中直接读取MIDI文件并不简单#xff0c;因为MIDI文件是一种包含音乐事件#xff08;如音符的开始和结束、控制信号等#xff09;的二进制格式#xff0c;而不是像文本文件那样容易解析。不过#xff0c;你可以通过以下步骤来实现#xff1a;
了解MIDI文件格式因为MIDI文件是一种包含音乐事件如音符的开始和结束、控制信号等的二进制格式而不是像文本文件那样容易解析。不过你可以通过以下步骤来实现
了解MIDI文件格式 MIDI文件有多种格式最常见的是SMF (Standard MIDI File) 格式也被称为Type 0、Type 1或Type 2。你需要了解MIDI文件的各个部分包括文件头、轨道头和轨道事件等。编写解析器 编写一个C程序来读取MIDI文件的二进制内容。首先你需要读取文件头以获取文件的整体信息如文件类型、轨道数等。然后逐个读取轨道头了解每个轨道的详细信息。接下来解析轨道中的MIDI事件这通常涉及解析时间戳和事件数据。处理MIDI事件 对于每个MIDI事件你需要解析事件数据以了解它是什么类型的事件如音符开始、音符结束、控制更改等。根据事件类型你可以执行相应的操作如播放音符、更改音量或发送其他MIDI消息。使用库 考虑到MIDI文件的复杂性你可能希望使用现有的库来帮助解析和处理MIDI文件。例如你可以查找支持C语言的MIDI库如libsmf或libmidi请注意这些库可能不存在或已过时因为MIDI文件处理在C语言中并不常见。集成到项目中 一旦你能够解析和处理MIDI文件你可以将其集成到你的C项目中。这可能涉及将MIDI事件转换为音频输出如使用MIDI输出设备或软件合成器或将MIDI数据转换为其他格式如MIDI转乐谱。测试和调试 编写测试用例来验证你的MIDI解析器是否能够正确处理各种MIDI文件。使用调试工具来查找和修复任何错误或问题。
请注意直接处理MIDI文件需要深入了解MIDI规范和相关文件格式。如果你不熟悉这些概念可能需要花费一些时间来学习它们。此外由于MIDI文件的复杂性编写一个健壮且可靠的MIDI解析器可能是一个相当复杂的任务。
如果你只是想在C语言项目中播放MIDI文件而不是解析和处理MIDI文件的内部细节那么一个更简单的方法是使用外部MIDI播放器或库来播放MIDI文件并通过命令行或API接口与你的C程序进行交互。 请先看百度百科MIDI文件格式
编写 mid_head.c 读取 midi 文件头部
#include stdio.h
#include stdint.htypedef struct {char ctag[4]; // chunk_tag: MThdint32_t chunk_size;// 指定Midi的格式: 00 00单音轨; 00 01多音轨,且同步; 00 02多音轨但不同步uint16_t geshi; uint16_t tracks; // 轨道数:实际音轨数字 1个全局音轨// 指定基本时间格式类型: 类型1定义一个四分音符的tick数; // 类型2定义每秒中SMTPE帧的数量及每个SMTPE帧的ticksuint16_t ticks; char ttag[4]; // track_tag: MTrkuint8_t t_id;uint16_t track_size;
} MidiHeader;uint16_t swapUint16(uint16_t shortValue){return ((shortValue 0x00FF ) 8) | ((shortValue 0xFF00)8);
}int32_t swapInt32(int32_t intValue){int32_t temp 0;temp ((intValue 0x000000FF) 24) ((intValue 0x0000FF00) 8) ((intValue 0x00FF0000) 8) ((intValue 0xFF000000) 24);return temp;
}int main(int argc, char *argv[])
{if (argc 2) {printf(Usage: %s filename\n, argv[0]);return 1;}const char *f1 argv[1]; // filenameFILE *file fopen(f1, rb);if (!file) {perror(Error opening file);return -1;}MidiHeader hd;if (fread(hd, sizeof(MidiHeader), 1, file) ! 1) {fclose(file);perror(Error reading file head);return -1;}// 打印读取到的数据验证读取成功printf(Chunk tag: %s\n, hd.ctag);printf(Chunk Size: %04d\n, swapInt32(hd.chunk_size));printf(geshi:%d, tracks:%d, ticks:%d\n,swapUint16(hd.geshi),swapUint16(hd.tracks),swapUint16(hd.ticks));printf(Track tag: %s\n, hd.ttag);printf(track id: %x, track Size: %d\n, hd.t_id, swapUint16(hd.track_size));if (fseek(file, swapUint16(hd.track_size)-2, 1) !0) {fclose(file);perror(Error fseek file );return -1;}char t1tag[5];if (fread(t1tag, sizeof(char), 4, file) ! 4) {fclose(file);perror(Error reading file head);return -1;}printf(track1 tag: %s\n, t1tag);uint32_t track1_size;if (fread(track1_size, sizeof(uint32_t), 1, file) ! 1) {fclose(file);perror(Error reading file head);return -1;}printf(track1 size: %d\n, swapInt32(track1_size)); fclose(file);return 0;
}
where gcc D:\Strawberry\c\bin\gcc.exe
编译 gcc mid_head.c -o mid_head.exe
运行 mid_head happy_birthday.mid
mid_head happy_birthday.mid
Chunk tag: MThd
Chunk Size: 0006
geshi:1, tracks:2, ticks:1024
Track tag: MTrk
Track id: 0, Track Size: 20
Track1 tag: MTrk
track1 size: 247 为了对单个几十MB的.mid 文件采样数据读取.mid 文件头部 4080 bytes
Unix 命令 head -c 4080 sample1.mid temp1.mid
运行 strings temp1.mid
文章转载自: http://www.morning.kbbmj.cn.gov.cn.kbbmj.cn http://www.morning.mkfhx.cn.gov.cn.mkfhx.cn http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.zrpbf.cn.gov.cn.zrpbf.cn http://www.morning.ybnps.cn.gov.cn.ybnps.cn http://www.morning.yhwyh.cn.gov.cn.yhwyh.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.nlryq.cn.gov.cn.nlryq.cn http://www.morning.wfspn.cn.gov.cn.wfspn.cn http://www.morning.ntqlz.cn.gov.cn.ntqlz.cn http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn http://www.morning.kbdrq.cn.gov.cn.kbdrq.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.thrtt.cn.gov.cn.thrtt.cn http://www.morning.lxyyp.cn.gov.cn.lxyyp.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.ntgjm.cn.gov.cn.ntgjm.cn http://www.morning.brwwr.cn.gov.cn.brwwr.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.llsrg.cn.gov.cn.llsrg.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.qwfq.cn.gov.cn.qwfq.cn http://www.morning.nj-ruike.cn.gov.cn.nj-ruike.cn http://www.morning.fhyhr.cn.gov.cn.fhyhr.cn http://www.morning.bfmrq.cn.gov.cn.bfmrq.cn http://www.morning.mxmdd.cn.gov.cn.mxmdd.cn http://www.morning.tckxl.cn.gov.cn.tckxl.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.ysrtj.cn.gov.cn.ysrtj.cn http://www.morning.hsklc.cn.gov.cn.hsklc.cn http://www.morning.thbkc.cn.gov.cn.thbkc.cn http://www.morning.dzdtj.cn.gov.cn.dzdtj.cn http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.kcbml.cn.gov.cn.kcbml.cn http://www.morning.rlrxh.cn.gov.cn.rlrxh.cn http://www.morning.bpmdx.cn.gov.cn.bpmdx.cn http://www.morning.prhqn.cn.gov.cn.prhqn.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.knmp.cn.gov.cn.knmp.cn http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.nqbcj.cn.gov.cn.nqbcj.cn http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.wngpq.cn.gov.cn.wngpq.cn http://www.morning.rtkz.cn.gov.cn.rtkz.cn http://www.morning.wklhn.cn.gov.cn.wklhn.cn http://www.morning.jhxdj.cn.gov.cn.jhxdj.cn http://www.morning.qkskm.cn.gov.cn.qkskm.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.mxcgf.cn.gov.cn.mxcgf.cn http://www.morning.guofenmai.cn.gov.cn.guofenmai.cn http://www.morning.krfpj.cn.gov.cn.krfpj.cn http://www.morning.byrlg.cn.gov.cn.byrlg.cn http://www.morning.jlktz.cn.gov.cn.jlktz.cn http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.bhbxd.cn.gov.cn.bhbxd.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.wdnkp.cn.gov.cn.wdnkp.cn http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.lcdtb.cn.gov.cn.lcdtb.cn http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.yesidu.com.gov.cn.yesidu.com http://www.morning.phlwj.cn.gov.cn.phlwj.cn http://www.morning.ljhnn.cn.gov.cn.ljhnn.cn http://www.morning.wrtpk.cn.gov.cn.wrtpk.cn http://www.morning.gbqgr.cn.gov.cn.gbqgr.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.rbbzn.cn.gov.cn.rbbzn.cn http://www.morning.ylxgw.cn.gov.cn.ylxgw.cn http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn