📄 mfi.c
字号:
/* TiMidity++ -- MIDI to WAVE converter and player Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp> Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA mfi.c (Melody Format for i-mode) by Kentaro Sato <kentaro@ranvis.com>*/#ifdef HAVE_CONFIG_H#include "config.h"#endif /* HAVE_CONFIG_H */#include "timidity.h"#include "common.h"#include "instrum.h"#include "playmidi.h"#include "readmidi.h"#include "controls.h"#include <stdlib.h>#define SETMIDIEVENT(e, at, t, ch, pa, pb) \ { (e).time = (at); (e).type = (t); \ (e).channel = (uint8)(ch); (e).a = (uint8)(pa); (e).b = (uint8)(pb); }#define MIDIEVENT(at, t, ch, pa, pb) \ { MidiEvent event; SETMIDIEVENT(event, at, t, ch, pa, pb); \ readmidi_add_event(&event); }#ifdef LITTLE_ENDIAN#define BE_FCC(type) ((uint32)XCHG_LONG(type))#else /* BIG ENDIAN */#define BE_FCC(type) ((uint32)BE_LONG(type))#endif /* Note: Comparing MFi FCC (Four Character Code) In MFi, FCC has the same bit length as 4-byte-integer such as dataLength, so loading and comparing it like an integer should be portable enough. */#define MAPBITS(effInt, effBits, mapBits) (((effInt) << ((mapBits) - (effBits))) | ((effInt) >> ((effBits) - ((mapBits) - (effBits)))))#define MAPBITS2(effInt, effBits, mapBits) (((effInt) << ((mapBits) - (effBits))) | (((effInt) << ((mapBits) - (effBits))) >> (effBits)) | (((effInt) << ((mapBits) - (effBits))) >> ((effBits) * 2)))#define MFI_DEBUG_PREFIX "MFi "#define MFI_DEBUG_MORE_VERBOSE 0#define MFI_DEBUG_NOTE_EVENT 0#define MFI_DEBUG_NOTE_EVENT_S 0#define MFI_DEBUG_CTL_DATA 0#define MFI_DEBUG_UNKNOWN_CTL_DATA 1#define POS_DS(pos) ((pos) / 48), ((pos) % 48)#if MFI_DEBUG_MORE_VERBOSE #undef VERB_DEBUG #undef VERB_NOISY #define VERB_DEBUG VERB_NORMAL #define VERB_NOISY VERB_NORMAL#endif#if MFI_DEBUG_NOTE_EVENT #define NOTE_EVENT_DEBUGSTR(channel, note, octave, velocity, duration) \ ctl->cmsg(CMSG_INFO, VERB_DEBUG, MFI_DEBUG_PREFIX "%d.%02d [%02d] %s%d@%d %d(%d.%02d)", POS_DS(pos), (channel)+1, note, octave, velocity, duration, POS_DS(pos + duration)) #define NOTE_EVENT_POSSIBLE_SLUR_DEBUGSTR() /* empty */#else #define NOTE_EVENT_DEBUGSTR(channel, note, octave, velocity, duration) /* empty */ #define NOTE_EVENT_POSSIBLE_SLUR_DEBUGSTR() /* empty */#endif#if MFI_DEBUG_NOTE_EVENT_S #define NOTE_BUF_EV_DEBUGSTR(channel, time, note, octave, velocity, offtime) \ ctl->cmsg(CMSG_INFO, VERB_DEBUG, MFI_DEBUG_PREFIX "<< %d [%02d] %s%d@%d %d", time, (channel)+1, note, octave, velocity, offtime)#else #define NOTE_BUF_EV_DEBUGSTR(channel, time, note, octave, velocity, offtime) /* empty */#endif#if MFI_DEBUG_CTL_DATA #define EX_DATA_DEBUGSTR1(exname, channel, paramstr, p1) \ ctl->cmsg(CMSG_INFO, VERB_DEBUG, MFI_DEBUG_PREFIX "%d.%02d [%02d] %02X(%s) : " paramstr, POS_DS(pos), (channel)+1, data[2], exname, p1) #define EX_NCDATA_DEBUGSTR1(exname, paramstr, p1) \ ctl->cmsg(CMSG_INFO, VERB_DEBUG, MFI_DEBUG_PREFIX "%d.%02d [--] %02X(%s) : " paramstr, POS_DS(pos), data[2], exname, p1) #define EX_DATA_DEBUGSTR2(exname, channel, paramstr, p1, p2) \ ctl->cmsg(CMSG_INFO, VERB_DEBUG, MFI_DEBUG_PREFIX "%d.%02d [%02d] %02X(%s) : " paramstr, POS_DS(pos), (channel)+1, data[2], exname, p1, p2)#else #define EX_DATA_DEBUGSTR1(exname, channel, paramstr, p1) /* empty */ #define EX_NCDATA_DEBUGSTR1(exname, channel, p1) /* empty */ #define EX_DATA_DEBUGSTR2(exname, channel, paramstr, p1, p2) /* empty */#endif#if MFI_DEBUG_UNKNOWN_CTL_DATA #define EX_UNKNOWN_DATA_DEBUGSTR() \ ctl->cmsg(CMSG_WARNING, VERB_NOISY, MFI_DEBUG_PREFIX "%d.%02d %02X (not implemented) : %02X", POS_DS(pos), data[2], data[3]) #define EX_UNKNOWN_EXT_DATA_DEBUGSTR(len) \ ctl->cmsg(CMSG_WARNING, VERB_NOISY, MFI_DEBUG_PREFIX "%d.%02d %02X (not implemented) : %04X", POS_DS(pos), data[2], len)#else #define EX_UNKNOWN_DATA_DEBUGSTR() /* empty */ #define EX_UNKNOWN_EXT_DATA_DEBUGSTR(len) /* empty */#endiftypedef struct timidity_file timidity_file;static int tf_read_beint16(int *, timidity_file *);static int tf_read_beint32(int *, timidity_file *);static int read_mfi_information(int, int *, int *, int *, timidity_file *);static int read_mfi_track(int, int, int, int, int, timidity_file *);int read_mfi_file(timidity_file *tf){ int length, dataLength, infoLength, dataType, mfiVersion, noteType, extStDLength; uint32 type; uint8 numTracks; int i; if (tf_read_beint32(&dataLength, tf) != 1 || tf_read_beint16(&infoLength, tf) != 1 || tf_read_beint16(&dataType, tf) != 1 || tf_read(&numTracks, 1, 1, tf) != 1) return 1; infoLength -= 2 + 1; if (dataType == 0x0202) { ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "MFi Type 2.2 may not be playable."); /* I'm not sure :-) */ return 1; } if (numTracks == 0) { ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "MFi contains no track."); return 1; } ctl->cmsg(CMSG_INFO, VERB_VERBOSE, "MFi Tracks: %d", numTracks); if (numTracks > MAX_CHANNELS / 4) { ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Too many tracks, last %d track(s) are ignored.", numTracks - (MAX_CHANNELS / 4)); numTracks = MAX_CHANNELS / 4; } current_file_info->divisions = 48; current_file_info->format = 1; current_file_info->tracks = numTracks; current_file_info->file_type = IS_MFI_FILE; noteType = extStDLength = 0; if (read_mfi_information(infoLength, &mfiVersion, ¬eType, &extStDLength, tf) != 0) return 1; for(i = 0; i < numTracks; i++) { if (tf_read(&type, 4, 1, tf) != 1 || tf_read_beint32(&length, tf) != 1) return 1; if (type != BE_FCC(0x74726163 /*trac*/)) { ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Unknown track signature."); return 1; } if (read_mfi_track(i, length, mfiVersion, noteType, extStDLength, tf) != 0) return 1; } return 0;}static int read_mfi_information(int infoLength, int *mfiVersion, int *noteType, int *extStDLength, timidity_file *tf){ int length; uint32 type; uint8 byteData; char buf[512]; *mfiVersion = 1; while (infoLength > 0) { if (infoLength < 4 + 2) { ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Odd information length."); return 1; } infoLength -= 6; if (tf_read(&type, 4, 1, tf) != 1 || tf_read_beint16(&length, tf) != 1) return 1; if (length == 0) continue; if (length > infoLength) { ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Total information length was too small."); return 1; } infoLength -= length; switch(type) { case BE_FCC(0x7469746C /*titl*/): { /* title */ char *title; if (current_file_info->seq_name == NULL) goto skip_info_data; title = safe_malloc(length + 1); if (tf_read(title, length, 1, tf) != 1) { free(title); return 1; } title[length] = '\0'; current_file_info->seq_name = title; ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "Title: %s", title); } break; case BE_FCC(0x736F7263 /*sorc*/): { /* source */ const char *srcInfo; if (length != 1) goto skip_info_data; if (tf_read(&byteData, 1, 1, tf) != 1) return 1; switch (byteData >> 1) { case 0x00: srcInfo = "network"; break; case 0x01: srcInfo = "manual"; break; case 0x02: srcInfo = "external"; break; default: srcInfo = "unknown"; } ctl->cmsg(CMSG_INFO, VERB_NOISY, "Source: %s%s", srcInfo, (byteData & 1) ? ", copyrighted" : ""); } break; case BE_FCC(0x76657273 /*vers*/): /* version (unused) */ if (tf_read(&type, 4, 1, tf) != 1) return 1; switch(type) { case BE_FCC(0x30313030 /*0100*/): *mfiVersion = 1; break; case BE_FCC(0x30323030 /*0200*/): *mfiVersion = 2; break; case BE_FCC(0x30333030 /*0300*/): *mfiVersion = 3; break; default: ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Unknown MFi version."); return 1; } ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "MFi Version: %d", *mfiVersion); /* info/controls which are marked as '(MFi*)' are only executable when mfiVersion == * or mfiVersion >= *. */ break; case BE_FCC(0x64617465 /*date*/): { /* created date */ char created[9]; if (length != 8) goto skip_info_data; if (tf_read(created, 8, 1, tf) != 1) return 1; created[8] = '\0'; ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "Date: %.4s-%.2s-%.2s", created, &created[4], &created[6]); } break; case BE_FCC(0x636F7079 /*copy*/): { /* copyright */ int lengthToRead; lengthToRead = length >= sizeof buf ? sizeof buf - 1 : length; if (lengthToRead > 0 && tf_read(buf, lengthToRead, 1, tf) != 1) return 1; buf[lengthToRead] = '\0'; ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "Copyright: %s", buf); if (lengthToRead < length && tf_seek(tf, length - lengthToRead, SEEK_CUR) == -1) return 1; } break; /* case BE_FCC(0x70726F74 /-*prot*-/): goto skip_info_data; */ case BE_FCC(0x6E6F7465 /*note*/): /* note (MFi2) */ if (length != 2 || tf_read_beint16(noteType, tf) != 1) return 1; if (*noteType > 1) { ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Unknown note information."); return 1; } ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Note Type: %d", *noteType); break; case BE_FCC(0x65787374 /*exst*/): /* extended status (MFi2) */ if (length != 2 || tf_read_beint16(extStDLength, tf) != 1) return 1; if (*extStDLength != 0) { ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Unknown extended status information. (%d)"); return 1; } break; default: skip_info_data: if (tf_seek(tf, length, SEEK_CUR) == -1) return 1; } } return 0;}char *get_mfi_file_title(timidity_file *tf){ int length, dataLength, infoLength, dataType; uint32 type; uint8 numTracks; if (tf_read_beint32(&dataLength, tf) != 1 || tf_read_beint16(&infoLength, tf) != 1 || tf_read_beint16(&dataType, tf) != 1 || dataType == 0x0202 || tf_read(&numTracks, 1, 1, tf) != 1) return NULL; infoLength -= 2 + 1; while (infoLength >= 4 + 2) { infoLength -= 6; if (tf_read(&type, 4, 1, tf) != 1 || tf_read_beint16(&length, tf) != 1) return NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -