SHARE
TWEET

kolibrimod (tiny .mod replayer)

8bitbubsy Sep 11th, 2016 (edited) 698 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* kolibrimod - <500 lines ProTracker .MOD replayer by 8bitbubsy - 21st of June 2019 (7:32PM) - remember to link winmm.lib!
  2. ** Missing effects: E3x (portamento type), E4x (vibrato type), E7x (tremolo type), E8x (Karplus-Strong)
  3. ** Do not use this replayer with an audio rate lower than 32kHz! Mixer is single-step only (max Amiga rate = ~31kHz)
  4. ** Note: This is not very optimized, the main focus is amount of code lines + okay accuracy */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdint.h>
  10. #include <stdbool.h>
  11. #include <conio.h> // _getch() */
  12. #include <math.h> // powf(), sinf()
  13. #include <windows.h> // Sleep(), mixer stuff
  14.  
  15. #define MIX_BUF_NUM 4
  16. #define MIX_BUF_LEN 4096
  17. #define FRAC_BITS 14 /* 18.14 - don't change this! */
  18. #define LERP8(s1, s2, f) { s2 -= s1; s2 *= (int32_t)(f); s1 <<= 8; s2 >>= (FRAC_BITS - 8); s1 += s2; }
  19. #define PERIOD2DELTA(x) (((x) <= 0) ? 0 : (((int32_t)(3546895.0f / roundf((x) * v->fFineTune)) << FRAC_BITS) / audioFreq))
  20. #define BPM2SAMPLES_PER_TICK(x) (int32_t)(((((audioFreq * 125.0f) / 50.0f) / (x))) + 0.5f)
  21. #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
  22.  
  23. typedef struct
  24. {
  25.     int8_t *sampleData, *swapData, vol;
  26.     int16_t panL, panR;
  27.     bool loopFlag, swapLoopFlag;
  28.     int32_t sampleEnd, loopStart, swapSampleEnd, swapLoopStart, volL, volR;
  29.     uint32_t oldPos, pos, delta;
  30.     float fFineTune;
  31. } ptVoice_t;
  32.  
  33. typedef struct
  34. {
  35.     int8_t volume, fineTune, *invLoopPtr, *sampleData, *invLoopStartPtr, jmpRow, jmpCnt;
  36.     uint8_t note, efx, efxDat, sampleOffsetMem, sampleOffset, portaMem, vibDepth, vibSpeed;
  37.     uint8_t tremoloDepth, tremoloSpeed, vibPos, tremoloPos, invLoopSpeed, invLoopPos;
  38.     int16_t rawPeriod, period, toPeriod;
  39.     uint32_t invLoopLen, sampleLen, loopStart, loopLen;
  40. } ptChannel_t;
  41.  
  42. static volatile bool isMixing;
  43. static bool newPosFlag, loopFlag;
  44. static int8_t modSpeed, pattNum, modDelay, newModRow, modRow, *mixerBuffer, *sampleDataPointers[31];
  45. static uint8_t stereoSep, *modData, modOrder;
  46. static int16_t modTick;
  47. static int32_t samplesLeft, samplesPerTick, audioFreq, mixBufferL[MIX_BUF_LEN / 4], mixBufferR[MIX_BUF_LEN / 4];
  48. static const uint8_t invLoopTable[16] = { 0, 5, 6, 7, 8, 10, 11, 13, 16, 19, 22, 26, 32, 43, 64, 128 };
  49. static const int16_t periodTable[36] = {856,808,762,720,678,640,604,570,538,508,480,453,428,404,381,360,339,320,
  50.                                         302,285,269,254,240,226,214,202,190,180,170,160,151,143,135,127,120,113};
  51. static ptVoice_t ptVoice[4];
  52. static ptChannel_t ptChannel[4];
  53. static WAVEHDR waveBlocks[MIX_BUF_NUM];
  54. static HWAVEOUT hWaveOut;
  55. static WAVEFORMATEX wfx;
  56.  
  57. static void tickReplayer(void);
  58.  
  59. static void mixChannels(int16_t *audioOutPtr, int32_t numSamples)
  60. {
  61.     int32_t sample, sample2, pos, pos2;
  62.     ptVoice_t *v;
  63.  
  64.     memset(mixBufferL, 0, sizeof (int32_t) * numSamples);
  65.     memset(mixBufferR, 0, sizeof (int32_t) * numSamples);
  66.  
  67.     for (int32_t i = 0; i < 4; i++)
  68.     {
  69.         v = &ptVoice[i];
  70.         if (v->sampleData == NULL || v->sampleEnd <= 0) continue; // voice is not active
  71.  
  72.         for (int32_t j = 0; j < numSamples; j++)
  73.         {
  74.             pos = v->pos >> FRAC_BITS;
  75.  
  76.             pos2 = pos + 1;
  77.             if (pos2 >= v->sampleEnd)
  78.                 pos2 = v->loopFlag ? v->loopStart : (v->sampleEnd - 1);
  79.  
  80.             if (pos >= v->sampleEnd)
  81.             {
  82.                 if (v->swapData == NULL)
  83.                 {
  84.                     if (v->loopFlag) { pos = v->loopStart; } else { v->sampleData = NULL; break; }
  85.                 }
  86.                 else
  87.                 {
  88.                     // do sample swapping
  89.                     if (!v->swapLoopFlag) { v->sampleData = NULL; break; } // illegal swap
  90.                     v->sampleData = v->swapData;
  91.                     v->sampleEnd = v->swapSampleEnd;
  92.                     v->loopStart = v->swapLoopStart;
  93.                     v->loopFlag = v->swapLoopFlag;
  94.                     v->swapData = NULL; // turn off sample swap
  95.                     pos = v->loopStart;
  96.                     pos2 = v->loopStart + 1;
  97.                 }
  98.             }
  99.  
  100.             v->pos &= (1 << FRAC_BITS) - 1; // keep fractional part only
  101.             sample = v->sampleData[pos];
  102.             sample2 = v->sampleData[pos2];
  103.             LERP8(sample, sample2, v->pos);
  104.             v->pos |= (pos << FRAC_BITS); // add updated position
  105.             v->pos += v->delta;
  106.             mixBufferL[j] += (sample * v->volL) >> 16; // -32768..32767
  107.             mixBufferR[j] += (sample * v->volR) >> 16; // -32768..32767
  108.         }
  109.     }
  110.  
  111.     for (int32_t i = 0; i < numSamples; i++)
  112.     {
  113.         *audioOutPtr++ = (int16_t)(mixBufferL[i] >> 2); // shift back into -32768..32767
  114.         *audioOutPtr++ = (int16_t)(mixBufferR[i] >> 2); // shift back into -32768..32767
  115.     }
  116. }
  117.  
  118. static void CALLBACK waveOutProc(HWAVEOUT _hWaveOut, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
  119. {
  120.     int16_t *outputStream;
  121.     int32_t sampleBlock, samplesTodo;
  122.     WAVEHDR *waveBlockHeader;
  123.  
  124.     if (uMsg != MM_WOM_DONE) return;
  125.     waveBlockHeader = (WAVEHDR *)dwParam1;
  126.     waveOutUnprepareHeader(_hWaveOut, waveBlockHeader, sizeof (WAVEHDR));
  127.  
  128.     if (!isMixing) return;
  129.     memcpy(waveBlockHeader->lpData, mixerBuffer, MIX_BUF_LEN);
  130.     waveOutPrepareHeader(_hWaveOut, waveBlockHeader, sizeof (WAVEHDR));
  131.     waveOutWrite(_hWaveOut, waveBlockHeader, sizeof (WAVEHDR));
  132.  
  133.     outputStream = (int16_t *)mixerBuffer;
  134.     sampleBlock = MIX_BUF_LEN / 4; // /2 for stereo + /2 for 16-bit
  135.     while (sampleBlock)
  136.     {
  137.         samplesTodo = (sampleBlock < samplesLeft) ? sampleBlock : samplesLeft;
  138.         if (samplesTodo > 0)
  139.         {
  140.             mixChannels(outputStream, samplesTodo);
  141.             outputStream += (uint32_t)samplesTodo * 2;
  142.             sampleBlock -= samplesTodo;
  143.             samplesLeft -= samplesTodo;
  144.         }
  145.         else
  146.         {
  147.             tickReplayer();
  148.             samplesLeft = samplesPerTick;
  149.         }
  150.     }
  151. }
  152.  
  153. void kolibrimodLoad(FILE *in, int32_t audioOutputFreq, uint8_t audioStereoSeparation)
  154. {
  155.     uint8_t *ptr8;
  156.     int32_t modFilesize;
  157.  
  158.     if (modData != NULL) return;
  159.  
  160.     fseek(in, 0, SEEK_END);
  161.     modFilesize = ftell(in);
  162.     modData = (uint8_t *)malloc(modFilesize);
  163.     rewind(in);
  164.     fread(modData, 1, modFilesize, in);
  165.  
  166.     // count number of patterns
  167.     for (uint8_t i = 0; i < 128; i++) if (modData[952 + i] > pattNum) pattNum = modData[952 + i];
  168.  
  169.     // set sample data pointers
  170.     ptr8 = &modData[1084 + (1024 * (pattNum + 1))];
  171.     for (uint8_t i = 0; i < 31; i++)
  172.     {
  173.         sampleDataPointers[i] = (int8_t *)ptr8;
  174.         ptr8 += ((modData[42 + (30 * i)] << 8) | modData[(42 + (30 * i)) + 1]) * 2;
  175.     }
  176.  
  177.     memset(&wfx, 0, sizeof (wfx));
  178.     audioFreq = audioOutputFreq;
  179.     stereoSep = audioStereoSeparation;
  180.     wfx.nSamplesPerSec = audioFreq;
  181.     wfx.wBitsPerSample = 16;
  182.     wfx.nChannels = 2;
  183.     wfx.wFormatTag = WAVE_FORMAT_PCM;
  184.     wfx.nBlockAlign = (wfx.wBitsPerSample * wfx.nChannels) / 8;
  185.     wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
  186.     mixerBuffer = (int8_t *)calloc(MIX_BUF_LEN, 1);
  187.  
  188.     waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, (DWORD_PTR)waveOutProc, 0, CALLBACK_FUNCTION);
  189.     for (uint8_t i = 0; i < MIX_BUF_NUM; i++)
  190.     {
  191.         waveBlocks[i].dwBufferLength = MIX_BUF_LEN;
  192.         waveBlocks[i].lpData = (LPSTR)calloc(MIX_BUF_LEN, 1);
  193.         waveOutPrepareHeader(hWaveOut, &waveBlocks[i], sizeof (WAVEHDR));
  194.         waveOutWrite(hWaveOut, &waveBlocks[i], sizeof (WAVEHDR));
  195.     }
  196. }
  197.  
  198. void kolibrimodPlay(void)
  199. {
  200.     if (modData == NULL) return; // no module loaded
  201.  
  202.     memset(ptChannel, 0, sizeof (ptChannel));
  203.     memset(ptVoice, 0, sizeof (ptVoice));
  204.  
  205.     for (uint8_t i = 0; i < 4; i++) // set up channel pannings from stereo separation (0..1024)
  206.     {
  207.         ptVoice[i].panR = ((i==0)||(i==3) ? (128-((stereoSep*128)/100)) : (128+((stereoSep*128)/100))) << 2;
  208.         ptVoice[i].panL = (256 << 2) - ptVoice[i].panR;
  209.     }
  210.  
  211.     samplesPerTick = BPM2SAMPLES_PER_TICK(125);
  212.     modSpeed = 6;
  213.     modTick  = modSpeed - 1; // don't render a tick of silence
  214.     loopFlag = modRow = newModRow = newPosFlag = 0;
  215.     isMixing = modDelay = 1;
  216. }
  217.  
  218. void kolibrimodFree(void)
  219. {
  220.     isMixing = 0;
  221.     for (uint8_t i = 0; i < MIX_BUF_NUM; i++) waveOutUnprepareHeader(hWaveOut, &waveBlocks[i], sizeof (WAVEHDR));
  222.  
  223.     for (uint8_t i = 0; i < MIX_BUF_NUM; i++)
  224.     {
  225.         while (waveBlocks[i].dwFlags & WHDR_PREPARED) SleepEx(1, 1); // wait
  226.         if (waveBlocks[i].lpData != NULL) free(waveBlocks[i].lpData); waveBlocks[i].lpData = NULL;
  227.     }
  228.  
  229.     waveOutReset(hWaveOut);
  230.     waveOutClose(hWaveOut);
  231.  
  232.     if (modData != NULL) free(modData); modData = NULL;
  233.     if (mixerBuffer != NULL) free(mixerBuffer); mixerBuffer = NULL;
  234. }
  235.  
  236. static void triggerVoice(ptChannel_t *c, ptVoice_t *v)
  237. {
  238.     v->sampleData = c->sampleData;
  239.     v->loopStart = c->loopStart;
  240.     v->loopFlag = (c->loopStart + c->loopLen) > 2;
  241.     v->sampleEnd = v->loopFlag ? (c->loopStart + c->loopLen) : c->sampleLen;
  242.     v->swapData = NULL;
  243.     v->fFineTune = powf(2.0f, c->fineTune * (1.0f / -(12.0f * 8.0f)));
  244.     v->oldPos = (c->sampleOffset * 256) << FRAC_BITS; // 9xx sample offset
  245.     v->pos = v->oldPos;
  246.     c->period = c->toPeriod;
  247. }
  248.  
  249. static void portamento(ptChannel_t *c)
  250. {
  251.     if (c->period <= 0 || c->toPeriod <= 0) return;
  252.  
  253.     if (c->period > c->toPeriod)
  254.     {
  255.         c->period -= c->portaMem;
  256.         if (c->period <= c->toPeriod) c->period = c->toPeriod;
  257.     }
  258.     else if (c->period < c->toPeriod)
  259.     {
  260.         c->period += c->portaMem;
  261.         if (c->period >= c->toPeriod) c->period = c->toPeriod;
  262.     }
  263. }
  264.  
  265. static void vibrato(ptChannel_t *c, ptVoice_t *v)
  266. {
  267.     if (c->period > 0)
  268.         v->delta = PERIOD2DELTA(CLAMP(c->period + (int32_t)(c->vibDepth*sinf(c->vibPos*(2.0f*3.1415926f/64.0f))), 113, 856));
  269.     if (modTick > 0) c->vibPos = (c->vibPos + c->vibSpeed) & 63;
  270. }
  271.  
  272. static void tremolo(ptChannel_t *c, ptVoice_t *v)
  273. {
  274.     v->vol = c->volume + (int8_t)(c->tremoloDepth * sinf(c->tremoloPos * (2.0f * 3.1415926f / 64.0f)));
  275.     v->vol = CLAMP(v->vol, 0, 64);
  276.     if (modTick > 0) c->tremoloPos = (c->tremoloPos + c->tremoloSpeed) & 63;
  277. }
  278.  
  279. static void volumeSlide(ptChannel_t *c)
  280. {
  281.     if (modTick > 0)
  282.         c->volume += (c->efxDat & 0xF0) ? (c->efxDat >> 4) : -(c->efxDat & 0x0F);
  283.     c->volume = CLAMP(c->volume, 0, 64);
  284. }
  285.  
  286. static void invertLoop(ptChannel_t *c)
  287. {
  288.     if ((c->invLoopPos += invLoopTable[c->invLoopSpeed]) < 128) return;
  289.     c->invLoopPos = 0;
  290.  
  291.     if (c->invLoopPtr == NULL) return;
  292.     if (++c->invLoopPtr >= c->invLoopStartPtr+c->invLoopLen) c->invLoopPtr = c->invLoopStartPtr;
  293.     *c->invLoopPtr = -1 - *c->invLoopPtr;
  294. }
  295.  
  296. static void checkEfx(void)
  297. {
  298.     int16_t period;
  299.     ptChannel_t *c;
  300.     ptVoice_t *v;
  301.  
  302.     for (int16_t i = 0; i < 4; i++) // do effects
  303.     {
  304.         c = &ptChannel[i]; v = &ptVoice[i];
  305.  
  306.         if (modTick > 0) invertLoop(c);
  307.  
  308.         if (c->efx != 0 || c->efxDat != 0)
  309.         {
  310.             // these are handled on non-zero ticks only in ProTracker, but it sounds SO BAD! PT vibrato/tremolo sucks, let's do it better
  311.             if (c->efx == 0x4) { if (c->efxDat & 0x0F) c->vibDepth = (c->efxDat & 0x0F) * 2; if (c->efxDat & 0xF0) c->vibSpeed = c->efxDat >> 4; vibrato(c, v); }
  312.             if (c->efx == 0x7) { if (c->efxDat & 0x0F) c->tremoloDepth = (c->efxDat & 0x0F) * 4; if (c->efxDat & 0xF0) c->tremoloSpeed = c->efxDat >> 4; tremolo(c, v); }
  313.             if (c->efx == 0x6) { vibrato(c, v); volumeSlide(c); }
  314.  
  315.             if (modTick == 0)
  316.             {    // tick=0 effects
  317.                      if (c->efx == 0xB) { newPosFlag = 1; newModRow = 0; modOrder = c->efxDat - 1; }
  318.                 else if (c->efx == 0xC) c->volume = (c->efxDat > 64) ? 64 : c->efxDat;
  319.                 else if (c->efx == 0xD) { newPosFlag = 1; newModRow = ((c->efxDat >> 4) * 10) + (c->efxDat & 0x0F); if (newModRow > 63) newModRow = 0; }
  320.                 else if (c->efx == 0xE)
  321.                 {
  322.                          if ((c->efxDat & 0xF0) == 0x10) { if (c->period > 0) { c->period -= c->efxDat & 0x0F; if (c->period < 113) c->period = 113; } }
  323.                     else if ((c->efxDat & 0xF0) == 0x20) { if (c->period > 0) { c->period += c->efxDat & 0x0F; if (c->period > 856) c->period = 856; } }
  324.                     else if ((c->efxDat & 0xF0) == 0x50) c->fineTune = ((c->efxDat & 0x0F) >= 8) ? ((c->efxDat & 0x0F) - 16) : (c->efxDat & 0x0F);
  325.                     else if ((c->efxDat & 0xF0) == 0x60) { if (c->efxDat == 0x60) c->jmpRow = modRow; else { if (!c->jmpCnt) c->jmpCnt = c->efxDat & 0x0F; else if (!--c->jmpCnt) return; newModRow = c->jmpRow; loopFlag = 1; }}
  326.                     else if ((c->efxDat & 0xF0) == 0xA0) { c->volume += c->efxDat & 0x0F; if (c->volume > 64) c->volume = 64; }
  327.                     else if ((c->efxDat & 0xF0) == 0xB0) { c->volume -= c->efxDat & 0x0F; if (c->volume < 0) c->volume = 0; }
  328.                     else if ((c->efxDat & 0xF0) == 0xE0) modDelay = (c->efxDat & 0x0F) + 1;
  329.                     else if ((c->efxDat & 0xF0) == 0xF0) { c->invLoopSpeed = c->efxDat & 0x0F; invertLoop(c); }
  330.                 }
  331.                 else if (c->efx == 0xF) if (c->efxDat < 0x20) { if (c->efxDat) modSpeed = c->efxDat; } else { samplesPerTick = BPM2SAMPLES_PER_TICK(c->efxDat); }
  332.             }
  333.             else
  334.             {   // tick>0 effects
  335.                      if (c->efx == 0x1) { if (c->period > 0) { c->period -= c->efxDat; if (c->period < 113) c->period = 113; } }
  336.                 else if (c->efx == 0x2) { if (c->period > 0) { c->period += c->efxDat; if (c->period > 856) c->period = 856; } }
  337.                 else if (c->efx == 0x3) { if (c->efxDat) c->portaMem = c->efxDat; portamento(c); }
  338.                 else if (c->efx == 0x5) { portamento(c); volumeSlide(c); }
  339.                 else if (c->efx == 0xE) { if ((c->efxDat & 0xF0) == 0x90) { if (c->efxDat == 0x90) return; if (!(modTick % (c->efxDat & 0x0F))) v->pos = v->oldPos; }}
  340.                 else if (c->efx == 0xA) volumeSlide(c);
  341.             }
  342.         }
  343.  
  344.              if ((c->efx == 0xE) && (c->efxDat & 0xF0) == 0xC0) { if (modTick == (c->efxDat & 0x0F)) c->volume = 0; }
  345.         else if ((c->efx == 0xE) && (c->efxDat & 0xF0) == 0xD0) { if (c->rawPeriod >= 113 && modTick == (c->efxDat & 0x0F)) triggerVoice(c, v); }
  346.  
  347.         period = c->period;
  348.         if (period > 0 && c->efx == 0 && c->efxDat != 0) // do arpeggio here
  349.         {
  350.                  if ((modTick % 3) == 0) period = periodTable[c->note];
  351.             else if ((modTick % 3) == 1) period = periodTable[(c->note + (c->efxDat >>   4)) % 36];
  352.             else if ((modTick % 3) == 2) period = periodTable[(c->note + (c->efxDat & 0x0F)) % 36];
  353.         }
  354.  
  355.         if (c->efx != 4 && c->efx != 6 && period > 0) v->delta = PERIOD2DELTA(period); // set voice pitch
  356.         if (c->efx != 7) v->vol = c->volume; // set voice volume
  357.         v->volL = v->vol * v->panL;
  358.         v->volR = v->vol * v->panR;
  359.     }
  360. }
  361.  
  362. static void tickReplayer(void)
  363. {
  364.     uint8_t *pattPtr, *ptr8, i, note, smp;
  365.     int16_t period;
  366.     ptChannel_t *c;
  367.     ptVoice_t *v;
  368.  
  369.     if (++modTick >= modSpeed*modDelay)
  370.     {
  371.         modTick = 0;
  372.         modDelay = 1; // reset EEx (pattern delay) | 1 = no delay
  373.  
  374.         for (i = 0; i < 4; i++) // read pattern data
  375.         {
  376.             c = &ptChannel[i]; v = &ptVoice[i];
  377.  
  378.             pattPtr = &modData[1084 + (modData[952 + modOrder] * 1024) + (modRow * 16) + (i * 4)];
  379.  
  380.             c->rawPeriod = period = (((pattPtr[0] & 0x0F) << 8) | pattPtr[1]);
  381.             c->efx = pattPtr[2] & 0x0F;
  382.             c->efxDat = pattPtr[3];
  383.  
  384.             smp = (pattPtr[0] & 0xF0) | (pattPtr[2] >> 4);
  385.             if (smp >= 1 && smp <= 31) // sample found in pattern data
  386.             {
  387.                 ptr8 = &modData[42 + (30 * (smp - 1))];
  388.                 c->fineTune = ((ptr8[2] & 0x0F) >= 8) ? ((ptr8[2] & 0x0F) - 16) : (ptr8[2] & 0x0F);
  389.                 c->volume = (ptr8[3] > 64) ? 64 : ptr8[3];
  390.                 c->sampleLen = ((ptr8[0] << 8) | ptr8[1]) * 2;
  391.                 c->loopStart = ((ptr8[4] << 8) | ptr8[5]) * 2;
  392.                 c->loopLen = ((ptr8[6] << 8) | ptr8[7]) * 2;
  393.                 c->sampleData = sampleDataPointers[smp - 1];
  394.                 c->sampleOffset = 0;
  395.  
  396.                 if (!(c->efx == 0xE && (c->efxDat & 0xF0) == 0xD0)) // don't update voice during note delay
  397.                 {
  398.                     if (v->sampleData && !period)
  399.                     {   // do sample swap (if voice is active and no pattern note)
  400.                         v->swapData = c->sampleData;
  401.                         v->swapLoopStart = c->loopStart;
  402.                         v->swapLoopFlag = (c->loopStart + c->loopLen) > 2;
  403.                         v->swapSampleEnd = v->swapLoopFlag ? (c->loopStart + c->loopLen) : c->sampleLen;
  404.                     }
  405.                     else
  406.                     {   // set sample like normal
  407.                         v->sampleData = c->sampleData;
  408.                         v->loopStart = c->loopStart;
  409.                         v->loopFlag = (c->loopStart + c->loopLen) > 2;
  410.                         v->sampleEnd = v->loopFlag ? (c->loopStart + c->loopLen) : c->sampleLen;
  411.                         v->swapData = NULL;
  412.                     }
  413.                 }
  414.  
  415.                 c->invLoopLen = c->loopLen;
  416.                 c->invLoopPtr = c->invLoopStartPtr = c->sampleData + c->loopStart;
  417.             }
  418.  
  419.             if (c->efx == 0x9) // 9xx "sample offset" effect (must be handled here)
  420.             {
  421.                 if (c->efxDat) c->sampleOffsetMem = c->efxDat;
  422.                 c->sampleOffset = c->sampleOffsetMem;
  423.             }
  424.  
  425.             if (period > 0) // note found in pattern data
  426.             {
  427.                 if (c->efx == 0xE && (c->efxDat & 0xF0) == 0x50)
  428.                     c->fineTune = ((c->efxDat & 0x0F) >= 8) ? ((c->efxDat & 0x0F) - 16) : (c->efxDat & 0x0F);
  429.  
  430.                 for (note = 0; note < 36; note++) if (period >= periodTable[note]) break;
  431.  
  432.                 if (note < 36) // note was found in period table
  433.                 {
  434.                     c->note = note;
  435.                     c->toPeriod = periodTable[note];
  436.  
  437.                     if (c->efx != 0x03 && c->efx != 0x05 && !(c->efx != 0xE && (c->efxDat & 0xF0) == 0xD0))
  438.                     {
  439.                         c->vibPos = c->tremoloPos = 0; // reset vibrato/tremolo position
  440.                         triggerVoice(c, v);
  441.                         v->delta = PERIOD2DELTA(c->period); // set initial voice pitch
  442.                     }
  443.                 }
  444.             }
  445.         }
  446.  
  447.         checkEfx();
  448.  
  449.         if (loopFlag) // handle pattern loop (E6x)
  450.         {
  451.             modRow = newModRow;
  452.             newPosFlag = loopFlag = newModRow = 0;
  453.         }
  454.         else if (++modRow >= 64) newPosFlag = true; // increase row like normal
  455.     }
  456.     else checkEfx();
  457.  
  458.     if (newPosFlag) // handle position jump (Bxx), pattern break (Dxx) or end of pattern
  459.     {
  460.         modRow = newModRow;
  461.         newModRow = 0;
  462.         newPosFlag = false;
  463.         if (++modOrder >= modData[950]) modOrder = (modData[951] < modData[950]) ? modData[951] : 0; // song loop
  464.     }
  465. }
  466.  
  467. int main(int argc, char *argv[]) // please edit this for your own use!
  468. {
  469.     FILE *f;
  470.  
  471.     if (argc != 2)
  472.     {
  473.         printf("Usage: kolibrimod.exe <module>\n");
  474.         return -1;
  475.     }
  476.  
  477.     f = fopen(argv[1], "rb");
  478.     if (f == NULL)
  479.     {
  480.         printf("ERROR: Can't open file!\n");
  481.         return 1;
  482.     }
  483.  
  484.     kolibrimodLoad(f, 48000, 19); // file, audio frequency, stereo separation (0..100%)
  485.     fclose(f);
  486.     kolibrimodPlay();
  487.  
  488.     printf("Playing at %dHz with %d%% stereo separation... Press any key to stop.\n", audioFreq, stereoSep);
  489.     while (!_getch()) Sleep(250);
  490.     kolibrimodFree();
  491.  
  492.     return 0;
  493. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top