daily pastebin goal
77%
SHARE
TWEET

fc14play v1.2

8bitbubsy Nov 17th, 2015 (edited) 390 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ** FC14PLAY v1.21 - 27th of August 2017 - http://16-bits.org
  3. ** =========================================================
  4. ** - NOT BIG ENDIAN SAFE! -
  5. **
  6. ** Very accurate C port of Future Composer 1.4's replayer,
  7. ** by Olav "8bitbubsy" Sørensen, using a FC1.4 disassembly (its supplied replayer code was buggy).
  8. ** Works perfectly with v1.0..v1.3 modules as well.
  9. **
  10. ** The BLEP (Band-Limited Step) and filter routines were coded by aciddose.
  11. **
  12. **
  13. ** You need to link winmm.lib for this to compile (-lwinmm)
  14. **
  15. ** User functions:
  16. **
  17. ** #include <stdint.h>
  18. **
  19. ** int8_t fc14play_Init(uint32_t outputFreq, const uint8_t *moduleData);
  20. ** void fc14play_Close(void);
  21. ** void fc14play_PauseSong(int8_t pause);
  22. ** void fc14play_PlaySong(void);
  23. ** void fc14play_SetStereoSep(uint8_t percentage);
  24. */
  25.  
  26. /* == USER ADJUSTABLE SETTINGS == */
  27. #define STEREO_SEP (17)    /* --> Initial stereo separation in percent - 0 = mono, 100 = hard pan (like Amiga) */
  28. #define NORM_FACTOR (4.0f) /* --> Slightly increase this value if the song is too loud. Decrease if too quiet... */
  29. #define USE_HIGHPASS       /* --> 5.2Hz high-pass filter present in all Amigas - comment out for a speed-up  */
  30. //#define USE_LOWPASS      /* --> 4.4kHz low-pass filter in all Amigas except A1200 - comment out for sharper sound */
  31. #define USE_BLEP           /* --> Reduces some unwanted aliasing (closer to real Amiga) - comment out for a speed-up */
  32.  
  33. /* for the best possible audio quality, enable everything except USE_LOWPASS */
  34. /* ------------------------------ */
  35.  
  36. #ifdef _MSC_VER
  37. #define inline __forceinline
  38. #endif
  39.  
  40. /* used for faster windows.h parsing when compiling */
  41. #ifndef WIN32_LEAN_AND_MEAN
  42. #define WIN32_LEAN_AND_MEAN
  43. #endif
  44.  
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <stdint.h>
  48. #include <math.h>     /* tanf() */
  49. #include <windows.h>  /* win32 audio mixer */
  50. #include <mmsystem.h> /* win32 audio mixer */
  51.  
  52. #define PAULA_PAL_CLK 3546895
  53. #define AMIGA_VBLANK_RATE 50 /* 60 for NTSC replay speed */
  54.  
  55. /* don't change these two unless you know what you're doing... */
  56. #define MIX_BUF_NUM 7
  57. #define MIX_BUF_LEN 2048
  58.  
  59. #define SEQ_SIZE 13
  60. #define PAT_MAX_SIZE 64
  61. #define PAT_END_MARKER 0x49
  62. #define VOL_TAB_SIZE 64
  63. #define FREQ_TAB_SIZE 64
  64. #define NUM_SAMPLES 10
  65. #define NUM_WAVEFORMS 80
  66. #define NUM_WAVEFORMS_SMOD 47
  67.  
  68. /* BLEP CONSTANTS */
  69. #define BLEP_ZC 8
  70. #define BLEP_OS 5
  71. #define BLEP_SP 5
  72. #define BLEP_NS (BLEP_ZC * BLEP_OS / BLEP_SP)
  73. #define BLEP_RNS 7 /* RNS = (2^ > NS) - 1 */
  74.  
  75. /* STRUCTS */
  76. typedef struct blep_t
  77. {
  78.     int32_t index, samplesLeft;
  79.     float buffer[BLEP_RNS + 1], lastValue;
  80. } blep_t;
  81.  
  82. typedef struct lossyIntegrator_t
  83. {
  84.     float buffer[2], coeff[2];
  85. } lossyIntegrator_t;
  86.  
  87. typedef struct ledFilter_t
  88. {
  89.     float led[4];
  90. } ledFilter_t;
  91.  
  92. typedef struct ledFilterCoeff_t
  93. {
  94.     float led, ledFb;
  95. } ledFilterCoeff_t;
  96.  
  97. typedef struct paulaVoice_t
  98. {
  99.     const int8_t *SRC_DAT;
  100.     int8_t DMA_ON;
  101.     const int8_t *DMA_DAT;
  102.     uint32_t DMA_LEN, DMA_POS, SRC_LEN;
  103.     float SRC_VOL, DELTA, FRAC, LASTDELTA, LASTFRAC, PANL, PANR;
  104. } paulaVoice_t;
  105.  
  106. typedef struct soundInfo_t
  107. {
  108.     /* don't change the order of these */
  109.     int8_t *data;
  110.     uint16_t length;
  111.     int8_t *repeat;
  112.     uint16_t replen;
  113. } soundInfo_t;
  114.  
  115. typedef struct fcChannel_t
  116. {
  117.     int8_t pitchBendValue, pitchBendCounter, note;
  118.     int8_t noteTranspose, soundTranspose, *loopStart;
  119.     int8_t volume, periodTranspose;
  120.     uint8_t voiceIndex, *seqStartPtr, *patPtr, *freqTabPtr;
  121.     uint8_t freqSusCounter, *volTabPtr, volSusCounter, DMAWaitFrames;
  122.     uint8_t pitchBendDelay, vibratoSpeed, vibratoDepth, vibratoCounter;
  123.     uint8_t vibratoDelay, vibratoUp, volSlideSpeed, volSlideDelay;
  124.     uint8_t volSlideCounter, portaParam, portaDelay, volDelayCounter;
  125.     uint8_t volDelayLength;
  126.     int16_t portaValue;
  127.     uint16_t loopLength, freqTabPos, volTabPos, patPos;
  128.     uint32_t seqPos;
  129. } fcChannel_t;
  130.  
  131. /* STATIC DATA */
  132.  
  133. static volatile int8_t mixingMutex, isMixing;
  134. static int8_t initdone, *ptr8s_1, *ptr8s_1, *mixerBuffer;
  135. static uint8_t spdtemp, spdtemp2, respcnt,  repspd, onoff, fc14;
  136. static uint8_t *SEQpoint, *PATpoint, *FRQpoint, *VOLpoint;
  137. static uint8_t *ptr8u_1, *ptr8u_2, *ptr8u_3;
  138. static uint16_t audtemp, *ptr16u_1, *ptr16u_2, *ptr16u_3;
  139. static int32_t soundBufferSize, samplesLeft;
  140. static uint32_t numSequences;
  141. static volatile uint32_t samplesPerFrame;
  142. static float *masterBufferL, *masterBufferR, f_outputFreq;
  143. static paulaVoice_t AUD[4] =
  144. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  145. static fcChannel_t Channel[4];
  146. static soundInfo_t samples[NUM_SAMPLES + NUM_WAVEFORMS];
  147. static WAVEHDR waveBlocks[MIX_BUF_NUM];
  148. static HWAVEOUT hWaveOut;
  149. static WAVEFORMATEX wfx;
  150.  
  151. #ifdef USE_BLEP
  152. static blep_t blep[4], blepVol[4];
  153. #endif
  154. #ifdef USE_HIGHPASS
  155. static lossyIntegrator_t filterHi;
  156. #endif
  157. #ifdef USE_LOWPASS
  158. static lossyIntegrator_t filterLo;
  159. #endif
  160.  
  161. /* MACROS */
  162.  
  163. #define LERP(x, y, z) ((x) + ((y) - (x)) * (z))
  164. #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
  165. #define CLAMP16(i) \
  166. { \
  167.     if ((int16_t)(i) != i) \
  168.         i = 0x7FFF ^ (i >> 31); \
  169. }
  170.  
  171. #define PTR2LONG(x) ((uint32_t *)(x))
  172. #define PTR2WORD(x) ((uint16_t *)(x))
  173. #define SWAP16(x) ((uint16_t)(((x) << 8) | ((x) >> 8)))
  174. #define SWAP32(value) \
  175. ( \
  176.     (((uint32_t)((value) & 0x000000FF)) << 24) | \
  177.     (((uint32_t)((value) & 0x0000FF00)) <<  8) | \
  178.     (((uint32_t)((value) & 0x00FF0000)) >>  8) | \
  179.     (((uint32_t)((value) & 0xFF000000)) >> 24)   \
  180. )
  181.  
  182. /* TABLES */
  183.  
  184. static uint8_t silentTable[8] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE1 };
  185.  
  186. static const uint16_t periods[128] =
  187. {
  188.     // 1.0..1.3 periods
  189.     0x06B0, 0x0650, 0x05F4, 0x05A0, 0x054C, 0x0500, 0x04B8, 0x0474,
  190.     0x0434, 0x03F8, 0x03C0, 0x038A, 0x0358, 0x0328, 0x02FA, 0x02D0,
  191.     0x02A6, 0x0280, 0x025C, 0x023A, 0x021A, 0x01FC, 0x01E0, 0x01C5,
  192.     0x01AC, 0x0194, 0x017D, 0x0168, 0x0153, 0x0140, 0x012E, 0x011D,
  193.     0x010D, 0x00FE, 0x00F0, 0x00E2, 0x00D6, 0x00CA, 0x00BE, 0x00B4,
  194.     0x00AA, 0x00A0, 0x0097, 0x008F, 0x0087, 0x007F, 0x0078, 0x0071,
  195.     0x0071, 0x0071, 0x0071, 0x0071, 0x0071, 0x0071, 0x0071, 0x0071,
  196.     0x0071, 0x0071, 0x0071, 0x0071,
  197.  
  198.     // 1.4 periods
  199.     0x0D60, 0x0CA0, 0x0BE8, 0x0B40, 0x0A98, 0x0A00, 0x0970, 0x08E8,
  200.     0x0868, 0x07F0, 0x0780, 0x0714, 0x06B0, 0x0650, 0x05F4, 0x05A0,
  201.     0x054C, 0x0500, 0x04B8, 0x0474, 0x0434, 0x03F8, 0x03C0, 0x038A,
  202.     0x0358, 0x0328, 0x02FA, 0x02D0, 0x02A6, 0x0280, 0x025C, 0x023A,
  203.     0x021A, 0x01FC, 0x01E0, 0x01C5, 0x01AC, 0x0194, 0x017D, 0x0168,
  204.     0x0153, 0x0140, 0x012E, 0x011D, 0x010D, 0x00FE, 0x00F0, 0x00E2,
  205.     0x00D6, 0x00CA, 0x00BE, 0x00B4, 0x00AA, 0x00A0, 0x0097, 0x008F,
  206.     0x0087, 0x007F, 0x0078, 0x0071, 0x0071, 0x0071, 0x0071, 0x0071,
  207.     0x0071, 0x0071, 0x0071, 0x0071
  208. };
  209.  
  210. static const int8_t waveformDatas[1344] =
  211. {
  212.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  213.     0x3F,0x37,0x2F,0x27,0x1F,0x17,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  214.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  215.     0xC0,0x37,0x2F,0x27,0x1F,0x17,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  216.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  217.     0xC0,0xB8,0x2F,0x27,0x1F,0x17,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  218.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  219.     0xC0,0xB8,0xB0,0x27,0x1F,0x17,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  220.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  221.     0xC0,0xB8,0xB0,0xA8,0x1F,0x17,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  222.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  223.     0xC0,0xB8,0xB0,0xA8,0xA0,0x17,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  224.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  225.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  226.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  227.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  228.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  229.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  230.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  231.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  232.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  233.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x88,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  234.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  235.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x88,0x90,0x17,0x1F,0x27,0x2F,0x37,
  236.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  237.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x88,0x90,0x98,0x1F,0x27,0x2F,0x37,
  238.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  239.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x88,0x90,0x98,0xA0,0x27,0x2F,0x37,
  240.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  241.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x88,0x90,0x98,0xA0,0xA8,0x2F,0x37,
  242.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  243.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x88,0x90,0x98,0xA0,0xA8,0xB0,0x37,
  244.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  245.     0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  246.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  247.     0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  248.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  249.     0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  250.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  251.     0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  252.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  253.     0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  254.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  255.     0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  256.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  257.     0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  258.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  259.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  260.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  261.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  262.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  263.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  264.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  265.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  266.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  267.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,
  268.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  269.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,
  270.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  271.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,
  272.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
  273.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x7F,0x7F,
  274.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
  275.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x7F,
  276.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  277.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  278.     0x80,0x80,0x80,0x80,0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  279.     0x80,0x80,0x80,0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  280.     0x80,0x80,0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  281.     0x80,0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  282.     0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  283.     0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  284.     0x80,0x80,0x90,0x98,0xA0,0xA8,0xB0,0xB8,0xC0,0xC8,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,
  285.     0x00,0x08,0x10,0x18,0x20,0x28,0x30,0x38,0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x7F,
  286.     0x80,0x80,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0,0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,
  287.     0x45,0x45,0x79,0x7D,0x7A,0x77,0x70,0x66,0x61,0x58,0x53,0x4D,0x2C,0x20,0x18,0x12,
  288.     0x04,0xDB,0xD3,0xCD,0xC6,0xBC,0xB5,0xAE,0xA8,0xA3,0x9D,0x99,0x93,0x8E,0x8B,0x8A,
  289.     0x45,0x45,0x79,0x7D,0x7A,0x77,0x70,0x66,0x5B,0x4B,0x43,0x37,0x2C,0x20,0x18,0x12,
  290.     0x04,0xF8,0xE8,0xDB,0xCF,0xC6,0xBE,0xB0,0xA8,0xA4,0x9E,0x9A,0x95,0x94,0x8D,0x83,
  291.     0x00,0x00,0x40,0x60,0x7F,0x60,0x40,0x20,0x00,0xE0,0xC0,0xA0,0x80,0xA0,0xC0,0xE0,
  292.     0x00,0x00,0x40,0x60,0x7F,0x60,0x40,0x20,0x00,0xE0,0xC0,0xA0,0x80,0xA0,0xC0,0xE0,
  293.     0x80,0x80,0x90,0x98,0xA0,0xA8,0xB0,0xB8,0xC0,0xC8,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,
  294.     0x00,0x08,0x10,0x18,0x20,0x28,0x30,0x38,0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x7F,
  295.     0x80,0x80,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0,0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70
  296. };
  297.  
  298. #ifdef USE_BLEP
  299. static const uint32_t blepData[48] =
  300. {
  301.     0x3F7FE1F1, 0x3F7FD548, 0x3F7FD6A3, 0x3F7FD4E3, 0x3F7FAD85, 0x3F7F2152,
  302.     0x3F7DBFAE, 0x3F7ACCDF, 0x3F752F1E, 0x3F6B7384, 0x3F5BFBCB, 0x3F455CF2,
  303.     0x3F26E524, 0x3F0128C4, 0x3EACC7DC, 0x3E29E86B, 0x3C1C1D29, 0xBDE4BBE6,
  304.     0xBE3AAE04, 0xBE48DEDD, 0xBE22AD7E, 0xBDB2309A, 0xBB82B620, 0x3D881411,
  305.     0x3DDADBF3, 0x3DE2C81D, 0x3DAAA01F, 0x3D1E769A, 0xBBC116D7, 0xBD1402E8,
  306.     0xBD38A069, 0xBD0C53BB, 0xBC3FFB8C, 0x3C465FD2, 0x3CEA5764, 0x3D0A51D6,
  307.     0x3CEAE2D5, 0x3C92AC5A, 0x3BE4CBF7, 0x00000000, 0x00000000, 0x00000000,
  308.     0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000
  309. };
  310. #endif
  311.  
  312. static const uint8_t waveformLengths[47] =
  313. {
  314.     0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
  315.     0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
  316.     0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
  317.     0x10, 0x10, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  318.     0x10, 0x08, 0x10, 0x10, 0x08, 0x08, 0x18
  319. };
  320.  
  321. /* CODE START */
  322.  
  323. static void PaulaTurnOffDMA(uint8_t i)
  324. {
  325.     AUD[i].DMA_ON = 0;
  326. }
  327.  
  328. static void PaulaRestartDMA(uint8_t i)
  329. {
  330.     AUD[i].FRAC    = 0.0f;
  331.     AUD[i].DMA_POS = 0;
  332.     AUD[i].DMA_DAT = AUD[i].SRC_DAT;
  333.     AUD[i].DMA_LEN = AUD[i].SRC_LEN;
  334.     AUD[i].DMA_ON  = 1;
  335. }
  336.  
  337. static void PaulaSetPeriod(uint8_t i, uint16_t period)
  338. {
  339.     if (period == 0)
  340.     {
  341.         AUD[i].DELTA = 0.0f;
  342.     }
  343.     else
  344.     {
  345.         /* confirmed behavior on real Amiga */
  346.         if (period < 113)
  347.             period = 113;
  348.  
  349.         AUD[i].DELTA = ((float)(PAULA_PAL_CLK) / period) / f_outputFreq;
  350.     }
  351. }
  352.  
  353. static void PaulaSetVolume(uint8_t i, uint16_t vol)
  354. {
  355.     vol &= 0x007F;
  356.     if (vol > 0x40)
  357.         vol = 0x40;
  358.  
  359.     AUD[i].SRC_VOL = vol * (1.0f / 64.0f);
  360. }
  361.  
  362. static void PaulaSetLength(uint8_t i, uint16_t len)
  363. {
  364.     AUD[i].SRC_LEN = 2 * len;
  365. }
  366.  
  367. static void PaulaSetData(uint8_t i, const int8_t *src)
  368. {
  369.     AUD[i].SRC_DAT = src;
  370. }
  371.  
  372. #if defined(USE_HIGHPASS) || defined(USE_LOWPASS)
  373. static void calcCoeffLossyIntegrator(float sr, float hz, lossyIntegrator_t *filter)
  374. {
  375.     filter->coeff[0] = tanf((3.1415927f * hz) / sr);
  376.     filter->coeff[1] = 1.0f / (1.0f + filter->coeff[0]);
  377. }
  378.  
  379. static void clearLossyIntegrator(lossyIntegrator_t *filter)
  380. {
  381.     filter->buffer[0] = 0.0f;
  382.     filter->buffer[1] = 0.0f;
  383. }
  384.  
  385. static inline void lossyIntegrator(lossyIntegrator_t *filter, float *in, float *out)
  386. {
  387.     float output;
  388.  
  389.     /* left channel low-pass */
  390.     output = (filter->coeff[0] * in[0] + filter->buffer[0]) * filter->coeff[1];
  391.     filter->buffer[0] = filter->coeff[0] * (in[0] - output) + output + 1e-10f;
  392.     out[0] = output;
  393.  
  394.     /* right channel low-pass */
  395.     output = (filter->coeff[0] * in[1] + filter->buffer[1]) * filter->coeff[1];
  396.     filter->buffer[1] = filter->coeff[0] * (in[1] - output) + output + 1e-10f;
  397.     out[1] = output;
  398. }
  399.  
  400. static inline void lossyIntegratorHighPass(lossyIntegrator_t *filter, float *in, float *out)
  401. {
  402.     float low[2];
  403.  
  404.     lossyIntegrator(filter, in, low);
  405.  
  406.     out[0] = in[0] - low[0];
  407.     out[1] = in[1] - low[1];
  408. }
  409. #endif
  410.  
  411. #ifdef USE_BLEP
  412. static inline void blepAdd(blep_t *b, float offset, float amplitude)
  413. {
  414.     int8_t n;
  415.     uint32_t i;
  416.     const float *blepSrc;
  417.     float f;
  418.  
  419.     i  = (uint32_t)(offset * BLEP_SP);
  420.     blepSrc = (const float *)(blepData) + i + BLEP_OS;
  421.  
  422.     f = (offset * BLEP_SP) - i;
  423.     i = b->index;
  424.     n = BLEP_NS;
  425.  
  426.     while (n--)
  427.     {
  428.         b->buffer[i] += (amplitude * LERP(blepSrc[0], blepSrc[1], f));
  429.         blepSrc += BLEP_SP;
  430.  
  431.         i++;
  432.         i &= BLEP_RNS;
  433.     }
  434.  
  435.     b->samplesLeft = BLEP_NS;
  436. }
  437.  
  438. static inline float blepRun(blep_t *b)
  439. {
  440.     float blepOutput;
  441.  
  442.     blepOutput = b->buffer[b->index];
  443.     b->buffer[b->index] = 0.0f;
  444.  
  445.     b->index++;
  446.     b->index &= BLEP_RNS;
  447.  
  448.     b->samplesLeft--;
  449.  
  450.     return (blepOutput);
  451. }
  452. #endif
  453.  
  454. static int8_t init_music(const uint8_t *moduleData)
  455. {
  456.     uint8_t i;
  457.  
  458.     /* "SMOD" and "FC14" */
  459.     if ((*PTR2LONG(&moduleData[0]) != 0x444F4D53) && (*PTR2LONG(&moduleData[0]) != 0x34314346))
  460.         return (0);
  461.  
  462.     fc14 = (*PTR2LONG(&moduleData[0]) == 0x34314346);
  463.  
  464.     /* setup pointers... */
  465.  
  466.     SEQpoint = (uint8_t *)(&moduleData[fc14 ? 180 : 100]);
  467.     PATpoint = (uint8_t *)(&moduleData[SWAP32(*PTR2LONG(&moduleData[8]))]);
  468.     FRQpoint = (uint8_t *)(&moduleData[SWAP32(*PTR2LONG(&moduleData[16]))]);
  469.     VOLpoint = (uint8_t *)(&moduleData[SWAP32(*PTR2LONG(&moduleData[24]))]);
  470.  
  471.     /* load samples */
  472.  
  473.     ptr8s_1 =  (int8_t *)(&moduleData[SWAP32(*PTR2LONG(&moduleData[32]))]);
  474.     ptr8u_2 = (uint8_t *)(&moduleData[40]);
  475.     for (i = 0; i < NUM_SAMPLES; ++i)
  476.     {
  477.         samples[i].data   = ptr8s_1;
  478.         samples[i].length = SWAP16(*PTR2WORD(ptr8u_2));                   ptr8u_2 += 2;
  479.         samples[i].repeat = &samples[i].data[SWAP16(*PTR2WORD(ptr8u_2))]; ptr8u_2 += 2;
  480.         samples[i].replen = SWAP16(*PTR2WORD(ptr8u_2));                   ptr8u_2 += 2;
  481.  
  482.         /* fix endless beep on non-looping samples (FC14 doesn't do this) */
  483.         if (samples[i].replen <= 1)
  484.         {
  485.             samples[i].replen = 1;
  486.             if (samples[i].length >= 1)
  487.             {
  488.                 if (*PTR2LONG(samples[i].data) != 0x504D5353) /* "SSMP" */
  489.                     *PTR2WORD(samples[i].data)  = 0;
  490.             }
  491.         }
  492.  
  493.         ptr8s_1 += ((samples[i].length * 2) + 2);
  494.     }
  495.  
  496.     /* load waveforms */
  497.  
  498.     if (fc14)
  499.     {
  500.         ptr8s_1 =  (int8_t *)(&moduleData[SWAP32(*PTR2LONG(&moduleData[36]))]);
  501.         ptr8u_2 = (uint8_t *)(&moduleData[100]);
  502.         for (i = 0; i < NUM_WAVEFORMS; ++i)
  503.         {
  504.             samples[NUM_SAMPLES + i].data   = ptr8s_1;
  505.             samples[NUM_SAMPLES + i].length = *ptr8u_2++;
  506.             samples[NUM_SAMPLES + i].repeat = &samples[NUM_SAMPLES + i].data[0];
  507.             samples[NUM_SAMPLES + i].replen = samples[NUM_SAMPLES + i].length;
  508.  
  509.             ptr8s_1 += (samples[NUM_SAMPLES + i].length * 2);
  510.         }
  511.     }
  512.     else
  513.     {
  514.         ptr8s_1 = (int8_t *)(waveformDatas);
  515.         for (i = 0; i < NUM_WAVEFORMS; ++i)
  516.         {
  517.             if (i < NUM_WAVEFORMS_SMOD)
  518.             {
  519.                 samples[NUM_SAMPLES + i].data   = ptr8s_1;
  520.                 samples[NUM_SAMPLES + i].length = waveformLengths[i];
  521.                 samples[NUM_SAMPLES + i].repeat = &samples[NUM_SAMPLES + i].data[0];
  522.                 samples[NUM_SAMPLES + i].replen = samples[NUM_SAMPLES + i].length;
  523.  
  524.                 ptr8s_1 += (samples[NUM_SAMPLES + i].length * 2);
  525.             }
  526.             else
  527.             {
  528.                 samples[NUM_SAMPLES + i].data   = NULL;
  529.                 samples[NUM_SAMPLES + i].length = 0;
  530.                 samples[NUM_SAMPLES + i].repeat = NULL;
  531.                 samples[NUM_SAMPLES + i].replen = 1;
  532.             }
  533.         }
  534.     }
  535.  
  536.     /* initialize channels */
  537.  
  538.     /* get number of sequences and make it a multiple of 13 (SEQ_SIZE) */
  539.     numSequences = (uint32_t)(SWAP32(*PTR2LONG(&moduleData[4])) / SEQ_SIZE) * SEQ_SIZE;
  540.  
  541.     memset(&Channel, 0, sizeof (Channel));
  542.     for (i = 0; i < 4; ++i)
  543.     {
  544.         Channel[i].voiceIndex      = (unsigned)(i);
  545.         Channel[i].loopStart       = NULL;
  546.         Channel[i].volTabPtr       = silentTable;
  547.         Channel[i].freqTabPtr      = silentTable;
  548.         Channel[i].volDelayCounter = 1;
  549.         Channel[i].volDelayLength  = 1;
  550.         Channel[i].pitchBendDelay  = 1;
  551.         Channel[i].seqPos          = SEQ_SIZE; /* yes */
  552.         Channel[i].seqStartPtr     = &SEQpoint[3 * i];
  553.         Channel[i].patPtr          = &PATpoint[Channel[i].seqStartPtr[0] * PAT_MAX_SIZE];
  554.         Channel[i].noteTranspose   = (signed)(Channel[i].seqStartPtr[1]);
  555.         Channel[i].soundTranspose  = (signed)(Channel[i].seqStartPtr[2]);
  556.     }
  557.  
  558.     repspd  = SEQpoint[12] ? SEQpoint[12] : 3;
  559.     respcnt = repspd;
  560.  
  561.     spdtemp  = 0;
  562.     spdtemp2 = 0;
  563.     initdone = 1;
  564.  
  565.     return (1);
  566. }
  567.  
  568. static void new_note(fcChannel_t *ch)
  569. {
  570.     uint8_t *tmpSeqPtr, *tmpPatPtr, note, info;
  571.  
  572.     tmpPatPtr = &ch->patPtr[ch->patPos]; /* temp pattern pointer */
  573.  
  574.     if ((fc14 && ((*tmpPatPtr & 0x7F) == PAT_END_MARKER)) || (ch->patPos == PAT_MAX_SIZE))
  575.     {
  576.         ch->patPos = 0;
  577.         if (ch->seqPos >= numSequences)
  578.             ch->seqPos = 0;
  579.  
  580.         tmpSeqPtr = &ch->seqStartPtr[ch->seqPos];
  581.         ch->patPtr = &PATpoint[tmpSeqPtr[0] * PAT_MAX_SIZE];
  582.         ch->noteTranspose = (signed)(tmpSeqPtr[1]);
  583.         ch->soundTranspose = (signed)(tmpSeqPtr[2]);
  584.  
  585.         if (++spdtemp == 4)
  586.         {
  587.             /* we've read all channels now, let's increase the pos used for RS */
  588.  
  589.             spdtemp = 0;
  590.             if (++spdtemp2 == (numSequences / SEQ_SIZE)) /* numSequences is a multiple of SEQ_SIZE */
  591.                 spdtemp2 = 0; /* wrap sequence position */
  592.         }
  593.  
  594.         /* read current RS (replay speed. only update if non-zero) */
  595.         if (SEQpoint[(spdtemp2 * 13) + 12] != 0)
  596.         {
  597.             repspd  = SEQpoint[(spdtemp2 * 13) + 12];
  598.             respcnt = repspd;
  599.         }
  600.  
  601.         ch->seqPos += SEQ_SIZE;
  602.         tmpPatPtr = ch->patPtr; /* set new temp pattern pointer */
  603.     }
  604.  
  605.     note = tmpPatPtr[0];
  606.     info = tmpPatPtr[1];
  607.  
  608.     if (note == 0)
  609.     {
  610.         info &= 0xC0;
  611.         if (info != 0)
  612.         {
  613.             ch->portaParam = 0;
  614.             if (info & (1 << 7))
  615.                 ch->portaParam = tmpPatPtr[3];
  616.         }
  617.     }
  618.     else
  619.     {
  620.         ch->portaValue = 0;
  621.         ch->portaParam = 0;
  622.         if (info & (1 << 7))
  623.             ch->portaParam = tmpPatPtr[3];
  624.     }
  625.  
  626.     note &= 0x7F;
  627.     if (note != 0)
  628.     {
  629.         ptr8u_1 = &VOLpoint[(((tmpPatPtr[1] & 0x3F) + ch->soundTranspose) & 0x3F) * VOL_TAB_SIZE];
  630.  
  631.         ch->note            = note;
  632.         ch->volDelayLength  = ptr8u_1[0];
  633.         ch->volDelayCounter = ch->volDelayLength;
  634.         ch->freqTabPtr      = &FRQpoint[(ptr8u_1[1] & 0x3F) * FREQ_TAB_SIZE];
  635.         ch->freqTabPos      = 0;
  636.         ch->freqSusCounter  = 0;
  637.         ch->vibratoSpeed    = ptr8u_1[2];
  638.         ch->vibratoDepth    = ptr8u_1[3];
  639.         ch->vibratoDelay    = ptr8u_1[4];
  640.         ch->vibratoCounter  = ch->vibratoDepth;
  641.         ch->vibratoUp       = 1; /* default initial state on new note */
  642.         ch->volTabPtr       = &ptr8u_1[5];
  643.         ch->volTabPos       = 0;
  644.         ch->volSusCounter   = 0;
  645.  
  646.         PaulaTurnOffDMA(ch->voiceIndex); /* yes, this is important */
  647.     }
  648.  
  649.     ch->patPos += 2;
  650. }
  651.  
  652. static void doFreqModulation(fcChannel_t *ch)
  653. {
  654.     uint8_t doTranspose, *tabPtr, *tmpPtr;
  655.     soundInfo_t sample;
  656.  
  657. testsustain:
  658.     if (ch->freqSusCounter > 0)
  659.     {
  660.         ch->freqSusCounter--;
  661.     }
  662.     else
  663.     {
  664.         tabPtr = &ch->freqTabPtr[ch->freqTabPos];
  665.  
  666. testeffects:
  667.         if (tabPtr[0] != 0xE1)
  668.         {
  669.             doTranspose = 1;
  670.  
  671.             if (tabPtr[0] == 0xE0) /* freq pos jump */
  672.             {
  673.                 ch->freqTabPos = tabPtr[1] & 0x3F;
  674.                 tabPtr = &ch->freqTabPtr[ch->freqTabPos];
  675.             }
  676.  
  677.             if (tabPtr[0] == 0xE2) /* set waveform */
  678.             {
  679.                 if (tabPtr[1] < (NUM_SAMPLES + NUM_WAVEFORMS))
  680.                 {
  681.                     sample = samples[tabPtr[1]];
  682.  
  683.                     ch->loopStart  = sample.repeat;
  684.                     ch->loopLength = sample.replen;
  685.  
  686.                     PaulaSetData(ch->voiceIndex, sample.data);
  687.                     PaulaSetLength(ch->voiceIndex, sample.length);
  688.                     PaulaRestartDMA(ch->voiceIndex);
  689.  
  690.                     ch->DMAWaitFrames = 3;
  691.                     ch->volTabPos = 0;
  692.                     ch->volDelayCounter = 1;
  693.                 }
  694.  
  695.                 ch->freqTabPos += 2;
  696.             }
  697.             else if (tabPtr[0] == 0xE4) /* update waveform */
  698.             {
  699.                 if (tabPtr[1] < (NUM_SAMPLES + NUM_WAVEFORMS))
  700.                 {
  701.                     sample = samples[tabPtr[1]];
  702.  
  703.                     ch->loopStart  = sample.repeat;
  704.                     ch->loopLength = sample.replen;
  705.  
  706.                     PaulaSetData(ch->voiceIndex, sample.data);
  707.                     PaulaSetLength(ch->voiceIndex, sample.length);
  708.  
  709.                     ch->DMAWaitFrames = 3;
  710.                 }
  711.  
  712.                 ch->freqTabPos += 2;
  713.             }
  714.             else if (tabPtr[0] == 0xE9) /* set packed waveform */
  715.             {
  716.                 if (tabPtr[1] < (NUM_SAMPLES + NUM_WAVEFORMS))
  717.                 {
  718.                     sample = samples[tabPtr[1]];
  719.                     if (*PTR2LONG(sample.data) == 0x504D5353) /* "SSMP" */
  720.                     {
  721.                         tmpPtr = (uint8_t *)(&sample.data[4 + (tabPtr[2] * 16)]);
  722.  
  723.                         ch->loopStart  = &sample.data[(4 + 320 + *PTR2LONG(&tmpPtr[0])) + *PTR2WORD(&tmpPtr[6])];
  724.                         ch->loopLength = *PTR2WORD(&tmpPtr[8]);
  725.  
  726.                         /* fix endless beep on non-looping samples (FC14 doesn't do this) */
  727.                         if (ch->loopLength <= 1)
  728.                         {
  729.                             ch->loopLength = 1;
  730.                             if (*PTR2WORD(&tmpPtr[4]) >= 1) /* sample length */
  731.                                 *PTR2WORD(&sample.data[4 + 320 + *PTR2LONG(&tmpPtr[0])]) = 0;
  732.                         }
  733.  
  734.                         PaulaSetData(ch->voiceIndex,  &sample.data[4 + 320 + *PTR2LONG(&tmpPtr[0])]);
  735.                         PaulaSetLength(ch->voiceIndex, *PTR2WORD(&tmpPtr[4]));
  736.                         PaulaRestartDMA(ch->voiceIndex);
  737.  
  738.                         ch->DMAWaitFrames = 3;
  739.                         ch->volTabPos = 0;
  740.                         ch->volDelayCounter = 1;
  741.                     }
  742.                 }
  743.  
  744.                 ch->freqTabPos += 3;
  745.             }
  746.             else if (tabPtr[0] == 0xE7) /* new freq pos */
  747.             {
  748.                 tabPtr = &FRQpoint[(tabPtr[1] & 0x3F) * FREQ_TAB_SIZE];
  749.                 ch->freqTabPtr = tabPtr;
  750.                 ch->freqTabPos = 0;
  751.  
  752.                 goto testeffects;
  753.             }
  754.             else if (tabPtr[0] == 0xE8) /* set freq sustain */
  755.             {
  756.                 ch->freqSusCounter = tabPtr[1];
  757.  
  758.                 ch->freqTabPos += 2;
  759.                 goto testsustain;
  760.             }
  761.             else if (tabPtr[0] == 0xE3) /* set vibrato */
  762.             {
  763.                 ch->freqTabPos += 3;
  764.  
  765.                 ch->vibratoSpeed = tabPtr[1];
  766.                 ch->vibratoDepth = tabPtr[2];
  767.  
  768.                 doTranspose = 0; /* don't do period transpose here */
  769.             }
  770.             else if (tabPtr[0] == 0xEA) /* set pitch bend */
  771.             {
  772.                 ch->pitchBendValue   = (signed)(tabPtr[1]);
  773.                 ch->pitchBendCounter = (signed)(tabPtr[2]);
  774.  
  775.                 ch->freqTabPos += 3;
  776.             }
  777.  
  778.             if (doTranspose)
  779.                 ch->periodTranspose = (signed)(ch->freqTabPtr[ch->freqTabPos++]);
  780.         }
  781.     }
  782. }
  783.  
  784. static void do_VOLbend(fcChannel_t *ch)
  785. {
  786.     ch->volSlideDelay = !ch->volSlideDelay;
  787.     if (ch->volSlideDelay)
  788.     {
  789.         ch->volSlideCounter--;
  790.  
  791.         ch->volume += ch->volSlideSpeed;
  792.         if (ch->volume < 0)
  793.         {
  794.             ch->volSlideCounter = 0;
  795.             ch->volume = 0;
  796.         }
  797.     }
  798. }
  799.  
  800. static void doVolModulation(fcChannel_t *ch)
  801. {
  802.     uint8_t *tabPtr;
  803.  
  804. VOLUfx:
  805.     if (ch->volSusCounter > 0)
  806.     {
  807.         ch->volSusCounter--;
  808.     }
  809.     else
  810.     {
  811.         if (ch->volSlideCounter > 0)
  812.         {
  813.             do_VOLbend(ch);
  814.         }
  815.         else
  816.         {
  817.             /* fun potential bug: ch->volDelayCounter==0 = mega delay... */
  818.             if (--ch->volDelayCounter == 0)
  819.             {
  820.                 ch->volDelayCounter = ch->volDelayLength;
  821. volu_cmd:
  822.                 tabPtr = &ch->volTabPtr[ch->volTabPos];
  823.  
  824.                 if (tabPtr[0] != 0xE1)
  825.                 {
  826.                     if (tabPtr[0] == 0xE8) /* set vol sustain */
  827.                     {
  828.                         ch->volSusCounter = tabPtr[1];
  829.                         ch->volTabPos += 2;
  830.  
  831.                         goto VOLUfx;
  832.                     }
  833.                     else if (tabPtr[0] == 0xEA) /* set vol slide */
  834.                     {
  835.                         ch->volSlideSpeed   = tabPtr[1];
  836.                         ch->volSlideCounter = tabPtr[2];
  837.  
  838.                         ch->volTabPos += 3;
  839.                         do_VOLbend(ch);
  840.                     }
  841.                     else if (tabPtr[0] == 0xE0) /* set vol pos */
  842.                     {
  843.                         if (((tabPtr[1] & 0x3F) - 5) < 0)
  844.                             ch->volTabPos = 0;
  845.                         else
  846.                             ch->volTabPos = (tabPtr[1] & 0x3F) - 5;
  847.  
  848.                         goto volu_cmd;
  849.                     }
  850.                     else
  851.                     {
  852.                         ch->volume = (signed)(ch->volTabPtr[ch->volTabPos++]);
  853.                     }
  854.                 }
  855.             }
  856.         }
  857.     }
  858. }
  859.  
  860. static void effects(fcChannel_t *ch)
  861. {
  862.     int8_t tmpNote;
  863.     int16_t tmpVibPeriod, tmpPeriod;
  864.     uint16_t tmpVibNote;
  865.  
  866.     doFreqModulation(ch);
  867.     doVolModulation(ch);
  868.  
  869.     /* get period from note and transposes... */
  870.     tmpNote = ch->periodTranspose;
  871.     if (tmpNote >= 0)
  872.     {
  873.         tmpNote += ch->note;
  874.         tmpNote += ch->noteTranspose;
  875.     }
  876.     tmpNote &= 0x7F;
  877.  
  878.     tmpPeriod = periods[tmpNote];
  879.  
  880.     /* apply vibrato to period */
  881.     if (ch->vibratoDelay > 0)
  882.     {
  883.         ch->vibratoDelay--;
  884.     }
  885.     else
  886.     {
  887.         tmpVibPeriod = ch->vibratoCounter;
  888.         if (!ch->vibratoUp)
  889.         {
  890.             tmpVibPeriod -= ch->vibratoSpeed;
  891.             if (tmpVibPeriod < 0)
  892.             {
  893.                 tmpVibPeriod  = 0;
  894.                 ch->vibratoUp = 1;
  895.             }
  896.         }
  897.         else
  898.         {
  899.             tmpVibPeriod += ch->vibratoSpeed;
  900.             if (tmpVibPeriod > (ch->vibratoDepth * 2))
  901.             {
  902.                 tmpVibPeriod  = ch->vibratoDepth * 2;
  903.                 ch->vibratoUp = 0;
  904.             }
  905.         }
  906.         ch->vibratoCounter = tmpVibPeriod & 0x00FF;
  907.  
  908.         tmpVibPeriod -= ch->vibratoDepth;
  909.  
  910.         tmpVibNote = tmpNote * 2;
  911.         while (tmpVibNote < (12 * 8))
  912.         {
  913.             tmpVibPeriod *= 2;
  914.             tmpVibNote   += (12 * 2);
  915.         }
  916.  
  917.         tmpPeriod += tmpVibPeriod;
  918.     }
  919.  
  920.     /* update portamento value (twice as slow on FC1.4) */
  921.     ch->portaDelay = !ch->portaDelay;
  922.     if (!fc14 || ch->portaDelay)
  923.     {
  924.         if (ch->portaParam > 0)
  925.         {
  926.             if (ch->portaParam > 0x1F)
  927.                 ch->portaValue += (ch->portaParam & 0x1F);
  928.             else
  929.                 ch->portaValue -= ch->portaParam;
  930.         }
  931.     }
  932.  
  933.     /* apply pitch bend to portamento value  */
  934.     ch->pitchBendDelay = !ch->pitchBendDelay;
  935.     if (ch->pitchBendDelay)
  936.     {
  937.         if (ch->pitchBendCounter > 0)
  938.         {
  939.             ch->pitchBendCounter--;
  940.             ch->portaValue -= ch->pitchBendValue;
  941.         }
  942.     }
  943.  
  944.     tmpPeriod += ch->portaValue;
  945.  
  946.     PaulaSetPeriod(ch->voiceIndex, CLAMP(tmpPeriod, 0x0071, fc14 ? 0x0D60 : 0x06B0));
  947.     PaulaSetVolume(ch->voiceIndex, ch->volume);
  948. }
  949.  
  950. static void play_music(void)
  951. {
  952.     uint8_t i;
  953.  
  954.     if (--respcnt == 0)
  955.     {
  956.         respcnt = repspd;
  957.  
  958.         new_note(&Channel[0]);
  959.         new_note(&Channel[1]);
  960.         new_note(&Channel[2]);
  961.         new_note(&Channel[3]);
  962.     }
  963.  
  964.     for (i = 0; i < 4; ++i)
  965.     {
  966.         effects(&Channel[i]);
  967.  
  968.         if (Channel[i].DMAWaitFrames > 0)
  969.         {
  970.             if (--Channel[i].DMAWaitFrames == 0)
  971.             {
  972.                 /* these take effect when the current DMA cycle is done */
  973.                 PaulaSetData(i, Channel[i].loopStart);
  974.                 PaulaSetLength(i, Channel[i].loopLength);
  975.             }
  976.         }
  977.     }
  978. }
  979.  
  980. static void mixSampleBlock(int16_t *streamOut, uint32_t numSamples)
  981. {
  982.     uint8_t i;
  983.     int16_t *sndOut;
  984.     uint16_t j;
  985.     int32_t smpL, smpR;
  986.     float tempSample, tempVolume, out_f[2];
  987.     paulaVoice_t *v;
  988.  
  989. #ifdef USE_BLEP
  990.     blep_t *bSmp, *bVol;
  991. #endif
  992.  
  993.     memset(masterBufferL, 0, sizeof (float) * numSamples);
  994.     memset(masterBufferR, 0, sizeof (float) * numSamples);
  995.  
  996.     for (i = 0; i < 4; ++i)
  997.     {
  998.         v = &AUD[i];
  999.  
  1000. #ifdef USE_BLEP
  1001.         bSmp = &blep[i];
  1002.         bVol = &blepVol[i];
  1003. #endif
  1004.         if (v->DMA_ON && (v->DMA_LEN > 0) && (v->DELTA > 0.0f))
  1005.         {
  1006.             for (j = 0; j < numSamples; ++j)
  1007.             {
  1008.                 tempSample = (v->DMA_DAT == NULL) ? 0.0f : (v->DMA_DAT[v->DMA_POS] * (1.0f / 128.0f));
  1009.                 tempVolume = (v->DMA_DAT == NULL) ? 0.0f : v->SRC_VOL;
  1010.  
  1011. #ifdef USE_BLEP
  1012.                 if (tempSample != bSmp->lastValue)
  1013.                 {
  1014.                     if ((v->LASTDELTA > 0.0f) && (v->LASTDELTA > v->LASTFRAC))
  1015.                         blepAdd(bSmp, v->LASTFRAC / v->LASTDELTA, bSmp->lastValue - tempSample);
  1016.  
  1017.                     bSmp->lastValue = tempSample;
  1018.                 }
  1019.  
  1020.                 if (tempVolume != bVol->lastValue)
  1021.                 {
  1022.                     blepAdd(bVol, 0.0f, bVol->lastValue - tempVolume);
  1023.                     bVol->lastValue = tempVolume;
  1024.                 }
  1025.  
  1026.                 if (bSmp->samplesLeft) tempSample += blepRun(bSmp);
  1027.                 if (bVol->samplesLeft) tempVolume += blepRun(bVol);
  1028. #endif
  1029.                 tempSample *= tempVolume;
  1030.                 masterBufferL[j] += (tempSample * v->PANL);
  1031.                 masterBufferR[j] += (tempSample * v->PANR);
  1032.  
  1033.                 v->FRAC += v->DELTA;
  1034.                 if (v->FRAC >= 1.0f)
  1035.                 {
  1036.                     v->FRAC -= 1.0f;
  1037.  
  1038.                     v->LASTFRAC  = v->FRAC;
  1039.                     v->LASTDELTA = v->DELTA;
  1040.  
  1041.                     if (++v->DMA_POS >= v->DMA_LEN)
  1042.                     {
  1043.                         v->DMA_POS = 0;
  1044.  
  1045.                         /* re-fetch Paula register values now */
  1046.                         v->DMA_LEN = v->SRC_LEN;
  1047.                         v->DMA_DAT = v->SRC_DAT;
  1048.                     }
  1049.                 }
  1050.             }
  1051.         }
  1052.     }
  1053.  
  1054.     sndOut = streamOut;
  1055.     for (j = 0; j < numSamples; ++j)
  1056.     {
  1057.         if (onoff)
  1058.         {
  1059.             out_f[0] = masterBufferL[j];
  1060.             out_f[1] = masterBufferR[j];
  1061.  
  1062. #ifdef USE_LOWPASS
  1063.             lossyIntegrator(&filterLo, out_f, out_f);
  1064. #endif
  1065.  
  1066. #ifdef USE_HIGHPASS
  1067.             lossyIntegratorHighPass(&filterHi, out_f, out_f);
  1068. #endif
  1069.             /* normalize amplitude */
  1070.             out_f[0] *= (32767.0f / NORM_FACTOR);
  1071.             out_f[1] *= (32767.0f / NORM_FACTOR);
  1072.  
  1073.             /* truncate to signed 32-bit integer */
  1074.             smpL = (int32_t)(out_f[0]);
  1075.             smpR = (int32_t)(out_f[1]);
  1076.  
  1077.             /* clamp to 16-bit signed range */
  1078.             CLAMP16(smpL);
  1079.             CLAMP16(smpR);
  1080.  
  1081.             /* truncate to signed 16-bit integer and store in output buffer */
  1082.             *sndOut++ = (int16_t)(smpL);
  1083.             *sndOut++ = (int16_t)(smpR);
  1084.         }
  1085.         else
  1086.         {
  1087.             *sndOut++ = 0;
  1088.             *sndOut++ = 0;
  1089.         }
  1090.     }
  1091. }
  1092.  
  1093. static void CALLBACK waveOutProc(HWAVEOUT _hWaveOut, UINT uMsg,
  1094.     DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
  1095. {
  1096.     int16_t *outputStream;
  1097.     int32_t  sampleBlock, samplesTodo;
  1098.     WAVEHDR *waveBlockHeader;
  1099.  
  1100.     /* make compiler happy! (warning C4100) */
  1101.     (void)(dwParam2);
  1102.     (void)(dwInstance);
  1103.  
  1104.     if (uMsg == MM_WOM_DONE)
  1105.     {
  1106.         mixingMutex = 1;
  1107.  
  1108.         waveBlockHeader = (WAVEHDR *)(dwParam1);
  1109.         waveOutUnprepareHeader(_hWaveOut, waveBlockHeader, sizeof (WAVEHDR));
  1110.  
  1111.         if (isMixing)
  1112.         {
  1113.             memcpy(waveBlockHeader->lpData, mixerBuffer, soundBufferSize);
  1114.  
  1115.             waveOutPrepareHeader(_hWaveOut, waveBlockHeader, sizeof (WAVEHDR));
  1116.             waveOutWrite(_hWaveOut, waveBlockHeader, sizeof (WAVEHDR));
  1117.  
  1118.             outputStream = (int16_t *)(mixerBuffer);
  1119.             sampleBlock  = soundBufferSize / 4;
  1120.  
  1121.             while (sampleBlock)
  1122.             {
  1123.                 samplesTodo = (sampleBlock < samplesLeft) ? sampleBlock : samplesLeft;
  1124.                 if (samplesTodo > soundBufferSize)
  1125.                     samplesTodo = soundBufferSize;
  1126.  
  1127.                 if (samplesTodo > 0)
  1128.                 {
  1129.                     mixSampleBlock(outputStream, samplesTodo);
  1130.                     outputStream += (samplesTodo * 2);
  1131.  
  1132.                     sampleBlock -= samplesTodo;
  1133.                     samplesLeft -= samplesTodo;
  1134.                 }
  1135.                 else
  1136.                 {
  1137.                     if (onoff)
  1138.                         play_music();
  1139.  
  1140.                     samplesLeft = samplesPerFrame;
  1141.                 }
  1142.             }
  1143.         }
  1144.  
  1145.         mixingMutex = 0;
  1146.     }
  1147. }
  1148.  
  1149. void fc14play_PauseSong(int8_t pause)
  1150. {
  1151.     onoff = pause ? 0 : 1;
  1152. }
  1153.  
  1154. /* these are used to create an equal powered panning */
  1155. static float sinApx(float x)
  1156. {
  1157.     x = x * (2.0f - x);
  1158.     return (x * 1.09742972f + x * x * 0.31678383f);
  1159. }
  1160.  
  1161. static float cosApx(float x)
  1162. {
  1163.     x = (1.0f - x) * (1.0f + x);
  1164.     return (x * 1.09742972f + x * x * 0.31678383f);
  1165. }
  1166. /* ------------------------------------------------- */
  1167.  
  1168. static void init_pans(uint8_t stereoSeparation)
  1169. {
  1170.     uint8_t scaledPanPos;
  1171.     float p;
  1172.  
  1173.     if (stereoSeparation > 100)
  1174.         stereoSeparation = 100;
  1175.  
  1176.     scaledPanPos = (stereoSeparation * 128) / 100;
  1177.  
  1178.     p = (128 - scaledPanPos) * (1.0f / 256.0f);
  1179.     AUD[0].PANL = cosApx(p);
  1180.     AUD[0].PANR = sinApx(p);
  1181.     AUD[3].PANL = cosApx(p);
  1182.     AUD[3].PANR = sinApx(p);
  1183.  
  1184.     p = (128 + scaledPanPos) * (1.0f / 256.0f);
  1185.     AUD[1].PANL = cosApx(p);
  1186.     AUD[1].PANR = sinApx(p);
  1187.     AUD[2].PANL = cosApx(p);
  1188.     AUD[2].PANR = sinApx(p);
  1189. }
  1190.  
  1191. void fc14play_PlaySong(void)
  1192. {
  1193.     uint8_t i;
  1194.  
  1195.     if (initdone)
  1196.     {
  1197.         onoff = 0;
  1198.  
  1199. #ifdef USE_BLEP
  1200.         memset(&blep,    0, sizeof (blep));
  1201.         memset(&blepVol, 0, sizeof (blepVol));
  1202. #endif
  1203.  
  1204. #ifdef USE_LOWPASS
  1205.         clearLossyIntegrator(&filterLo);
  1206. #endif
  1207.  
  1208. #ifdef USE_HIGHPASS
  1209.         clearLossyIntegrator(&filterHi);
  1210. #endif
  1211.  
  1212.         memset(AUD, 0, sizeof (AUD));
  1213.         for (i = 0; i < 4; ++i)
  1214.         {
  1215.             AUD[i].DMA_DAT = NULL;
  1216.             AUD[i].SRC_DAT = NULL;
  1217.         }
  1218.  
  1219.         init_pans(STEREO_SEP);
  1220.  
  1221.         onoff = 1;
  1222.     }
  1223. }
  1224.  
  1225. static int8_t openMixer(uint32_t _samplingFrequency, uint32_t _soundBufferSize)
  1226. {
  1227.     uint8_t i;
  1228.     MMRESULT r;
  1229.  
  1230.     if (!hWaveOut)
  1231.     {
  1232.         f_outputFreq        = (float)(_samplingFrequency);
  1233.         soundBufferSize     = _soundBufferSize;
  1234.         masterBufferL       = (float *)(malloc(soundBufferSize * sizeof (float)));
  1235.         masterBufferR       = (float *)(malloc(soundBufferSize * sizeof (float)));
  1236.         wfx.nSamplesPerSec  = _samplingFrequency;
  1237.         wfx.wBitsPerSample  = 16;
  1238.         wfx.nChannels       = 2;
  1239.         wfx.cbSize          = 0;
  1240.         wfx.wFormatTag      = WAVE_FORMAT_PCM;
  1241.         wfx.nBlockAlign     = (wfx.wBitsPerSample * wfx.nChannels) / 8;
  1242.         wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
  1243.  
  1244.         if ((masterBufferL == NULL) || (masterBufferR == NULL))
  1245.             return (0); /* gets free'd later */
  1246.  
  1247.         r = waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, (DWORD_PTR)(waveOutProc), 0L, CALLBACK_FUNCTION);
  1248.         if (r != MMSYSERR_NOERROR) return (0);
  1249.  
  1250.         for (i = 0; i < MIX_BUF_NUM; ++i)
  1251.         {
  1252.             waveBlocks[i].dwBufferLength = soundBufferSize;
  1253.             waveBlocks[i].lpData = (LPSTR)(calloc(soundBufferSize, 1));
  1254.             if (waveBlocks[i].lpData == NULL)
  1255.                 return (0); /* gets free'd later */
  1256.  
  1257.             waveOutPrepareHeader(hWaveOut, &waveBlocks[i], sizeof (WAVEHDR));
  1258.             waveOutWrite(hWaveOut, &waveBlocks[i], sizeof (WAVEHDR));
  1259.         }
  1260.  
  1261.         mixerBuffer = (int8_t *)(calloc(soundBufferSize, 1));
  1262.         if (mixerBuffer == NULL)
  1263.             return (0); /* gets free'd later */
  1264.  
  1265.         samplesPerFrame = (uint32_t)((f_outputFreq / AMIGA_VBLANK_RATE) + 0.5f);
  1266.  
  1267. #ifdef USE_LOWPASS
  1268.         // Amiga 500 RC low-pass filter (R = 360 ohm, C = 0.1uF)
  1269.         // hz = 1 / (2pi * R * C)    = ~4421.0Hz
  1270.         calcCoeffLossyIntegrator(f_outputFreq, 4421.0f, &filterLo);
  1271. #endif
  1272.  
  1273. #ifdef USE_HIGHPASS
  1274.         // Amiga 500 RC high-pass filter (R = 1390 ohm, C = 22uF)
  1275.         // hz = 1 / (2pi * R * C)    = ~5.2Hz
  1276.         calcCoeffLossyIntegrator(f_outputFreq, 5.2f, &filterHi);
  1277. #endif
  1278.  
  1279.         isMixing = 1;
  1280.         return (1);
  1281.     }
  1282.  
  1283.     return (1);
  1284. }
  1285.  
  1286. void fc14play_Close(void)
  1287. {
  1288.     uint8_t i;
  1289.  
  1290.     onoff = 0;
  1291.  
  1292.     if (isMixing)
  1293.     {
  1294.         isMixing = 0;
  1295.         while (mixingMutex);
  1296.  
  1297.         if (hWaveOut)
  1298.         {
  1299.             for (i = 0; i < MIX_BUF_NUM; ++i)
  1300.                 waveOutUnprepareHeader(hWaveOut, &waveBlocks[i], sizeof (WAVEHDR));
  1301.  
  1302.             for (i = 0; i < MIX_BUF_NUM; ++i)
  1303.             {
  1304.                 while (waveBlocks[i].dwFlags & WHDR_PREPARED) SleepEx(1, 1); // wait
  1305.                
  1306.                 if (waveBlocks[i].lpData != NULL)
  1307.                 {
  1308.                     free(waveBlocks[i].lpData);
  1309.                     waveBlocks[i].lpData = NULL;
  1310.                 }
  1311.             }
  1312.  
  1313.             waveOutReset(hWaveOut);
  1314.             waveOutClose(hWaveOut);
  1315.  
  1316.             hWaveOut = 0;
  1317.  
  1318.             if (mixerBuffer != NULL)
  1319.             {
  1320.                 free(mixerBuffer);
  1321.                 mixerBuffer = NULL;
  1322.             }
  1323.  
  1324.             if (masterBufferL != NULL)
  1325.             {
  1326.                 free(masterBufferL);
  1327.                 masterBufferL = NULL;
  1328.             }
  1329.  
  1330.             if (masterBufferR != NULL)
  1331.             {
  1332.                 free(masterBufferR);
  1333.                 masterBufferR = NULL;
  1334.             }
  1335.         }
  1336.     }
  1337. }
  1338.  
  1339. void fc14play_SetStereoSep(uint8_t percentage)
  1340. {
  1341.     init_pans(percentage);
  1342. }
  1343.  
  1344. int8_t fc14play_Init(uint32_t outputFreq, const uint8_t *moduleData)
  1345. {
  1346.     if (!init_music(moduleData))
  1347.         return (0);
  1348.  
  1349.     if (!openMixer(outputFreq, MIX_BUF_LEN))
  1350.     {
  1351.         fc14play_Close();
  1352.         return (0);
  1353.     }
  1354.  
  1355.     return (1);
  1356. }
  1357.  
  1358. /* END OF FILE */
RAW Paste Data
Top