PageRenderTime 118ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/libmodplug/src/sndfile.cpp

https://github.com/MaDDoGo/xbmc
C++ | 1885 lines | 1618 code | 145 blank | 122 comment | 441 complexity | 7735a1558fb0ae17b77fb28f713a930d MD5 | raw file
  1. /*
  2. * This source code is public domain.
  3. *
  4. * Authors: Olivier Lapicque <olivierl@jps.net>,
  5. * Adam Goode <adam@evdebs.org> (endian and char fixes for PPC)
  6. */
  7. #include <math.h> //for GCCFIX
  8. #include "libmodplug/stdafx.h"
  9. #include "libmodplug/sndfile.h"
  10. #define MMCMP_SUPPORT
  11. #ifdef MMCMP_SUPPORT
  12. extern BOOL MMCMP_Unpack(LPCBYTE *ppMemFile, LPDWORD pdwMemLength);
  13. #endif
  14. // External decompressors
  15. extern void AMSUnpack(const char *psrc, UINT inputlen, char *pdest, UINT dmax, char packcharacter);
  16. extern WORD MDLReadBits(DWORD &bitbuf, UINT &bitnum, LPBYTE &ibuf, CHAR n);
  17. extern int DMFUnpack(LPBYTE psample, LPBYTE ibuf, LPBYTE ibufmax, UINT maxlen);
  18. extern DWORD ITReadBits(DWORD &bitbuf, UINT &bitnum, LPBYTE &ibuf, CHAR n);
  19. extern void ITUnpack8Bit(signed char *pSample, DWORD dwLen, LPBYTE lpMemFile, DWORD dwMemLength, BOOL b215);
  20. extern void ITUnpack16Bit(signed char *pSample, DWORD dwLen, LPBYTE lpMemFile, DWORD dwMemLength, BOOL b215);
  21. #define MAX_PACK_TABLES 3
  22. // Compression table
  23. static const signed char UnpackTable[MAX_PACK_TABLES][16] =
  24. //--------------------------------------------
  25. {
  26. // CPU-generated dynamic table
  27. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  28. // u-Law table
  29. {0, 1, 2, 4, 8, 16, 32, 64,
  30. -1, -2, -4, -8, -16, -32, -48, -64},
  31. // Linear table
  32. {0, 1, 2, 3, 5, 7, 12, 19,
  33. -1, -2, -3, -5, -7, -12, -19, -31}
  34. };
  35. //////////////////////////////////////////////////////////
  36. // CSoundFile
  37. CSoundFile::CSoundFile()
  38. //----------------------
  39. {
  40. m_nType = MOD_TYPE_NONE;
  41. m_dwSongFlags = 0;
  42. m_nChannels = 0;
  43. m_nMixChannels = 0;
  44. m_nSamples = 0;
  45. m_nInstruments = 0;
  46. m_nPatternNames = 0;
  47. m_lpszPatternNames = NULL;
  48. m_lpszSongComments = NULL;
  49. m_nFreqFactor = m_nTempoFactor = 128;
  50. m_nMasterVolume = 128;
  51. m_nMinPeriod = 0x20;
  52. m_nMaxPeriod = 0x7FFF;
  53. m_nRepeatCount = 0;
  54. memset(Chn, 0, sizeof(Chn));
  55. memset(ChnMix, 0, sizeof(ChnMix));
  56. memset(Ins, 0, sizeof(Ins));
  57. memset(ChnSettings, 0, sizeof(ChnSettings));
  58. memset(Headers, 0, sizeof(Headers));
  59. memset(Order, 0xFF, sizeof(Order));
  60. memset(Patterns, 0, sizeof(Patterns));
  61. memset(m_szNames, 0, sizeof(m_szNames));
  62. memset(m_MixPlugins, 0, sizeof(m_MixPlugins));
  63. }
  64. CSoundFile::~CSoundFile()
  65. //-----------------------
  66. {
  67. Destroy();
  68. }
  69. BOOL CSoundFile::Create(LPCBYTE lpStream, DWORD dwMemLength)
  70. //----------------------------------------------------------
  71. {
  72. int i;
  73. m_nType = MOD_TYPE_NONE;
  74. m_dwSongFlags = 0;
  75. m_nChannels = 0;
  76. m_nMixChannels = 0;
  77. m_nSamples = 0;
  78. m_nInstruments = 0;
  79. m_nFreqFactor = m_nTempoFactor = 128;
  80. m_nMasterVolume = 128;
  81. m_nDefaultGlobalVolume = 256;
  82. m_nGlobalVolume = 256;
  83. m_nOldGlbVolSlide = 0;
  84. m_nDefaultSpeed = 6;
  85. m_nDefaultTempo = 125;
  86. m_nPatternDelay = 0;
  87. m_nFrameDelay = 0;
  88. m_nNextRow = 0;
  89. m_nRow = 0;
  90. m_nPattern = 0;
  91. m_nCurrentPattern = 0;
  92. m_nNextPattern = 0;
  93. m_nRestartPos = 0;
  94. m_nMinPeriod = 16;
  95. m_nMaxPeriod = 32767;
  96. m_nSongPreAmp = 0x30;
  97. m_nPatternNames = 0;
  98. m_nMaxOrderPosition = 0;
  99. m_lpszPatternNames = NULL;
  100. m_lpszSongComments = NULL;
  101. memset(Ins, 0, sizeof(Ins));
  102. memset(ChnMix, 0, sizeof(ChnMix));
  103. memset(Chn, 0, sizeof(Chn));
  104. memset(Headers, 0, sizeof(Headers));
  105. memset(Order, 0xFF, sizeof(Order));
  106. memset(Patterns, 0, sizeof(Patterns));
  107. memset(m_szNames, 0, sizeof(m_szNames));
  108. memset(m_MixPlugins, 0, sizeof(m_MixPlugins));
  109. ResetMidiCfg();
  110. for (UINT npt=0; npt<MAX_PATTERNS; npt++) PatternSize[npt] = 64;
  111. for (UINT nch=0; nch<MAX_BASECHANNELS; nch++)
  112. {
  113. ChnSettings[nch].nPan = 128;
  114. ChnSettings[nch].nVolume = 64;
  115. ChnSettings[nch].dwFlags = 0;
  116. ChnSettings[nch].szName[0] = 0;
  117. }
  118. if (lpStream)
  119. {
  120. #ifdef MMCMP_SUPPORT
  121. BOOL bMMCmp = MMCMP_Unpack(&lpStream, &dwMemLength);
  122. #endif
  123. if ((!ReadXM(lpStream, dwMemLength))
  124. && (!ReadS3M(lpStream, dwMemLength))
  125. && (!ReadIT(lpStream, dwMemLength))
  126. && (!ReadWav(lpStream, dwMemLength))
  127. #ifndef MODPLUG_BASIC_SUPPORT
  128. /* Sequencer File Format Support */
  129. && (!ReadABC(lpStream, dwMemLength))
  130. && (!ReadMID(lpStream, dwMemLength))
  131. && (!ReadPAT(lpStream, dwMemLength))
  132. && (!ReadSTM(lpStream, dwMemLength))
  133. && (!ReadMed(lpStream, dwMemLength))
  134. && (!ReadMTM(lpStream, dwMemLength))
  135. && (!ReadMDL(lpStream, dwMemLength))
  136. && (!ReadDBM(lpStream, dwMemLength))
  137. && (!Read669(lpStream, dwMemLength))
  138. && (!ReadFAR(lpStream, dwMemLength))
  139. && (!ReadAMS(lpStream, dwMemLength))
  140. && (!ReadOKT(lpStream, dwMemLength))
  141. && (!ReadPTM(lpStream, dwMemLength))
  142. && (!ReadUlt(lpStream, dwMemLength))
  143. && (!ReadDMF(lpStream, dwMemLength))
  144. && (!ReadDSM(lpStream, dwMemLength))
  145. && (!ReadUMX(lpStream, dwMemLength))
  146. && (!ReadAMF(lpStream, dwMemLength))
  147. && (!ReadPSM(lpStream, dwMemLength))
  148. && (!ReadMT2(lpStream, dwMemLength))
  149. #endif // MODPLUG_BASIC_SUPPORT
  150. && (!ReadMod(lpStream, dwMemLength))) m_nType = MOD_TYPE_NONE;
  151. #ifdef MMCMP_SUPPORT
  152. if (bMMCmp)
  153. {
  154. GlobalFreePtr(lpStream);
  155. lpStream = NULL;
  156. }
  157. #endif
  158. }
  159. // Adjust song names
  160. for (i=0; i<MAX_SAMPLES; i++)
  161. {
  162. LPSTR p = m_szNames[i];
  163. int j = 31;
  164. p[j] = 0;
  165. while ((j>=0) && (p[j]<=' ')) p[j--] = 0;
  166. while (j>=0)
  167. {
  168. if (((BYTE)p[j]) < ' ') p[j] = ' ';
  169. j--;
  170. }
  171. }
  172. // Adjust channels
  173. for (i=0; i<MAX_BASECHANNELS; i++)
  174. {
  175. if (ChnSettings[i].nVolume > 64) ChnSettings[i].nVolume = 64;
  176. if (ChnSettings[i].nPan > 256) ChnSettings[i].nPan = 128;
  177. Chn[i].nPan = ChnSettings[i].nPan;
  178. Chn[i].nGlobalVol = ChnSettings[i].nVolume;
  179. Chn[i].dwFlags = ChnSettings[i].dwFlags;
  180. Chn[i].nVolume = 256;
  181. Chn[i].nCutOff = 0x7F;
  182. }
  183. // Checking instruments
  184. MODINSTRUMENT *pins = Ins;
  185. for (i=0; i<MAX_INSTRUMENTS; i++, pins++)
  186. {
  187. if (pins->pSample)
  188. {
  189. if (pins->nLoopEnd > pins->nLength) pins->nLoopEnd = pins->nLength;
  190. if (pins->nLoopStart + 3 >= pins->nLoopEnd)
  191. {
  192. pins->nLoopStart = 0;
  193. pins->nLoopEnd = 0;
  194. }
  195. if (pins->nSustainEnd > pins->nLength) pins->nSustainEnd = pins->nLength;
  196. if (pins->nSustainStart + 3 >= pins->nSustainEnd)
  197. {
  198. pins->nSustainStart = 0;
  199. pins->nSustainEnd = 0;
  200. }
  201. } else
  202. {
  203. pins->nLength = 0;
  204. pins->nLoopStart = 0;
  205. pins->nLoopEnd = 0;
  206. pins->nSustainStart = 0;
  207. pins->nSustainEnd = 0;
  208. }
  209. if (!pins->nLoopEnd) pins->uFlags &= ~CHN_LOOP;
  210. if (!pins->nSustainEnd) pins->uFlags &= ~CHN_SUSTAINLOOP;
  211. if (pins->nGlobalVol > 64) pins->nGlobalVol = 64;
  212. }
  213. // Check invalid instruments
  214. while ((m_nInstruments > 0) && (!Headers[m_nInstruments]))
  215. m_nInstruments--;
  216. // Set default values
  217. if (m_nSongPreAmp < 0x20) m_nSongPreAmp = 0x20;
  218. if (m_nDefaultTempo < 32) m_nDefaultTempo = 125;
  219. if (!m_nDefaultSpeed) m_nDefaultSpeed = 6;
  220. m_nMusicSpeed = m_nDefaultSpeed;
  221. m_nMusicTempo = m_nDefaultTempo;
  222. m_nGlobalVolume = m_nDefaultGlobalVolume;
  223. m_nNextPattern = 0;
  224. m_nCurrentPattern = 0;
  225. m_nPattern = 0;
  226. m_nBufferCount = 0;
  227. m_nTickCount = m_nMusicSpeed;
  228. m_nNextRow = 0;
  229. m_nRow = 0;
  230. if ((m_nRestartPos >= MAX_ORDERS) || (Order[m_nRestartPos] >= MAX_PATTERNS)) m_nRestartPos = 0;
  231. // Load plugins
  232. if (gpMixPluginCreateProc)
  233. {
  234. for (UINT iPlug=0; iPlug<MAX_MIXPLUGINS; iPlug++)
  235. {
  236. if ((m_MixPlugins[iPlug].Info.dwPluginId1)
  237. || (m_MixPlugins[iPlug].Info.dwPluginId2))
  238. {
  239. gpMixPluginCreateProc(&m_MixPlugins[iPlug]);
  240. if (m_MixPlugins[iPlug].pMixPlugin)
  241. {
  242. m_MixPlugins[iPlug].pMixPlugin->RestoreAllParameters();
  243. }
  244. }
  245. }
  246. }
  247. if (m_nType)
  248. {
  249. UINT maxpreamp = 0x10+(m_nChannels*8);
  250. if (maxpreamp > 100) maxpreamp = 100;
  251. if (m_nSongPreAmp > maxpreamp) m_nSongPreAmp = maxpreamp;
  252. return TRUE;
  253. }
  254. return FALSE;
  255. }
  256. BOOL CSoundFile::Destroy()
  257. //------------------------
  258. {
  259. int i;
  260. for (i=0; i<MAX_PATTERNS; i++) if (Patterns[i])
  261. {
  262. FreePattern(Patterns[i]);
  263. Patterns[i] = NULL;
  264. }
  265. m_nPatternNames = 0;
  266. if (m_lpszPatternNames)
  267. {
  268. delete m_lpszPatternNames;
  269. m_lpszPatternNames = NULL;
  270. }
  271. if (m_lpszSongComments)
  272. {
  273. delete m_lpszSongComments;
  274. m_lpszSongComments = NULL;
  275. }
  276. for (i=1; i<MAX_SAMPLES; i++)
  277. {
  278. MODINSTRUMENT *pins = &Ins[i];
  279. if (pins->pSample)
  280. {
  281. FreeSample(pins->pSample);
  282. pins->pSample = NULL;
  283. }
  284. }
  285. for (i=0; i<MAX_INSTRUMENTS; i++)
  286. {
  287. if (Headers[i])
  288. {
  289. delete Headers[i];
  290. Headers[i] = NULL;
  291. }
  292. }
  293. for (i=0; i<MAX_MIXPLUGINS; i++)
  294. {
  295. if ((m_MixPlugins[i].nPluginDataSize) && (m_MixPlugins[i].pPluginData))
  296. {
  297. m_MixPlugins[i].nPluginDataSize = 0;
  298. delete [] (signed char*)m_MixPlugins[i].pPluginData;
  299. m_MixPlugins[i].pPluginData = NULL;
  300. }
  301. m_MixPlugins[i].pMixState = NULL;
  302. if (m_MixPlugins[i].pMixPlugin)
  303. {
  304. m_MixPlugins[i].pMixPlugin->Release();
  305. m_MixPlugins[i].pMixPlugin = NULL;
  306. }
  307. }
  308. m_nType = MOD_TYPE_NONE;
  309. m_nChannels = m_nSamples = m_nInstruments = 0;
  310. return TRUE;
  311. }
  312. //////////////////////////////////////////////////////////////////////////
  313. // Memory Allocation
  314. MODCOMMAND *CSoundFile::AllocatePattern(UINT rows, UINT nchns)
  315. //------------------------------------------------------------
  316. {
  317. MODCOMMAND *p = new MODCOMMAND[rows*nchns];
  318. if (p) memset(p, 0, rows*nchns*sizeof(MODCOMMAND));
  319. return p;
  320. }
  321. void CSoundFile::FreePattern(LPVOID pat)
  322. //--------------------------------------
  323. {
  324. if (pat) delete [] (signed char*)pat;
  325. }
  326. signed char* CSoundFile::AllocateSample(UINT nbytes)
  327. //-------------------------------------------
  328. {
  329. signed char * p = (signed char *)GlobalAllocPtr(GHND, (nbytes+39) & ~7);
  330. if (p) p += 16;
  331. return p;
  332. }
  333. void CSoundFile::FreeSample(LPVOID p)
  334. //-----------------------------------
  335. {
  336. if (p)
  337. {
  338. GlobalFreePtr(((LPSTR)p)-16);
  339. }
  340. }
  341. //////////////////////////////////////////////////////////////////////////
  342. // Misc functions
  343. void CSoundFile::ResetMidiCfg()
  344. //-----------------------------
  345. {
  346. memset(&m_MidiCfg, 0, sizeof(m_MidiCfg));
  347. lstrcpy(&m_MidiCfg.szMidiGlb[MIDIOUT_START*32], "FF");
  348. lstrcpy(&m_MidiCfg.szMidiGlb[MIDIOUT_STOP*32], "FC");
  349. lstrcpy(&m_MidiCfg.szMidiGlb[MIDIOUT_NOTEON*32], "9c n v");
  350. lstrcpy(&m_MidiCfg.szMidiGlb[MIDIOUT_NOTEOFF*32], "9c n 0");
  351. lstrcpy(&m_MidiCfg.szMidiGlb[MIDIOUT_PROGRAM*32], "Cc p");
  352. lstrcpy(&m_MidiCfg.szMidiSFXExt[0], "F0F000z");
  353. for (int iz=0; iz<16; iz++) wsprintf(&m_MidiCfg.szMidiZXXExt[iz*32], "F0F001%02X", iz*8);
  354. }
  355. UINT CSoundFile::GetNumChannels() const
  356. //-------------------------------------
  357. {
  358. UINT n = 0;
  359. for (UINT i=0; i<m_nChannels; i++) if (ChnSettings[i].nVolume) n++;
  360. return n;
  361. }
  362. UINT CSoundFile::GetSongComments(LPSTR s, UINT len, UINT linesize)
  363. //----------------------------------------------------------------
  364. {
  365. LPCSTR p = m_lpszSongComments;
  366. if (!p) return 0;
  367. UINT i = 2, ln=0;
  368. if ((len) && (s)) s[0] = '\x0D';
  369. if ((len > 1) && (s)) s[1] = '\x0A';
  370. while ((*p) && (i+2 < len))
  371. {
  372. BYTE c = (BYTE)*p++;
  373. if ((c == 0x0D) || ((c == ' ') && (ln >= linesize)))
  374. { if (s) { s[i++] = '\x0D'; s[i++] = '\x0A'; } else i+= 2; ln=0; }
  375. else
  376. if (c >= 0x20) { if (s) s[i++] = c; else i++; ln++; }
  377. }
  378. if (s) s[i] = 0;
  379. return i;
  380. }
  381. UINT CSoundFile::GetRawSongComments(LPSTR s, UINT len, UINT linesize)
  382. //-------------------------------------------------------------------
  383. {
  384. LPCSTR p = m_lpszSongComments;
  385. if (!p) return 0;
  386. UINT i = 0, ln=0;
  387. while ((*p) && (i < len-1))
  388. {
  389. BYTE c = (BYTE)*p++;
  390. if ((c == 0x0D) || (c == 0x0A))
  391. {
  392. if (ln)
  393. {
  394. while (ln < linesize) { if (s) s[i] = ' '; i++; ln++; }
  395. ln = 0;
  396. }
  397. } else
  398. if ((c == ' ') && (!ln))
  399. {
  400. UINT k=0;
  401. while ((p[k]) && (p[k] >= ' ')) k++;
  402. if (k <= linesize)
  403. {
  404. if (s) s[i] = ' ';
  405. i++;
  406. ln++;
  407. }
  408. } else
  409. {
  410. if (s) s[i] = c;
  411. i++;
  412. ln++;
  413. if (ln == linesize) ln = 0;
  414. }
  415. }
  416. if (ln)
  417. {
  418. while ((ln < linesize) && (i < len))
  419. {
  420. if (s) s[i] = ' ';
  421. i++;
  422. ln++;
  423. }
  424. }
  425. if (s) s[i] = 0;
  426. return i;
  427. }
  428. BOOL CSoundFile::SetWaveConfig(UINT nRate,UINT nBits,UINT nChannels,BOOL bMMX)
  429. //----------------------------------------------------------------------------
  430. {
  431. BOOL bReset = FALSE;
  432. DWORD d = gdwSoundSetup & ~SNDMIX_ENABLEMMX;
  433. if (bMMX) d |= SNDMIX_ENABLEMMX;
  434. if ((gdwMixingFreq != nRate) || (gnBitsPerSample != nBits) || (gnChannels != nChannels) || (d != gdwSoundSetup)) bReset = TRUE;
  435. gnChannels = nChannels;
  436. gdwSoundSetup = d;
  437. gdwMixingFreq = nRate;
  438. gnBitsPerSample = nBits;
  439. InitPlayer(bReset);
  440. return TRUE;
  441. }
  442. BOOL CSoundFile::SetResamplingMode(UINT nMode)
  443. //--------------------------------------------
  444. {
  445. DWORD d = gdwSoundSetup & ~(SNDMIX_NORESAMPLING|SNDMIX_HQRESAMPLER|SNDMIX_ULTRAHQSRCMODE);
  446. switch(nMode)
  447. {
  448. case SRCMODE_NEAREST: d |= SNDMIX_NORESAMPLING; break;
  449. case SRCMODE_LINEAR: break;
  450. case SRCMODE_SPLINE: d |= SNDMIX_HQRESAMPLER; break;
  451. case SRCMODE_POLYPHASE: d |= (SNDMIX_HQRESAMPLER|SNDMIX_ULTRAHQSRCMODE); break;
  452. default:
  453. return FALSE;
  454. }
  455. gdwSoundSetup = d;
  456. return TRUE;
  457. }
  458. BOOL CSoundFile::SetMasterVolume(UINT nVol, BOOL bAdjustAGC)
  459. //----------------------------------------------------------
  460. {
  461. if (nVol < 1) nVol = 1;
  462. if (nVol > 0x200) nVol = 0x200; // x4 maximum
  463. if ((nVol < m_nMasterVolume) && (nVol) && (gdwSoundSetup & SNDMIX_AGC) && (bAdjustAGC))
  464. {
  465. gnAGC = gnAGC * m_nMasterVolume / nVol;
  466. if (gnAGC > AGC_UNITY) gnAGC = AGC_UNITY;
  467. }
  468. m_nMasterVolume = nVol;
  469. return TRUE;
  470. }
  471. void CSoundFile::SetAGC(BOOL b)
  472. //-----------------------------
  473. {
  474. if (b)
  475. {
  476. if (!(gdwSoundSetup & SNDMIX_AGC))
  477. {
  478. gdwSoundSetup |= SNDMIX_AGC;
  479. gnAGC = AGC_UNITY;
  480. }
  481. } else gdwSoundSetup &= ~SNDMIX_AGC;
  482. }
  483. UINT CSoundFile::GetNumPatterns() const
  484. //-------------------------------------
  485. {
  486. UINT i = 0;
  487. while ((i < MAX_ORDERS) && (Order[i] < 0xFF)) i++;
  488. return i;
  489. }
  490. UINT CSoundFile::GetNumInstruments() const
  491. //----------------------------------------
  492. {
  493. UINT n=0;
  494. for (UINT i=0; i<MAX_INSTRUMENTS; i++) if (Ins[i].pSample) n++;
  495. return n;
  496. }
  497. UINT CSoundFile::GetMaxPosition() const
  498. //-------------------------------------
  499. {
  500. UINT max = 0;
  501. UINT i = 0;
  502. while ((i < MAX_ORDERS) && (Order[i] != 0xFF))
  503. {
  504. if (Order[i] < MAX_PATTERNS) max += PatternSize[Order[i]];
  505. i++;
  506. }
  507. return max;
  508. }
  509. UINT CSoundFile::GetCurrentPos() const
  510. //------------------------------------
  511. {
  512. UINT pos = 0;
  513. for (UINT i=0; i<m_nCurrentPattern; i++) if (Order[i] < MAX_PATTERNS)
  514. pos += PatternSize[Order[i]];
  515. return pos + m_nRow;
  516. }
  517. void CSoundFile::SetCurrentPos(UINT nPos)
  518. //---------------------------------------
  519. {
  520. UINT i, nPattern;
  521. for (i=0; i<MAX_CHANNELS; i++)
  522. {
  523. Chn[i].nNote = Chn[i].nNewNote = Chn[i].nNewIns = 0;
  524. Chn[i].pInstrument = NULL;
  525. Chn[i].pHeader = NULL;
  526. Chn[i].nPortamentoDest = 0;
  527. Chn[i].nCommand = 0;
  528. Chn[i].nPatternLoopCount = 0;
  529. Chn[i].nPatternLoop = 0;
  530. Chn[i].nFadeOutVol = 0;
  531. Chn[i].dwFlags |= CHN_KEYOFF|CHN_NOTEFADE;
  532. Chn[i].nTremorCount = 0;
  533. }
  534. if (!nPos)
  535. {
  536. for (i=0; i<MAX_CHANNELS; i++)
  537. {
  538. Chn[i].nPeriod = 0;
  539. Chn[i].nPos = Chn[i].nLength = 0;
  540. Chn[i].nLoopStart = 0;
  541. Chn[i].nLoopEnd = 0;
  542. Chn[i].nROfs = Chn[i].nLOfs = 0;
  543. Chn[i].pSample = NULL;
  544. Chn[i].pInstrument = NULL;
  545. Chn[i].pHeader = NULL;
  546. Chn[i].nCutOff = 0x7F;
  547. Chn[i].nResonance = 0;
  548. Chn[i].nLeftVol = Chn[i].nRightVol = 0;
  549. Chn[i].nNewLeftVol = Chn[i].nNewRightVol = 0;
  550. Chn[i].nLeftRamp = Chn[i].nRightRamp = 0;
  551. Chn[i].nVolume = 256;
  552. if (i < MAX_BASECHANNELS)
  553. {
  554. Chn[i].dwFlags = ChnSettings[i].dwFlags;
  555. Chn[i].nPan = ChnSettings[i].nPan;
  556. Chn[i].nGlobalVol = ChnSettings[i].nVolume;
  557. } else
  558. {
  559. Chn[i].dwFlags = 0;
  560. Chn[i].nPan = 128;
  561. Chn[i].nGlobalVol = 64;
  562. }
  563. }
  564. m_nGlobalVolume = m_nDefaultGlobalVolume;
  565. m_nMusicSpeed = m_nDefaultSpeed;
  566. m_nMusicTempo = m_nDefaultTempo;
  567. }
  568. m_dwSongFlags &= ~(SONG_PATTERNLOOP|SONG_CPUVERYHIGH|SONG_FADINGSONG|SONG_ENDREACHED|SONG_GLOBALFADE);
  569. for (nPattern = 0; nPattern < MAX_ORDERS; nPattern++)
  570. {
  571. UINT ord = Order[nPattern];
  572. if (ord == 0xFE) continue;
  573. if (ord == 0xFF) break;
  574. if (ord < MAX_PATTERNS)
  575. {
  576. if (nPos < (UINT)PatternSize[ord]) break;
  577. nPos -= PatternSize[ord];
  578. }
  579. }
  580. // Buggy position ?
  581. if ((nPattern >= MAX_ORDERS)
  582. || (Order[nPattern] >= MAX_PATTERNS)
  583. || (nPos >= PatternSize[Order[nPattern]]))
  584. {
  585. nPos = 0;
  586. nPattern = 0;
  587. }
  588. UINT nRow = nPos;
  589. if ((nRow) && (Order[nPattern] < MAX_PATTERNS))
  590. {
  591. MODCOMMAND *p = Patterns[Order[nPattern]];
  592. if ((p) && (nRow < PatternSize[Order[nPattern]]))
  593. {
  594. BOOL bOk = FALSE;
  595. while ((!bOk) && (nRow > 0))
  596. {
  597. UINT n = nRow * m_nChannels;
  598. for (UINT k=0; k<m_nChannels; k++, n++)
  599. {
  600. if (p[n].note)
  601. {
  602. bOk = TRUE;
  603. break;
  604. }
  605. }
  606. if (!bOk) nRow--;
  607. }
  608. }
  609. }
  610. m_nNextPattern = nPattern;
  611. m_nNextRow = nRow;
  612. m_nTickCount = m_nMusicSpeed;
  613. m_nBufferCount = 0;
  614. m_nPatternDelay = 0;
  615. m_nFrameDelay = 0;
  616. }
  617. void CSoundFile::SetCurrentOrder(UINT nPos)
  618. //-----------------------------------------
  619. {
  620. while ((nPos < MAX_ORDERS) && (Order[nPos] == 0xFE)) nPos++;
  621. if ((nPos >= MAX_ORDERS) || (Order[nPos] >= MAX_PATTERNS)) return;
  622. for (UINT j=0; j<MAX_CHANNELS; j++)
  623. {
  624. Chn[j].nPeriod = 0;
  625. Chn[j].nNote = 0;
  626. Chn[j].nPortamentoDest = 0;
  627. Chn[j].nCommand = 0;
  628. Chn[j].nPatternLoopCount = 0;
  629. Chn[j].nPatternLoop = 0;
  630. Chn[j].nTremorCount = 0;
  631. }
  632. if (!nPos)
  633. {
  634. SetCurrentPos(0);
  635. } else
  636. {
  637. m_nNextPattern = nPos;
  638. m_nRow = m_nNextRow = 0;
  639. m_nPattern = 0;
  640. m_nTickCount = m_nMusicSpeed;
  641. m_nBufferCount = 0;
  642. m_nTotalCount = 0;
  643. m_nPatternDelay = 0;
  644. m_nFrameDelay = 0;
  645. }
  646. m_dwSongFlags &= ~(SONG_PATTERNLOOP|SONG_CPUVERYHIGH|SONG_FADINGSONG|SONG_ENDREACHED|SONG_GLOBALFADE);
  647. }
  648. void CSoundFile::ResetChannels()
  649. //------------------------------
  650. {
  651. m_dwSongFlags &= ~(SONG_CPUVERYHIGH|SONG_FADINGSONG|SONG_ENDREACHED|SONG_GLOBALFADE);
  652. m_nBufferCount = 0;
  653. for (UINT i=0; i<MAX_CHANNELS; i++)
  654. {
  655. Chn[i].nROfs = Chn[i].nLOfs = 0;
  656. }
  657. }
  658. void CSoundFile::LoopPattern(int nPat, int nRow)
  659. //----------------------------------------------
  660. {
  661. if ((nPat < 0) || (nPat >= MAX_PATTERNS) || (!Patterns[nPat]))
  662. {
  663. m_dwSongFlags &= ~SONG_PATTERNLOOP;
  664. } else
  665. {
  666. if ((nRow < 0) || (nRow >= PatternSize[nPat])) nRow = 0;
  667. m_nPattern = nPat;
  668. m_nRow = m_nNextRow = nRow;
  669. m_nTickCount = m_nMusicSpeed;
  670. m_nPatternDelay = 0;
  671. m_nFrameDelay = 0;
  672. m_nBufferCount = 0;
  673. m_dwSongFlags |= SONG_PATTERNLOOP;
  674. }
  675. }
  676. UINT CSoundFile::GetBestSaveFormat() const
  677. //----------------------------------------
  678. {
  679. if ((!m_nSamples) || (!m_nChannels)) return MOD_TYPE_NONE;
  680. if (!m_nType) return MOD_TYPE_NONE;
  681. if (m_nType & (MOD_TYPE_MOD|MOD_TYPE_OKT))
  682. return MOD_TYPE_MOD;
  683. if (m_nType & (MOD_TYPE_S3M|MOD_TYPE_STM|MOD_TYPE_ULT|MOD_TYPE_FAR|MOD_TYPE_PTM))
  684. return MOD_TYPE_S3M;
  685. if (m_nType & (MOD_TYPE_XM|MOD_TYPE_MED|MOD_TYPE_MTM|MOD_TYPE_MT2))
  686. return MOD_TYPE_XM;
  687. return MOD_TYPE_IT;
  688. }
  689. UINT CSoundFile::GetSaveFormats() const
  690. //-------------------------------------
  691. {
  692. UINT n = 0;
  693. if ((!m_nSamples) || (!m_nChannels) || (m_nType == MOD_TYPE_NONE)) return 0;
  694. switch(m_nType)
  695. {
  696. case MOD_TYPE_MOD: n = MOD_TYPE_MOD;
  697. case MOD_TYPE_S3M: n = MOD_TYPE_S3M;
  698. }
  699. n |= MOD_TYPE_XM | MOD_TYPE_IT;
  700. if (!m_nInstruments)
  701. {
  702. if (m_nSamples < 32) n |= MOD_TYPE_MOD;
  703. n |= MOD_TYPE_S3M;
  704. }
  705. return n;
  706. }
  707. UINT CSoundFile::GetSampleName(UINT nSample,LPSTR s) const
  708. //--------------------------------------------------------
  709. {
  710. char sztmp[40] = ""; // changed from CHAR
  711. memcpy(sztmp, m_szNames[nSample],32);
  712. sztmp[31] = 0;
  713. if (s) strcpy(s, sztmp);
  714. return strlen(sztmp);
  715. }
  716. UINT CSoundFile::GetInstrumentName(UINT nInstr,LPSTR s) const
  717. //-----------------------------------------------------------
  718. {
  719. char sztmp[40] = ""; // changed from CHAR
  720. if ((nInstr >= MAX_INSTRUMENTS) || (!Headers[nInstr]))
  721. {
  722. if (s) *s = 0;
  723. return 0;
  724. }
  725. INSTRUMENTHEADER *penv = Headers[nInstr];
  726. memcpy(sztmp, penv->name, 32);
  727. sztmp[31] = 0;
  728. if (s) strcpy(s, sztmp);
  729. return strlen(sztmp);
  730. }
  731. #ifndef NO_PACKING
  732. UINT CSoundFile::PackSample(int &sample, int next)
  733. //------------------------------------------------
  734. {
  735. UINT i = 0;
  736. int delta = next - sample;
  737. if (delta >= 0)
  738. {
  739. for (i=0; i<7; i++) if (delta <= (int)CompressionTable[i+1]) break;
  740. } else
  741. {
  742. for (i=8; i<15; i++) if (delta >= (int)CompressionTable[i+1]) break;
  743. }
  744. sample += (int)CompressionTable[i];
  745. return i;
  746. }
  747. BOOL CSoundFile::CanPackSample(LPSTR pSample, UINT nLen, UINT nPacking, BYTE *result)
  748. //-----------------------------------------------------------------------------------
  749. {
  750. int pos, old, oldpos, besttable = 0;
  751. DWORD dwErr, dwTotal, dwResult;
  752. int i,j;
  753. if (result) *result = 0;
  754. if ((!pSample) || (nLen < 1024)) return FALSE;
  755. // Try packing with different tables
  756. dwResult = 0;
  757. for (j=1; j<MAX_PACK_TABLES; j++)
  758. {
  759. memcpy(CompressionTable, UnpackTable[j], 16);
  760. dwErr = 0;
  761. dwTotal = 1;
  762. old = pos = oldpos = 0;
  763. for (i=0; i<(int)nLen; i++)
  764. {
  765. int s = (int)pSample[i];
  766. PackSample(pos, s);
  767. dwErr += abs(pos - oldpos);
  768. dwTotal += abs(s - old);
  769. old = s;
  770. oldpos = pos;
  771. }
  772. dwErr = _muldiv(dwErr, 100, dwTotal);
  773. if (dwErr >= dwResult)
  774. {
  775. dwResult = dwErr;
  776. besttable = j;
  777. }
  778. }
  779. memcpy(CompressionTable, UnpackTable[besttable], 16);
  780. if (result)
  781. {
  782. if (dwResult > 100) *result = 100; else *result = (BYTE)dwResult;
  783. }
  784. return (dwResult >= nPacking) ? TRUE : FALSE;
  785. }
  786. #endif // NO_PACKING
  787. #ifndef MODPLUG_NO_FILESAVE
  788. UINT CSoundFile::WriteSample(FILE *f, MODINSTRUMENT *pins, UINT nFlags, UINT nMaxLen)
  789. //-----------------------------------------------------------------------------------
  790. {
  791. UINT len = 0, bufcount;
  792. signed char buffer[4096];
  793. signed char *pSample = (signed char *)pins->pSample;
  794. UINT nLen = pins->nLength;
  795. if ((nMaxLen) && (nLen > nMaxLen)) nLen = nMaxLen;
  796. if ((!pSample) || (f == NULL) || (!nLen)) return 0;
  797. switch(nFlags)
  798. {
  799. #ifndef NO_PACKING
  800. // 3: 4-bit ADPCM data
  801. case RS_ADPCM4:
  802. {
  803. int pos;
  804. len = (nLen + 1) / 2;
  805. fwrite(CompressionTable, 16, 1, f);
  806. bufcount = 0;
  807. pos = 0;
  808. for (UINT j=0; j<len; j++)
  809. {
  810. BYTE b;
  811. // Sample #1
  812. b = PackSample(pos, (int)pSample[j*2]);
  813. // Sample #2
  814. b |= PackSample(pos, (int)pSample[j*2+1]) << 4;
  815. buffer[bufcount++] = (signed char)b;
  816. if (bufcount >= sizeof(buffer))
  817. {
  818. fwrite(buffer, 1, bufcount, f);
  819. bufcount = 0;
  820. }
  821. }
  822. if (bufcount) fwrite(buffer, 1, bufcount, f);
  823. len += 16;
  824. }
  825. break;
  826. #endif // NO_PACKING
  827. // 16-bit samples
  828. case RS_PCM16U:
  829. case RS_PCM16D:
  830. case RS_PCM16S:
  831. {
  832. int16_t *p = (int16_t *)pSample;
  833. int s_old = 0, s_ofs;
  834. len = nLen * 2;
  835. bufcount = 0;
  836. s_ofs = (nFlags == RS_PCM16U) ? 0x8000 : 0;
  837. for (UINT j=0; j<nLen; j++)
  838. {
  839. int s_new = *p;
  840. p++;
  841. if (pins->uFlags & CHN_STEREO)
  842. {
  843. s_new = (s_new + (*p) + 1) >> 1;
  844. p++;
  845. }
  846. if (nFlags == RS_PCM16D)
  847. {
  848. int16_t temp = bswapLE16((int16_t)(s_new - s_old));
  849. *((int16_t*)(&buffer[bufcount])) = temp;
  850. s_old = s_new;
  851. } else
  852. {
  853. int16_t temp = bswapLE16((int16_t)(s_new + s_ofs));
  854. *((int16_t *)(&buffer[bufcount])) = temp;
  855. }
  856. bufcount += 2;
  857. if (bufcount >= sizeof(buffer) - 1)
  858. {
  859. fwrite(buffer, 1, bufcount, f);
  860. bufcount = 0;
  861. }
  862. }
  863. if (bufcount) fwrite(buffer, 1, bufcount, f);
  864. }
  865. break;
  866. // 8-bit Stereo samples (not interleaved)
  867. case RS_STPCM8S:
  868. case RS_STPCM8U:
  869. case RS_STPCM8D:
  870. {
  871. int s_ofs = (nFlags == RS_STPCM8U) ? 0x80 : 0;
  872. for (UINT iCh=0; iCh<2; iCh++)
  873. {
  874. signed char *p = pSample + iCh;
  875. int s_old = 0;
  876. bufcount = 0;
  877. for (UINT j=0; j<nLen; j++)
  878. {
  879. int s_new = *p;
  880. p += 2;
  881. if (nFlags == RS_STPCM8D)
  882. {
  883. buffer[bufcount++] = (signed char)(s_new - s_old);
  884. s_old = s_new;
  885. } else
  886. {
  887. buffer[bufcount++] = (signed char)(s_new + s_ofs);
  888. }
  889. if (bufcount >= sizeof(buffer))
  890. {
  891. fwrite(buffer, 1, bufcount, f);
  892. bufcount = 0;
  893. }
  894. }
  895. if (bufcount) fwrite(buffer, 1, bufcount, f);
  896. }
  897. }
  898. len = nLen * 2;
  899. break;
  900. // 16-bit Stereo samples (not interleaved)
  901. case RS_STPCM16S:
  902. case RS_STPCM16U:
  903. case RS_STPCM16D:
  904. {
  905. int s_ofs = (nFlags == RS_STPCM16U) ? 0x8000 : 0;
  906. for (UINT iCh=0; iCh<2; iCh++)
  907. {
  908. int16_t *p = ((int16_t *)pSample) + iCh;
  909. int s_old = 0;
  910. bufcount = 0;
  911. for (UINT j=0; j<nLen; j++)
  912. {
  913. int s_new = *p;
  914. p += 2;
  915. if (nFlags == RS_STPCM16D)
  916. {
  917. int16_t temp = bswapLE16((int16_t)(s_new - s_old));
  918. *((int16_t *)(&buffer[bufcount])) = temp;
  919. s_old = s_new;
  920. } else
  921. {
  922. int16_t temp = bswapLE16((int16_t)(s_new - s_ofs));
  923. *((int16_t*)(&buffer[bufcount])) = temp;
  924. }
  925. bufcount += 2;
  926. if (bufcount >= sizeof(buffer))
  927. {
  928. fwrite(buffer, 1, bufcount, f);
  929. bufcount = 0;
  930. }
  931. }
  932. if (bufcount) fwrite(buffer, 1, bufcount, f);
  933. }
  934. }
  935. len = nLen*4;
  936. break;
  937. // Stereo signed interleaved
  938. case RS_STIPCM8S:
  939. case RS_STIPCM16S:
  940. len = nLen * 2;
  941. if (nFlags == RS_STIPCM16S) len *= 2;
  942. fwrite(pSample, 1, len, f);
  943. break;
  944. // Default: assume 8-bit PCM data
  945. default:
  946. len = nLen;
  947. bufcount = 0;
  948. {
  949. signed char *p = pSample;
  950. int sinc = (pins->uFlags & CHN_16BIT) ? 2 : 1;
  951. int s_old = 0, s_ofs = (nFlags == RS_PCM8U) ? 0x80 : 0;
  952. if (pins->uFlags & CHN_16BIT) p++;
  953. for (UINT j=0; j<len; j++)
  954. {
  955. int s_new = (signed char)(*p);
  956. p += sinc;
  957. if (pins->uFlags & CHN_STEREO)
  958. {
  959. s_new = (s_new + ((int)*p) + 1) >> 1;
  960. p += sinc;
  961. }
  962. if (nFlags == RS_PCM8D)
  963. {
  964. buffer[bufcount++] = (signed char)(s_new - s_old);
  965. s_old = s_new;
  966. } else
  967. {
  968. buffer[bufcount++] = (signed char)(s_new + s_ofs);
  969. }
  970. if (bufcount >= sizeof(buffer))
  971. {
  972. fwrite(buffer, 1, bufcount, f);
  973. bufcount = 0;
  974. }
  975. }
  976. if (bufcount) fwrite(buffer, 1, bufcount, f);
  977. }
  978. }
  979. return len;
  980. }
  981. #endif // MODPLUG_NO_FILESAVE
  982. // Flags:
  983. // 0 = signed 8-bit PCM data (default)
  984. // 1 = unsigned 8-bit PCM data
  985. // 2 = 8-bit ADPCM data with linear table
  986. // 3 = 4-bit ADPCM data
  987. // 4 = 16-bit ADPCM data with linear table
  988. // 5 = signed 16-bit PCM data
  989. // 6 = unsigned 16-bit PCM data
  990. UINT CSoundFile::ReadSample(MODINSTRUMENT *pIns, UINT nFlags, LPCSTR lpMemFile, DWORD dwMemLength)
  991. //------------------------------------------------------------------------------
  992. {
  993. UINT len = 0, mem = pIns->nLength+6;
  994. // Disable >2Gb samples,(preventing buffer overflow in AllocateSample)
  995. if ((!pIns) || ((int)pIns->nLength < 4) || (!lpMemFile)) return 0;
  996. if (pIns->nLength > MAX_SAMPLE_LENGTH) pIns->nLength = MAX_SAMPLE_LENGTH;
  997. pIns->uFlags &= ~(CHN_16BIT|CHN_STEREO);
  998. if (nFlags & RSF_16BIT)
  999. {
  1000. mem *= 2;
  1001. pIns->uFlags |= CHN_16BIT;
  1002. }
  1003. if (nFlags & RSF_STEREO)
  1004. {
  1005. mem *= 2;
  1006. pIns->uFlags |= CHN_STEREO;
  1007. }
  1008. if ((pIns->pSample = AllocateSample(mem)) == NULL)
  1009. {
  1010. pIns->nLength = 0;
  1011. return 0;
  1012. }
  1013. switch(nFlags)
  1014. {
  1015. // 1: 8-bit unsigned PCM data
  1016. case RS_PCM8U:
  1017. {
  1018. len = pIns->nLength;
  1019. if (len > dwMemLength) len = pIns->nLength = dwMemLength;
  1020. signed char *pSample = pIns->pSample;
  1021. for (UINT j=0; j<len; j++) pSample[j] = (signed char)(lpMemFile[j] - 0x80);
  1022. }
  1023. break;
  1024. // 2: 8-bit ADPCM data with linear table
  1025. case RS_PCM8D:
  1026. {
  1027. len = pIns->nLength;
  1028. if (len > dwMemLength) break;
  1029. signed char *pSample = pIns->pSample;
  1030. const signed char *p = (const signed char *)lpMemFile;
  1031. int delta = 0;
  1032. for (UINT j=0; j<len; j++)
  1033. {
  1034. delta += p[j];
  1035. *pSample++ = (signed char)delta;
  1036. }
  1037. }
  1038. break;
  1039. // 3: 4-bit ADPCM data
  1040. case RS_ADPCM4:
  1041. {
  1042. len = (pIns->nLength + 1) / 2;
  1043. if (len > dwMemLength - 16) break;
  1044. memcpy(CompressionTable, lpMemFile, 16);
  1045. lpMemFile += 16;
  1046. signed char *pSample = pIns->pSample;
  1047. signed char delta = 0;
  1048. for (UINT j=0; j<len; j++)
  1049. {
  1050. BYTE b0 = (BYTE)lpMemFile[j];
  1051. BYTE b1 = (BYTE)(lpMemFile[j] >> 4);
  1052. delta = (signed char)GetDeltaValue((int)delta, b0);
  1053. pSample[0] = delta;
  1054. delta = (signed char)GetDeltaValue((int)delta, b1);
  1055. pSample[1] = delta;
  1056. pSample += 2;
  1057. }
  1058. len += 16;
  1059. }
  1060. break;
  1061. // 4: 16-bit ADPCM data with linear table
  1062. case RS_PCM16D:
  1063. {
  1064. len = pIns->nLength * 2;
  1065. if (len > dwMemLength) break;
  1066. int16_t *pSample = (int16_t *)pIns->pSample;
  1067. int16_t *p = (int16_t *)lpMemFile;
  1068. int delta16 = 0;
  1069. for (UINT j=0; j<len; j+=2)
  1070. {
  1071. delta16 += bswapLE16(*p++);
  1072. *pSample++ = (int16_t )delta16;
  1073. }
  1074. }
  1075. break;
  1076. // 5: 16-bit signed PCM data
  1077. case RS_PCM16S:
  1078. {
  1079. len = pIns->nLength * 2;
  1080. if (len <= dwMemLength) memcpy(pIns->pSample, lpMemFile, len);
  1081. int16_t *pSample = (int16_t *)pIns->pSample;
  1082. for (UINT j=0; j<len; j+=2)
  1083. {
  1084. int16_t rawSample = *pSample;
  1085. *pSample++ = bswapLE16(rawSample);
  1086. }
  1087. }
  1088. break;
  1089. // 16-bit signed mono PCM motorola byte order
  1090. case RS_PCM16M:
  1091. len = pIns->nLength * 2;
  1092. if (len > dwMemLength) len = dwMemLength & ~1;
  1093. if (len > 1)
  1094. {
  1095. signed char *pSample = (signed char *)pIns->pSample;
  1096. signed char *pSrc = (signed char *)lpMemFile;
  1097. for (UINT j=0; j<len; j+=2)
  1098. {
  1099. // pSample[j] = pSrc[j+1];
  1100. // pSample[j+1] = pSrc[j];
  1101. *((uint16_t *)(pSample+j)) = bswapBE16(*((uint16_t *)(pSrc+j)));
  1102. }
  1103. }
  1104. break;
  1105. // 6: 16-bit unsigned PCM data
  1106. case RS_PCM16U:
  1107. {
  1108. len = pIns->nLength * 2;
  1109. if (len > dwMemLength) break;
  1110. int16_t *pSample = (int16_t *)pIns->pSample;
  1111. int16_t *pSrc = (int16_t *)lpMemFile;
  1112. for (UINT j=0; j<len; j+=2) *pSample++ = bswapLE16(*(pSrc++)) - 0x8000;
  1113. }
  1114. break;
  1115. // 16-bit signed stereo big endian
  1116. case RS_STPCM16M:
  1117. len = pIns->nLength * 2;
  1118. if (len*2 <= dwMemLength)
  1119. {
  1120. signed char *pSample = (signed char *)pIns->pSample;
  1121. signed char *pSrc = (signed char *)lpMemFile;
  1122. for (UINT j=0; j<len; j+=2)
  1123. {
  1124. // pSample[j*2] = pSrc[j+1];
  1125. // pSample[j*2+1] = pSrc[j];
  1126. // pSample[j*2+2] = pSrc[j+1+len];
  1127. // pSample[j*2+3] = pSrc[j+len];
  1128. *((uint16_t *)(pSample+j*2)) = bswapBE16(*((uint16_t *)(pSrc+j)));
  1129. *((uint16_t *)(pSample+j*2+2)) = bswapBE16(*((uint16_t *)(pSrc+j+len)));
  1130. }
  1131. len *= 2;
  1132. }
  1133. break;
  1134. // 8-bit stereo samples
  1135. case RS_STPCM8S:
  1136. case RS_STPCM8U:
  1137. case RS_STPCM8D:
  1138. {
  1139. int iadd_l = 0, iadd_r = 0;
  1140. if (nFlags == RS_STPCM8U) { iadd_l = iadd_r = -128; }
  1141. len = pIns->nLength;
  1142. signed char *psrc = (signed char *)lpMemFile;
  1143. signed char *pSample = (signed char *)pIns->pSample;
  1144. if (len*2 > dwMemLength) break;
  1145. for (UINT j=0; j<len; j++)
  1146. {
  1147. pSample[j*2] = (signed char)(psrc[0] + iadd_l);
  1148. pSample[j*2+1] = (signed char)(psrc[len] + iadd_r);
  1149. psrc++;
  1150. if (nFlags == RS_STPCM8D)
  1151. {
  1152. iadd_l = pSample[j*2];
  1153. iadd_r = pSample[j*2+1];
  1154. }
  1155. }
  1156. len *= 2;
  1157. }
  1158. break;
  1159. // 16-bit stereo samples
  1160. case RS_STPCM16S:
  1161. case RS_STPCM16U:
  1162. case RS_STPCM16D:
  1163. {
  1164. int iadd_l = 0, iadd_r = 0;
  1165. if (nFlags == RS_STPCM16U) { iadd_l = iadd_r = -0x8000; }
  1166. len = pIns->nLength;
  1167. int16_t *psrc = (int16_t *)lpMemFile;
  1168. int16_t *pSample = (int16_t *)pIns->pSample;
  1169. if (len*4 > dwMemLength) break;
  1170. for (UINT j=0; j<len; j++)
  1171. {
  1172. pSample[j*2] = (int16_t) (bswapLE16(psrc[0]) + iadd_l);
  1173. pSample[j*2+1] = (int16_t) (bswapLE16(psrc[len]) + iadd_r);
  1174. psrc++;
  1175. if (nFlags == RS_STPCM16D)
  1176. {
  1177. iadd_l = pSample[j*2];
  1178. iadd_r = pSample[j*2+1];
  1179. }
  1180. }
  1181. len *= 4;
  1182. }
  1183. break;
  1184. // IT 2.14 compressed samples
  1185. case RS_IT2148:
  1186. case RS_IT21416:
  1187. case RS_IT2158:
  1188. case RS_IT21516:
  1189. len = dwMemLength;
  1190. if (len < 4) break;
  1191. if ((nFlags == RS_IT2148) || (nFlags == RS_IT2158))
  1192. ITUnpack8Bit(pIns->pSample, pIns->nLength, (LPBYTE)lpMemFile, dwMemLength, (nFlags == RS_IT2158));
  1193. else
  1194. ITUnpack16Bit(pIns->pSample, pIns->nLength, (LPBYTE)lpMemFile, dwMemLength, (nFlags == RS_IT21516));
  1195. break;
  1196. #ifndef MODPLUG_BASIC_SUPPORT
  1197. #ifndef MODPLUG_FASTSOUNDLIB
  1198. // 8-bit interleaved stereo samples
  1199. case RS_STIPCM8S:
  1200. case RS_STIPCM8U:
  1201. {
  1202. int iadd = 0;
  1203. if (nFlags == RS_STIPCM8U) { iadd = -0x80; }
  1204. len = pIns->nLength;
  1205. if (len*2 > dwMemLength) len = dwMemLength >> 1;
  1206. LPBYTE psrc = (LPBYTE)lpMemFile;
  1207. LPBYTE pSample = (LPBYTE)pIns->pSample;
  1208. for (UINT j=0; j<len; j++)
  1209. {
  1210. pSample[j*2] = (signed char)(psrc[0] + iadd);
  1211. pSample[j*2+1] = (signed char)(psrc[1] + iadd);
  1212. psrc+=2;
  1213. }
  1214. len *= 2;
  1215. }
  1216. break;
  1217. // 16-bit interleaved stereo samples
  1218. case RS_STIPCM16S:
  1219. case RS_STIPCM16U:
  1220. {
  1221. int iadd = 0;
  1222. if (nFlags == RS_STIPCM16U) iadd = -32768;
  1223. len = pIns->nLength;
  1224. if (len*4 > dwMemLength) len = dwMemLength >> 2;
  1225. int16_t *psrc = (int16_t *)lpMemFile;
  1226. int16_t *pSample = (int16_t *)pIns->pSample;
  1227. for (UINT j=0; j<len; j++)
  1228. {
  1229. pSample[j*2] = (int16_t)(bswapLE16(psrc[0]) + iadd);
  1230. pSample[j*2+1] = (int16_t)(bswapLE16(psrc[1]) + iadd);
  1231. psrc += 2;
  1232. }
  1233. len *= 4;
  1234. }
  1235. break;
  1236. // AMS compressed samples
  1237. case RS_AMS8:
  1238. case RS_AMS16:
  1239. len = 9;
  1240. if (dwMemLength > 9)
  1241. {
  1242. const char *psrc = lpMemFile;
  1243. char packcharacter = lpMemFile[8], *pdest = (char *)pIns->pSample;
  1244. len += bswapLE32(*((LPDWORD)(lpMemFile+4)));
  1245. if (len > dwMemLength) len = dwMemLength;
  1246. UINT dmax = pIns->nLength;
  1247. if (pIns->uFlags & CHN_16BIT) dmax <<= 1;
  1248. AMSUnpack(psrc+9, len-9, pdest, dmax, packcharacter);
  1249. }
  1250. break;
  1251. // PTM 8bit delta to 16-bit sample
  1252. case RS_PTM8DTO16:
  1253. {
  1254. len = pIns->nLength * 2;
  1255. if (len > dwMemLength) break;
  1256. int8_t *pSample = (int8_t *)pIns->pSample;
  1257. int8_t delta8 = 0;
  1258. for (UINT j=0; j<len; j++)
  1259. {
  1260. delta8 += lpMemFile[j];
  1261. *pSample++ = delta8;
  1262. }
  1263. uint16_t *pSampleW = (uint16_t *)pIns->pSample;
  1264. for (UINT j=0; j<len; j+=2) // swaparoni!
  1265. {
  1266. uint16_t rawSample = *pSampleW;
  1267. *pSampleW++ = bswapLE16(rawSample);
  1268. }
  1269. }
  1270. break;
  1271. // Huffman MDL compressed samples
  1272. case RS_MDL8:
  1273. case RS_MDL16:
  1274. len = dwMemLength;
  1275. if (len >= 4)
  1276. {
  1277. LPBYTE pSample = (LPBYTE)pIns->pSample;
  1278. LPBYTE ibuf = (LPBYTE)lpMemFile;
  1279. DWORD bitbuf = bswapLE32(*((DWORD *)ibuf));
  1280. UINT bitnum = 32;
  1281. BYTE dlt = 0, lowbyte = 0;
  1282. ibuf += 4;
  1283. for (UINT j=0; j<pIns->nLength; j++)
  1284. {
  1285. BYTE hibyte;
  1286. BYTE sign;
  1287. if (nFlags == RS_MDL16) lowbyte = (BYTE)MDLReadBits(bitbuf, bitnum, ibuf, 8);
  1288. sign = (BYTE)MDLReadBits(bitbuf, bitnum, ibuf, 1);
  1289. if (MDLReadBits(bitbuf, bitnum, ibuf, 1))
  1290. {
  1291. hibyte = (BYTE)MDLReadBits(bitbuf, bitnum, ibuf, 3);
  1292. } else
  1293. {
  1294. hibyte = 8;
  1295. while (!MDLReadBits(bitbuf, bitnum, ibuf, 1)) hibyte += 0x10;
  1296. hibyte += MDLReadBits(bitbuf, bitnum, ibuf, 4);
  1297. }
  1298. if (sign) hibyte = ~hibyte;
  1299. dlt += hibyte;
  1300. if (nFlags != RS_MDL16)
  1301. pSample[j] = dlt;
  1302. else
  1303. {
  1304. pSample[j<<1] = lowbyte;
  1305. pSample[(j<<1)+1] = dlt;
  1306. }
  1307. }
  1308. }
  1309. break;
  1310. case RS_DMF8:
  1311. case RS_DMF16:
  1312. len = dwMemLength;
  1313. if (len >= 4)
  1314. {
  1315. UINT maxlen = pIns->nLength;
  1316. if (pIns->uFlags & CHN_16BIT) maxlen <<= 1;
  1317. LPBYTE ibuf = (LPBYTE)lpMemFile, ibufmax = (LPBYTE)(lpMemFile+dwMemLength);
  1318. len = DMFUnpack((LPBYTE)pIns->pSample, ibuf, ibufmax, maxlen);
  1319. }
  1320. break;
  1321. #ifdef MODPLUG_TRACKER
  1322. // PCM 24-bit signed -> load sample, and normalize it to 16-bit
  1323. case RS_PCM24S:
  1324. case RS_PCM32S:
  1325. len = pIns->nLength * 3;
  1326. if (nFlags == RS_PCM32S) len += pIns->nLength;
  1327. if (len > dwMemLength) break;
  1328. if (len > 4*8)
  1329. {
  1330. UINT slsize = (nFlags == RS_PCM32S) ? 4 : 3;
  1331. LPBYTE pSrc = (LPBYTE)lpMemFile;
  1332. LONG max = 255;
  1333. if (nFlags == RS_PCM32S) pSrc++;
  1334. for (UINT j=0; j<len; j+=slsize)
  1335. {
  1336. LONG l = ((((pSrc[j+2] << 8) + pSrc[j+1]) << 8) + pSrc[j]) << 8;
  1337. l /= 256;
  1338. if (l > max) max = l;
  1339. if (-l > max) max = -l;
  1340. }
  1341. max = (max / 128) + 1;
  1342. int16_t *pDest = (int16_t *)pIns->pSample;
  1343. for (UINT k=0; k<len; k+=slsize)
  1344. {
  1345. LONG l = ((((pSrc[k+2] << 8) + pSrc[k+1]) << 8) + pSrc[k]) << 8;
  1346. *pDest++ = (uint16_t)(l / max);
  1347. }
  1348. }
  1349. break;
  1350. // Stereo PCM 24-bit signed -> load sample, and normalize it to 16-bit
  1351. case RS_STIPCM24S:
  1352. case RS_STIPCM32S:
  1353. len = pIns->nLength * 6;
  1354. if (nFlags == RS_STIPCM32S) len += pIns->nLength * 2;
  1355. if (len > dwMemLength) break;
  1356. if (len > 8*8)
  1357. {
  1358. UINT slsize = (nFlags == RS_STIPCM32S) ? 4 : 3;
  1359. LPBYTE pSrc = (LPBYTE)lpMemFile;
  1360. LONG max = 255;
  1361. if (nFlags == RS_STIPCM32S) pSrc++;
  1362. for (UINT j=0; j<len; j+=slsize)
  1363. {
  1364. LONG l = ((((pSrc[j+2] << 8) + pSrc[j+1]) << 8) + pSrc[j]) << 8;
  1365. l /= 256;
  1366. if (l > max) max = l;
  1367. if (-l > max) max = -l;
  1368. }
  1369. max = (max / 128) + 1;
  1370. int16_t *pDest = (int16_t *)pIns->pSample;
  1371. for (UINT k=0; k<len; k+=slsize)
  1372. {
  1373. LONG lr = ((((pSrc[k+2] << 8) + pSrc[k+1]) << 8) + pSrc[k]) << 8;
  1374. k += slsize;
  1375. LONG ll = ((((pSrc[k+2] << 8) + pSrc[k+1]) << 8) + pSrc[k]) << 8;
  1376. pDest[0] = (int16_t)ll;
  1377. pDest[1] = (int16_t)lr;
  1378. pDest += 2;
  1379. }
  1380. }
  1381. break;
  1382. // 16-bit signed big endian interleaved stereo
  1383. case RS_STIPCM16M:
  1384. {
  1385. len = pIns->nLength;
  1386. if (len*4 > dwMemLength) len = dwMemLength >> 2;
  1387. LPCBYTE psrc = (LPCBYTE)lpMemFile;
  1388. int16_t *pSample = (int16_t *)pIns->pSample;
  1389. for (UINT j=0; j<len; j++)
  1390. {
  1391. pSample[j*2] = (int16_t)(((UINT)psrc[0] << 8) | (psrc[1]));
  1392. pSample[j*2+1] = (int16_t)(((UINT)psrc[2] << 8) | (psrc[3]));
  1393. psrc += 4;
  1394. }
  1395. len *= 4;
  1396. }
  1397. break;
  1398. #endif // MODPLUG_TRACKER
  1399. #endif // !MODPLUG_FASTSOUNDLIB
  1400. #endif // !MODPLUG_BASIC_SUPPORT
  1401. // Default: 8-bit signed PCM data
  1402. default:
  1403. len = pIns->nLength;
  1404. if (len > dwMemLength) len = pIns->nLength = dwMemLength;
  1405. memcpy(pIns->pSample, lpMemFile, len);
  1406. }
  1407. if (len > dwMemLength)
  1408. {
  1409. if (pIns->pSample)
  1410. {
  1411. pIns->nLength = 0;
  1412. FreeSample(pIns->pSample);
  1413. pIns->pSample = NULL;
  1414. }
  1415. return 0;
  1416. }
  1417. AdjustSampleLoop(pIns);
  1418. return len;
  1419. }
  1420. void CSoundFile::AdjustSampleLoop(MODINSTRUMENT *pIns)
  1421. //----------------------------------------------------
  1422. {
  1423. if (!pIns->pSample) return;
  1424. if (pIns->nLoopEnd > pIns->nLength) pIns->nLoopEnd = pIns->nLength;
  1425. if (pIns->nLoopStart+2 >= pIns->nLoopEnd)
  1426. {
  1427. pIns->nLoopStart = pIns->nLoopEnd = 0;
  1428. pIns->uFlags &= ~CHN_LOOP;
  1429. }
  1430. UINT len = pIns->nLength;
  1431. if (pIns->uFlags & CHN_16BIT)
  1432. {
  1433. int16_t *pSample = (int16_t *)pIns->pSample;
  1434. // Adjust end of sample
  1435. if (pIns->uFlags & CHN_STEREO)
  1436. {
  1437. pSample[len*2+6] = pSample[len*2+4] = pSample[len*2+2] = pSample[len*2] = 0;
  1438. pSample[len*2+7] = pSample[len*2+5] = pSample[len*2+3] = pSample[len*2+1] = 0;
  1439. } else
  1440. {
  1441. pSample[len+4] = pSample[len+3] = pSample[len+2] = pSample[len+1] = pSample[len] = 0;
  1442. }
  1443. if ((pIns->uFlags & (CHN_LOOP|CHN_PINGPONGLOOP|CHN_STEREO)) == CHN_LOOP)
  1444. {
  1445. // Fix bad loops
  1446. if ((pIns->nLoopEnd+3 >= pIns->nLength) || (m_nType & MOD_TYPE_S3M))
  1447. {
  1448. pSample[pIns->nLoopEnd] = pSample[pIns->nLoopStart];
  1449. pSample[pIns->nLoopEnd+1] = pSample[pIns->nLoopStart+1];
  1450. pSample[pIns->nLoopEnd+2] = pSample[pIns->nLoopStart+2];
  1451. pSample[pIns->nLoopEnd+3] = pSample[pIns->nLoopStart+3];
  1452. pSample[pIns->nLoopEnd+4] = pSample[pIns->nLoopStart+4];
  1453. }
  1454. }
  1455. } else
  1456. {
  1457. signed char *pSample = pIns->pSample;
  1458. #ifndef MODPLUG_FASTSOUNDLIB
  1459. // Crappy samples (except chiptunes) ?
  1460. if ((pIns->nLength > 0x100) && (m_nType & (MOD_TYPE_MOD|MOD_TYPE_S3M))
  1461. && (!(pIns->uFlags & CHN_STEREO)))
  1462. {
  1463. int smpend = pSample[pIns->nLength-1], smpfix = 0, kscan;
  1464. for (kscan=pIns->nLength-1; kscan>0; kscan--)
  1465. {
  1466. smpfix = pSample[kscan-1];
  1467. if (smpfix != smpend) break;
  1468. }
  1469. int delta = smpfix - smpend;
  1470. if (((!(pIns->uFlags & CHN_LOOP)) || (kscan > (int)pIns->nLoopEnd))
  1471. && ((delta < -8) || (delta > 8)))
  1472. {
  1473. while (kscan<(int)pIns->nLength)
  1474. {
  1475. if (!(kscan & 7))
  1476. {
  1477. if (smpfix > 0) smpfix--;
  1478. if (smpfix < 0) smpfix++;
  1479. }
  1480. pSample[kscan] = (signed char)smpfix;
  1481. kscan++;
  1482. }
  1483. }
  1484. }
  1485. #endif
  1486. // Adjust end of sample
  1487. if (pIns->uFlags & CHN_STEREO)
  1488. {
  1489. pSample[len*2+6] = pSample[len*2+4] = pSample[len*2+2] = pSample[len*2] = 0;
  1490. pSample[len*2+7] = pSample[len*2+5] = pSample[len*2+3] = pSample[len*2+1] = 0;
  1491. } else
  1492. {
  1493. pSample[len+4] = pSample[len+3] = pSample[len+2] = pSample[len+1] = pSample[len] = 0;
  1494. }
  1495. if ((pIns->uFlags & (CHN_LOOP|CHN_PINGPONGLOOP|CHN_STEREO)) == CHN_LOOP)
  1496. {
  1497. if ((pIns->nLoopEnd+3 >= pIns->nLength) || (m_nType & (MOD_TYPE_MOD|MOD_TYPE_S3M)))
  1498. {
  1499. pSample[pIns->nLoopEnd] = pSample[pIns->nLoopStart];
  1500. pSample[pIns->nLoopEnd+1] = pSample[pIns->nLoopStart+1];
  1501. pSample[pIns->nLoopEnd+2] = pSample[pIns->nLoopStart+2];
  1502. pSample[pIns->nLoopEnd+3] = pSample[pIns->nLoopStart+3];
  1503. pSample[pIns->nLoopEnd+4] = pSample[pIns->nLoopStart+4];
  1504. }
  1505. }
  1506. }
  1507. }
  1508. /////////////////////////////////////////////////////////////
  1509. // Transpose <-> Frequency conversions
  1510. // returns 8363*2^((transp*128+ftune)/(12*128))
  1511. DWORD CSoundFile::TransposeToFrequency(int transp, int ftune)
  1512. //-----------------------------------------------------------
  1513. {
  1514. #ifdef MSC_VER
  1515. const float _fbase = 8363;
  1516. const float _factor = 1.0f/(12.0f*128.0f);
  1517. int result;
  1518. DWORD freq;
  1519. transp = (transp << 7) + ftune;
  1520. _asm {
  1521. fild transp
  1522. fld _factor
  1523. fmulp st(1), st(0)
  1524. fist result
  1525. fisub result
  1526. f2xm1
  1527. fild result
  1528. fld _fbase
  1529. fscale
  1530. fstp st(1)
  1531. fmul st(1), st(0)
  1532. faddp st(1), st(0)
  1533. fistp freq
  1534. }
  1535. UINT derr = freq % 11025;
  1536. if (derr <= 8) freq -= derr;
  1537. if (derr >= 11015) freq += 11025-derr;
  1538. derr = freq % 1000;
  1539. if (derr <= 5) freq -= derr;
  1540. if (derr >= 995) freq += 1000-derr;
  1541. return freq;
  1542. #else
  1543. //---GCCFIX: Removed assembly.
  1544. return (DWORD)(8363*pow(2, (transp*128+ftune)/(1536)));
  1545. #endif
  1546. }
  1547. // returns 12*128*log2(freq/8363)
  1548. int CSoundFile::FrequencyToTranspose(DWORD freq)
  1549. //----------------------------------------------
  1550. {
  1551. #ifdef MSC_VER
  1552. const float _f1_8363 = 1.0f / 8363.0f;
  1553. const float _factor = 128 * 12;
  1554. LONG result;
  1555. if (!freq) return 0;
  1556. _asm {
  1557. fld _factor
  1558. fild freq
  1559. fld _f1_8363
  1560. fmulp st(1), st(0)
  1561. fyl2x
  1562. fistp result
  1563. }
  1564. return result;
  1565. #else
  1566. //---GCCFIX: Removed assembly.
  1567. return int(1536*(log(freq/8363)/log(2)));
  1568. #endif
  1569. }
  1570. void CSoundFile::FrequencyToTranspose(MODINSTRUMENT *psmp)
  1571. //--------------------------------------------------------
  1572. {
  1573. int f2t = FrequencyToTranspose(psmp->nC4Speed);
  1574. int transp = f2t >> 7;
  1575. int ftune = f2t & 0x7F;
  1576. if (ftune > 80)
  1577. {
  1578. transp++;
  1579. ftune -= 128;
  1580. }
  1581. if (transp > 127) transp = 127;
  1582. if (transp < -127) transp = -127;
  1583. psmp->RelativeTone = transp;
  1584. psmp->nFineTune = ftune;
  1585. }
  1586. void CSoundFile::CheckCPUUsage(UINT nCPU)
  1587. //---------------------------------------
  1588. {
  1589. if (nCPU > 100) nCPU = 100;
  1590. gnCPUUsage = nCPU;
  1591. if (nCPU < 90)
  1592. {
  1593. m_dwSongFlags &= ~SONG_CPUVERYHIGH;
  1594. } else
  1595. if ((m_dwSongFlags & SONG_CPUVERYHIGH) && (nCPU >= 94))
  1596. {
  1597. UINT i=MAX_CHANNELS;
  1598. while (i >= 8)
  1599. {
  1600. i--;
  1601. if (Chn[i].nLength)
  1602. {
  1603. Chn[i].nLength = Chn[i].nPos = 0;
  1604. nCPU -= 2;
  1605. if (nCPU < 94) break;
  1606. }
  1607. }
  1608. } else
  1609. if (nCPU > 90)
  1610. {
  1611. m_dwSongFlags |= SONG_CPUVERYHIGH;
  1612. }
  1613. }
  1614. BOOL CSoundFile::SetPatternName(UINT nPat, LPCSTR lpszName)
  1615. //---------------------------------------------------------
  1616. {
  1617. char szName[MAX_PATTERNNAME] = ""; // changed from CHAR
  1618. if (nPat >= MAX_PATTERNS) return FALSE;
  1619. if (lpszName) lstrcpyn(szName, lpszName, MAX_PATTERNNAME);
  1620. szName[MAX_PATTERNNAME-1] = 0;
  1621. if (!m_lpszPatternNames) m_nPatternNames = 0;
  1622. if (nPat >= m_nPatternNames)
  1623. {
  1624. if (!lpszName[0]) return TRUE;
  1625. UINT len = (nPat+1)*MAX_PATTERNNAME;
  1626. char *p = new char[len]; // changed from CHAR
  1627. if (!p) return FALSE;
  1628. memset(p, 0, len);
  1629. if (m_lpszPatternNames)
  1630. {
  1631. memcpy(p, m_lpszPatternNames, m_nPatternNames * MAX_PATTERNNAME);
  1632. delete m_lpszPatternNames;
  1633. m_lpszPatternNames = NULL;
  1634. }
  1635. m_lpszPatternNames = p;
  1636. m_nPatternNames = nPat + 1;
  1637. }
  1638. memcpy(m_lpszPatternNames + nPat * MAX_PATTERNNAME, szName, MAX_PATTERNNAME);
  1639. return TRUE;
  1640. }
  1641. BOOL CSoundFile::GetPatternName(UINT nPat, LPSTR lpszName, UINT cbSize) const
  1642. //---------------------------------------------------------------------------
  1643. {
  1644. if ((!lpszName) || (!cbSize)) return FALSE;
  1645. lpszName[0] = 0;
  1646. if (cbSize > MAX_PATTERNNAME) cbSize = MAX_PATTERNNAME;
  1647. if ((m_lpszPatternNames) && (nPat < m_nPatternNames))
  1648. {
  1649. memcpy(lpszName, m_lpszPatternNames + nPat * MAX_PATTERNNAME, cbSize);
  1650. lpszName[cbSize-1] = 0;
  1651. return TRUE;
  1652. }
  1653. return FALSE;
  1654. }
  1655. #ifndef MODPLUG_FASTSOUNDLIB
  1656. UINT CSoundFile::DetectUnusedSamples(BOOL *pbIns)
  1657. //-----------------------------------------------
  1658. {
  1659. UINT nExt = 0;
  1660. if (!pbIns) return 0;
  1661. if (m_nInstruments)
  1662. {
  1663. memset(pbIns, 0, MAX_SAMPLES * sizeof(BOOL));
  1664. for (UINT ipat=0; ipat<MAX_PATTERNS; ipat++)
  1665. {
  1666. MODCOMMAND *p = Patterns[ipat];
  1667. if (p)
  1668. {
  1669. UINT jmax = PatternSize[ipat] * m_nChannels;
  1670. for (UINT j=0; j<jmax; j++, p++)
  1671. {
  1672. if ((p->note) && (p->note <= NOTE_MAX))
  1673. {
  1674. if ((p->instr) && (p->instr < MAX_INSTRUMENTS))
  1675. {
  1676. INSTRUMENTHEADER *penv = Headers[p->instr];
  1677. if (penv)
  1678. {
  1679. UINT n = penv->Keyboard[p->note-1];
  1680. if (n < MAX_SAMPLES) pbIns[n] = TRUE;
  1681. }
  1682. } else
  1683. {
  1684. for (UINT k=1; k<=m_nInstruments; k++)
  1685. {
  1686. INSTRUMENTHEADER *penv = Headers[k];
  1687. if (penv)
  1688. {
  1689. UINT n = penv->Keyboard[p->note-1];
  1690. if (n < MAX_SAMPLES) pbIns[n] = TRUE;
  1691. }
  1692. }
  1693. }
  1694. }
  1695. }
  1696. }
  1697. }
  1698. for (UINT ichk=1; ichk<=m_nSamples; ichk++)
  1699. {
  1700. if ((!pbIns[ichk]) && (Ins[ichk].pSample)) nExt++;
  1701. }
  1702. }
  1703. return nExt;
  1704. }
  1705. BOOL CSoundFile::RemoveSelectedSamples(BOOL *pbIns)
  1706. //-------------------------------------------------
  1707. {
  1708. if (!pbIns) return FALSE;
  1709. for (UINT j=1; j<MAX_SAMPLES; j++)
  1710. {
  1711. if ((!pbIns[j]) && (Ins[j].pSample))
  1712. {
  1713. DestroySample(j);
  1714. if ((j == m_nSamples) && (j > 1)) m_nSamples--;
  1715. }
  1716. }
  1717. return TRUE;
  1718. }
  1719. BOOL CSoundFile::DestroySample(UINT nSample)
  1720. //------------------------------------------
  1721. {
  1722. if ((!nSample) || (nSample >= MAX_SAMPLES)) return FALSE;
  1723. if (!Ins[nSample].pSample) return TRUE;
  1724. MODINSTRUMENT *pins = &Ins[nSample];
  1725. signed char *pSample = pins->pSample;
  1726. pins->pSample = NULL;
  1727. pins->nLength = 0;
  1728. pins->uFlags &= ~(CHN_16BIT);
  1729. for (UINT i=0; i<MAX_CHANNELS; i++)
  1730. {
  1731. if (Chn[i].pSample == pSample)
  1732. {
  1733. Chn[i].nPos = Chn[i].nLength = 0;
  1734. Chn[i].pSample = Chn[i].pCurrentSample = NULL;
  1735. }
  1736. }
  1737. FreeSample(pSample);
  1738. return TRUE;
  1739. }
  1740. #endif // MODPLUG_FASTSOUNDLIB