GameSound.c
上传用户:hjhsjcl
上传日期:2020-09-25
资源大小:11378k
文件大小:7k
源码类别:

压缩解压

开发平台:

C++ Builder

  1. #define _GAME_SOUND_H_
  2. #include <DP8051XP.H>
  3. #include "TypeDef.h"
  4. #include "Const.h"
  5. #include "Common.h"
  6. #include "Reg5661.h"
  7. #include "GameSound.h"
  8. #include "GameSoundChip.h"
  9. #include "Idma.h"
  10. #include "IntCodec.h"
  11. //private variable
  12. XBYTE gxbCurrentSector;
  13. XBYTE gxbBufferStatus;
  14. XBYTE* pxbBufferAddress[2];
  15. XBYTE gxbBufferSector;
  16. XBYTE gxbCurrentVolume;
  17. XBYTE gxbDecodeStatus;
  18. BYTE InitSound(void * pBuffer0, void* pBuffer1, BYTE nFlags)
  19. {
  20.  WORD wDspVal;
  21.  unsigned int code cwSampleRate[8]=
  22.  {8000, 
  23.  11025,
  24.  16000,
  25.  22050,
  26.  24000,
  27.  32000,
  28.  44100,
  29.  48000};
  30. unsigned int code  cwFormat[2] = {0x0700,0x0200};
  31. unsigned int code  cwChannel[2] = {0x0001, 0x0002};
  32. unsigned int code  cwBlockSize[2][8] = {  {256, 256, 256, 512, 512, 512, 1024, 1024},  //mono
  33. {256, 512, 512, 1024, 1024, 1024, 2048, 2048}};  //stereo
  34. unsigned int code   cwBitPerSample[2] = {0x0008, 0x0004};
  35. unsigned int code cwCountMono[2] = { 0x0004,  // 8bit  MONO PCM
  36. 0x0008}; // 16bit MONO PCM
  37. unsigned int code cwCountStereo[2] ={ 0x0002,  // 8bit  STEREO PCM
  38. 0x0004}; // 16bit STEREO PCM
  39. #define FORMAT_FLAG (nFlags&0x01) 
  40. #define CHANNEL_FLAG ((nFlags&0x02)>>1)
  41. #define SAMPLRATE_FLAG ((nFlags&0x1C)>>2)
  42. #define BUFFERSIZE_FLAG ((nFlags&0xE0)>>5)
  43. #define DSP_NUM_CH 0x1FBB
  44. #define DSP_FORMAT_TAG 0x1FB9
  45. #define DSP_SMP_RATE 0x1FBA
  46. #define DSP_BLOCK_SIZE 0x1FB8
  47. #define DSP_FILE_OFFSET 0x11EB
  48. #define DSP_COUNT_MONO 0x1FA4
  49. #define DSP_COUNT_STEREO 0x1FA5
  50. #define DSP_BIT_PER_SMP 0x1FA6
  51.  // initial 
  52.  gxbCurrentSector = 0; // default sector 0
  53.  gxbBufferStatus = 0;  // default buffer 0
  54.  gxbDecodeStatus = GAME_SOUND_STOP;
  55.  // load informantion
  56.  pxbBufferAddress[0] = pBuffer0;  
  57.  pxbBufferAddress[1] = pBuffer1;
  58.  gxbBufferSector = (1 + BUFFERSIZE_FLAG)*2;
  59.  gxwSampleRate = cwSampleRate[SAMPLRATE_FLAG];
  60.  CdcSetSampleRate(); //tne 2006/12/26 #1
  61.  if(gxwSampleRate>16000)
  62.   obCLKDSPDIVF = 0;
  63. //MAX3111DWORD(0xaa55aa55);
  64. //MAX3111DWORD(gxbBufferSector);
  65.    wDspVal = 1;
  66. IdmaPioDmxW(&wDspVal, 0x1fbc,1,DSP_W16); //bypass HDR
  67.   wDspVal = cwFormat[FORMAT_FLAG];
  68. IdmaPioDmxW(&wDspVal, DSP_FORMAT_TAG,1,DSP_W16); //format
  69. wDspVal = cwChannel[CHANNEL_FLAG];
  70. IdmaPioDmxW(&wDspVal, DSP_NUM_CH,1,DSP_W16); //channel
  71.  
  72. wDspVal = gxwSampleRate >> 1;
  73. IdmaPioDmxW(&wDspVal, DSP_SMP_RATE,1,DSP_W16);   //sample rate
  74.  
  75. wDspVal = cwBlockSize[CHANNEL_FLAG][SAMPLRATE_FLAG];
  76. IdmaPioDmxW(&wDspVal, DSP_BLOCK_SIZE,1,DSP_W16);   // blocksize
  77.   wDspVal = cwBitPerSample[FORMAT_FLAG];
  78. IdmaPioDmxW(&wDspVal, DSP_BIT_PER_SMP,1,DSP_W16);   // bit per sample
  79. if(FORMAT_FLAG == 0)  // uLaw
  80. {
  81. if(CHANNEL_FLAG == 0)
  82. {
  83.   wDspVal = cwCountMono[0];
  84. IdmaPioDmxW(&wDspVal, DSP_COUNT_MONO,1,DSP_W16);   // count mono
  85. }
  86. else
  87. {
  88.   wDspVal = cwCountStereo[0];
  89. IdmaPioDmxW(&wDspVal, DSP_COUNT_STEREO,1,DSP_W16);   // count stereo
  90. }
  91. }
  92. //wDspVal = 2;
  93. //IdmaPioDmxW(&wDspVal, 0x11a6,1,DSP_W16);   // count stereo
  94.    //wDspVal = 0x0160;
  95.  //IdmaPioDmxW(&wDspVal, 0x1ff3,1,DSP_W16);   // bit rate
  96. return 1;
  97. }
  98. BYTE SetSoundVolume(BYTE bVol)
  99. {
  100.  gxbCurrentVolume = bVol;
  101.  GameSoundChipSetVolume(bVol);
  102.  return 1;
  103. }
  104. BYTE GetSoundVolume()
  105. {
  106.  return gxbCurrentVolume;
  107. }
  108. BYTE PlaySound()
  109. {
  110.  volatile unsigned char code cbStateChange[4] = { 0x00, 
  111.   GAME_SOUND_PLAY,  //  GAME_SOUND_STOP
  112. GAME_SOUND_PLAY,  //  GAME_SOUND_PAUSE
  113. GAME_SOUND_PLAY}; //  GAME_SOUND_PLAY
  114.  ClkDspDis();
  115.  MCU_ACCESS_CODEC_I2S_EN();   //Renshuo050216#A
  116.  obALGPAREG = 0x00;
  117.  MCU_ACCESS_CODEC_I2S_DIS();  //Renshuo050216#A
  118.  ClkDspEn();
  119.  gxbDecodeStatus = cbStateChange[gxbDecodeStatus];
  120.  return 1;
  121. }
  122. BYTE PauseSound()
  123. {
  124.  volatile unsigned char code cbStateChange[4] = { 0x00, 
  125.   GAME_SOUND_STOP,  //  GAME_SOUND_STOP
  126. GAME_SOUND_PAUSE,  //  GAME_SOUND_PAUSE
  127. GAME_SOUND_PAUSE}; //  GAME_SOUND_PLAY
  128.  gxbDecodeStatus = cbStateChange[gxbDecodeStatus];
  129.  ClkDspDis();
  130.  MCU_ACCESS_CODEC_I2S_EN();   //Renshuo050216#A
  131.  obALGPAREG = 0xb8;
  132.  obDIGCTRL1 |= DAC_FADE_OUT_EN;
  133.  MCU_ACCESS_CODEC_I2S_DIS();  //Renshuo050216#A
  134.  ClkDspEn();
  135.  return 1;
  136. }
  137. BYTE ResumeSound()
  138. {
  139.  volatile unsigned char code cbStateChange[4] = { 0x00, 
  140.   GAME_SOUND_STOP,  //  GAME_SOUND_STOP
  141. GAME_SOUND_PLAY,  //  GAME_SOUND_PAUSE
  142. GAME_SOUND_PLAY}; //  GAME_SOUND_PLAY
  143.  gxbDecodeStatus = cbStateChange[gxbDecodeStatus];
  144. ClkDspDis();
  145. MCU_ACCESS_CODEC_I2S_EN();   //Renshuo050216#A
  146. obDIGCTRL2 = CDC_FIFO_CLR;  //Renshuo050126#A clear FIFO avoid LR exchange
  147. obDIGCTRL2 = CDC_EN;
  148.  obALGPAREG = 0x00;
  149. obDIGCTRL1 &= DAC_FADE_OUT_DISJ;
  150. MCU_ACCESS_CODEC_I2S_DIS();  //Renshuo050216#A
  151. ClkDspEn();
  152. return 1;
  153. }
  154. BYTE StopSound()
  155. {
  156.   //ORD wDspVal;
  157.  volatile unsigned char code cbStateChange[4] = { 0x00, 
  158.   GAME_SOUND_STOP,  //  GAME_SOUND_STOP
  159. GAME_SOUND_STOP,  //  GAME_SOUND_PAUSE
  160. GAME_SOUND_STOP}; //  GAME_SOUND_PLAY
  161. /*
  162. wDspVal = TRUE;
  163. IdmaPioDmxW(&wDspVal, DSP_REW_EN_ADDR, 1, DSP_W16);
  164. while(wDspVal) //wait dsp reset state
  165. {
  166.   IdmaPioDmxR(DSP_REW_EN_ADDR,&wDspVal,1,DSP_W16); //dsp reset ready
  167. }
  168. wDspVal = DSP_FADE_IN_EN|DSP_FADE_FORCE|DSP_FADE_1_UNIT;
  169. IdmaPioDmxW(&wDspVal,DSP_FADE_ADDR,1,DSP_W16);
  170. */
  171.  ClkDspDis();
  172.  MCU_ACCESS_CODEC_I2S_EN();   //Renshuo050216#A
  173.  obALGPAREG = 0xb8;
  174.  MCU_ACCESS_CODEC_I2S_DIS();  //Renshuo050216#A
  175.  ClkDspEn();
  176.  gxbDecodeStatus = cbStateChange[gxbDecodeStatus];
  177.  return 1;
  178. }
  179. BYTE GetSoundStatus()
  180. {
  181. BYTE bRet = gxbDecodeStatus;
  182. if(gxbDecodeStatus != GAME_SOUND_STOP)
  183. {
  184. if(gxbBufferStatus&0x01)
  185. {
  186.   bRet |= 0x08;
  187. }
  188. else
  189. {
  190.   bRet |= 0x04;
  191. }
  192. }
  193.     return bRet;
  194. }
  195. //gxbBufferStatus
  196. // b0: current buffer
  197. BYTE GameSoundFillDspBuffer()
  198. {
  199. if(gxbDecodeStatus == GAME_SOUND_PLAY)
  200. {
  201. if(GameSoundChipPollDspBuffer())
  202. {
  203.   if(gxbCurrentSector >= gxbBufferSector) //current buffer is out
  204.   {
  205.    gxbBufferStatus ^= (0x01);  // switch to buffer 1
  206.    gxbCurrentSector = 0;    // reset pointer
  207.   }
  208. /*
  209. MAX3111DWORD(0xccccdddd);
  210. MAX3111DWORD(gxbBufferStatus);
  211. MAX3111DWORD(gxbCurrentSector);
  212. */
  213.      GameSoundChipFillDspBuffer(pxbBufferAddress[gxbBufferStatus&0x01]+gxbCurrentSector*512);
  214.      gxbCurrentSector ++;
  215. }
  216. }
  217. return 1;
  218. }
  219. BYTE CopyBuffer(BYTE * pbSource, BYTE*  pbTarget, WORD wLen)
  220. {
  221.  for( ;wLen>0 ; wLen--) 
  222.  {
  223.   *pbTarget = *pbSource;
  224.   pbTarget++;
  225.   pbSource++;
  226.  }
  227.  return 1;
  228. }
  229. BYTE GameSoundFillMcuBuffer(BYTE* pbBuffer, WORD wSector)
  230. {
  231. NandReadPhySec(wSector,1,SRAM_DATA_ADDR);
  232. CopyBuffer(gxbFsData, pbBuffer, 512);
  233. //MAX3111DWORD(0xDDDDDDDD);
  234. //MAX3111Dump(pbBuffer, 8);
  235. NandReadPhySec(wSector+1,1,SRAM_DATA_ADDR);
  236. CopyBuffer(gxbFsData, pbBuffer+512, 512);
  237. return 1;
  238. }
  239. BYTE GameSoundGetBinSector(BYTE bBinNumber, WORD* wSec, WORD* wLen)
  240. {
  241. NandReadPhySec(gxwAddrPMI,1,SRAM_DATA_ADDR);
  242. *wSec = ((WORD)gxbFsData[bBinNumber*4 + 5] << 8) | gxbFsData[bBinNumber*4 + 4];
  243. *wLen = ((WORD)gxbFsData[bBinNumber*4 + 7] << 8) | gxbFsData[bBinNumber*4 + 6];
  244. return 1;
  245. // *wSec = (WORD)((WORD*)(gxbFsData + bBinNumber*4 + 4));
  246. // *wLen = (WORD)((WORD*)(gxbFsData + bBinNumber*4 + 6));
  247. }