📄 maphrcnv.c
字号:
/****************************************************************************
*
* Copyright (C) 2002-2003 YAMAHA CORPORATION. All rights reserved.
*
* Module : maphrcnv.c
*
* Description : MA-5 SMAF/Phrase and Audio Stream Converter Module.
*
* Version : 1.1.2 2003.07.14
*
****************************************************************************/
#include "mamachdep.h"
#include "madebug.h"
#include "masndseq.h" /* MA-X Sound Sequencer */
#include "masnddrv.h" /* MA-X Sound Driver */
#include "maresmgr.h" /* MA-X Resource Manager */
#include "malib.h"
#include "maphrcnv.h"
/****************************************************************************
* macro
****************************************************************************/
#define MAPHR_MAKEDWORD(a, b, c, d) (UINT32)(((UINT32)(a) << 24) | \
((UINT32)(b) << 16) | ((UINT32)(c) << 8) | (UINT32)(d))
/****************************************************************************
* define
****************************************************************************/
/* for common */
#define MAPHR_MASK_PHRASE (0x01)
#define MAPHR_MASK_AUDIO (0x02)
#define MAPHR_SIZE_OF_CHUNKHEADER (8)
#define MAPHR_CTRL_CLEAR_ALL (0x00000000)
#define MAPHR_CTRL_SYSTEM_ON (0x80000000)
#define MAPHR_CTRL_MASTER_VOLUME (0x40000000)
#define MAPHR_CTRL_VOLUME (0x00000001)
#define MAPHR_CTRL_PANPOT (0x00000002)
#define MAPHR_WAIT_TIMEOUT (1000) /* ms */
#define MAPHR_PHRASE_TIMEBASE (20)
/* for Phrase */
#define MAPHR_VERSION_LV1 (1)
#define MAPHR_VERSION_LV2 (2)
#define MAPHR_MAX_SLOT (4)
#define MAPHR_MAX_CHANNEL (4)
#define MAPHR_MAX_WAVES (16)
#define MAPHR_MAX_LV1_VOICES (4)
#define MAPHR_MAX_LV2_VOICES MAPLIB_MAX_LV2_VOICES
#define MAPHR_MA2_4OP_SIZE (25)
#define MAPHR_MA5_2OP_SIZE (16)
#define MAPHR_MA5_4OP_SIZE (30)
#define MAPHR_MA5_WT_SIZE (13)
#define MAPHR_END_OF_SEQUENCE (-1)
#define MAPHR_REPEAT (-2)
/* for Audio */
#define MAPHR_AUDIO_CNV_ID (4)
#define MAPHR_MAX_AUDIO_DATA (2)
/****************************************************************************
* function
****************************************************************************/
extern void machdep_memcpy(UINT8*, UINT8*, UINT32);
extern void machdep_Sleep(UINT32);
static SINT32 MaPhrAud_Convert(void);
/****************************************************************************
* typedef
****************************************************************************/
typedef enum _MAPHRSTATUS
{
MAPHR_STATUS_UNNOWN = 0,
MAPHR_STATUS_NODATA,
MAPHR_STATUS_READY,
MAPHR_STATUS_PLAY,
MAPHR_STATUS_ENDING,
MAPHR_STATUS_PAUSE
} MAPHRSTATUS;
typedef struct _MAPHRWTBANK
{
UINT8* pbWave; /* Pointer to the wave data */
UINT32 dSize; /* Size of wave data */
UINT32 dRamAdr;
} MAPHRWTBANK, *PMAPHRWTBANK;
typedef struct _MAPHRAPIINFO
{
MAPHRSTATUS Status; /* Status */
SINT32 sdMasterChannel;
UINT32 dVolume; /* Phrase API Volume(0..127 def:100) */
UINT32 dPan; /* Phrase API Panpot(0..127 def:64) */
UINT32 dSlave;
UINT32 dLength; /* play back time[ms] */
UINT32 dLoopCount; /* Loop count(0:infinite 1..255) */
UINT32 dRamAdr; /* Ram address */
UINT32 dRamEndAdr; /* Ram end address */
MALIBPHRINFO sDataInfo; /* SMAF/Phrase information */
} MAPHRAPIINFO, *PMAPHRAPIINFO;
typedef struct _MAPHRCHINFO
{
UINT32 dKey; /* Key number(0..127) */
UINT32 dVelocity; /* Velocity(0..127 def:127) */
UINT32 dNewBank; /* */
UINT32 dBankNo; /* Bank Number(0,1,128,129) */
UINT32 dProgNo; /* Program Number(0..127 def:0) */
UINT32 dVolume; /* Volume(0..127 def:100) */
UINT32 dPan; /* Panpot (0..127 def:64) */
UINT32 dExp; /* Expression (0..127 def:100) */
UINT32 dMod; /* Modulation (0..127 def:0) */
UINT32 dNewRange; /* */
UINT32 dRange; /* Pitch Bend Range 100cent
unit(0..24 def:2) */
UINT32 dPitch; /* Pitch Bend (0..127 def:64) */
UINT32 dExNoteFlag; /* */
UINT32 dRegL; /* */
UINT32 dRegH; /* */
SINT32 sdOctShift; /* Octave Shift (-4..0..4 def:0) */
UINT32 dNoteOffTime; /* Note off time[tick](def:0xFFFFFFFF) */
} MAPHRCHINFO, *PMAPHRCHINFO;
typedef struct _MAPHRINFO
{
/* common */
UINT32 dCreateStatus; /* bit0:SMAF/Phrase bit1:SMAF/Audio */
UINT32 dPlayStatus;
SINT32 sdSeqId; /* Sequencer ID */
UINT32 dCtrlStatus;
UINT32 dRamAdr;
UINT32 dRamSize;
/* for Phrase */
UINT32 dSlave;
UINT32 dPhrStatus;
void (*pfnEvHandler)(struct event*);
MAPHRAPIINFO sApiInfo[MAPHR_MAX_CHANNEL];
MAPHRCHINFO sChInfo[MASMW_NUM_CHANNEL];
/* for Audio */
UINT32 dVolume;
UINT32 dPanpot;
UINT32 dPause;
UINT32 dAudStatus;
MALIBAUDINFO sAudInfo[MAPHR_MAX_AUDIO_DATA];
} MAPHRINFO, *PMAPHRINFO;
/****************************************************************************
* gloval values
****************************************************************************/
static MAPHRINFO gPhrInfo;
/* table */
static const UINT32 gdMaskPlay[5] = {
0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010
};
static const UINT32 gdMaskStart[4] = {
0x00000001, 0x00000010, 0x00000100, 0x00001000
};
static const UINT32 gdMaskPause[4] = {
0x00000004, 0x00000040, 0x00000400, 0x00004000
};
static const UINT32 gdMaskRestart[4] = {
0x00000008, 0x00000080, 0x00000800, 0x00008000
};
static const UINT32 gdMaskStop[4] = {
0x00000002, 0x00000020, 0x00000200, 0x00002000
};
static const UINT32 gdShortVolTable[16] = {
0, 0, 31, 39, 47, 55, 63, 71, 79, 87, 95, 103, 111, 119, 127, 127
};
static const UINT32 gdShortModTable[16] = {
/* +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F */
0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4
};
static const UINT32 gdNormalModTable[128] = {
/* +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F */
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
};
/****************************************************************************
* ResourceCreate
*
* Function:
* Create sequencer.
* Argument:
*
* Return:
* 0 success
* < 0 error
*
****************************************************************************/
static SINT32 ResourceCreate(void)
{
SINT32 sdSeqId;
UINT32 dRamAdr;
UINT32 dRamSize;
if(gPhrInfo.sdSeqId >= 0L) return (MASMW_ERROR);
sdSeqId = MaSndDrv_Create(
(UINT32)MASMW_SEQTYPE_DIRECT,
(UINT32)MAPHR_PHRASE_TIMEBASE,
/* 20msec */
1L, /* Resource mode */
0x07L, /* FM mode ... 4op */
/* Drum sound setting ... GM Level1 */
/* WT volume setting ... Standard mode */
1L, /* Number of Streams */
0xFFL, /* AL Channel */
MaPhrAud_Convert, /* Call back function */
&dRamAdr, /* Starting sddress of RAM */
&dRamSize); /* Size of RAM */
if(sdSeqId < 0L) {
gPhrInfo.sdSeqId = -1L;
return (sdSeqId);
}
gPhrInfo.sdSeqId = sdSeqId;
gPhrInfo.dRamAdr = dRamAdr;
gPhrInfo.dRamSize = dRamSize;
/* Set Volume */
MaSndDrv_SetVolume(sdSeqId, 0x7FL, 0x07L); /* STREAM | WT | FM */
MaSndDrv_SetVolShift(sdSeqId, 2L, 0x02L); /* WT +12dB */
/* Start Sequencer */
gPhrInfo.dCtrlStatus = MAPHR_CTRL_CLEAR_ALL;
gPhrInfo.dCtrlStatus |= MAPHR_CTRL_SYSTEM_ON;
gPhrInfo.dCtrlStatus |= MAPHR_CTRL_MASTER_VOLUME;
MaSndDrv_ControlSequencer(sdSeqId, 1);
return (MASMW_SUCCESS);
}
/****************************************************************************
* ResourceFree
*
* Function:
* Free sequencer.
* Argument:
*
* Return:
* 0 success
* < 0 error
*
****************************************************************************/
static SINT32 ResourceFree(void)
{
SINT32 sdRet;
if(gPhrInfo.sdSeqId < 0L) return (MASMW_ERROR);
/* Stop Sequencer */
MaSndDrv_ControlSequencer(gPhrInfo.sdSeqId, 0L);
/* Free Sequencer */
sdRet = MaSndDrv_Free(gPhrInfo.sdSeqId);
if(sdRet < 0L) return (sdRet);
gPhrInfo.sdSeqId = -1L;
gPhrInfo.dRamAdr = 0xFFFFFFFF;
gPhrInfo.dRamSize = 0L;
return (MASMW_SUCCESS);
}
/****************************************************************************
* ConvertMA3Voice
*
* Function:
* Convert MA-2 FM Voice Parameter to MA-3 format.
* Argument:
* pbM2V Pointer to MA-2 FM voice parameter
* dM2Size Size of MA-2 FM voice parameter
* pbM3V Pointer to MA-3 FM voice parameter
* Return:
* converted size
*
****************************************************************************/
static UINT32 ConvertMA3Voice(UINT8* pbM2V, UINT32 dM2Size, UINT8* pbM3V)
{
UINT8 bFb;
UINT8 bAlg;
UINT8 bNumofOp;
UINT8 bOp;
UINT8 bVib;
UINT8 bEgt;
UINT8 bSus;
UINT8 bKsr;
UINT8 bRR;
UINT8 bDR;
UINT8 bDvb;
UINT8 bDam;
UINT8 bAm;
UINT8 bWs;
UINT32 dSize = 0L;
bNumofOp = 4;
bFb = (UINT8)((pbM2V[3] & 0x38) >> 3);
bAlg = (UINT8)(pbM2V[3] & 0x07);
if((bAlg & 0x06) == 0) bNumofOp = 2;
if(dM2Size < MAPHR_MA2_4OP_SIZE) bNumofOp = 2;
/* panpot = 0x10 center */
pbM3V[0] = (UINT8)(0x80 | (pbM2V[4] & 0x03));
pbM3V[1] = (UINT8)((pbM2V[3] & 0xC0) | bAlg);
pbM2V += 5;
pbM3V += 2;
dSize = 2;
for(bOp = 0; bOp < bNumofOp; bOp++) {
bVib = (UINT8)((pbM2V[0] & 0x08) >> 3);
bEgt = (UINT8)((pbM2V[0] & 0x04) >> 2);
bSus = (UINT8)((pbM2V[0] & 0x02) >> 1);
bKsr = (UINT8)((pbM2V[0] & 0x01) >> 0);
bRR = (UINT8)((pbM2V[1] & 0xF0) >> 4);
bDR = (UINT8)(pbM2V[1] & 0x0F);
bDvb = (UINT8)((pbM2V[4] & 0xC0) >> 6);
bDam = (UINT8)((pbM2V[4] & 0x30) >> 4);
bAm = (UINT8)((pbM2V[4] & 0x08) >> 3);
bWs = (UINT8)(pbM2V[4] & 0x07);
/* SR | DR */
pbM3V[0] = (UINT8)((((bEgt) ? 0x00 : bRR) << 4) | 0x04 | bKsr);
/* RR | DR */
pbM3V[1] = (UINT8)((((bSus) ? 0x04 : bRR) << 4) | bDR);
/* AR | SL */
pbM3V[2] = pbM2V[2];
/* TL | KSL */
pbM3V[3] = pbM2V[3];
/* DAM | EAM | DVB | EVB */
pbM3V[4] = (UINT8)((bDam << 5) | (bAm << 4) | (bDvb << 1) | bVib);
/* MULTI | DT=0 */
pbM3V[5] = (UINT8)(pbM2V[0] & 0xF0);
/* WS | FB */
pbM3V[6] = (UINT8)((bWs << 3) | ((bOp == 0) ? bFb : 0x00));
pbM2V += 5;
pbM3V += 7;
dSize += 7;
}
return(dSize);
}
/****************************************************************************
* GetVoiceInfo
*
* Function:
* Get extend voice information.
* Argument:
* dBankNo Bank Number(1 or 129)
* dProgNo Program Number(0..127)
* pVi Pointer to the top of voice information list.
* Return:
* ! NULL Pointer to the voice information.
* NULL error
*
****************************************************************************/
static PMALIBVOCINFO GetVoiceInfo(UINT32 dBankNo, UINT32 dProgNo,
PMALIBVOCINFO pVi)
{
UINT32 dIdx;
if(pVi == NULL) return (NULL);
if((dBankNo != 0x01L) && (dBankNo != 0x81L)) return (NULL);
for(dIdx = 0; dIdx < MAPHR_MAX_LV2_VOICES; dIdx++, pVi++) {
if( (pVi->dBankNo == dBankNo) &&
(pVi->dProgNo == dProgNo)) {
return (pVi);
}
else if(pVi->dBankNo == 0xFFFFFFFFL) {
return (NULL);
}
}
return (NULL);
}
/****************************************************************************
* Velocity
*
* Function:
* Update velocity(for LV2).
* Argument:
* dCh #Channel(0..15)
* dVelocity Velocity(0..127)
* Return:
* 0 success
*
****************************************************************************/
static SINT32 Velocity(UINT32 dCh, UINT32 dVelocity)
{
if(dVelocity < 128L) {
gPhrInfo.sChInfo[dCh].dVelocity = dVelocity;
}
return (0L);
}
/****************************************************************************
* CnvOctaveShift
*
* Function:
* Update OctaveShift(for LV1).
* Argument:
* dCh #Channel(0..15)
* dOct OctaveShift(0..4, 129..132)
* Return:
* 0 success
*
****************************************************************************/
static SINT32 OctaveShift(UINT32 dCh, UINT32 dOct)
{
MAPHRCNV_DBGMSG(("OctaveShift[%d %02X] \n", dCh, dOct));
switch(dOct) {
case 0x00L:
case 0x01L:
case 0x02L:
case 0x03L:
case 0x04L:
gPhrInfo.sChInfo[dCh].sdOctShift = (SINT32)dOct;
break;
case 0x81L:
gPhrInfo.sChInfo[dCh].sdOctShift = -1L;
break;
case 0x82L:
gPhrInfo.sChInfo[dCh].sdOctShift = -2L;
break;
case 0x83L:
gPhrInfo.sChInfo[dCh].sdOctShift = -3L;
break;
case 0x84L:
gPhrInfo.sChInfo[dCh].sdOctShift = -4L;
break;
default: /* reserved */
gPhrInfo.sChInfo[dCh].sdOctShift = 0L;
break;
}
return (0L);
}
/****************************************************************************
* CnvOctaveShift2
*
* Function:
* Update OctaveShift(for LV2).
* Argument:
* dCh #Channel(0..15)
* dOct OctaveShift(0..4, 129..132)
* Return:
* 0 success
*
****************************************************************************/
static SINT32 OctaveShift2(UINT32 dCh, UINT32 dOct)
{
MAPHRCNV_DBGMSG(("OctaveShift2[%d %02X] \n", dCh, dOct));
switch(dOct) {
case 0x00L:
case 0x01L:
case 0x02L:
case 0x03L:
case 0x04L:
gPhrInfo.sChInfo[dCh].sdOctShift = (SINT32)dOct;
break;
case 0x81L:
gPhrInfo.sChInfo[dCh].sdOctShift = -1L;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -