Advertisement
8bitbubsy

kolibrimod (small .mod replayer)

Sep 11th, 2016 (edited)
1,723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 24.59 KB | None | 0 0
  1. /* kolibrimod - small 1..99ch .MOD replayer by 8bitbubsy - 19th of December 2024 - remember to link winmm.lib!
  2. ** License: BSD-3 clause
  3. */
  4.  
  5. #define AUDIO_RATE 44100
  6. #define AUDIO_BUF_LEN 1024
  7.  
  8. #include <assert.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdint.h>
  13. #include <stdbool.h>
  14. #include <conio.h> // _getch()
  15. #include <math.h> // powf()
  16. #include <windows.h> // Sleep(), mixer stuff
  17.  
  18. #define AMIGA_STEREO_SEPARATION 25 /* percentage */
  19. #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
  20. #define BPM_TO_SAMPLESPERTICK(bpm) ((audioFreq * 5) / (bpm * 2))
  21.  
  22. typedef struct
  23. {
  24.     uint8_t note, sample, efx, param;
  25. } note_t;
  26.  
  27. typedef struct
  28. {
  29.     bool loopFlag;
  30.     int8_t *data;
  31.     uint8_t finetune, volume;
  32.     uint32_t length, loopStart, loopLength;
  33. } sample_t;
  34.  
  35. typedef struct
  36. {
  37.     int8_t *sampleData, *swapData;
  38.     int16_t pan; // 0..256
  39.     bool loopFlag, swapLoopFlag;
  40.     uint32_t sampleEnd, loopStart, loopLength, swapSampleEnd, swapLoopStart, swapLoopLength, oldPos, pos;
  41.     float fVolL, fVolR, fFrac, fDelta;
  42. } voice_t;
  43.  
  44. typedef struct
  45. {
  46.     bool noteFlag, sampleLoopFlag;
  47.     int8_t vol, volOut, finetune, *invLoopPtr, *sampleData, *invLoopStartPtr, E6Row, E6Cnt;
  48.     uint8_t note, efx, efx2, portaMem, vibratoDepth, vibratoSpeed, glissandoMode, lastSample;
  49.     uint8_t tremoloDepth, tremoloSpeed, vibratoPos, vibratoType, tremoloPos, tremoloType, invLoopSpeed, invLoopPos;
  50.     uint16_t period, toPeriod, periodOut;
  51.     int32_t sampleOffsetMem, sampleOffset, invLoopLen, sampleLength, sampleLoopStart, sampleLoopLength;
  52. } channel_t;
  53.  
  54. static volatile bool isMixing;
  55. static bool newPosFlag, patLoop, ptMode;
  56. static char songName[20+1];
  57. static int8_t modSpeed, pattNum, tickExtendFactor, newRow, row, *mixerBuffer;
  58. static uint8_t modOrder, restartPos, orderNum, orders[128];
  59. static uint16_t modTick, minPeriod, maxPeriod, periodTab[16][96];
  60. static int32_t pattSize, channels, audioFreq, tickSampleCounter, samplesPerTick;
  61. static note_t *patterns[128], *patternPtr;
  62. static sample_t samples[31];
  63. static float fMixBuffer[AUDIO_BUF_LEN*2], fPeriod2DeltaDividend;
  64. static const uint8_t invLoopTab[16] = { 0, 5, 6, 7, 8, 10, 11, 13, 16, 19, 22, 26, 32, 43, 64, 128 };
  65. static const uint8_t arpTab[32] = { 0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1 };
  66. static const uint16_t ptPeriods[3*12] = // PT C-3..B-5 note->period table (values multiplied by 4)
  67. {
  68.     3424,3232,3048,2880,2712,2560,2416,2280,2152,2032,1920,1812,
  69.     1712,1616,1524,1440,1356,1280,1208,1140,1076,1016, 960, 904,
  70.      856, 808, 760, 720, 680, 640, 604, 572, 540, 508, 480, 452
  71. };
  72. static const uint16_t loadPeriods[8*12] = // table for period->note on load
  73. {
  74.     6848, 6464, 6096, 5760, 5424, 5120, 4832, 4560, 4304, 4064, 3840, 3624,
  75.     3424, 3232, 3048, 2880, 2712, 2560, 2416, 2280, 2152, 2032, 1920, 1812,
  76.     1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016,  960,  906,
  77.      856,  808,  762,  720,  678,  640,  604,  570,  538,  508,  480,  453,
  78.      428,  404,  381,  360,  339,  320,  302,  285,  269,  254,  240,  226,
  79.      214,  202,  190,  180,  170,  160,  151,  143,  135,  127,  120,  113,
  80.      107,  101,   95,   90,   85,   80,   75,   71,   67,   63,   60,   56,
  81.       53,   50,   47,   45,   42,   40,   37,   35,   33,   31,   30,   28
  82. };
  83. static const uint16_t ft2Periods[8*12] = // FT2 C-0..B-7 note->period table (Amiga periods, some notes differ from PT)
  84. {
  85.     27392,25856,24384,23040,21696,20480,19328,18240,17216,16256,15360,14512,
  86.     13696,12928,12192,11520,10848,10240, 9664, 9120, 8608, 8128, 7680, 7256,
  87.      6848, 6464, 6096, 5760, 5424, 5120, 4832, 4560, 4304, 4064, 3840, 3628,
  88.      3424, 3232, 3048, 2880, 2712, 2560, 2416, 2280, 2152, 2032, 1920, 1814,
  89.      1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016,  960,  907,
  90.       856,  808,  762,  720,  678,  640,  604,  570,  538,  508,  480,  453,
  91.       428,  404,  381,  360,  339,  320,  302,  285,  269,  254,  240,  227,
  92.       214,  202,  190,  180,  169,  160,  151,  142,  134,  127,  120,  113
  93. };
  94. static const uint8_t sinusTab[32] =
  95. {
  96.     0x00,0x18,0x31,0x4A,0x61,0x78,0x8D,0xA1,0xB4,0xC5,0xD4,0xE0,0xEB,0xF4,0xFA,0xFD,
  97.     0xFF,0xFD,0xFA,0xF4,0xEB,0xE0,0xD4,0xC5,0xB4,0xA1,0x8D,0x78,0x61,0x4A,0x31,0x18
  98. };
  99. static voice_t voice[100];
  100. static channel_t channel[100];
  101. static WAVEHDR waveBlocks[4];
  102. static HWAVEOUT hWaveOut;
  103. static WAVEFORMATEX wfx;
  104.  
  105. static void tickReplayer(void);
  106.  
  107. static uint8_t periodToNote(uint16_t period, uint8_t ftune)
  108. {
  109.     int32_t i;
  110.     for (i = 0; i < 8*12; i++) if (period >= periodTab[ftune][i]) break;
  111.     return (uint8_t)i;
  112. }
  113.  
  114. static void mixChannels(float *fMixPtr, uint32_t numSamples)
  115. {
  116.     voice_t *v = voice;
  117.     for (int32_t i = 0; i < channels; i++, v++)
  118.     {
  119.         if (v->sampleData == NULL || v->sampleEnd == 0) continue; // voice is not active
  120.         float *fCurrMixPtr = fMixPtr;
  121.         for (uint32_t j = 0; j < numSamples; j++)
  122.         {
  123.             const float fSample1 = v->sampleData[v->pos];
  124.  
  125.             float fSample2; // read next sample point for linear interpolation
  126.             if (v->pos+1 < v->sampleEnd) // next sample point below end of sample?
  127.             {
  128.                 fSample2 = v->sampleData[v->pos+1];
  129.             }
  130.             else // next sample is after sample's end (or loop), fetch correct sample point
  131.             {
  132.                 if (v->swapData == NULL) // no sample swap (common case)
  133.                     fSample2 = v->loopFlag ? v->sampleData[v->loopStart] : (float)v->sampleData[v->sampleEnd-1];
  134.                 else // sample swap
  135.                     fSample2 = v->swapLoopFlag ? v->sampleData[v->swapLoopStart] : (float)v->sampleData[v->sampleEnd-1];
  136.             }
  137.  
  138.             const float fSample = fSample1 + ((fSample2 - fSample1) * v->fFrac);
  139.             *fCurrMixPtr++ += fSample * v->fVolL;
  140.             *fCurrMixPtr++ += fSample * v->fVolR;
  141.             v->fFrac += v->fDelta;
  142.             const int32_t wholeSamples = (int32_t)v->fFrac;
  143.             v->fFrac -= wholeSamples;
  144.             v->pos += wholeSamples;
  145.  
  146.             if (v->pos >= v->sampleEnd) // end of sample?
  147.             {
  148.                 if (v->swapData == NULL) // no sample swap to do
  149.                 {
  150.                     if (v->loopFlag)
  151.                     {
  152.                         do
  153.                         {
  154.                             v->pos -= v->loopLength;
  155.                         }
  156.                         while (v->pos >= v->sampleEnd);
  157.                     }
  158.                     else
  159.                     {
  160.                         v->sampleData = NULL;
  161.                         break;
  162.                     }
  163.                 }
  164.                 else // do sample swapping
  165.                 {
  166.                     if (!v->swapLoopFlag)
  167.                     {
  168.                         v->sampleData = NULL; // illegal swap, turn off voice
  169.                         break;
  170.                     }
  171.  
  172.                     v->sampleData = v->swapData;
  173.                     v->sampleEnd = v->swapSampleEnd;
  174.                     v->loopStart = v->swapLoopStart;
  175.                     v->loopLength = v->swapLoopLength;
  176.                     v->loopFlag = v->swapLoopFlag;
  177.                     v->pos = v->loopStart;
  178.                     v->swapData = NULL; // current sample swap has finished
  179.                 }
  180.             }
  181.         }
  182.     }
  183. }
  184.  
  185. static void CALLBACK waveOutProc(HWAVEOUT _hWaveOut, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
  186. {
  187.     if (uMsg != MM_WOM_DONE) return;
  188.     WAVEHDR *waveBlockHeader = (WAVEHDR *)dwParam1;
  189.     waveOutUnprepareHeader(_hWaveOut, waveBlockHeader, sizeof (WAVEHDR));
  190.  
  191.     if (!isMixing) return;
  192.     memcpy(waveBlockHeader->lpData, mixerBuffer, AUDIO_BUF_LEN * 2 * sizeof (int16_t));
  193.     waveOutPrepareHeader(_hWaveOut, waveBlockHeader, sizeof (WAVEHDR));
  194.     waveOutWrite(_hWaveOut, waveBlockHeader, sizeof (WAVEHDR));
  195.  
  196.     memset(fMixBuffer, 0, AUDIO_BUF_LEN * 2 * sizeof (float));
  197.  
  198.     float *fMixBufferPtr = fMixBuffer;
  199.     int32_t samplesLeft = AUDIO_BUF_LEN;
  200.     while (samplesLeft > 0)
  201.     {
  202.         if (tickSampleCounter == 0)
  203.         {
  204.             tickReplayer();
  205.             tickSampleCounter += samplesPerTick;
  206.         }
  207.  
  208.         int32_t samplesToMix = samplesLeft;
  209.         if (samplesToMix > tickSampleCounter) samplesToMix = tickSampleCounter;
  210.  
  211.         mixChannels(fMixBufferPtr, samplesToMix);
  212.         fMixBufferPtr += samplesToMix * 2;
  213.         samplesLeft -= samplesToMix;
  214.         tickSampleCounter -= samplesToMix;
  215.     }
  216.  
  217.     fMixBufferPtr = fMixBuffer;
  218.     int16_t *streamOut = (int16_t *)mixerBuffer;
  219.     for (int32_t i = 0; i < AUDIO_BUF_LEN; i++)
  220.     {
  221.         const int32_t smpL = (int32_t)(*fMixBufferPtr++ * (32768.0f / 2.0f));
  222.         const int32_t smpR = (int32_t)(*fMixBufferPtr++ * (32768.0f / 2.0f));
  223.         *streamOut++ = (int16_t)CLAMP(smpL, INT16_MIN, INT16_MAX);
  224.         *streamOut++ = (int16_t)CLAMP(smpR, INT16_MIN, INT16_MAX);
  225.     }
  226.  
  227.     (void)dwParam2;
  228.     (void)dwInstance;
  229. }
  230.  
  231. #define ID(x) !strcmp(magic, x)
  232. static bool getNumbersOfChannels(FILE *in)
  233. {
  234.     char magic[5]; magic[4] = '\0';
  235.     fseek(in, 1080, SEEK_SET);
  236.     fread(magic, 1, 4, in);
  237.     rewind(in);
  238.  
  239.     ptMode = false;
  240.     channels = 0;
  241.     if (ID("M.K.") || ID("M!K!") || ID("NSMS") || ID("LARD") || ID("PATT")) { ptMode = true; channels = 4; } // ProTracker (or compatible)
  242.     else if (magic[1] == 'C' && magic[2] == 'H' && magic[3] == 'N') channels = magic[0] - '0'; // FT2/others
  243.     else if (magic[2] == 'C' && magic[3] == 'H') channels = ((magic[0] - '0') * 10) + (magic[1] - '0'); // FT2/others
  244.  
  245.     if (channels < 1 || channels > 99) return false;
  246.     pattSize = channels * (64 * 4);
  247.     return true;
  248. }
  249.  
  250. static void freeData(void)
  251. {
  252.     for (int32_t i = 0; i < 128; i++)
  253.     {
  254.         if (patterns[i] != NULL)
  255.         {
  256.             free(patterns[i]);
  257.             patterns[i] = NULL;
  258.         }
  259.     }
  260.  
  261.     for (int32_t i = 0; i < 31; i++)
  262.     {
  263.         if (samples[i].data != NULL)
  264.         {
  265.             free(samples[i].data);
  266.             samples[i].data = NULL;
  267.         }
  268.     }
  269. }
  270.  
  271. bool kolibrimodLoad(FILE *in, int32_t audioOutputFreq)
  272. {
  273.     freeData();
  274.     if (!getNumbersOfChannels(in)) return false;
  275.  
  276.     rewind(in);
  277.     fread(songName, 1, 20, in); songName[20] = '\0';
  278.     fseek(in, 950, SEEK_SET);
  279.     fread(&orderNum, 1, 1, in);
  280.     fread(&restartPos, 1, 1, in);
  281.     if (orderNum > 128) return false; // too many orders!
  282.  
  283.     // count number of patterns
  284.     pattNum = 0;
  285.     fseek(in, 952, SEEK_SET);
  286.     for (uint8_t i = 0; i < 128; i++)
  287.     {
  288.         orders[i] = (uint8_t)fgetc(in);
  289.         if (orders[i] > pattNum) pattNum = orders[i];
  290.     }
  291.     pattNum++;
  292.  
  293.     // read sample structs
  294.     memset(samples, 0, sizeof (samples));
  295.     fseek(in, 20, SEEK_SET);
  296.     sample_t *smp = samples;
  297.     for (int32_t i = 0; i < 31; i++, smp++)
  298.     {
  299.         fseek(in, 22, SEEK_CUR);
  300.         uint8_t bytes[8]; fread(bytes, 1, 8, in);
  301.         smp->finetune = bytes[2] & 0xF;
  302.         smp->volume = (bytes[3] > 64) ? 64 : bytes[3];
  303.         smp->length = ((bytes[0] << 8) | bytes[1]) * 2;
  304.         smp->loopStart = ((bytes[4] << 8) | bytes[5]) * 2;
  305.         smp->loopLength = ((bytes[6] << 8) | bytes[7]) * 2;
  306.         smp->loopFlag = (smp->loopStart+smp->loopLength) > 2;
  307.     }
  308.  
  309.     // load pattern data
  310.     fseek(in, 1084, SEEK_SET);
  311.     for (int32_t i = 0; i < pattNum; i++)
  312.     {
  313.         patterns[i] = (note_t *)malloc(pattSize);
  314.         if (patterns[i] == NULL) { freeData(); return false; };
  315.         note_t *p = patterns[i];
  316.         for (int32_t j = 0; j < channels*64; j++, p++)
  317.         {
  318.             uint8_t bytes[4]; fread(bytes, 1, 4, in);
  319.             uint16_t period = ((bytes[0] & 0x0F) << 8) | bytes[1];
  320.             if (ptMode && period != 0 && (period < 113 || period > 856)) // scan for extended periods
  321.                 ptMode = false;
  322.        
  323.             int32_t k = 0; // convert period to note number (0..95, 96 = none/illegal)
  324.             for (; k < 8*12; k++)
  325.                 if (period >= loadPeriods[k])
  326.                     break;
  327.             p->note = k;
  328.  
  329.             p->sample = (bytes[0] & 0xF0) | (bytes[2] >> 4);
  330.             p->efx = bytes[2] & 0xF;
  331.             p->param = bytes[3];
  332.         }
  333.     }
  334.  
  335.     // load sample data
  336.     fseek(in, 1084 + (pattSize * pattNum), SEEK_SET);
  337.     smp = samples;
  338.     for (int32_t i = 0; i < 31; i++, smp++)
  339.     {
  340.         if (smp->length == 0) continue;
  341.         smp->data = (int8_t *)malloc(smp->length);
  342.         if (smp->data == NULL) { freeData(); return false; };
  343.         fread(smp->data, 1, smp->length, in);
  344.     }
  345.  
  346.     memset(&wfx, 0, sizeof (wfx));
  347.     audioFreq = audioOutputFreq;
  348.     wfx.nSamplesPerSec = audioFreq;
  349.     wfx.wBitsPerSample = 16;
  350.     wfx.nChannels = 2;
  351.     wfx.wFormatTag = WAVE_FORMAT_PCM;
  352.     wfx.nBlockAlign = (wfx.wBitsPerSample * wfx.nChannels) / 8;
  353.     wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
  354.     mixerBuffer = (int8_t *)calloc(AUDIO_BUF_LEN*4, 1);
  355.  
  356.     waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, (DWORD_PTR)waveOutProc, 0, CALLBACK_FUNCTION);
  357.     for (int32_t i = 0; i < 4; i++)
  358.     {
  359.         waveBlocks[i].dwBufferLength = AUDIO_BUF_LEN*4;
  360.         waveBlocks[i].lpData = (LPSTR)calloc(AUDIO_BUF_LEN*4, 1);
  361.         waveOutPrepareHeader(hWaveOut, &waveBlocks[i], sizeof (WAVEHDR));
  362.         waveOutWrite(hWaveOut, &waveBlocks[i], sizeof (WAVEHDR));
  363.     }
  364.  
  365.     // calculate finetuned period table
  366.     const int32_t noteOfs = ptMode ? (3*12) : 0;
  367.     const uint16_t *note2Period = ptMode ? ptPeriods : ft2Periods;
  368.     for (int32_t ftune = 0; ftune < 16; ftune++)
  369.     {
  370.         const float fMul = powf(2.0f, ((ftune ^ 8) - 8) / -(12.0f * 8.0f));
  371.         for (int32_t note = 0; note < 8*12; note++)
  372.         {
  373.             periodTab[ftune][note] = UINT16_MAX; // illegal note
  374.             if (!(ptMode && (note < 3*12 || note >= 6*12)))
  375.                 periodTab[ftune][note] = (uint16_t)((note2Period[note-noteOfs] * fMul) + 0.5f); // rounded
  376.         }
  377.     }
  378.  
  379.     minPeriod = ptMode ? 113 * 4 : 1;
  380.     maxPeriod = 32000-1;
  381.  
  382.     const int32_t periodClock = ptMode ? 3546895 * 4 : 3579364 * 4;
  383.     fPeriod2DeltaDividend = (float)periodClock / audioFreq;
  384.     return true;
  385. }
  386.  
  387. bool kolibrimodPlay(void)
  388. {
  389.     memset(channel, 0, sizeof (channel));
  390.     memset(voice, 0, sizeof (voice));
  391.  
  392.     for (int32_t i = 0; i < channels; i++) // set up initial LRRL channel pannings
  393.     {
  394.         voice[i].pan = 128; // center (initial position for non-PT modules)
  395.         const int32_t panning = (AMIGA_STEREO_SEPARATION * 128) / 100;
  396.         if (ptMode)
  397.             voice[i].pan = !((i + 1) & 2) ? (128-panning) : (128+panning); // Amiga panning - LRRL
  398.     }
  399.  
  400.     modSpeed = 6;
  401.     samplesPerTick = BPM_TO_SAMPLESPERTICK(125);
  402.     tickSampleCounter = 0; // zero tick sample counter so that it will instantly initiate a tick
  403.     modTick = modSpeed-1; // don't render a tick of silence
  404.     row = newRow = modOrder = 0;
  405.     tickExtendFactor = 1; // 1 = no delay
  406.     newPosFlag = patLoop = false;
  407.     patternPtr = patterns[orders[modOrder]];
  408.     isMixing = true;
  409.     return true;
  410. }
  411.  
  412. void kolibrimodFree(void)
  413. {
  414.     isMixing = false;
  415.     for (int32_t i = 0; i < 4; i++)
  416.         waveOutUnprepareHeader(hWaveOut, &waveBlocks[i], sizeof (WAVEHDR));
  417.  
  418.     for (int32_t i = 0; i < 4; i++)
  419.     {
  420.         while (waveBlocks[i].dwFlags & WHDR_PREPARED) SleepEx(1, 1); // wait
  421.         if (waveBlocks[i].lpData != NULL) free(waveBlocks[i].lpData); waveBlocks[i].lpData = NULL;
  422.     }
  423.  
  424.     waveOutReset(hWaveOut);
  425.     waveOutClose(hWaveOut);
  426.  
  427.     if (mixerBuffer != NULL)
  428.     {
  429.         free(mixerBuffer);
  430.         mixerBuffer = NULL;
  431.     }
  432.  
  433.     freeData();
  434. }
  435.  
  436. static void triggerVoice(channel_t *c, voice_t *v)
  437. {
  438.     v->sampleData = c->sampleData;
  439.     v->loopStart = c->sampleLoopStart;
  440.     v->loopFlag = c->sampleLoopFlag;
  441.     v->sampleEnd = v->loopFlag ? (c->sampleLoopStart + c->sampleLoopLength) : c->sampleLength;
  442.     v->loopLength = c->sampleLoopLength;
  443.     v->swapData = NULL;
  444.     v->fFrac = 0.0f;
  445.     c->period = c->toPeriod;
  446.  
  447.     v->pos = v->oldPos = c->sampleOffset; // 9xx sample offset * 256
  448.     if (v->pos >= v->sampleEnd)
  449.     {
  450.         if (v->loopFlag)
  451.             v->pos = v->loopStart;
  452.         else
  453.             v->sampleData = NULL; // cut voice
  454.     }
  455. }
  456.  
  457. static void portamento(channel_t *c)
  458. {
  459.     if (c->period == 0 || c->toPeriod == 0) return;
  460.  
  461.     if (c->period > c->toPeriod)
  462.     {
  463.         c->period -= c->portaMem*4;
  464.         if ((int16_t)c->period <= c->toPeriod) c->period = c->toPeriod;
  465.     }
  466.     else if (c->period < c->toPeriod)
  467.     {
  468.         c->period += c->portaMem*4;
  469.         if (c->period >= c->toPeriod) c->period = c->toPeriod;
  470.     }
  471.  
  472.     c->periodOut = c->period;
  473.     if (c->glissandoMode != 0) // semitones slide
  474.     {
  475.         const uint8_t note = periodToNote(c->periodOut, c->finetune);
  476.         if (note < 96) c->periodOut = periodTab[c->finetune][note];
  477.     }
  478. }
  479.  
  480. static uint16_t getWaveform(channel_t *c, bool tremoloFlag)
  481. {
  482.     const uint8_t type = tremoloFlag ? c->tremoloType : c->vibratoType;
  483.     uint8_t pos = ((tremoloFlag ? c->tremoloPos : c->vibratoPos) >> 2) & 0x1F;
  484.  
  485.     uint8_t data;
  486.          if (type == 0) { data = sinusTab[pos]; }
  487.     else if (type == 1) { data = pos << 3; if (c->vibratoPos >= 128) data = ~data; } // PT2/FT2 bug: always uses vibrato pos
  488.     else                { data = 255; }
  489.  
  490.     return tremoloFlag ? ((data * c->tremoloDepth) >> 6) : ((data * c->vibratoDepth) >> (7-2));
  491. }
  492.  
  493. static void vibrato(channel_t *c)
  494. {
  495.     const uint16_t data = getWaveform(c, false);
  496.     c->periodOut = c->period + ((c->vibratoPos < 128) ? data : -data);
  497.     if (modTick > 0) c->vibratoPos += c->vibratoSpeed;
  498. }
  499.  
  500. static void tremolo(channel_t *c)
  501. {
  502.     const uint16_t data = getWaveform(c, true);
  503.     c->volOut = c->vol + ((c->tremoloPos < 128) ? data : -data);
  504.     c->volOut = CLAMP(c->volOut, 0, 64);
  505.     if (modTick > 0) c->tremoloPos += c->tremoloSpeed;
  506. }
  507.  
  508. static void volumeSlide(channel_t *c)
  509. {
  510.     if (modTick == 0) return;
  511.     c->vol += (c->efx2 & 0xF0) ? (c->efx2 >> 4) : -(c->efx2 & 0xF);
  512.     c->vol = CLAMP(c->vol, 0, 64);
  513. }
  514.  
  515. static void invertLoop(channel_t *c) // ProTracker only
  516. {
  517.     if ((c->invLoopPos += invLoopTab[c->invLoopSpeed]) < 128) return;
  518.     c->invLoopPos = 0;
  519.     if (c->invLoopPtr == NULL) return;
  520.     if (++c->invLoopPtr >= c->invLoopStartPtr+c->invLoopLen) c->invLoopPtr = c->invLoopStartPtr;
  521.     *c->invLoopPtr = -1 - *c->invLoopPtr;
  522. }
  523.  
  524. static void retrigNote(channel_t *c, voice_t *v) // E9x effect
  525. {
  526.     if (c->efx2 > 0x90 && !(modTick == 0 && c->noteFlag) && !(modTick % (c->efx2 & 0xF)))
  527.     {
  528.         v->pos = v->oldPos;
  529.         v->fFrac = 0.0f;
  530.     }
  531. }
  532.  
  533. static void checkEfx(void)
  534. {
  535.     uint16_t period;
  536.     channel_t *c = channel;
  537.     voice_t *v = voice;
  538.     for (int32_t i = 0; i < channels; i++, c++, v++) // do effects
  539.     {
  540.         if (ptMode && modTick > 0) invertLoop(c);
  541.  
  542.         if (c->efx != 0 || c->efx2 != 0)
  543.         {
  544.             if (c->efx == 0x4) { if (c->efx2&0xF) c->vibratoDepth = c->efx2&0xF; if (c->efx2&0xF0) c->vibratoSpeed = (c->efx2>>4)*4; vibrato(c); }
  545.             if (c->efx == 0x7) { if (c->efx2&0xF) c->tremoloDepth = c->efx2&0xF; if (c->efx2&0xF0) c->tremoloSpeed = (c->efx2>>4)*4; tremolo(c); }
  546.             if (c->efx == 0x6) { vibrato(c); volumeSlide(c); }
  547.  
  548.             if (modTick == 0)
  549.             {   // tick=0 effects
  550.                      if (c->efx == 0x8) { if (!ptMode) v->pan = c->efx2; }
  551.                 else if (c->efx == 0xB) { newPosFlag = true; newRow = 0; modOrder = c->efx2 - 1; }
  552.                 else if (c->efx == 0xC) { c->vol = (c->efx2 > 64) ? 64 : c->efx2; }
  553.                 else if (c->efx == 0xD) { newPosFlag = true; newRow = ((c->efx2 >> 4) * 10) + (c->efx2 & 0xF); if (newRow > 63) newRow = 0; }
  554.                 else if (c->efx == 0xE)
  555.                 {
  556.                          if ((c->efx2&0xF0)==0x10) { if (c->period > 0) { c->period -= (c->efx2 & 0xF)*4; if ((int16_t)c->period < minPeriod) c->period = minPeriod; } }
  557.                     else if ((c->efx2&0xF0)==0x20) { if (c->period > 0) { c->period += (c->efx2 & 0xF)*4; if ((int16_t)c->period > maxPeriod) c->period = maxPeriod; } }
  558.                     else if ((c->efx2&0xF0)==0x30) { c->glissandoMode = c->efx2 & 0xF; }
  559.                     else if ((c->efx2&0xF0)==0x40) { c->vibratoType = c->efx2 & 0xF; }
  560.                     else if ((c->efx2&0xF0)==0x50) { c->finetune = ptMode ? (c->efx2 & 0xF) : (c->efx2 - 8) & 0xF; }
  561.                     else if ((c->efx2&0xF0)==0x60) { if (c->efx2==0x60) c->E6Row = row; else { if (!c->E6Cnt) c->E6Cnt = c->efx2&0xF;
  562.                                                                            else if (!--c->E6Cnt) return; newRow = c->E6Row; patLoop = true; }}
  563.                     else if ((c->efx2&0xF0)==0x70) { c->tremoloType = c->efx2 & 0xF; }
  564.                     else if ((c->efx2&0xF0)==0x90) retrigNote(c, v);
  565.                     else if ((c->efx2&0xF0)==0xA0) { c->vol += c->efx2 & 0xF; if (c->vol > 64) c->vol = 64; }
  566.                     else if ((c->efx2&0xF0)==0xB0) { c->vol -= c->efx2 & 0xF; if (c->vol < 0) c->vol = 0; }
  567.                     else if ((c->efx2&0xF0)==0xE0) { tickExtendFactor = (c->efx2 & 0xF) + 1; }
  568.                     else if ((c->efx2&0xF0)==0xF0) { if (ptMode) { c->invLoopSpeed = c->efx2 & 0xF; invertLoop(c); } }
  569.                 }
  570.                 else if (c->efx == 0xF) if (c->efx2 < 0x20) { if (c->efx2) modSpeed = c->efx2; } else { samplesPerTick = BPM_TO_SAMPLESPERTICK(c->efx2); }
  571.             }
  572.             else
  573.             {   // tick>0 effects
  574.                      if (c->efx == 0x1) { if (c->period > 0) { c->period -= c->efx2*4; if ((int16_t)c->period < minPeriod) c->period = minPeriod; } }
  575.                 else if (c->efx == 0x2) { if (c->period > 0) { c->period += c->efx2*4; if ((int16_t)c->period > maxPeriod) c->period = maxPeriod; } }
  576.                 else if (c->efx == 0x3) { if (c->efx2) c->portaMem = c->efx2; portamento(c); }
  577.                 else if (c->efx == 0x5) { portamento(c); volumeSlide(c); }
  578.                 else if (c->efx == 0xE) { if ((c->efx2 & 0xF0) == 0x90) retrigNote(c, v); }
  579.                 else if (c->efx == 0xA) { volumeSlide(c); }
  580.             }
  581.         }
  582.        
  583.              if ((c->efx == 0xE) && (c->efx2 & 0xF0) == 0xC0) { if (modTick == (c->efx2 & 0xF)) c->vol = 0; }
  584.         else if ((c->efx == 0xE) && (c->efx2 & 0xF0) == 0xD0) { if (modTick == (c->efx2 & 0xF) && c->noteFlag) triggerVoice(c, v); }
  585.  
  586.         period = c->period;
  587.         if (c->efx == 0 && c->efx2 != 0 && period > 0) // do arpeggio here
  588.         {
  589.             uint8_t note = periodToNote(period, c->finetune);
  590.             if (note < 96)
  591.             {
  592.                 const uint8_t arpTick = arpTab[modTick];
  593.                      if (arpTick == 1) note += c->efx2 >> 4;
  594.                 else if (arpTick == 2) note += c->efx2 & 0xF;
  595.                 if (note > 95) note = 95;
  596.                 period = periodTab[c->finetune][note];
  597.             }
  598.         }
  599.  
  600.         // vibrato and portamento modifies the output period directly
  601.         if (c->efx != 4 && c->efx != 6 && c->efx != 3 && c->efx != 5) c->periodOut = period;
  602.         if (c->periodOut == 0) v->fDelta = 0.0f; else v->fDelta = (float)(fPeriod2DeltaDividend / CLAMP(c->periodOut, minPeriod, maxPeriod));
  603.  
  604.         if (c->efx != 7) c->volOut = c->vol; // tremolo modifies the output volume directly
  605.         v->fVolL = (c->volOut * (256-v->pan)) * (1.0f / (64.0f * 256.0f * 128.0f)); // scale = vol(0..64) * pan(0..256) * 8BitSmpScale(128)
  606.         v->fVolR = (c->volOut *      v->pan ) * (1.0f / (64.0f * 256.0f * 128.0f));
  607.     }
  608. }
  609.  
  610. static void tickReplayer(void)
  611. {
  612.     if (++modTick >= modSpeed*tickExtendFactor)
  613.     {
  614.         modTick = 0;
  615.         tickExtendFactor = 1; // reset EEx (pattern delay). 1 = no delay
  616.  
  617.         channel_t *c = channel;
  618.         voice_t *v = voice;
  619.         for (uint8_t i = 0; i < channels; i++, c++, v++) // read pattern data
  620.         {
  621.             const note_t *note = &patternPtr[(row*channels) + i];
  622.             c->noteFlag = note->note != 96;
  623.             const uint8_t sample = note->sample;
  624.             c->efx = note->efx;
  625.             c->efx2 = note->param;
  626.  
  627.             const bool noteDelay = (c->efx == 0xE) && ((c->efx2 & 0xF0) == 0xD0);
  628.             const bool porta = (c->efx == 0x3 || c->efx == 0x5);
  629.  
  630.             if (sample >= 1 && sample <= 31) // sample found in pattern data
  631.             {
  632.                 sample_t *s = &samples[sample-1];
  633.                 c->finetune = s->finetune;
  634.                 c->volOut = c->vol = s->volume;
  635.                 c->sampleLength = s->length;
  636.                 c->sampleLoopStart = s->loopStart;
  637.                 c->sampleLoopLength = s->loopLength;
  638.                 c->sampleLoopFlag = s->loopFlag;
  639.                 c->sampleData = s->data;
  640.                 c->sampleOffset = 0;
  641.                 if (!ptMode) v->pan = 128; // reset panning (FT2 mode)
  642.  
  643.                 if (sample != c->lastSample && ptMode && (v->sampleData != NULL) && (!c->noteFlag || porta))
  644.                 {   // do ProTracker sample swap (if voice is active and no pattern note)
  645.                     v->swapData = c->sampleData;
  646.                     v->swapLoopStart = c->sampleLoopStart;
  647.                     v->swapLoopLength = c->sampleLoopLength;
  648.                     v->swapLoopFlag = c->sampleLoopFlag;
  649.                     v->swapSampleEnd = v->swapLoopFlag ? (c->sampleLoopStart + c->sampleLoopLength) : c->sampleLength;
  650.                 }
  651.  
  652.                 c->invLoopLen = c->sampleLoopLength;
  653.                 c->invLoopPtr = c->invLoopStartPtr = &c->sampleData[c->sampleLoopStart];
  654.                 c->lastSample = sample;
  655.             }
  656.  
  657.             if (c->efx == 0x9) // 9xx "set sample offset" efx must be handled here
  658.             {
  659.                 if (c->efx2 > 0) c->sampleOffsetMem = c->efx2 << 8;
  660.                 c->sampleOffset = c->sampleOffsetMem;
  661.             }
  662.  
  663.             if (c->noteFlag) // note found in pattern data
  664.             {
  665.                 if (c->efx == 0xE && (c->efx2 & 0xF0) == 0x50) // set-finetune efx must be handled here as well
  666.                     c->finetune = ptMode ? (c->efx2 & 0xF) : (c->efx2 - 8) & 0xF;
  667.  
  668.                 c->note = note->note;
  669.                 c->toPeriod = periodTab[c->finetune][c->note];
  670.  
  671.                 // trigger sample if no portamento (3xx and 5xy) and no note delay
  672.                 if (c->efx != 0x03 && c->efx != 0x05 && !noteDelay)
  673.                 {
  674.                     c->vibratoPos = c->tremoloPos = 0; // reset vibrato/tremolo position
  675.                     triggerVoice(c, v);
  676.                     c->periodOut = c->period = c->toPeriod; // set initial period
  677.                 }
  678.             }
  679.         }
  680.  
  681.         checkEfx();
  682.  
  683.         if (patLoop) // handle pattern loop (E6x)
  684.         {
  685.             row = newRow;
  686.             newRow = 0;
  687.             newPosFlag = patLoop = false;
  688.         }
  689.         else if (++row >= 64) newPosFlag = true; // increase row like normal
  690.     }
  691.     else checkEfx();
  692.  
  693.     if (newPosFlag) // handle position jump (Bxx), pattern break (Dxx) or end of pattern
  694.     {
  695.         row = newRow;
  696.         newRow = 0;
  697.         newPosFlag = false;
  698.         if (++modOrder >= orderNum)
  699.             modOrder = (restartPos < orderNum) ? restartPos : 0; // loop (wrap) song
  700.         patternPtr = patterns[orders[modOrder]];
  701.     }
  702. }
  703.  
  704. int main(int argc, char *argv[]) // please edit this for your own use!
  705. {
  706. #ifdef _DEBUG
  707.     FILE *f = fopen("debug.mod", "rb");
  708. #else
  709.     if (argc != 2)
  710.     {
  711.         printf("Usage: kolibrimod.exe <module>\n");
  712.         return -1;
  713.     }
  714.  
  715.     FILE *f = fopen(argv[1], "rb");
  716. #endif
  717.     if (f == NULL)
  718.     {
  719.         printf("ERROR: Can't open file!\n");
  720.         return 1;
  721.     }
  722.  
  723.     if (!kolibrimodLoad(f, AUDIO_RATE))
  724.     {
  725.         printf("ERROR: This .MOD is not supported!\n");
  726.         return 1;
  727.     }
  728.  
  729.     fclose(f);
  730.     kolibrimodPlay();
  731.  
  732.     printf("Playing \"%s\" (%dHz - %d channels)\nPress any key to stop...\n",
  733.         songName, audioFreq, channels);
  734.     while (!_getch()) Sleep(250);
  735.     kolibrimodFree();
  736.  
  737.     return 0;
  738. }
  739.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement