| 1 | /* |
| 2 | * This source code is public domain. |
| 3 | * |
| 4 | * Authors: Olivier Lapicque <[email protected]>, |
| 5 | * Adam Goode <[email protected]> (endian and char fixes for PPC) |
| 6 | */ |
| 7 | |
| 8 | #include <math.h> |
| 9 | #include "stdafx.h" |
| 10 | #include "sndfile.h" |
| 11 | |
| 12 | |
| 13 | ////////////////////////////////////////////////////////// |
| 14 | // CSoundFile |
| 15 | |
| 16 | CSoundFile::CSoundFile() |
| 17 | //---------------------- |
| 18 | { |
| 19 | m_nType = MOD_TYPE_NONE; |
| 20 | m_dwSongFlags = 0; |
| 21 | m_nChannels = 0; |
| 22 | m_nMixChannels = 0; |
| 23 | m_nSamples = 0; |
| 24 | m_nInstruments = 0; |
| 25 | m_nPatternNames = 0; |
| 26 | m_lpszPatternNames = NULL; |
| 27 | m_lpszSongComments = NULL; |
| 28 | m_nFreqFactor = m_nTempoFactor = 128; |
| 29 | m_nMasterVolume = 128; |
| 30 | m_nMinPeriod = 0x20; |
| 31 | m_nMaxPeriod = 0x7FFF; |
| 32 | m_nRepeatCount = 0; |
| 33 | memset(Chn, 0, sizeof(Chn)); |
| 34 | memset(ChnMix, 0, sizeof(ChnMix)); |
| 35 | memset(Ins, 0, sizeof(Ins)); |
| 36 | memset(ChnSettings, 0, sizeof(ChnSettings)); |
| 37 | memset(Headers, 0, sizeof(Headers)); |
| 38 | memset(Order, 0xFF, sizeof(Order)); |
| 39 | memset(Patterns, 0, sizeof(Patterns)); |
| 40 | memset(m_szNames, 0, sizeof(m_szNames)); |
| 41 | memset(m_MixPlugins, 0, sizeof(m_MixPlugins)); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | CSoundFile::~CSoundFile() |
| 46 | //----------------------- |
| 47 | { |
| 48 | Destroy(); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | BOOL CSoundFile::Create(LPCBYTE lpStream, DWORD dwMemLength) |
| 53 | //---------------------------------------------------------- |
| 54 | { |
| 55 | int i; |
| 56 | |
| 57 | m_nType = MOD_TYPE_NONE; |
| 58 | m_dwSongFlags = 0; |
| 59 | m_nChannels = 0; |
| 60 | m_nMixChannels = 0; |
| 61 | m_nSamples = 0; |
| 62 | m_nInstruments = 0; |
| 63 | m_nFreqFactor = m_nTempoFactor = 128; |
| 64 | m_nMasterVolume = 128; |
| 65 | m_nDefaultGlobalVolume = 256; |
| 66 | m_nGlobalVolume = 256; |
| 67 | m_nOldGlbVolSlide = 0; |
| 68 | m_nDefaultSpeed = 6; |
| 69 | m_nDefaultTempo = 125; |
| 70 | m_nPatternDelay = 0; |
| 71 | m_nFrameDelay = 0; |
| 72 | m_nNextRow = 0; |
| 73 | m_nRow = 0; |
| 74 | m_nNextStartRow = 0; |
| 75 | m_nPattern = 0; |
| 76 | m_nCurrentPattern = 0; |
| 77 | m_nNextPattern = 0; |
| 78 | m_nRestartPos = 0; |
| 79 | m_nMinPeriod = 16; |
| 80 | m_nMaxPeriod = 32767; |
| 81 | m_nSongPreAmp = 0x30; |
| 82 | m_nPatternNames = 0; |
| 83 | m_nMaxOrderPosition = 0; |
| 84 | m_lpszPatternNames = NULL; |
| 85 | m_lpszSongComments = NULL; |
| 86 | memset(Ins, 0, sizeof(Ins)); |
| 87 | memset(ChnMix, 0, sizeof(ChnMix)); |
| 88 | memset(Chn, 0, sizeof(Chn)); |
| 89 | memset(Headers, 0, sizeof(Headers)); |
| 90 | memset(Order, 0xFF, sizeof(Order)); |
| 91 | memset(Patterns, 0, sizeof(Patterns)); |
| 92 | memset(m_szNames, 0, sizeof(m_szNames)); |
| 93 | memset(m_MixPlugins, 0, sizeof(m_MixPlugins)); |
| 94 | ResetMidiCfg(); |
| 95 | for (UINT npt=0; npt<MAX_PATTERNS; npt++) PatternSize[npt] = 64; |
| 96 | for (UINT nch=0; nch<MAX_BASECHANNELS; nch++) |
| 97 | { |
| 98 | ChnSettings[nch].nPan = 128; |
| 99 | ChnSettings[nch].nVolume = 64; |
| 100 | ChnSettings[nch].dwFlags = 0; |
| 101 | ChnSettings[nch].szName[0] = 0; |
| 102 | } |
| 103 | if (lpStream) |
| 104 | { |
| 105 | if (!ReadPSM(lpStream, dwMemLength)) |
| 106 | m_nType = MOD_TYPE_NONE; |
| 107 | } |
| 108 | // Adjust song names |
| 109 | for (i=0; i<MAX_SAMPLES; i++) |
| 110 | { |
| 111 | LPSTR p = m_szNames[i]; |
| 112 | int j = 31; |
| 113 | p[j] = 0; |
| 114 | while ((j>=0) && (p[j]<=' ')) p[j--] = 0; |
| 115 | while (j>=0) |
| 116 | { |
| 117 | if (((BYTE)p[j]) < ' ') p[j] = ' '; |
| 118 | j--; |
| 119 | } |
| 120 | } |
| 121 | // Adjust channels |
| 122 | for (i=0; i<MAX_BASECHANNELS; i++) |
| 123 | { |
| 124 | if (ChnSettings[i].nVolume > 64) ChnSettings[i].nVolume = 64; |
| 125 | if (ChnSettings[i].nPan > 256) ChnSettings[i].nPan = 128; |
| 126 | Chn[i].nPan = ChnSettings[i].nPan; |
| 127 | Chn[i].nGlobalVol = ChnSettings[i].nVolume; |
| 128 | Chn[i].dwFlags = ChnSettings[i].dwFlags; |
| 129 | Chn[i].nVolume = 256; |
| 130 | Chn[i].nCutOff = 0x7F; |
| 131 | } |
| 132 | // Checking instruments |
| 133 | MODINSTRUMENT *pins = Ins; |
| 134 | |
| 135 | for (i=0; i<MAX_INSTRUMENTS; i++, pins++) |
| 136 | { |
| 137 | if (pins->pSample) |
| 138 | { |
| 139 | if (pins->nLoopEnd > pins->nLength) pins->nLoopEnd = pins->nLength; |
| 140 | if (pins->nLoopStart + 3 >= pins->nLoopEnd) |
| 141 | { |
| 142 | pins->nLoopStart = 0; |
| 143 | pins->nLoopEnd = 0; |
| 144 | } |
| 145 | if (pins->nSustainEnd > pins->nLength) pins->nSustainEnd = pins->nLength; |
| 146 | if (pins->nSustainStart + 3 >= pins->nSustainEnd) |
| 147 | { |
| 148 | pins->nSustainStart = 0; |
| 149 | pins->nSustainEnd = 0; |
| 150 | } |
| 151 | } else |
| 152 | { |
| 153 | pins->nLength = 0; |
| 154 | pins->nLoopStart = 0; |
| 155 | pins->nLoopEnd = 0; |
| 156 | pins->nSustainStart = 0; |
| 157 | pins->nSustainEnd = 0; |
| 158 | } |
| 159 | if (!pins->nLoopEnd) pins->uFlags &= ~CHN_LOOP; |
| 160 | if (!pins->nSustainEnd) pins->uFlags &= ~CHN_SUSTAINLOOP; |
| 161 | if (pins->nGlobalVol > 64) pins->nGlobalVol = 64; |
| 162 | } |
| 163 | // Check invalid instruments |
| 164 | while ((m_nInstruments > 0) && (!Headers[m_nInstruments])) |
| 165 | m_nInstruments--; |
| 166 | // Set default values |
| 167 | if (m_nSongPreAmp < 0x20) m_nSongPreAmp = 0x20; |
| 168 | if (m_nDefaultTempo < 32) m_nDefaultTempo = 125; |
| 169 | if (!m_nDefaultSpeed) m_nDefaultSpeed = 6; |
| 170 | m_nMusicSpeed = m_nDefaultSpeed; |
| 171 | m_nMusicTempo = m_nDefaultTempo; |
| 172 | m_nGlobalVolume = m_nDefaultGlobalVolume; |
| 173 | m_nNextPattern = 0; |
| 174 | m_nCurrentPattern = 0; |
| 175 | m_nPattern = 0; |
| 176 | m_nBufferCount = 0; |
| 177 | m_nTickCount = m_nMusicSpeed; |
| 178 | m_nNextRow = 0; |
| 179 | m_nRow = 0; |
| 180 | m_nNextStartRow = 0; |
| 181 | if ((m_nRestartPos >= MAX_ORDERS) || (Order[m_nRestartPos] >= MAX_PATTERNS)) m_nRestartPos = 0; |
| 182 | // Load plugins |
| 183 | if (gpMixPluginCreateProc) |
| 184 | { |
| 185 | for (UINT iPlug=0; iPlug<MAX_MIXPLUGINS; iPlug++) |
| 186 | { |
| 187 | if ((m_MixPlugins[iPlug].Info.dwPluginId1) |
| 188 | || (m_MixPlugins[iPlug].Info.dwPluginId2)) |
| 189 | { |
| 190 | gpMixPluginCreateProc(&m_MixPlugins[iPlug]); |
| 191 | if (m_MixPlugins[iPlug].pMixPlugin) |
| 192 | { |
| 193 | m_MixPlugins[iPlug].pMixPlugin->RestoreAllParameters(); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | if (m_nType) |
| 199 | { |
| 200 | UINT maxpreamp = 0x10+(m_nChannels*8); |
| 201 | if (maxpreamp > 100) maxpreamp = 100; |
| 202 | if (m_nSongPreAmp > maxpreamp) m_nSongPreAmp = maxpreamp; |
| 203 | return TRUE; |
| 204 | } |
| 205 | return FALSE; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | BOOL CSoundFile::Destroy() |
| 210 | |
| 211 | //------------------------ |
| 212 | { |
| 213 | int i; |
| 214 | for (i=0; i<MAX_PATTERNS; i++) if (Patterns[i]) |
| 215 | { |
| 216 | FreePattern(Patterns[i]); |
| 217 | Patterns[i] = NULL; |
| 218 | } |
| 219 | m_nPatternNames = 0; |
| 220 | if (m_lpszPatternNames) |
| 221 | { |
| 222 | delete [] m_lpszPatternNames; |
| 223 | m_lpszPatternNames = NULL; |
| 224 | } |
| 225 | if (m_lpszSongComments) |
| 226 | { |
| 227 | delete [] m_lpszSongComments; |
| 228 | m_lpszSongComments = NULL; |
| 229 | } |
| 230 | for (i=1; i<MAX_SAMPLES; i++) |
| 231 | { |
| 232 | MODINSTRUMENT *pins = &Ins[i]; |
| 233 | if (pins->pSample) |
| 234 | { |
| 235 | FreeSample(pins->pSample); |
| 236 | pins->pSample = NULL; |
| 237 | } |
| 238 | } |
| 239 | for (i=0; i<MAX_INSTRUMENTS; i++) |
| 240 | { |
| 241 | if (Headers[i]) |
| 242 | { |
| 243 | delete Headers[i]; |
| 244 | Headers[i] = NULL; |
| 245 | } |
| 246 | } |
| 247 | for (i=0; i<MAX_MIXPLUGINS; i++) |
| 248 | { |
| 249 | if ((m_MixPlugins[i].nPluginDataSize) && (m_MixPlugins[i].pPluginData)) |
| 250 | { |
| 251 | m_MixPlugins[i].nPluginDataSize = 0; |
| 252 | delete [] (signed char*)m_MixPlugins[i].pPluginData; |
| 253 | m_MixPlugins[i].pPluginData = NULL; |
| 254 | } |
| 255 | m_MixPlugins[i].pMixState = NULL; |
| 256 | if (m_MixPlugins[i].pMixPlugin) |
| 257 | { |
| 258 | m_MixPlugins[i].pMixPlugin->Release(); |
| 259 | m_MixPlugins[i].pMixPlugin = NULL; |
| 260 | } |
| 261 | } |
| 262 | m_nType = MOD_TYPE_NONE; |
| 263 | m_nChannels = m_nSamples = m_nInstruments = 0; |
| 264 | return TRUE; |
| 265 | } |
| 266 | |
| 267 | |
| 268 | ////////////////////////////////////////////////////////////////////////// |
| 269 | // Memory Allocation |
| 270 | |
| 271 | MODCOMMAND *CSoundFile::AllocatePattern(UINT rows, UINT nchns) |
| 272 | //------------------------------------------------------------ |
| 273 | { |
| 274 | MODCOMMAND *p = new MODCOMMAND[rows*nchns]; |
| 275 | if (p) memset(p, 0, rows*nchns*sizeof(MODCOMMAND)); |
| 276 | return p; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | void CSoundFile::FreePattern(LPVOID pat) |
| 281 | //-------------------------------------- |
| 282 | { |
| 283 | if (pat) delete [] (signed char*)pat; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | signed char* CSoundFile::AllocateSample(UINT nbytes) |
| 288 | //------------------------------------------- |
| 289 | { |
| 290 | signed char * p = (signed char *)GlobalAllocPtr(GHND, (nbytes+39) & ~7); |
| 291 | if (p) p += 16; |
| 292 | return p; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | void CSoundFile::FreeSample(LPVOID p) |
| 297 | //----------------------------------- |
| 298 | { |
| 299 | if (p) |
| 300 | { |
| 301 | GlobalFreePtr(((LPSTR)p)-16); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | |
| 306 | ////////////////////////////////////////////////////////////////////////// |
| 307 | // Misc functions |
| 308 | |
| 309 | void CSoundFile::ResetMidiCfg() |
| 310 | //----------------------------- |
| 311 | { |
| 312 | memset(&m_MidiCfg, 0, sizeof(m_MidiCfg)); |
| 313 | lstrcpyA(&m_MidiCfg.szMidiGlb[MIDIOUT_START*32], "FF"); |
| 314 | lstrcpyA(&m_MidiCfg.szMidiGlb[MIDIOUT_STOP*32], "FC"); |
| 315 | lstrcpyA(&m_MidiCfg.szMidiGlb[MIDIOUT_NOTEON*32], "9c n v"); |
| 316 | lstrcpyA(&m_MidiCfg.szMidiGlb[MIDIOUT_NOTEOFF*32], "9c n 0"); |
| 317 | lstrcpyA(&m_MidiCfg.szMidiGlb[MIDIOUT_PROGRAM*32], "Cc p"); |
| 318 | lstrcpyA(&m_MidiCfg.szMidiSFXExt[0], "F0F000z"); |
| 319 | for (int iz=0; iz<16; iz++) wsprintfA(&m_MidiCfg.szMidiZXXExt[iz*32], "F0F001%02X", iz*8); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | UINT CSoundFile::GetNumChannels() const |
| 324 | //------------------------------------- |
| 325 | { |
| 326 | UINT n = 0; |
| 327 | for (UINT i=0; i<m_nChannels; i++) if (ChnSettings[i].nVolume) n++; |
| 328 | return n; |
| 329 | } |
| 330 | |
| 331 | |
| 332 | UINT CSoundFile::GetSongComments(LPSTR s, UINT len, UINT linesize) |
| 333 | //---------------------------------------------------------------- |
| 334 | { |
| 335 | LPCSTR p = m_lpszSongComments; |
| 336 | if (!p) return 0; |
| 337 | UINT i = 2, ln=0; |
| 338 | if ((len) && (s)) s[0] = '\x0D'; |
| 339 | if ((len > 1) && (s)) s[1] = '\x0A'; |
| 340 | while ((*p) && (i+2 < len)) |
| 341 | { |
| 342 | BYTE c = (BYTE)*p++; |
| 343 | if ((c == 0x0D) || ((c == ' ') && (ln >= linesize))) |
| 344 | { if (s) { s[i++] = '\x0D'; s[i++] = '\x0A'; } else i+= 2; ln=0; } |
| 345 | else |
| 346 | if (c >= 0x20) { if (s) s[i++] = c; else i++; ln++; } |
| 347 | } |
| 348 | if (s) s[i] = 0; |
| 349 | return i; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | UINT CSoundFile::GetRawSongComments(LPSTR s, UINT len, UINT linesize) |
| 354 | //------------------------------------------------------------------- |
| 355 | { |
| 356 | LPCSTR p = m_lpszSongComments; |
| 357 | if (!p) return 0; |
| 358 | UINT i = 0, ln=0; |
| 359 | while ((*p) && (i < len-1)) |
| 360 | { |
| 361 | BYTE c = (BYTE)*p++; |
| 362 | if ((c == 0x0D) || (c == 0x0A)) |
| 363 | { |
| 364 | if (ln) |
| 365 | { |
| 366 | while (ln < linesize) { if (s) s[i] = ' '; i++; ln++; } |
| 367 | ln = 0; |
| 368 | } |
| 369 | } else |
| 370 | if ((c == ' ') && (!ln)) |
| 371 | { |
| 372 | UINT k=0; |
| 373 | while ((p[k]) && (p[k] >= ' ')) k++; |
| 374 | if (k <= linesize) |
| 375 | { |
| 376 | if (s) s[i] = ' '; |
| 377 | i++; |
| 378 | ln++; |
| 379 | } |
| 380 | } else |
| 381 | { |
| 382 | if (s) s[i] = c; |
| 383 | i++; |
| 384 | ln++; |
| 385 | if (ln == linesize) ln = 0; |
| 386 | } |
| 387 | } |
| 388 | if (ln) |
| 389 | { |
| 390 | while ((ln < linesize) && (i < len)) |
| 391 | { |
| 392 | if (s) s[i] = ' '; |
| 393 | i++; |
| 394 | ln++; |
| 395 | } |
| 396 | } |
| 397 | if (s) s[i] = 0; |
| 398 | return i; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | BOOL CSoundFile::SetWaveConfig(UINT nRate,UINT nBits,UINT nChannels,BOOL bMMX) |
| 403 | //---------------------------------------------------------------------------- |
| 404 | { |
| 405 | BOOL bReset = FALSE; |
| 406 | DWORD d = gdwSoundSetup & ~SNDMIX_ENABLEMMX; |
| 407 | if (bMMX) d |= SNDMIX_ENABLEMMX; |
| 408 | if ((gdwMixingFreq != nRate) || (gnBitsPerSample != nBits) || (gnChannels != nChannels) || (d != gdwSoundSetup)) bReset = TRUE; |
| 409 | gnChannels = nChannels; |
| 410 | gdwSoundSetup = d; |
| 411 | gdwMixingFreq = nRate; |
| 412 | gnBitsPerSample = nBits; |
| 413 | InitPlayer(bReset); |
| 414 | return TRUE; |
| 415 | } |
| 416 | |
| 417 | BOOL CSoundFile::SetMixConfig(UINT nStereoSeparation, UINT nMaxMixChannels) |
| 418 | //------------------------------------------------------------------------- |
| 419 | { |
| 420 | if (nMaxMixChannels < 2) return FALSE; |
| 421 | |
| 422 | m_nMaxMixChannels = nMaxMixChannels; |
| 423 | m_nStereoSeparation = nStereoSeparation; |
| 424 | return TRUE; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | BOOL CSoundFile::SetResamplingMode(UINT nMode) |
| 429 | //-------------------------------------------- |
| 430 | { |
| 431 | DWORD d = gdwSoundSetup & ~(SNDMIX_NORESAMPLING|SNDMIX_HQRESAMPLER|SNDMIX_ULTRAHQSRCMODE); |
| 432 | switch(nMode) |
| 433 | { |
| 434 | case SRCMODE_NEAREST: d |= SNDMIX_NORESAMPLING; break; |
| 435 | case SRCMODE_LINEAR: break; |
| 436 | case SRCMODE_SPLINE: d |= SNDMIX_HQRESAMPLER; break; |
| 437 | case SRCMODE_POLYPHASE: d |= (SNDMIX_HQRESAMPLER|SNDMIX_ULTRAHQSRCMODE); break; |
| 438 | default: |
| 439 | return FALSE; |
| 440 | } |
| 441 | gdwSoundSetup = d; |
| 442 | return TRUE; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | BOOL CSoundFile::SetMasterVolume(UINT nVol, BOOL bAdjustAGC) |
| 447 | //---------------------------------------------------------- |
| 448 | { |
| 449 | if (nVol < 1) nVol = 1; |
| 450 | if (nVol > 0x200) nVol = 0x200; // x4 maximum |
| 451 | if ((nVol < m_nMasterVolume) && (nVol) && (gdwSoundSetup & SNDMIX_AGC) && (bAdjustAGC)) |
| 452 | { |
| 453 | gnAGC = gnAGC * m_nMasterVolume / nVol; |
| 454 | if (gnAGC > AGC_UNITY) gnAGC = AGC_UNITY; |
| 455 | } |
| 456 | m_nMasterVolume = nVol; |
| 457 | return TRUE; |
| 458 | } |
| 459 | |
| 460 | BOOL CSoundFile::SetTempoFactor(UINT nTemp) |
| 461 | //---------------------------------------------------------- |
| 462 | { |
| 463 | if (nTemp < 1) nTemp = 1; |
| 464 | if (nTemp > 0x100) nTemp = 0x100; |
| 465 | |
| 466 | m_nTempoFactor = nTemp; |
| 467 | |
| 468 | return TRUE; |
| 469 | } |
| 470 | |
| 471 | UINT CSoundFile::GetNumPatterns() const |
| 472 | //------------------------------------- |
| 473 | { |
| 474 | UINT i = 0; |
| 475 | while ((i < MAX_ORDERS) && (Order[i] < 0xFF)) i++; |
| 476 | return i; |
| 477 | } |
| 478 | |
| 479 | |
| 480 | UINT CSoundFile::GetNumInstruments() const |
| 481 | //---------------------------------------- |
| 482 | { |
| 483 | UINT n=0; |
| 484 | for (UINT i=0; i<MAX_INSTRUMENTS; i++) if (Ins[i].pSample) n++; |
| 485 | return n; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | UINT CSoundFile::GetMaxPosition() const |
| 490 | //------------------------------------- |
| 491 | { |
| 492 | UINT max = 0; |
| 493 | UINT i = 0; |
| 494 | |
| 495 | while ((i < MAX_ORDERS) && (Order[i] != 0xFF)) |
| 496 | { |
| 497 | if (Order[i] < MAX_PATTERNS) max += PatternSize[Order[i]]; |
| 498 | i++; |
| 499 | } |
| 500 | return max; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | UINT CSoundFile::GetCurrentPos() const |
| 505 | //------------------------------------ |
| 506 | { |
| 507 | UINT pos = 0; |
| 508 | |
| 509 | for (UINT i=0; i<m_nCurrentPattern; i++) if (Order[i] < MAX_PATTERNS) |
| 510 | pos += PatternSize[Order[i]]; |
| 511 | return pos + m_nRow; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | void CSoundFile::SetCurrentPos(UINT nPos) |
| 516 | //--------------------------------------- |
| 517 | { |
| 518 | UINT i, nPattern; |
| 519 | |
| 520 | for (i=0; i<MAX_CHANNELS; i++) |
| 521 | { |
| 522 | Chn[i].nNote = Chn[i].nNewNote = Chn[i].nNewIns = 0; |
| 523 | Chn[i].pInstrument = NULL; |
| 524 | Chn[i].pHeader = NULL; |
| 525 | Chn[i].nPortamentoDest = 0; |
| 526 | Chn[i].nCommand = 0; |
| 527 | Chn[i].nPatternLoopCount = 0; |
| 528 | Chn[i].nPatternLoop = 0; |
| 529 | Chn[i].nFadeOutVol = 0; |
| 530 | Chn[i].dwFlags |= CHN_KEYOFF|CHN_NOTEFADE; |
| 531 | Chn[i].nTremorCount = 0; |
| 532 | } |
| 533 | if (!nPos) |
| 534 | { |
| 535 | for (i=0; i<MAX_CHANNELS; i++) |
| 536 | { |
| 537 | Chn[i].nPeriod = 0; |
| 538 | Chn[i].nPos = Chn[i].nLength = 0; |
| 539 | Chn[i].nLoopStart = 0; |
| 540 | Chn[i].nLoopEnd = 0; |
| 541 | Chn[i].nROfs = Chn[i].nLOfs = 0; |
| 542 | Chn[i].pSample = NULL; |
| 543 | Chn[i].pInstrument = NULL; |
| 544 | Chn[i].pHeader = NULL; |
| 545 | Chn[i].nCutOff = 0x7F; |
| 546 | Chn[i].nResonance = 0; |
| 547 | Chn[i].nLeftVol = Chn[i].nRightVol = 0; |
| 548 | Chn[i].nNewLeftVol = Chn[i].nNewRightVol = 0; |
| 549 | Chn[i].nLeftRamp = Chn[i].nRightRamp = 0; |
| 550 | Chn[i].nVolume = 256; |
| 551 | if (i < MAX_BASECHANNELS) |
| 552 | { |
| 553 | Chn[i].dwFlags = ChnSettings[i].dwFlags; |
| 554 | Chn[i].nPan = ChnSettings[i].nPan; |
| 555 | Chn[i].nGlobalVol = ChnSettings[i].nVolume; |
| 556 | } else |
| 557 | { |
| 558 | Chn[i].dwFlags = 0; |
| 559 | Chn[i].nPan = 128; |
| 560 | Chn[i].nGlobalVol = 64; |
| 561 | } |
| 562 | } |
| 563 | m_nGlobalVolume = m_nDefaultGlobalVolume; |
| 564 | m_nMusicSpeed = m_nDefaultSpeed; |
| 565 | m_nMusicTempo = m_nDefaultTempo; |
| 566 | } |
| 567 | m_dwSongFlags &= ~(SONG_PATTERNLOOP|SONG_CPUVERYHIGH|SONG_FADINGSONG|SONG_ENDREACHED|SONG_GLOBALFADE); |
| 568 | for (nPattern = 0; nPattern < MAX_ORDERS; nPattern++) |
| 569 | { |
| 570 | UINT ord = Order[nPattern]; |
| 571 | if (ord == 0xFE) continue; |
| 572 | if (ord == 0xFF) break; |
| 573 | if (ord < MAX_PATTERNS) |
| 574 | { |
| 575 | if (nPos < (UINT)PatternSize[ord]) break; |
| 576 | nPos -= PatternSize[ord]; |
| 577 | } |
| 578 | } |
| 579 | // Buggy position ? |
| 580 | if ((nPattern >= MAX_ORDERS) |
| 581 | || (Order[nPattern] >= MAX_PATTERNS) |
| 582 | || (nPos >= PatternSize[Order[nPattern]])) |
| 583 | { |
| 584 | nPos = 0; |
| 585 | nPattern = 0; |
| 586 | } |
| 587 | UINT nRow = nPos; |
| 588 | if ((nRow) && (Order[nPattern] < MAX_PATTERNS)) |
| 589 | { |
| 590 | MODCOMMAND *p = Patterns[Order[nPattern]]; |
| 591 | if ((p) && (nRow < PatternSize[Order[nPattern]])) |
| 592 | { |
| 593 | BOOL bOk = FALSE; |
| 594 | while ((!bOk) && (nRow > 0)) |
| 595 | { |
| 596 | UINT n = nRow * m_nChannels; |
| 597 | for (UINT k=0; k<m_nChannels; k++, n++) |
| 598 | { |
| 599 | if (p[n].note) |
| 600 | { |
| 601 | bOk = TRUE; |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | if (!bOk) nRow--; |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | m_nNextPattern = nPattern; |
| 610 | m_nNextRow = nRow; |
| 611 | m_nNextStartRow = 0; |
| 612 | m_nTickCount = m_nMusicSpeed; |
| 613 | m_nBufferCount = 0; |
| 614 | m_nPatternDelay = 0; |
| 615 | m_nFrameDelay = 0; |
| 616 | } |
| 617 | |
| 618 | |
| 619 | void CSoundFile::SetCurrentOrder(UINT nPos) |
| 620 | //----------------------------------------- |
| 621 | { |
| 622 | while ((nPos < MAX_ORDERS) && (Order[nPos] == 0xFE)) nPos++; |
| 623 | if ((nPos >= MAX_ORDERS) || (Order[nPos] >= MAX_PATTERNS)) return; |
| 624 | for (UINT j=0; j<MAX_CHANNELS; j++) |
| 625 | { |
| 626 | Chn[j].nPeriod = 0; |
| 627 | Chn[j].nNote = 0; |
| 628 | Chn[j].nPortamentoDest = 0; |
| 629 | Chn[j].nCommand = 0; |
| 630 | Chn[j].nPatternLoopCount = 0; |
| 631 | Chn[j].nPatternLoop = 0; |
| 632 | Chn[j].nTremorCount = 0; |
| 633 | } |
| 634 | if (!nPos) |
| 635 | { |
| 636 | SetCurrentPos(0); |
| 637 | } else |
| 638 | { |
| 639 | m_nNextPattern = nPos; |
| 640 | m_nRow = m_nNextRow = m_nNextStartRow = 0; |
| 641 | m_nPattern = 0; |
| 642 | m_nTickCount = m_nMusicSpeed; |
| 643 | m_nBufferCount = 0; |
| 644 | m_nTotalCount = 0; |
| 645 | m_nPatternDelay = 0; |
| 646 | m_nFrameDelay = 0; |
| 647 | } |
| 648 | m_dwSongFlags &= ~(SONG_PATTERNLOOP|SONG_CPUVERYHIGH|SONG_FADINGSONG|SONG_ENDREACHED|SONG_GLOBALFADE); |
| 649 | } |
| 650 | |
| 651 | |
| 652 | void CSoundFile::ResetChannels() |
| 653 | //------------------------------ |
| 654 | { |
| 655 | m_dwSongFlags &= ~(SONG_CPUVERYHIGH|SONG_FADINGSONG|SONG_ENDREACHED|SONG_GLOBALFADE); |
| 656 | m_nBufferCount = 0; |
| 657 | for (UINT i=0; i<MAX_CHANNELS; i++) |
| 658 | { |
| 659 | Chn[i].nROfs = Chn[i].nLOfs = 0; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | |
| 664 | void CSoundFile::LoopPattern(int nPat, int nRow) |
| 665 | //---------------------------------------------- |
| 666 | { |
| 667 | if ((nPat < 0) || (nPat >= MAX_PATTERNS) || (!Patterns[nPat])) |
| 668 | { |
| 669 | m_dwSongFlags &= ~SONG_PATTERNLOOP; |
| 670 | } else |
| 671 | { |
| 672 | if ((nRow < 0) || (nRow >= PatternSize[nPat])) nRow = 0; |
| 673 | m_nPattern = nPat; |
| 674 | m_nRow = m_nNextRow = nRow; |
| 675 | m_nTickCount = m_nMusicSpeed; |
| 676 | m_nPatternDelay = 0; |
| 677 | m_nFrameDelay = 0; |
| 678 | m_nBufferCount = 0; |
| 679 | m_dwSongFlags |= SONG_PATTERNLOOP; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | |
| 684 | UINT CSoundFile::GetSampleName(UINT nSample,LPSTR s) const |
| 685 | //-------------------------------------------------------- |
| 686 | { |
| 687 | char sztmp[40] = ""; // changed from CHAR |
| 688 | if (nSample < MAX_SAMPLES) |
| 689 | memcpy(sztmp, m_szNames[nSample], 32); |
| 690 | sztmp[31] = 0; |
| 691 | if (s) strcpy(s, sztmp); |
| 692 | return strlen(sztmp); |
| 693 | } |
| 694 | |
| 695 | |
| 696 | UINT CSoundFile::GetInstrumentName(UINT nInstr,LPSTR s) const |
| 697 | //----------------------------------------------------------- |
| 698 | { |
| 699 | char sztmp[40] = ""; // changed from CHAR |
| 700 | if ((nInstr >= MAX_INSTRUMENTS) || (!Headers[nInstr])) |
| 701 | { |
| 702 | if (s) *s = 0; |
| 703 | return 0; |
| 704 | } |
| 705 | INSTRUMENTHEADER *penv = Headers[nInstr]; |
| 706 | memcpy(sztmp, penv->name, 32); |
| 707 | sztmp[31] = 0; |
| 708 | if (s) strcpy(s, sztmp); |
| 709 | return strlen(sztmp); |
| 710 | } |
| 711 | |
| 712 | // Flags: |
| 713 | // 0 = signed 8-bit PCM data (default) |
| 714 | // 1 = unsigned 8-bit PCM data |
| 715 | // 2 = 8-bit ADPCM data with linear table |
| 716 | // 3 = 4-bit ADPCM data |
| 717 | // 4 = 16-bit ADPCM data with linear table |
| 718 | // 5 = signed 16-bit PCM data |
| 719 | // 6 = unsigned 16-bit PCM data |
| 720 | // |
| 721 | // Does unaligned WORD/DWORD stores to pIns->pSample with the |
| 722 | // assumption that malloc() returns aligned memory already. |
| 723 | // |
| 724 | UINT CSoundFile::ReadSample(MODINSTRUMENT *pIns, UINT nFlags, LPCSTR lpMemFile, DWORD dwMemLength) |
| 725 | //------------------------------------------------------------------------------ |
| 726 | { |
| 727 | UINT len = 0, mem; |
| 728 | |
| 729 | // Disable >2Gb samples,(preventing buffer overflow in AllocateSample) |
| 730 | if ((!pIns) || ((int)pIns->nLength < 4) || (!lpMemFile)) return 0; |
| 731 | if (pIns->nLength > MAX_SAMPLE_LENGTH) pIns->nLength = MAX_SAMPLE_LENGTH; |
| 732 | mem = pIns->nLength+6; |
| 733 | pIns->uFlags &= ~(CHN_16BIT|CHN_STEREO); |
| 734 | if (nFlags & RSF_16BIT) |
| 735 | { |
| 736 | mem *= 2; |
| 737 | pIns->uFlags |= CHN_16BIT; |
| 738 | } |
| 739 | if (nFlags & RSF_STEREO) |
| 740 | { |
| 741 | mem *= 2; |
| 742 | pIns->uFlags |= CHN_STEREO; |
| 743 | } |
| 744 | if ((pIns->pSample = AllocateSample(mem)) == NULL) |
| 745 | { |
| 746 | pIns->nLength = 0; |
| 747 | return 0; |
| 748 | } |
| 749 | switch(nFlags) |
| 750 | { |
| 751 | // 1: 8-bit unsigned PCM data |
| 752 | case RS_PCM8U: |
| 753 | { |
| 754 | len = pIns->nLength; |
| 755 | if (len > dwMemLength) len = pIns->nLength = dwMemLength; |
| 756 | signed char *pSample = pIns->pSample; |
| 757 | for (UINT j=0; j<len; j++) pSample[j] = (signed char)(lpMemFile[j] - 0x80); |
| 758 | } |
| 759 | break; |
| 760 | |
| 761 | // 2: 8-bit ADPCM data with linear table |
| 762 | case RS_PCM8D: |
| 763 | { |
| 764 | len = pIns->nLength; |
| 765 | if (len > dwMemLength) break; |
| 766 | signed char *pSample = pIns->pSample; |
| 767 | const signed char *p = (const signed char *)lpMemFile; |
| 768 | int delta = 0; |
| 769 | |
| 770 | for (UINT j=0; j<len; j++) |
| 771 | { |
| 772 | delta += p[j]; |
| 773 | *pSample++ = (signed char)delta; |
| 774 | } |
| 775 | } |
| 776 | break; |
| 777 | |
| 778 | // 3: 4-bit ADPCM data |
| 779 | case RS_ADPCM4: |
| 780 | { |
| 781 | len = (pIns->nLength + 1) / 2; |
| 782 | if (len > dwMemLength - 16 || dwMemLength < 16) break; |
| 783 | memcpy(CompressionTable, lpMemFile, 16); |
| 784 | lpMemFile += 16; |
| 785 | signed char *pSample = pIns->pSample; |
| 786 | signed char delta = 0; |
| 787 | for (UINT j=0; j<len; j++) |
| 788 | { |
| 789 | BYTE b0 = (BYTE)lpMemFile[j]; |
| 790 | BYTE b1 = (BYTE)(lpMemFile[j] >> 4); |
| 791 | delta = (signed char)GetDeltaValue((int)delta, b0); |
| 792 | pSample[0] = delta; |
| 793 | delta = (signed char)GetDeltaValue((int)delta, b1); |
| 794 | pSample[1] = delta; |
| 795 | pSample += 2; |
| 796 | } |
| 797 | len += 16; |
| 798 | } |
| 799 | break; |
| 800 | |
| 801 | // 4: 16-bit ADPCM data with linear table |
| 802 | case RS_PCM16D: |
| 803 | { |
| 804 | len = pIns->nLength * 2; |
| 805 | if (len > dwMemLength) break; |
| 806 | int16_t *pSample = (int16_t *)pIns->pSample; |
| 807 | LPCBYTE p = (LPCBYTE)lpMemFile; |
| 808 | int delta16 = 0; |
| 809 | for (UINT j=0; j<len; j+=2, p += 2) |
| 810 | { |
| 811 | delta16 += (int16_t)READ_LE16(p); |
| 812 | *pSample++ = (int16_t )delta16; |
| 813 | } |
| 814 | } |
| 815 | break; |
| 816 | |
| 817 | // 5: 16-bit signed PCM data |
| 818 | case RS_PCM16S: |
| 819 | { |
| 820 | len = pIns->nLength * 2; |
| 821 | if (len <= dwMemLength) memcpy(pIns->pSample, lpMemFile, len); |
| 822 | int16_t *pSample = (int16_t *)pIns->pSample; |
| 823 | for (UINT j=0; j<len; j+=2) |
| 824 | { |
| 825 | int16_t rawSample = *pSample; |
| 826 | *pSample++ = bswapLE16(rawSample); |
| 827 | } |
| 828 | } |
| 829 | break; |
| 830 | |
| 831 | // 16-bit signed mono PCM motorola byte order |
| 832 | case RS_PCM16M: |
| 833 | len = pIns->nLength * 2; |
| 834 | if (len > dwMemLength) len = dwMemLength & ~1; |
| 835 | if (len > 1) |
| 836 | { |
| 837 | char *pSample = (char *)pIns->pSample; |
| 838 | LPCBYTE pSrc = (LPCBYTE)lpMemFile; |
| 839 | for (UINT j=0; j<len; j+=2) { |
| 840 | *((uint16_t *)(pSample+j)) = READ_BE16(pSrc+j); |
| 841 | } |
| 842 | } |
| 843 | break; |
| 844 | |
| 845 | // 6: 16-bit unsigned PCM data |
| 846 | case RS_PCM16U: |
| 847 | { |
| 848 | len = pIns->nLength * 2; |
| 849 | if (len > dwMemLength) break; |
| 850 | int16_t *pSample = (int16_t *)pIns->pSample; |
| 851 | LPCBYTE pSrc = (LPCBYTE)lpMemFile; |
| 852 | for (UINT j=0; j<len; j+=2, pSrc += 2) { |
| 853 | *pSample++ = (int16_t)(READ_LE16(pSrc) - 0x8000); |
| 854 | } |
| 855 | } |
| 856 | break; |
| 857 | |
| 858 | // 16-bit signed stereo big endian |
| 859 | case RS_STPCM16M: |
| 860 | len = pIns->nLength * 2; |
| 861 | if (len*2 <= dwMemLength) |
| 862 | { |
| 863 | char *pSample = (char *)pIns->pSample; |
| 864 | LPCBYTE pSrc = (LPCBYTE)lpMemFile; |
| 865 | for (UINT j=0; j<len; j+=2) { |
| 866 | *((uint16_t *)(pSample+j*2)) = READ_BE16(pSrc+j); |
| 867 | *((uint16_t *)(pSample+j*2+2)) = READ_BE16(pSrc+j+len); |
| 868 | } |
| 869 | len *= 2; |
| 870 | } |
| 871 | break; |
| 872 | |
| 873 | // 8-bit stereo samples |
| 874 | case RS_STPCM8S: |
| 875 | case RS_STPCM8U: |
| 876 | case RS_STPCM8D: |
| 877 | { |
| 878 | int iadd_l = 0, iadd_r = 0; |
| 879 | if (nFlags == RS_STPCM8U) { iadd_l = iadd_r = -128; } |
| 880 | len = pIns->nLength; |
| 881 | signed char *psrc = (signed char *)lpMemFile; |
| 882 | signed char *pSample = (signed char *)pIns->pSample; |
| 883 | if (len*2 > dwMemLength) break; |
| 884 | for (UINT j=0; j<len; j++) |
| 885 | { |
| 886 | pSample[j*2] = (signed char)(psrc[0] + iadd_l); |
| 887 | pSample[j*2+1] = (signed char)(psrc[len] + iadd_r); |
| 888 | psrc++; |
| 889 | if (nFlags == RS_STPCM8D) |
| 890 | { |
| 891 | iadd_l = pSample[j*2]; |
| 892 | iadd_r = pSample[j*2+1]; |
| 893 | } |
| 894 | } |
| 895 | len *= 2; |
| 896 | } |
| 897 | break; |
| 898 | |
| 899 | // 16-bit stereo samples |
| 900 | case RS_STPCM16S: |
| 901 | case RS_STPCM16U: |
| 902 | case RS_STPCM16D: |
| 903 | { |
| 904 | int iadd_l = 0, iadd_r = 0; |
| 905 | if (nFlags == RS_STPCM16U) { iadd_l = iadd_r = -0x8000; } |
| 906 | len = pIns->nLength; |
| 907 | LPCBYTE psrc0 = (LPCBYTE)lpMemFile; |
| 908 | LPCBYTE psrc1 = (LPCBYTE)lpMemFile + len*2; |
| 909 | int16_t *pSample = (int16_t *)pIns->pSample; |
| 910 | if (len*4 > dwMemLength) break; |
| 911 | for (UINT j=0; j<len; j++) |
| 912 | { |
| 913 | pSample[j*2] = (int16_t) (READ_LE16(psrc0) + iadd_l); |
| 914 | pSample[j*2+1] = (int16_t) (READ_LE16(psrc1) + iadd_r); |
| 915 | psrc0 += 2; |
| 916 | psrc1 += 2; |
| 917 | if (nFlags == RS_STPCM16D) |
| 918 | { |
| 919 | iadd_l = pSample[j*2]; |
| 920 | iadd_r = pSample[j*2+1]; |
| 921 | } |
| 922 | } |
| 923 | len *= 4; |
| 924 | } |
| 925 | break; |
| 926 | |
| 927 | #ifndef MODPLUG_BASIC_SUPPORT |
| 928 | // 8-bit interleaved stereo samples |
| 929 | case RS_STIPCM8S: |
| 930 | case RS_STIPCM8U: |
| 931 | { |
| 932 | int iadd = 0; |
| 933 | if (nFlags == RS_STIPCM8U) { iadd = -0x80; } |
| 934 | len = pIns->nLength; |
| 935 | if (len*2 > dwMemLength) len = dwMemLength >> 1; |
| 936 | LPBYTE psrc = (LPBYTE)lpMemFile; |
| 937 | LPBYTE pSample = (LPBYTE)pIns->pSample; |
| 938 | for (UINT j=0; j<len; j++) |
| 939 | { |
| 940 | pSample[j*2] = (signed char)(psrc[0] + iadd); |
| 941 | pSample[j*2+1] = (signed char)(psrc[1] + iadd); |
| 942 | psrc+=2; |
| 943 | } |
| 944 | len *= 2; |
| 945 | } |
| 946 | break; |
| 947 | |
| 948 | // 16-bit interleaved stereo samples |
| 949 | case RS_STIPCM16S: |
| 950 | case RS_STIPCM16U: |
| 951 | { |
| 952 | int iadd = 0; |
| 953 | if (nFlags == RS_STIPCM16U) iadd = -32768; |
| 954 | len = pIns->nLength; |
| 955 | if (len*4 > dwMemLength) len = dwMemLength >> 2; |
| 956 | LPCBYTE psrc = (LPCBYTE)lpMemFile; |
| 957 | int16_t *pSample = (int16_t *)pIns->pSample; |
| 958 | for (UINT j=0; j<len; j++) |
| 959 | { |
| 960 | pSample[j*2] = (int16_t)(READ_LE16(psrc+0) + iadd); |
| 961 | pSample[j*2+1] = (int16_t)(READ_LE16(psrc+2) + iadd); |
| 962 | psrc += 4; |
| 963 | } |
| 964 | len *= 4; |
| 965 | } |
| 966 | break; |
| 967 | |
| 968 | // PTM 8bit delta to 16-bit sample |
| 969 | case RS_PTM8DTO16: |
| 970 | { |
| 971 | len = pIns->nLength * 2; |
| 972 | if (len > dwMemLength) break; |
| 973 | int8_t *pSample = (int8_t *)pIns->pSample; |
| 974 | int8_t delta8 = 0; |
| 975 | for (UINT j=0; j<len; j++) |
| 976 | { |
| 977 | delta8 += lpMemFile[j]; |
| 978 | *pSample++ = delta8; |
| 979 | } |
| 980 | uint16_t *pSampleW = (uint16_t *)pIns->pSample; |
| 981 | for (UINT j=0; j<len; j+=2) // swaparoni! |
| 982 | { |
| 983 | uint16_t rawSample = *pSampleW; |
| 984 | *pSampleW++ = bswapLE16(rawSample); |
| 985 | } |
| 986 | } |
| 987 | break; |
| 988 | #endif // !MODPLUG_BASIC_SUPPORT |
| 989 | |
| 990 | // Default: 8-bit signed PCM data |
| 991 | default: |
| 992 | len = pIns->nLength; |
| 993 | if (len > dwMemLength) len = pIns->nLength = dwMemLength; |
| 994 | memcpy(pIns->pSample, lpMemFile, len); |
| 995 | } |
| 996 | if (len > dwMemLength) |
| 997 | { |
| 998 | if (pIns->pSample) |
| 999 | { |
| 1000 | pIns->nLength = 0; |
| 1001 | FreeSample(pIns->pSample); |
| 1002 | pIns->pSample = NULL; |
| 1003 | } |
| 1004 | return 0; |
| 1005 | } |
| 1006 | AdjustSampleLoop(pIns); |
| 1007 | return len; |
| 1008 | } |
| 1009 | |
| 1010 | |
| 1011 | void CSoundFile::AdjustSampleLoop(MODINSTRUMENT *pIns) |
| 1012 | //---------------------------------------------------- |
| 1013 | { |
| 1014 | if (!pIns->pSample) return; |
| 1015 | if (pIns->nLength > MAX_SAMPLE_LENGTH) pIns->nLength = MAX_SAMPLE_LENGTH; |
| 1016 | if (pIns->nLoopEnd > pIns->nLength) pIns->nLoopEnd = pIns->nLength; |
| 1017 | if (pIns->nLoopStart > pIns->nLength+2) pIns->nLoopStart = pIns->nLength+2; |
| 1018 | if (pIns->nLoopStart+2 >= pIns->nLoopEnd) |
| 1019 | { |
| 1020 | pIns->nLoopStart = pIns->nLoopEnd = 0; |
| 1021 | pIns->uFlags &= ~CHN_LOOP; |
| 1022 | } |
| 1023 | UINT len = pIns->nLength; |
| 1024 | if (pIns->uFlags & CHN_16BIT) |
| 1025 | { |
| 1026 | int16_t *pSample = (int16_t *)pIns->pSample; |
| 1027 | // Adjust end of sample |
| 1028 | if (pIns->uFlags & CHN_STEREO) |
| 1029 | { |
| 1030 | pSample[len*2+6] = pSample[len*2+4] = pSample[len*2+2] = pSample[len*2] = 0; |
| 1031 | pSample[len*2+7] = pSample[len*2+5] = pSample[len*2+3] = pSample[len*2+1] = 0; |
| 1032 | } else |
| 1033 | { |
| 1034 | pSample[len+4] = pSample[len+3] = pSample[len+2] = pSample[len+1] = pSample[len] = 0; |
| 1035 | } |
| 1036 | if ((pIns->uFlags & (CHN_LOOP|CHN_PINGPONGLOOP|CHN_STEREO)) == CHN_LOOP) |
| 1037 | { |
| 1038 | // Fix bad loops |
| 1039 | if ((pIns->nLoopEnd+3 >= pIns->nLength) || (m_nType & MOD_TYPE_S3M)) |
| 1040 | { |
| 1041 | pSample[pIns->nLoopEnd] = pSample[pIns->nLoopStart]; |
| 1042 | pSample[pIns->nLoopEnd+1] = pSample[pIns->nLoopStart+1]; |
| 1043 | pSample[pIns->nLoopEnd+2] = pSample[pIns->nLoopStart+2]; |
| 1044 | pSample[pIns->nLoopEnd+3] = pSample[pIns->nLoopStart+3]; |
| 1045 | pSample[pIns->nLoopEnd+4] = pSample[pIns->nLoopStart+4]; |
| 1046 | } |
| 1047 | } |
| 1048 | } else |
| 1049 | { |
| 1050 | signed char *pSample = pIns->pSample; |
| 1051 | // Crappy samples (except chiptunes) ? |
| 1052 | if ((pIns->nLength > 0x100) && (m_nType & (MOD_TYPE_MOD|MOD_TYPE_S3M)) |
| 1053 | && (!(pIns->uFlags & CHN_STEREO))) |
| 1054 | { |
| 1055 | int smpend = pSample[pIns->nLength-1], smpfix = 0, kscan; |
| 1056 | for (kscan=pIns->nLength-1; kscan>0; kscan--) |
| 1057 | { |
| 1058 | smpfix = pSample[kscan-1]; |
| 1059 | if (smpfix != smpend) break; |
| 1060 | } |
| 1061 | int delta = smpfix - smpend; |
| 1062 | if (((!(pIns->uFlags & CHN_LOOP)) || (kscan > (int)pIns->nLoopEnd)) |
| 1063 | && ((delta < -8) || (delta > 8))) |
| 1064 | { |
| 1065 | while (kscan<(int)pIns->nLength) |
| 1066 | { |
| 1067 | if (!(kscan & 7)) |
| 1068 | { |
| 1069 | if (smpfix > 0) smpfix--; |
| 1070 | if (smpfix < 0) smpfix++; |
| 1071 | } |
| 1072 | pSample[kscan] = (signed char)smpfix; |
| 1073 | kscan++; |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | // Adjust end of sample |
| 1078 | if (pIns->uFlags & CHN_STEREO) |
| 1079 | { |
| 1080 | pSample[len*2+6] = pSample[len*2+4] = pSample[len*2+2] = pSample[len*2] = 0; |
| 1081 | pSample[len*2+7] = pSample[len*2+5] = pSample[len*2+3] = pSample[len*2+1] = 0; |
| 1082 | |
| 1083 | } else |
| 1084 | { |
| 1085 | pSample[len+4] = pSample[len+3] = pSample[len+2] = pSample[len+1] = pSample[len] = 0; |
| 1086 | } |
| 1087 | if ((pIns->uFlags & (CHN_LOOP|CHN_PINGPONGLOOP|CHN_STEREO)) == CHN_LOOP) |
| 1088 | { |
| 1089 | if ((pIns->nLoopEnd+3 >= pIns->nLength) || (m_nType & (MOD_TYPE_MOD|MOD_TYPE_S3M))) |
| 1090 | { |
| 1091 | pSample[pIns->nLoopEnd] = pSample[pIns->nLoopStart]; |
| 1092 | pSample[pIns->nLoopEnd+1] = pSample[pIns->nLoopStart+1]; |
| 1093 | pSample[pIns->nLoopEnd+2] = pSample[pIns->nLoopStart+2]; |
| 1094 | pSample[pIns->nLoopEnd+3] = pSample[pIns->nLoopStart+3]; |
| 1095 | pSample[pIns->nLoopEnd+4] = pSample[pIns->nLoopStart+4]; |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | ///////////////////////////////////////////////////////////// |
| 1103 | // Transpose <-> Frequency conversions |
| 1104 | |
| 1105 | // returns 8363*2^((transp*128+ftune)/(12*128)) |
| 1106 | DWORD CSoundFile::TransposeToFrequency(int transp, int ftune) |
| 1107 | //----------------------------------------------------------- |
| 1108 | { |
| 1109 | return (DWORD)(8363*pow(2, (double)(transp*128+ftune)/(1536))); |
| 1110 | } |
| 1111 | |
| 1112 | |
| 1113 | // returns 12*128*log2(freq/8363) |
| 1114 | int CSoundFile::FrequencyToTranspose(DWORD freq) |
| 1115 | //---------------------------------------------- |
| 1116 | { |
| 1117 | return int(1536*(log(freq/8363.0)/log(2.0))); |
| 1118 | } |
| 1119 | |
| 1120 | |
| 1121 | void CSoundFile::FrequencyToTranspose(MODINSTRUMENT *psmp) |
| 1122 | //-------------------------------------------------------- |
| 1123 | { |
| 1124 | int f2t = FrequencyToTranspose(psmp->nC4Speed); |
| 1125 | int transp = f2t >> 7; |
| 1126 | int ftune = f2t & 0x7F; |
| 1127 | if (ftune > 80) |
| 1128 | { |
| 1129 | transp++; |
| 1130 | ftune -= 128; |
| 1131 | } |
| 1132 | if (transp > 127) transp = 127; |
| 1133 | if (transp < -127) transp = -127; |
| 1134 | psmp->RelativeTone = transp; |
| 1135 | psmp->nFineTune = ftune; |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | void CSoundFile::CheckCPUUsage(UINT nCPU) |
| 1140 | //--------------------------------------- |
| 1141 | { |
| 1142 | if (nCPU > 100) nCPU = 100; |
| 1143 | gnCPUUsage = nCPU; |
| 1144 | if (nCPU < 90) |
| 1145 | { |
| 1146 | m_dwSongFlags &= ~SONG_CPUVERYHIGH; |
| 1147 | } else |
| 1148 | if ((m_dwSongFlags & SONG_CPUVERYHIGH) && (nCPU >= 94)) |
| 1149 | { |
| 1150 | UINT i=MAX_CHANNELS; |
| 1151 | while (i >= 8) |
| 1152 | { |
| 1153 | i--; |
| 1154 | if (Chn[i].nLength) |
| 1155 | { |
| 1156 | Chn[i].nLength = Chn[i].nPos = 0; |
| 1157 | nCPU -= 2; |
| 1158 | if (nCPU < 94) break; |
| 1159 | } |
| 1160 | } |
| 1161 | } else |
| 1162 | if (nCPU > 90) |
| 1163 | { |
| 1164 | m_dwSongFlags |= SONG_CPUVERYHIGH; |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | |
| 1169 | BOOL CSoundFile::SetPatternName(UINT nPat, LPCSTR lpszName) |
| 1170 | //--------------------------------------------------------- |
| 1171 | { |
| 1172 | char szName[MAX_PATTERNNAME] = ""; |
| 1173 | // check input arguments |
| 1174 | if (nPat >= MAX_PATTERNS) return FALSE; |
| 1175 | if (lpszName == NULL) return(FALSE); |
| 1176 | |
| 1177 | if (lpszName) lstrcpynA(szName, lpszName, MAX_PATTERNNAME-1); |
| 1178 | szName[MAX_PATTERNNAME-1] = 0; |
| 1179 | if (!m_lpszPatternNames) m_nPatternNames = 0; |
| 1180 | if (nPat >= m_nPatternNames) |
| 1181 | { |
| 1182 | if (!lpszName[0]) return TRUE; |
| 1183 | UINT len = (nPat+1)*MAX_PATTERNNAME; |
| 1184 | char *p = new char[len]; |
| 1185 | if (!p) return FALSE; |
| 1186 | memset(p, 0, len); |
| 1187 | if (m_lpszPatternNames) |
| 1188 | { |
| 1189 | memcpy(p, m_lpszPatternNames, m_nPatternNames * MAX_PATTERNNAME); |
| 1190 | delete [] m_lpszPatternNames; |
| 1191 | m_lpszPatternNames = NULL; |
| 1192 | } |
| 1193 | m_lpszPatternNames = p; |
| 1194 | m_nPatternNames = nPat + 1; |
| 1195 | } |
| 1196 | memcpy(m_lpszPatternNames + nPat * MAX_PATTERNNAME, szName, MAX_PATTERNNAME); |
| 1197 | return TRUE; |
| 1198 | } |
| 1199 | |
| 1200 | |
| 1201 | BOOL CSoundFile::GetPatternName(UINT nPat, LPSTR lpszName, UINT cbSize) const |
| 1202 | //--------------------------------------------------------------------------- |
| 1203 | { |
| 1204 | if ((!lpszName) || (!cbSize)) return FALSE; |
| 1205 | lpszName[0] = 0; |
| 1206 | if (cbSize > MAX_PATTERNNAME) cbSize = MAX_PATTERNNAME; |
| 1207 | if ((m_lpszPatternNames) && (nPat < m_nPatternNames)) |
| 1208 | { |
| 1209 | memcpy(lpszName, m_lpszPatternNames + nPat * MAX_PATTERNNAME, cbSize); |
| 1210 | lpszName[cbSize-1] = 0; |
| 1211 | return TRUE; |
| 1212 | } |
| 1213 | return FALSE; |
| 1214 | } |
| 1215 | |
| 1216 | |
| 1217 | UINT CSoundFile::DetectUnusedSamples(BOOL *pbIns) |
| 1218 | //----------------------------------------------- |
| 1219 | { |
| 1220 | UINT nExt = 0; |
| 1221 | |
| 1222 | if (!pbIns) return 0; |
| 1223 | if (m_nInstruments) |
| 1224 | { |
| 1225 | memset(pbIns, 0, MAX_SAMPLES * sizeof(BOOL)); |
| 1226 | for (UINT ipat=0; ipat<MAX_PATTERNS; ipat++) |
| 1227 | { |
| 1228 | MODCOMMAND *p = Patterns[ipat]; |
| 1229 | if (p) |
| 1230 | { |
| 1231 | UINT jmax = PatternSize[ipat] * m_nChannels; |
| 1232 | for (UINT j=0; j<jmax; j++, p++) |
| 1233 | { |
| 1234 | if ((p->note) && (p->note <= NOTE_MAX)) |
| 1235 | { |
| 1236 | if ((p->instr) && (p->instr < MAX_INSTRUMENTS)) |
| 1237 | { |
| 1238 | INSTRUMENTHEADER *penv = Headers[p->instr]; |
| 1239 | if (penv) |
| 1240 | { |
| 1241 | UINT n = penv->Keyboard[p->note-1]; |
| 1242 | if (n < MAX_SAMPLES) pbIns[n] = TRUE; |
| 1243 | } |
| 1244 | } else |
| 1245 | { |
| 1246 | for (UINT k=1; k<=m_nInstruments; k++) |
| 1247 | { |
| 1248 | INSTRUMENTHEADER *penv = Headers[k]; |
| 1249 | if (penv) |
| 1250 | { |
| 1251 | UINT n = penv->Keyboard[p->note-1]; |
| 1252 | if (n < MAX_SAMPLES) pbIns[n] = TRUE; |
| 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | for (UINT ichk=1; ichk<=m_nSamples; ichk++) |
| 1261 | { |
| 1262 | if ((!pbIns[ichk]) && (Ins[ichk].pSample)) nExt++; |
| 1263 | } |
| 1264 | } |
| 1265 | return nExt; |
| 1266 | } |
| 1267 | |
| 1268 | |
| 1269 | BOOL CSoundFile::RemoveSelectedSamples(BOOL *pbIns) |
| 1270 | //------------------------------------------------- |
| 1271 | { |
| 1272 | if (!pbIns) return FALSE; |
| 1273 | for (UINT j=1; j<MAX_SAMPLES; j++) |
| 1274 | { |
| 1275 | if ((!pbIns[j]) && (Ins[j].pSample)) |
| 1276 | { |
| 1277 | DestroySample(j); |
| 1278 | if ((j == m_nSamples) && (j > 1)) m_nSamples--; |
| 1279 | } |
| 1280 | } |
| 1281 | return TRUE; |
| 1282 | } |
| 1283 | |
| 1284 | |
| 1285 | BOOL CSoundFile::DestroySample(UINT nSample) |
| 1286 | //------------------------------------------ |
| 1287 | { |
| 1288 | if ((!nSample) || (nSample >= MAX_SAMPLES)) return FALSE; |
| 1289 | if (!Ins[nSample].pSample) return TRUE; |
| 1290 | MODINSTRUMENT *pins = &Ins[nSample]; |
| 1291 | signed char *pSample = pins->pSample; |
| 1292 | pins->pSample = NULL; |
| 1293 | pins->nLength = 0; |
| 1294 | pins->uFlags &= ~(CHN_16BIT); |
| 1295 | for (UINT i=0; i<MAX_CHANNELS; i++) |
| 1296 | { |
| 1297 | if (Chn[i].pSample == pSample) |
| 1298 | { |
| 1299 | Chn[i].nPos = Chn[i].nLength = 0; |
| 1300 | Chn[i].pSample = Chn[i].pCurrentSample = NULL; |
| 1301 | } |
| 1302 | } |
| 1303 | FreeSample(pSample); |
| 1304 | return TRUE; |
| 1305 | } |