📄 mammf.c
字号:
#include "pccdef.h"
#include "B_FileIO.h"
#include "Mammf.h"
#include "Madev_initial.h"
#include "Masnddrv.h"
int MMF_initial(u8 mode)
{
u32 result;
result = MaDevDrv_Initialize( mode );
if( result != 0 )
{
printf_uart2("\n Initialization failed! Exit!");
return 0;
}
printf_uart2("\n Initialization SUCCESS!");
MaDevDrv_ClearFifo();
return 1;
}
int file_test(void)
{
u8 i;
char filename[20];
u8 buf[20000],channel_state[16];
u32 file_chunk,file_size,rfile_size,chunkID,chunk_size;
u32 pstart, pstop;
u16 wCalc_CRC, wFile_CRC;
FILE *fp;
printf_uart2("\n Please input the SMAF file name:");
gets_uart2(filename,20,0);
if((fp = Fopen(filename,"rb")) == NULL)
{
printf_uart2("Can't open the MMF file!\n");
return 0;
}
Fread(buf,1,20000,fp); /* load file data to buffer */
file_chunk = get4b(buf);
file_size = get4b(buf+num);
rfile_size = file_size+8;
printf_uart2("\nThe real file size = 0x%x",rfile_size);
if(rfile_size > 20000)
{
printf_uart2("\nThe file is too big! EXIT!");
return 0;
}
if(file_chunk != 0x4D4D4D44) /* only "MMMD" can be supported */
{
printf_uart2("\nUnknow type! Exit!");
return 0;
}
/* CRC check */
wFile_CRC = (u16)((buf[file_size - 2+8] << 8) + buf[file_size - 1+8]);
wCalc_CRC = makeCRC(rfile_size,&buf[0]);
printf_uart2("\n file_CRC = %x, Calc_CRC = %x", wFile_CRC,wCalc_CRC);
/*if (wCalc_CRC != wFile_CRC)
{
printf_uart2("\nCRC Check Error!");
return 0;
}*/
/* Contents Info Chunk */
chunkID = get4b(buf+num);
if(chunkID != 0x434E5449)
{
printf_uart2("\nNo Contents Info Chunk(CNTI)! EXIT!");
return 0;
}
chunk_size = get4b(buf+num);
if( buf[num+1] == 0x32 )
{
cnvmode = 16; /* maximum channel number is 16 */
}
else
if( buf[num+1] == 0x33 )
{
cnvmode = 32; /* maximum channel number is 32 */
}
else
return 0;
printf_uart2("\ncnvmode = %d", cnvmode);
/* Optional Data Chunk */
num += chunk_size;
chunkID = get4b(buf+num);
chunk_size = get4b(buf+num);
if(chunkID != 0x4F504441)
{
printf_uart2("\nNO Optional Data Chunk(opda)!");
num -= 8;
}
else num += chunk_size;
/* Score Track Chunk */
chunkID = get4b(buf+num);
if(chunkID != 0x4D545205)
{
printf_uart2("\nNo Score Track Chunk(MTR0x5)! EXIT!");
return 0;
}
chunk_size = get4b(buf+num);
/* Score track chunk information */
if((buf[num] != 0x02) || (buf[num+1] != 0x0)) /* Only No Compress and Stream can pass */
{
printf_uart2("\nThe format type can't be supported! Exit!");
return 0;
}
if(buf[num+2] != buf[num+3])
{
printf_uart2("\nTimebaseD != TimebaseG! Exit!");
return 0;
}
timebase = get_timebase(buf[num+2]);
if(timebase == 0)
{
printf_uart2("\nThe timebase can't be supported! Exit!");
return 0;
}
else printf_uart2("\ntimebase = %d", timebase);
/* Get channel status */
for(i = 0;i < 16;i++)
{
channel_state[i] = buf[num+4+i];
printf_uart2("\nChannel %d state = 0x%x", i, channel_state[i]);
}
num += 20;
/* Score track chunk---Sub Chunk */
for(i = 0; i < 4; i++)
{
if(num >= (rfile_size-2)) break;
chunkID = get4b(buf+num);
switch(chunkID)
{
case 0x4D747375:
chunk_size = get4b(buf+num);
printf_uart2("\nMstu chunk size=0x%x", chunk_size);
Exclusive_check(&buf[num], chunk_size);
num += chunk_size;
break;
case 0x4D737049:
chunk_size = get4b(buf+num);
printf_uart2("\nMspI chunk size=0x%x",chunk_size);
MspI_check(&buf[num], chunk_size, &pstart, &pstop);
printf_uart2("\nfinal START = 0x%x", pstart);
printf_uart2("\nfinal STOP = 0x%x", pstop);
num += chunk_size;
break;
case 0x4D747371:
chunk_size = get4b(buf+num);
printf_uart2("\nMtsq chunk size=0x%x",chunk_size);
Event_check(&buf[num],chunk_size);
num += chunk_size;
break;
case 0x4D747370:
chunk_size = get4b(buf+num);
num += chunk_size;
printf_uart2("\nMtsp chunk size=0x%x",chunk_size);
break;
default:
printf_uart2("\nNo specificated data chunk!");
break;
}
}
return 1;
}
u32 get4b(u8 *buff)
{
u32 result;
result = ((u32)*buff << 24) | ((u32)*(buff+1) << 16) |
((u32)*(buff+2) << 8) | ((u32)*(buff+3));
num += 4;
return result;
}
u16 makeCRC(u32 n, u8 *c)
{
u16 res;
u16 crc;
u8 data0;
res = 0xFFFFU;
crc = 0;
while( --n >= 2 )
{
data0 = *c++;
res = (u16)((res << 8) ^ crctable[(u8)(res >> 8) ^ data0]);
crc = (u16)((crc << 8) | data0);
}
return (u16)(~res & 0xFFFFU);
}
u8 get_timebase(u8 index)
{
switch( index )
{
case 0x00: return 0;
case 0x01: return 0;
case 0x02: return 4;
case 0x03: return 5;
case 0x10: return 10;
case 0x11: return 20;
case 0x12: return 40;
case 0x13: return 50;
default: return 0; /* ERROR */
}
}
void Event_check(u8 *pevent, u32 chunksize)
{
u32 i;
u8 j = 0, k = 0, m = 0;
u8 note, velocity, prog, ctrl_no, ctrl_val, master_vol, bBankM, bBankL;
u8 bData0, bData1, bData2, bChannel, bRpnL, bRpnM;
u16 bend, PitchBendFlag=0;
u32 duration=1, gatetime, next_dura, total_time=0;
u32 dwMsize=0;
if( create_flag ==0 )
{
MaSndDrv_Create( timebase, cnvmode, 0, &dwRamAdr, &dwRamSize );
}
for(i = 0; i < chunksize; i+=dwMsize)
{
duration = get_flex(pevent+i, &j);
bData0 = *(pevent+i+j);
bChannel = (u8)(bData0 & 0x0F);
total_time += duration;
switch( bData0 & 0xF0)
{
case 0x80:
note = *(pevent+i+j+1) & 0x7F;
velocity = 64;
gatetime = get_flex(pevent+i+j+2, &k);
printf_uart2("\nNote Message (No velocity) found! position = %d", total_time);
printf_uart2("\n Duration = 0x%x, Channel = 0x%x", duration, bChannel);
printf_uart2("\n Note = 0x%x, Velocity = 0x%x, Gatetime = 0x%x", note, velocity, gatetime);
dwMsize = j+k+2;
next_dura = get_flex(pevent+i+dwMsize, &m);
MaSndDrv_NoteOn( duration, bChannel, note, velocity, gatetime, next_dura );
break;
case 0x90:
note = *(pevent+i+j+1) & 0x7F;
bData1 = *(pevent+i+j+2) & 0x7F;
velocity = VelocityTable[bData1];
gatetime = get_flex(pevent+i+j+3, &k);
printf_uart2("\nNote Message (No velocity) found! position = %d", total_time);
printf_uart2("\n Duration = 0x%x, Channel = 0x%x", duration, bChannel);
printf_uart2("\n Note = 0x%x, Velocity = 0x%x, Gatetime = 0x%x", note, velocity, gatetime);
dwMsize = j+k+3;
next_dura = get_flex(pevent+i+dwMsize, &m);
MaSndDrv_NoteOn( duration, bChannel, note, velocity, gatetime, next_dura );
break;
case 0xC0:
prog = *(pevent+i+j+1) & 0x7F;
printf_uart2("\nProgram channel Message found! Duration = 0x%x Channel = 0x%x", duration, bChannel);
printf_uart2("\n Prog No = 0x%x", prog);
MaSndDrv_ProgramChange( duration, bChannel, bBankM, bBankL, prog );
dwMsize = j+2;
break;
case 0xE0:
/* Pitch Bend */
bData1 = *(pevent+i+j+1) & 0x7F;
bData2 = *(pevent+i+j+1) & 0x7F;
bend = (UINT16)((bData1 & 0x7F) | ((bData2 & 0x7F) << 7));
printf_uart2("\n pitch bend = 0x%x", bend);
if (PitchBendFlag & (0x0001 << bChannel))
{
MaSndDrv_Nop( duration, 0 );
break;
}
MaSndDrv_PitchBend( duration, bChannel, bend ); /* bend */
dwMsize = j+3;
break;
case 0xB0:
ctrl_no = *(pevent+i+j+1) & 0x7F;
ctrl_val = *(pevent+i+j+2) & 0x7F;
printf_uart2("\nControl Message found! Duration = 0x%x Channel = 0x%x", duration, bChannel);
dwMsize = j+3;
switch( ctrl_no )
{
case 0x00:
/* Bank select MSB */
bBankM = ctrl_val;
printf_uart2("\n BankM = 0x%x", bBankM);
MaSndDrv_Nop( duration, 0 );
break;
case 0x20:
/* Bank select LSB */
bBankL = ctrl_val;
printf_uart2("\n BankL = 0x%x", bBankL);
MaSndDrv_Nop( duration, 0 );
break;
case 0x07:
/* Channel value */
printf_uart2("\n Channel Vol = 0x%x", ctrl_val);
MaSndDrv_ChannelVolume( duration, bChannel, ctrl_val );
break;
case 0x01:
/* Modulation depth */
printf_uart2("\n Modulation depth = 0x%x", ctrl_val);
if(ctrl_val == 0)
ctrl_val = 0;
else if(ctrl_val <= 0x1F)
ctrl_val = 1;
else if(ctrl_val <= 0x3F)
ctrl_val = 2;
else if(ctrl_val <= 0x5F)
ctrl_val = 3;
else
ctrl_val = 4;
MaSndDrv_ModulationDepth( duration, bChannel, ctrl_val );
break;
case 0x0A:
/* Pan */
printf_uart2("\n Panpot = 0x%x", ctrl_val);
MaSndDrv_Panpot( duration, bChannel, ctrl_val );
break;
case 0x0B:
/* Expression */
printf_uart2("\n Expression = 0x%x", ctrl_val);
MaSndDrv_Expression( duration, bChannel, ctrl_val );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -