daily pastebin goal
62%
SHARE
TWEET

fc14play v1.22

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