SHARE
TWEET

fc14play v1.26a

8bitbubsy Nov 17th, 2015 (edited) 831 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ** fc14play v1.26a - 21st of June 2019 - 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 correctly 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).f
  14. ** Alternatively, you can change out the mixer functions at the bottom with
  15. ** your own for your OS.
  16. **
  17. ** Example of fc14play usage:
  18. ** #include "fc14play.h"
  19. ** #include "songdata.h"
  20. **
  21. ** fc14play_PlaySong(songData, songDataLength, 44100);
  22. ** mainLoop();
  23. ** fc14play_Close();
  24. **
  25. ** To turn a song into an include file like in the example, you can use my win32
  26. ** bin2h tool from here: https://16-bits.org/etc/bin2h.zip
  27. **
  28. ** Changes in v1.26a:
  29. ** - Code cleanup
  30. **
  31. ** Changes in v1.26:
  32. ** - Removed the "DMA Wait" stuff, this is not needed in the way I do things.
  33. **   This actually fixes "double kickdrum" in tristar-scoopex.fc.
  34. ** - Mixer is now using double-precision instead of single-precision accuracy.
  35. **
  36. ** Changes in v1.25:
  37. ** - Code cleanup (uses the "bool" type now, spaces -> tabs, comment style change)
  38. **
  39. ** Changes in v1.24:
  40. ** - Some code cleanup
  41. ** - Small optimziation to audio mixer
  42. **
  43. ** Changes in v1.23:
  44. ** - WinMM mixer has been rewritten to be safe (DON'T use syscalls in callback -MSDN)
  45. ** - Some small changes to the fc14play functions (easier to use and safer!)
  46. */
  47.  
  48. /* fc14play.h:
  49.  
  50. #pragma once
  51.  
  52. #include <stdint.h>
  53. #include <stdbool.h>
  54.  
  55. bool fc14play_PlaySong(const uint8_t *moduleData, uint32_t dataLength, uint32_t audioFreq);
  56. void fc14play_Close(void);
  57. void fc14play_PauseSong(bool flag); // true/false
  58. void fc14play_TogglePause(void);
  59. void fc14play_SetStereoSep(uint8_t percentage); // 0..100
  60. uint32_t fc14play_GetMixerTicks(void); // returns the amount of milliseconds of mixed audio (not realtime)
  61. */
  62.  
  63. // == USER ADJUSTABLE SETTINGS ==
  64. #define STEREO_SEP (19)    /* --> Stereo separation in percent - 0 = mono, 100 = hard pan (like Amiga) */
  65. #define USE_HIGHPASS       /* --> ~5Hz HP filter present in all Amigas - comment out for a tiny speed-up */
  66. //#define USE_LOWPASS      /* --> ~5kHz LP filter present in all Amigas except A1200 - comment out for sharper sound (and tiny speed-up) */
  67. #define USE_BLEP           /* --> Reduces some aliasing in the sound (closer to real Amiga) - comment out for a speed-up */
  68. #define MIX_BUF_SAMPLES 4096
  69.  
  70. #ifdef _MSC_VER
  71. #define inline __forceinline
  72. #endif
  73.  
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76. #include <string.h>
  77. #include <stdint.h>
  78. #include <stdbool.h>
  79. #include <math.h> // tanf()
  80.  
  81. // main crystal oscillator
  82. #define AMIGA_PAL_XTAL_HZ 28375160
  83.  
  84. #define PAULA_CLK (AMIGA_PAL_XTAL_HZ / 8)
  85. #define AMIGA_VOICES 4
  86. #define SEQ_SIZE 13
  87. #define PAT_END_MARKER 0x49
  88. #define NUM_SAMPLES 10
  89. #define NUM_WAVEFORMS 80
  90. #define NUM_WAVEFORMS_SMOD 47
  91.  
  92. #define BLEP_ZC 8
  93. #define BLEP_OS 5
  94. #define BLEP_SP 5
  95. #define BLEP_NS (BLEP_ZC * BLEP_OS / BLEP_SP)
  96. #define BLEP_RNS 7 // RNS = (2^ > NS) - 1
  97.  
  98. #ifdef USE_BLEP
  99. typedef struct blep_t
  100. {
  101.     int32_t index, samplesLeft;
  102.     double dBuffer[BLEP_RNS + 1], dLastValue;
  103. } blep_t;
  104. #endif
  105.  
  106. typedef struct paulaVoice_t
  107. {
  108.     volatile bool active;
  109.     const int8_t *data, *newData;
  110.     int32_t length, newLength, pos;
  111.     double dVolume, dDelta, dPhase, dPanL, dPanR;
  112. #ifdef USE_BLEP
  113.     double dLastDelta, dLastPhase;
  114. #endif
  115. } paulaVoice_t;
  116.  
  117. #if defined(USE_HIGHPASS) || defined(USE_LOWPASS)
  118. typedef struct lossyIntegrator_t
  119. {
  120.     double dBuffer[2], dCoeff[2];
  121. } lossyIntegrator_t;
  122. #endif
  123.  
  124. typedef struct soundInfo_t // do not touch!
  125. {
  126.     int8_t *data;
  127.     uint16_t length;
  128.     int8_t *repeat;
  129.     uint16_t replen;
  130. } soundInfo_t;
  131.  
  132. typedef struct fcChannel_t
  133. {
  134.     bool vibratoUp, portaDelay, pitchBendDelay, volSlideDelay;
  135.     int8_t pitchBendValue, pitchBendCounter, note, noteTranspose;
  136.     int8_t soundTranspose, *loopStart, volume, periodTranspose;
  137.     const uint8_t *freqTabPtr, *volTabPtr;
  138.     uint8_t voiceIndex, *seqStartPtr, *patPtr;
  139.     uint8_t freqSusCounter, volSusCounter;
  140.     uint8_t vibratoSpeed, vibratoDepth, vibratoCounter;
  141.     uint8_t vibratoDelay, volSlideSpeed;
  142.     uint8_t volSlideCounter, portaParam, volDelayCounter;
  143.     uint8_t volDelayLength;
  144.     int16_t portaValue;
  145.     uint16_t loopLength, freqTabPos, volTabPos, patPos;
  146.     uint32_t seqPos;
  147. } fcChannel_t;
  148.  
  149. static volatile bool musicPaused;
  150. static bool fc14;
  151. static int8_t *ptr8s_1, *ptr8s_1;
  152. static uint8_t *songData, *ptr8u_1, *ptr8u_2, spdtemp, spdtemp2, respcnt, repspd;
  153. static uint8_t *SEQpoint, *PATpoint, *FRQpoint, *VOLpoint, stereoSep = STEREO_SEP;
  154. static uint16_t oldPeriod;
  155. static int32_t soundBufferSize, samplesPerFrameLeft, samplesPerFrame;
  156. static uint32_t audioRate, numSequences, sampleCounter;
  157. static double oldVoiceDelta, *dMixerBufferL, *dMixerBufferR, dAudioRate, dPeriodToDeltaDiv;
  158. static paulaVoice_t paula[AMIGA_VOICES];
  159. static fcChannel_t Channel[AMIGA_VOICES];
  160. static soundInfo_t samples[NUM_SAMPLES + NUM_WAVEFORMS];
  161. #ifdef USE_BLEP
  162. static blep_t blep[AMIGA_VOICES], blepVol[AMIGA_VOICES];
  163. #endif
  164. #ifdef USE_HIGHPASS
  165. static lossyIntegrator_t filterHi;
  166. #endif
  167. #ifdef USE_LOWPASS
  168. static lossyIntegrator_t filterLo;
  169. #endif
  170.  
  171. #define LERP(x, y, z) ((x) + ((y) - (x)) * (z))
  172. #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
  173. #define CLAMP16(i) if ((int16_t)(i) != i) i = 0x7FFF ^ (i >> 31);
  174. #define PTR2LONG(x) ((uint32_t *)(x))
  175. #define PTR2WORD(x) ((uint16_t *)(x))
  176. #define SWAP16(x) ((uint16_t)(((x) << 8) | ((x) >> 8)))
  177. #define SWAP32(value) \
  178. ( \
  179.     (((uint32_t)((value) & 0x000000FF)) << 24) | \
  180.     (((uint32_t)((value) & 0x0000FF00)) <<  8) | \
  181.     (((uint32_t)((value) & 0x00FF0000)) >>  8) | \
  182.     (((uint32_t)((value) & 0xFF000000)) >> 24)   \
  183. )
  184.  
  185. static const uint8_t silentTable[8] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE1 };
  186.  
  187. static const uint16_t periods[128] =
  188. {
  189.     // 1.0..1.3 periods
  190.     0x06B0,0x0650,0x05F4,0x05A0,0x054C,0x0500,0x04B8,0x0474,0x0434,0x03F8,0x03C0,0x038A,
  191.     0x0358,0x0328,0x02FA,0x02D0,0x02A6,0x0280,0x025C,0x023A,0x021A,0x01FC,0x01E0,0x01C5,
  192.     0x01AC,0x0194,0x017D,0x0168,0x0153,0x0140,0x012E,0x011D,0x010D,0x00FE,0x00F0,0x00E2,
  193.     0x00D6,0x00CA,0x00BE,0x00B4,0x00AA,0x00A0,0x0097,0x008F,0x0087,0x007F,0x0078,0x0071,
  194.     0x0071,0x0071,0x0071,0x0071,0x0071,0x0071,0x0071,0x0071,0x0071,0x0071,0x0071,0x0071,
  195.  
  196.     // 1.4 periods (one extra octave)
  197.     0x0D60,0x0CA0,0x0BE8,0x0B40,0x0A98,0x0A00,0x0970,0x08E8,0x0868,0x07F0,0x0780,0x0714,
  198.     0x06B0,0x0650,0x05F4,0x05A0,0x054C,0x0500,0x04B8,0x0474,0x0434,0x03F8,0x03C0,0x038A,
  199.     0x0358,0x0328,0x02FA,0x02D0,0x02A6,0x0280,0x025C,0x023A,0x021A,0x01FC,0x01E0,0x01C5,
  200.     0x01AC,0x0194,0x017D,0x0168,0x0153,0x0140,0x012E,0x011D,0x010D,0x00FE,0x00F0,0x00E2,
  201.     0x00D6,0x00CA,0x00BE,0x00B4,0x00AA,0x00A0,0x0097,0x008F,0x0087,0x007F,0x0078,0x0071,
  202.     0x0071,0x0071,0x0071,0x0071,0x0071,0x0071,0x0071,0x0071
  203. };
  204.  
  205. static const int8_t waveformDatas[1344] =
  206. {
  207.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  208.     0x3F,0x37,0x2F,0x27,0x1F,0x17,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  209.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  210.     0xC0,0x37,0x2F,0x27,0x1F,0x17,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  211.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  212.     0xC0,0xB8,0x2F,0x27,0x1F,0x17,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  213.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  214.     0xC0,0xB8,0xB0,0x27,0x1F,0x17,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  215.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  216.     0xC0,0xB8,0xB0,0xA8,0x1F,0x17,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  217.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  218.     0xC0,0xB8,0xB0,0xA8,0xA0,0x17,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  219.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  220.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x0F,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  221.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  222.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x07,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  223.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  224.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0xFF,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  225.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  226.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x07,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  227.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  228.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x88,0x0F,0x17,0x1F,0x27,0x2F,0x37,
  229.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  230.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x88,0x90,0x17,0x1F,0x27,0x2F,0x37,
  231.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  232.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x88,0x90,0x98,0x1F,0x27,0x2F,0x37,
  233.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  234.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x88,0x90,0x98,0xA0,0x27,0x2F,0x37,
  235.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  236.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x88,0x90,0x98,0xA0,0xA8,0x2F,0x37,
  237.     0xC0,0xC0,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,0x00,0xF8,0xF0,0xE8,0xE0,0xD8,0xD0,0xC8,
  238.     0xC0,0xB8,0xB0,0xA8,0xA0,0x98,0x90,0x88,0x80,0x88,0x90,0x98,0xA0,0xA8,0xB0,0x37,
  239.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  240.     0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  241.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  242.     0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  243.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  244.     0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  245.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  246.     0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  247.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  248.     0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  249.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  250.     0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  251.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  252.     0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  253.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  254.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  255.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  256.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  257.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  258.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  259.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  260.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  261.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  262.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,0x7F,
  263.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  264.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,0x7F,
  265.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,
  266.     0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x7F,0x7F,0x7F,
  267.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
  268.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x7F,0x7F,
  269.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
  270.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x7F,
  271.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  272.     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  273.     0x80,0x80,0x80,0x80,0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  274.     0x80,0x80,0x80,0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  275.     0x80,0x80,0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  276.     0x80,0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  277.     0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  278.     0x80,0x80,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,
  279.     0x80,0x80,0x90,0x98,0xA0,0xA8,0xB0,0xB8,0xC0,0xC8,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,
  280.     0x00,0x08,0x10,0x18,0x20,0x28,0x30,0x38,0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x7F,
  281.     0x80,0x80,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0,0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,
  282.     0x45,0x45,0x79,0x7D,0x7A,0x77,0x70,0x66,0x61,0x58,0x53,0x4D,0x2C,0x20,0x18,0x12,
  283.     0x04,0xDB,0xD3,0xCD,0xC6,0xBC,0xB5,0xAE,0xA8,0xA3,0x9D,0x99,0x93,0x8E,0x8B,0x8A,
  284.     0x45,0x45,0x79,0x7D,0x7A,0x77,0x70,0x66,0x5B,0x4B,0x43,0x37,0x2C,0x20,0x18,0x12,
  285.     0x04,0xF8,0xE8,0xDB,0xCF,0xC6,0xBE,0xB0,0xA8,0xA4,0x9E,0x9A,0x95,0x94,0x8D,0x83,
  286.     0x00,0x00,0x40,0x60,0x7F,0x60,0x40,0x20,0x00,0xE0,0xC0,0xA0,0x80,0xA0,0xC0,0xE0,
  287.     0x00,0x00,0x40,0x60,0x7F,0x60,0x40,0x20,0x00,0xE0,0xC0,0xA0,0x80,0xA0,0xC0,0xE0,
  288.     0x80,0x80,0x90,0x98,0xA0,0xA8,0xB0,0xB8,0xC0,0xC8,0xD0,0xD8,0xE0,0xE8,0xF0,0xF8,
  289.     0x00,0x08,0x10,0x18,0x20,0x28,0x30,0x38,0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x7F,
  290.     0x80,0x80,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0,0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70
  291. };
  292.  
  293. #ifdef USE_BLEP
  294.  
  295. /* Why this table is not represented as readable float (double) numbers:
  296. ** Accurate float (double) representation in string format requires at least 14 digits and normalized
  297. ** (scientific) notation, notwithstanding compiler issues with precision or rounding error.
  298. ** Also, don't touch this table ever, just keep it exactly identical! */
  299.  
  300. // TODO: get a proper double-precision table. This one is converted from float
  301. static const uint64_t dBlepData[48] =
  302. {
  303.     0x3FEFFC3E20000000, 0x3FEFFAA900000000, 0x3FEFFAD460000000, 0x3FEFFA9C60000000,
  304.     0x3FEFF5B0A0000000, 0x3FEFE42A40000000, 0x3FEFB7F5C0000000, 0x3FEF599BE0000000,
  305.     0x3FEEA5E3C0000000, 0x3FED6E7080000000, 0x3FEB7F7960000000, 0x3FE8AB9E40000000,
  306.     0x3FE4DCA480000000, 0x3FE0251880000000, 0x3FD598FB80000000, 0x3FC53D0D60000000,
  307.     0x3F8383A520000000, 0xBFBC977CC0000000, 0xBFC755C080000000, 0xBFC91BDBA0000000,
  308.     0xBFC455AFC0000000, 0xBFB6461340000000, 0xBF7056C400000000, 0x3FB1028220000000,
  309.     0x3FBB5B7E60000000, 0x3FBC5903A0000000, 0x3FB55403E0000000, 0x3FA3CED340000000,
  310.     0xBF7822DAE0000000, 0xBFA2805D00000000, 0xBFA7140D20000000, 0xBFA18A7760000000,
  311.     0xBF87FF7180000000, 0x3F88CBFA40000000, 0x3F9D4AEC80000000, 0x3FA14A3AC0000000,
  312.     0x3F9D5C5AA0000000, 0x3F92558B40000000, 0x3F7C997EE0000000, 0x0000000000000000,
  313.     0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  314.     0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000
  315. };
  316. #endif
  317.  
  318. static const uint8_t waveformLengths[47] =
  319. {
  320.     0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
  321.     0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
  322.     0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
  323.     0x10, 0x10, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  324.     0x10, 0x08, 0x10, 0x10, 0x08, 0x08, 0x18
  325. };
  326.  
  327. static bool openMixer(uint32_t audioFreq);
  328. static void closeMixer(void);
  329.  
  330. // CODE START
  331.  
  332. static void paulaStopDMA(uint8_t i)
  333. {
  334.     paula[i].active = false;
  335. }
  336.  
  337. static void paulaStartDMA(uint8_t i)
  338. {
  339.     paulaVoice_t *v = &paula[i];
  340.  
  341.     v->dPhase = 0.0;
  342.     v->pos = 0;
  343.     v->data = v->newData;
  344.     v->length = v->newLength;
  345.     v->active = true;
  346. }
  347.  
  348. static void paulaSetPeriod(uint8_t i, uint16_t period)
  349. {
  350.     paulaVoice_t *v = &paula[i];
  351.  
  352.     if (period == 0)
  353.     {
  354.         v->dDelta = 0.0;
  355.         return;
  356.     }
  357.  
  358.     // confirmed Paula behavior
  359.     if (period < 113)
  360.         period = 113;
  361.  
  362.     if (period == oldPeriod)
  363.     {
  364.         v->dDelta = oldVoiceDelta;
  365.     }
  366.     else
  367.     {
  368.         oldPeriod = period;
  369.         v->dDelta = dPeriodToDeltaDiv / period;
  370.         oldVoiceDelta = v->dDelta;
  371.     }
  372.  
  373. #ifdef USE_BLEP
  374.     if (v->dLastDelta == 0.0)
  375.         v->dLastDelta = v->dDelta;
  376. #endif
  377. }
  378.  
  379. static void paulaSetVolume(uint8_t i, uint16_t vol)
  380. {
  381.     vol &= 127;
  382.     if (vol > 64)
  383.         vol = 64;
  384.  
  385.     paula[i].dVolume = vol * (1.0 / 64.0);
  386. }
  387.  
  388. static void paulaSetLength(uint8_t i, uint16_t len)
  389. {
  390.     paula[i].newLength = len * 2;
  391. }
  392.  
  393. static void paulaSetData(uint8_t i, const int8_t *src)
  394. {
  395.     paula[i].newData = src;
  396. }
  397.  
  398. #if defined(USE_HIGHPASS) || defined(USE_LOWPASS)
  399. static void calcCoeffLossyIntegrator(double dSr, double dHz, lossyIntegrator_t *filter)
  400. {
  401.     filter->dCoeff[0] = tan((M_PI * dHz) / dSr);
  402.     filter->dCoeff[1] = 1.0 / (1.0 + filter->dCoeff[0]);
  403. }
  404.  
  405. static void clearLossyIntegrator(lossyIntegrator_t *filter)
  406. {
  407.     filter->dBuffer[0] = 0.0;
  408.     filter->dBuffer[1] = 0.0;
  409. }
  410.  
  411. void lossyIntegrator(lossyIntegrator_t *filter, double *dIn, double *dOut)
  412. {
  413.     double dOutput;
  414.  
  415.     // left channel low-pass
  416.     dOutput = (filter->dCoeff[0] * dIn[0] + filter->dBuffer[0]) * filter->dCoeff[1];
  417.     filter->dBuffer[0] = filter->dCoeff[0] * (dIn[0] - dOutput) + dOutput + 1e-10;
  418.     dOut[0] = dOutput;
  419.  
  420.     // right channel low-pass
  421.     dOutput = (filter->dCoeff[0] * dIn[1] + filter->dBuffer[1]) * filter->dCoeff[1];
  422.     filter->dBuffer[1] = filter->dCoeff[0] * (dIn[1] - dOutput) + dOutput + 1e-10;
  423.     dOut[1] = dOutput;
  424. }
  425.  
  426. void lossyIntegratorHighPass(lossyIntegrator_t *filter, double *dIn, double *dOut)
  427. {
  428.     double dLow[2];
  429.  
  430.     lossyIntegrator(filter, dIn, dLow);
  431.  
  432.     dOut[0] = dIn[0] - dLow[0];
  433.     dOut[1] = dIn[1] - dLow[1];
  434. }
  435. #endif
  436.  
  437. #ifdef USE_BLEP
  438. void blepAdd(blep_t *b, double dOffset, double dAmplitude)
  439. {
  440.     int8_t n;
  441.     int32_t i;
  442.     const double *dBlepSrc;
  443.     double f;
  444.  
  445.     f = dOffset * BLEP_SP;
  446.     i = (int32_t)f; // get integer part of f
  447.     dBlepSrc = (const double *)dBlepData + i + BLEP_OS;
  448.     f -= i; // remove integer part from f
  449.  
  450.     i = b->index;
  451.  
  452.     n = BLEP_NS;
  453.     while (n--)
  454.     {
  455.         b->dBuffer[i] += (dAmplitude * LERP(dBlepSrc[0], dBlepSrc[1], f));
  456.         i = (i + 1) & BLEP_RNS;
  457.  
  458.         dBlepSrc += BLEP_SP;
  459.     }
  460.  
  461.     b->samplesLeft = BLEP_NS;
  462. }
  463.  
  464. double blepRun(blep_t *b)
  465. {
  466.     double fBlepOutput;
  467.  
  468.     fBlepOutput = b->dBuffer[b->index];
  469.     b->dBuffer[b->index] = 0.0;
  470.  
  471.     b->index = (b->index + 1) & BLEP_RNS;
  472.  
  473.     b->samplesLeft--;
  474.     return fBlepOutput;
  475. }
  476. #endif
  477.  
  478. static bool init_music(const uint8_t *moduleData)
  479. {
  480.     uint8_t i;
  481.     soundInfo_t *s;
  482.  
  483.     fc14 = (*PTR2LONG(&moduleData[0]) == 0x34314346); // "FC14"
  484.  
  485.     if (*PTR2LONG(&moduleData[0]) != 0x444F4D53 && !fc14) // "SMOD"
  486.         return false;
  487.  
  488.     // setup pointers...
  489.  
  490.     SEQpoint = (uint8_t *)&moduleData[fc14 ? 180 : 100];
  491.     PATpoint = (uint8_t *)&moduleData[SWAP32(*PTR2LONG(&moduleData[8]))];
  492.     FRQpoint = (uint8_t *)&moduleData[SWAP32(*PTR2LONG(&moduleData[16]))];
  493.     VOLpoint = (uint8_t *)&moduleData[SWAP32(*PTR2LONG(&moduleData[24]))];
  494.    
  495.     // load samples
  496.  
  497.     ptr8s_1 =  (int8_t *)&moduleData[SWAP32(*PTR2LONG(&moduleData[32]))];
  498.     ptr8u_2 = (uint8_t *)&moduleData[40];
  499.  
  500.     for (i = 0; i < NUM_SAMPLES; i++)
  501.     {
  502.         samples[i].data = ptr8s_1;
  503.         samples[i].length = SWAP16(*PTR2WORD(ptr8u_2)); ptr8u_2 += 2;
  504.         samples[i].repeat = &samples[i].data[SWAP16(*PTR2WORD(ptr8u_2))]; ptr8u_2 += 2;
  505.         samples[i].replen = SWAP16(*PTR2WORD(ptr8u_2)); ptr8u_2 += 2;
  506.  
  507.         // fix endless beep on non-looping samples (FC14 doesn't do this)
  508.         if (samples[i].replen <= 1)
  509.         {
  510.             samples[i].replen = 1;
  511.             if (samples[i].length >= 1)
  512.             {
  513.                 if (*PTR2LONG(samples[i].data) != 0x504D5353) // "SSMP"
  514.                     *PTR2WORD(samples[i].data) = 0;
  515.             }
  516.         }
  517.  
  518.         ptr8s_1 += (samples[i].length * 2) + 2;
  519.     }
  520.  
  521.     // load waveforms
  522.  
  523.     if (fc14)
  524.     {
  525.         ptr8s_1 = (int8_t *)&moduleData[SWAP32(*PTR2LONG(&moduleData[36]))];
  526.         ptr8u_2 = (uint8_t *)&moduleData[100];
  527.  
  528.         for (i = 0; i < NUM_WAVEFORMS; i++)
  529.         {
  530.             s = &samples[NUM_SAMPLES + i];
  531.  
  532.             s->data = ptr8s_1;
  533.             s->length = *ptr8u_2++;
  534.             s->repeat = ptr8s_1;
  535.             s->replen = s->length;
  536.  
  537.             ptr8s_1 += s->length * 2;
  538.         }
  539.     }
  540.     else
  541.     {
  542.         ptr8s_1 = (int8_t *)waveformDatas;
  543.         for (i = 0; i < NUM_WAVEFORMS; i++)
  544.         {
  545.             s = &samples[NUM_SAMPLES + i];
  546.  
  547.             if (i < NUM_WAVEFORMS_SMOD)
  548.             {
  549.                 s->data = ptr8s_1;
  550.                 s->length = waveformLengths[i];
  551.                 s->repeat = s->data;
  552.                 s->replen = s->length;
  553.  
  554.                 ptr8s_1 += s->length * 2;
  555.             }
  556.             else
  557.             {
  558.                 s->data = NULL;
  559.                 s->length = 0;
  560.                 s->repeat = NULL;
  561.                 s->replen = 1;
  562.             }
  563.         }
  564.     }
  565.  
  566.     // get number of sequences and make it a multiple of 13 (SEQ_SIZE)
  567.     numSequences = (uint32_t)(SWAP32(*PTR2LONG(&moduleData[4])) / SEQ_SIZE) * SEQ_SIZE;
  568.  
  569.     return true;
  570. }
  571.  
  572. static void restart_song(void)
  573. {
  574.     fcChannel_t *ch;
  575.  
  576.     memset(Channel, 0, sizeof (Channel));
  577.     for (uint8_t i = 0; i < AMIGA_VOICES; i++)
  578.     {
  579.         ch = &Channel[i];
  580.  
  581.         ch->voiceIndex = i;
  582.         ch->volTabPtr = silentTable;
  583.         ch->freqTabPtr = silentTable;
  584.         ch->volDelayCounter = 1;
  585.         ch->volDelayLength = 1;
  586.         ch->pitchBendDelay = true;
  587.         ch->seqPos = SEQ_SIZE; // yes
  588.         ch->seqStartPtr = &SEQpoint[3 * i];
  589.         ch->patPtr = &PATpoint[ch->seqStartPtr[0] << 6];
  590.         ch->noteTranspose = (int8_t)ch->seqStartPtr[1];
  591.         ch->soundTranspose = (int8_t)ch->seqStartPtr[2];
  592.     }
  593.  
  594.     repspd = (SEQpoint[12] > 0) ? SEQpoint[12] : 3;
  595.     respcnt = repspd;
  596.     spdtemp = 0;
  597.     spdtemp2 = 0;
  598. }
  599.  
  600. static void new_note(fcChannel_t *ch)
  601. {
  602.     uint8_t *tmpSeqPtr, *tmpPatPtr, note, info;
  603.  
  604.     tmpPatPtr = &ch->patPtr[ch->patPos]; // temp pattern pointer
  605.  
  606.     if ((fc14 && (*tmpPatPtr & 0x7F) == PAT_END_MARKER) || ch->patPos == 64)
  607.     {
  608.         ch->patPos = 0;
  609.         if (ch->seqPos >= numSequences)
  610.             ch->seqPos = 0;
  611.  
  612.         tmpSeqPtr = &ch->seqStartPtr[ch->seqPos];
  613.         ch->patPtr = &PATpoint[tmpSeqPtr[0] << 6];
  614.  
  615.         ch->noteTranspose = (int8_t)tmpSeqPtr[1];
  616.         ch->soundTranspose = (int8_t)tmpSeqPtr[2];
  617.  
  618.         if (++spdtemp == 4)
  619.         {
  620.             spdtemp = 0;
  621.  
  622.             // we've read all channels now, let's increase the pos used for RS
  623.             if (++spdtemp2 == numSequences/SEQ_SIZE) // numSequences is a multiple of SEQ_SIZE
  624.                 spdtemp2 = 0; // wrap sequence position
  625.         }
  626.  
  627.         // read current RS (replay speed. only update if non-zero)
  628.         if (SEQpoint[(spdtemp2 * 13) + 12] != 0)
  629.         {
  630.             repspd = SEQpoint[(spdtemp2 * 13) + 12];
  631.             respcnt = repspd;
  632.         }
  633.  
  634.         ch->seqPos += SEQ_SIZE;
  635.         tmpPatPtr = ch->patPtr; // set new temp pattern pointer
  636.     }
  637.  
  638.     note = tmpPatPtr[0];
  639.     info = tmpPatPtr[1];
  640.  
  641.     if (note == 0)
  642.     {
  643.         info &= 0xC0;
  644.         if (info != 0)
  645.         {
  646.             ch->portaParam = 0;
  647.             if (info & (1 << 7))
  648.                 ch->portaParam = tmpPatPtr[3];
  649.         }
  650.     }
  651.     else
  652.     {
  653.         ch->portaValue = 0;
  654.         ch->portaParam = 0;
  655.  
  656.         if (info & (1 << 7))
  657.             ch->portaParam = tmpPatPtr[3];
  658.     }
  659.  
  660.     note &= 0x7F;
  661.     if (note != 0)
  662.     {
  663.         ptr8u_1 = &VOLpoint[((tmpPatPtr[1] & 0x3F) + ch->soundTranspose) << 6];
  664.  
  665.         ch->note = note;
  666.         ch->volDelayLength = ptr8u_1[0];
  667.         ch->volDelayCounter = ch->volDelayLength;
  668.         ch->freqTabPtr = &FRQpoint[ptr8u_1[1] << 6];
  669.         ch->freqTabPos = 0;
  670.         ch->freqSusCounter = 0;
  671.         ch->vibratoSpeed = ptr8u_1[2];
  672.         ch->vibratoDepth = ptr8u_1[3];
  673.         ch->vibratoDelay = ptr8u_1[4];
  674.         ch->vibratoCounter = ch->vibratoDepth;
  675.         ch->vibratoUp = true; // default initial state on new note
  676.         ch->volTabPtr = &ptr8u_1[5];
  677.         ch->volTabPos = 0;
  678.         ch->volSusCounter = 0;
  679.  
  680.         paulaStopDMA(ch->voiceIndex); // yes, this is important
  681.     }
  682.  
  683.     ch->patPos += 2;
  684. }
  685.  
  686. static void doFreqModulation(fcChannel_t *ch)
  687. {
  688.     bool doTranspose;
  689.     uint8_t *tmpPtr;
  690.     const uint8_t *tabPtr;
  691.     soundInfo_t *sample;
  692.  
  693. testsustain:
  694.     if (ch->freqSusCounter > 0)
  695.     {
  696.         ch->freqSusCounter--;
  697.     }
  698.     else
  699.     {
  700.         tabPtr = &ch->freqTabPtr[ch->freqTabPos];
  701.  
  702. testeffects:
  703.         if (tabPtr[0] != 0xE1)
  704.         {
  705.             doTranspose = true;
  706.  
  707.             if (tabPtr[0] == 0xE0) // freq pos jump
  708.             {
  709.                 ch->freqTabPos = tabPtr[1] & 0x3F;
  710.                 tabPtr = &ch->freqTabPtr[ch->freqTabPos];
  711.             }
  712.  
  713.             if (tabPtr[0] == 0xE2) // set waveform
  714.             {
  715.                 if (tabPtr[1] < NUM_SAMPLES+NUM_WAVEFORMS)
  716.                 {
  717.                     sample = &samples[tabPtr[1]];
  718.  
  719.                     ch->loopStart = sample->repeat;
  720.                     ch->loopLength = sample->replen;
  721.  
  722.                     paulaSetData(ch->voiceIndex, sample->data);
  723.                     paulaSetLength(ch->voiceIndex, sample->length);
  724.                     paulaStartDMA(ch->voiceIndex);
  725.  
  726.                     ch->volTabPos = 0;
  727.                     ch->volDelayCounter = 1;
  728.                 }
  729.  
  730.                 ch->freqTabPos += 2;
  731.             }
  732.             else if (tabPtr[0] == 0xE4) // update waveform
  733.             {
  734.                 if (tabPtr[1] < NUM_SAMPLES+NUM_WAVEFORMS)
  735.                 {
  736.                     sample = &samples[tabPtr[1]];
  737.  
  738.                     ch->loopStart = sample->repeat;
  739.                     ch->loopLength = sample->replen;
  740.  
  741.                     paulaSetData(ch->voiceIndex, sample->data);
  742.                     paulaSetLength(ch->voiceIndex, sample->length);
  743.                 }
  744.  
  745.                 ch->freqTabPos += 2;
  746.             }
  747.             else if (tabPtr[0] == 0xE9) // set packed waveform
  748.             {
  749.                 if (tabPtr[1] < NUM_SAMPLES+NUM_WAVEFORMS)
  750.                 {
  751.                     sample = &samples[tabPtr[1]];
  752.                     if (*PTR2LONG(sample->data) == 0x504D5353) // "SSMP"
  753.                     {
  754.                         tmpPtr = (uint8_t *)&sample->data[4 + (tabPtr[2] * 16)];
  755.  
  756.                         ch->loopStart = &sample->data[(4 + 320 + *PTR2LONG(&tmpPtr[0])) + *PTR2WORD(&tmpPtr[6])];
  757.                         ch->loopLength = *PTR2WORD(&tmpPtr[8]);
  758.  
  759.                         // fix endless beep on non-looping samples (FC14 doesn't do this)
  760.                         if (ch->loopLength <= 1)
  761.                         {
  762.                             ch->loopLength = 1;
  763.                             if (*PTR2WORD(&tmpPtr[4]) >= 1) // sample length
  764.                                 *PTR2WORD(&sample->data[4 + 320 + *PTR2LONG(&tmpPtr[0])]) = 0;
  765.                         }
  766.  
  767.                         paulaSetData(ch->voiceIndex,  &sample->data[4 + 320 + *PTR2LONG(&tmpPtr[0])]);
  768.                         paulaSetLength(ch->voiceIndex, *PTR2WORD(&tmpPtr[4]));
  769.                         paulaStartDMA(ch->voiceIndex);
  770.  
  771.                         ch->volTabPos = 0;
  772.                         ch->volDelayCounter = 1;
  773.                     }
  774.                 }
  775.  
  776.                 ch->freqTabPos += 3;
  777.             }
  778.             else if (tabPtr[0] == 0xE7) // new freq pos
  779.             {
  780.                 tabPtr = &FRQpoint[(tabPtr[1] & 0x3F) << 6];
  781.  
  782.                 ch->freqTabPtr = tabPtr;
  783.                 ch->freqTabPos = 0;
  784.  
  785.                 goto testeffects;
  786.             }
  787.             else if (tabPtr[0] == 0xE8) // set freq sustain
  788.             {
  789.                 ch->freqSusCounter = tabPtr[1];
  790.  
  791.                 ch->freqTabPos += 2;
  792.                 goto testsustain;
  793.             }
  794.             else if (tabPtr[0] == 0xE3) // set vibrato
  795.             {
  796.                 ch->freqTabPos += 3;
  797.  
  798.                 ch->vibratoSpeed = tabPtr[1];
  799.                 ch->vibratoDepth = tabPtr[2];
  800.  
  801.                 doTranspose = false; // don't do period transpose here
  802.             }
  803.             else if (tabPtr[0] == 0xEA) // set pitch bend
  804.             {
  805.                 ch->pitchBendValue = (int8_t)tabPtr[1];
  806.                 ch->pitchBendCounter = (int8_t)tabPtr[2];
  807.  
  808.                 ch->freqTabPos += 3;
  809.             }
  810.  
  811.             if (doTranspose)
  812.                 ch->periodTranspose = (int8_t)ch->freqTabPtr[ch->freqTabPos++];
  813.         }
  814.     }
  815. }
  816.  
  817. static void do_VOLbend(fcChannel_t *ch)
  818. {
  819.     ch->volSlideDelay = !ch->volSlideDelay;
  820.     if (ch->volSlideDelay)
  821.     {
  822.         ch->volSlideCounter--;
  823.  
  824.         ch->volume += ch->volSlideSpeed;
  825.         if (ch->volume < 0)
  826.         {
  827.             ch->volSlideCounter = 0;
  828.             ch->volume = 0;
  829.         }
  830.     }
  831. }
  832.  
  833. static void doVolModulation(fcChannel_t *ch)
  834. {
  835.     const uint8_t *tabPtr;
  836.  
  837. VOLUfx:
  838.     if (ch->volSusCounter > 0)
  839.     {
  840.         ch->volSusCounter--;
  841.     }
  842.     else
  843.     {
  844.         if (ch->volSlideCounter > 0)
  845.         {
  846.             do_VOLbend(ch);
  847.         }
  848.         else
  849.         {
  850.             if (--ch->volDelayCounter == 0)
  851.             {
  852.                 ch->volDelayCounter = ch->volDelayLength;
  853. volu_cmd:
  854.                 tabPtr = &ch->volTabPtr[ch->volTabPos];
  855.  
  856.                 if (tabPtr[0] != 0xE1)
  857.                 {
  858.                     if (tabPtr[0] == 0xE8) // set vol sustain
  859.                     {
  860.                         ch->volSusCounter = tabPtr[1];
  861.                         ch->volTabPos += 2;
  862.  
  863.                         goto VOLUfx;
  864.                     }
  865.                     else if (tabPtr[0] == 0xEA) // set vol slide
  866.                     {
  867.                         ch->volSlideSpeed = tabPtr[1];
  868.                         ch->volSlideCounter = tabPtr[2];
  869.  
  870.                         ch->volTabPos += 3;
  871.                         do_VOLbend(ch);
  872.                     }
  873.                     else if (tabPtr[0] == 0xE0) // set vol pos
  874.                     {
  875.                         if ((int8_t)(tabPtr[1] & 0x3F)-5 < 0)
  876.                             ch->volTabPos = 0;
  877.                         else
  878.                             ch->volTabPos = (tabPtr[1] & 0x3F) - 5;
  879.  
  880.                         goto volu_cmd;
  881.                     }
  882.                     else
  883.                     {
  884.                         ch->volume = (int8_t)ch->volTabPtr[ch->volTabPos++];
  885.                     }
  886.                 }
  887.             }
  888.         }
  889.     }
  890. }
  891.  
  892. static void effects(fcChannel_t *ch)
  893. {
  894.     int8_t tmpNote;
  895.     int16_t tmpVibPeriod, tmpPeriod;
  896.     uint16_t tmpVibNote;
  897.  
  898.     doFreqModulation(ch);
  899.     doVolModulation(ch);
  900.  
  901.     // get period from note and transposes...
  902.     tmpNote = ch->periodTranspose;
  903.     if (tmpNote >= 0)
  904.     {
  905.         tmpNote += ch->note;
  906.         tmpNote += ch->noteTranspose;
  907.     }
  908.     tmpNote &= 0x7F;
  909.  
  910.     tmpPeriod = periods[tmpNote];
  911.  
  912.     // apply vibrato to period
  913.     if (ch->vibratoDelay > 0)
  914.     {
  915.         ch->vibratoDelay--;
  916.     }
  917.     else
  918.     {
  919.         tmpVibPeriod = ch->vibratoCounter;
  920.         if (!ch->vibratoUp)
  921.         {
  922.             tmpVibPeriod -= ch->vibratoSpeed;
  923.             if (tmpVibPeriod < 0)
  924.             {
  925.                 tmpVibPeriod = 0;
  926.                 ch->vibratoUp = true;
  927.             }
  928.         }
  929.         else
  930.         {
  931.             tmpVibPeriod += ch->vibratoSpeed;
  932.             if (tmpVibPeriod > ch->vibratoDepth*2)
  933.             {
  934.                 tmpVibPeriod = ch->vibratoDepth*2;
  935.                 ch->vibratoUp = false;
  936.             }
  937.         }
  938.         ch->vibratoCounter = tmpVibPeriod & 0xFF;
  939.  
  940.         tmpVibPeriod -= ch->vibratoDepth;
  941.  
  942.         tmpVibNote = tmpNote * 2;
  943.         while (tmpVibNote < 12*8)
  944.         {
  945.             tmpVibPeriod *= 2;
  946.             tmpVibNote += 12*2;
  947.         }
  948.  
  949.         tmpPeriod += tmpVibPeriod;
  950.     }
  951.  
  952.     // update portamento value (twice as slow on FC1.4)
  953.     ch->portaDelay = !ch->portaDelay;
  954.     if (!fc14 || ch->portaDelay)
  955.     {
  956.         if (ch->portaParam > 0)
  957.         {
  958.             if (ch->portaParam > 0x1F)
  959.                 ch->portaValue += ch->portaParam & 0x1F;
  960.             else
  961.                 ch->portaValue -= ch->portaParam;
  962.         }
  963.     }
  964.  
  965.     // apply pitch bend to portamento value
  966.     ch->pitchBendDelay = !ch->pitchBendDelay;
  967.     if (ch->pitchBendDelay)
  968.     {
  969.         if (ch->pitchBendCounter > 0)
  970.         {
  971.             ch->pitchBendCounter--;
  972.             ch->portaValue -= ch->pitchBendValue;
  973.         }
  974.     }
  975.  
  976.     tmpPeriod += ch->portaValue;
  977.     tmpPeriod = CLAMP(tmpPeriod, 0x0071, fc14 ? 0x0D60 : 0x06B0);
  978.  
  979.     paulaSetPeriod(ch->voiceIndex, tmpPeriod);
  980.     paulaSetVolume(ch->voiceIndex, ch->volume);
  981. }
  982.  
  983. static void play_music(void)
  984. {
  985.     uint8_t i;
  986.  
  987.     if (--respcnt == 0)
  988.     {
  989.         respcnt = repspd;
  990.         for (i = 0; i < AMIGA_VOICES; i++)
  991.             new_note(&Channel[i]);
  992.     }
  993.  
  994.     for (i = 0; i < AMIGA_VOICES; i++)
  995.     {
  996.         effects(&Channel[i]);
  997.  
  998.         // these take effect when the current DMA cycle is done
  999.         paulaSetData(i, Channel[i].loopStart);
  1000.         paulaSetLength(i, Channel[i].loopLength);
  1001.     }
  1002. }
  1003.  
  1004. static void mixAudio(int16_t *stream, int32_t sampleBlockLength)
  1005. {
  1006.     int32_t i, j, smpL, smpR;
  1007.     double dSample, dVolume, dOut[2];
  1008.     paulaVoice_t *v;
  1009. #ifdef USE_BLEP
  1010.     blep_t *bSmp, *bVol;
  1011. #endif
  1012.  
  1013.     memset(dMixerBufferL, 0, sizeof (double) * sampleBlockLength);
  1014.     memset(dMixerBufferR, 0, sizeof (double) * sampleBlockLength);
  1015.  
  1016.     if (musicPaused)
  1017.     {
  1018.         memset(stream, 0, sampleBlockLength * (sizeof (int16_t) * 2));
  1019.         return;
  1020.     }
  1021.  
  1022.     for (i = 0; i < AMIGA_VOICES; i++)
  1023.     {
  1024.         v = &paula[i];
  1025.         if (!v->active || v->length < 2 || v->data == NULL)
  1026.             continue;
  1027.  
  1028. #ifdef USE_BLEP
  1029.         bSmp = &blep[i];
  1030.         bVol = &blepVol[i];
  1031. #endif
  1032.         for (j = 0; j < sampleBlockLength; j++)
  1033.         {
  1034.             dSample = v->data[v->pos] * (1.0 / 128.0);
  1035.             dVolume = v->dVolume;
  1036.  
  1037. #ifdef USE_BLEP
  1038.             if (dSample != bSmp->dLastValue)
  1039.             {
  1040.                 if (v->dLastDelta > 0.0 && v->dLastDelta > v->dLastPhase)
  1041.                     blepAdd(bSmp, v->dLastPhase / v->dLastDelta, bSmp->dLastValue - dSample);
  1042.  
  1043.                 bSmp->dLastValue = dSample;
  1044.             }
  1045.  
  1046.             if (dVolume != bVol->dLastValue)
  1047.             {
  1048.                 blepAdd(bVol, 0.0, bVol->dLastValue - dVolume);
  1049.                 bVol->dLastValue = dVolume;
  1050.             }
  1051.  
  1052.             if (bSmp->samplesLeft > 0) dSample += blepRun(bSmp);
  1053.             if (bVol->samplesLeft > 0) dVolume += blepRun(bVol);
  1054. #endif
  1055.             dSample *= dVolume;
  1056.  
  1057.             dMixerBufferL[j] += dSample * v->dPanL;
  1058.             dMixerBufferR[j] += dSample * v->dPanR;
  1059.  
  1060.             v->dPhase += v->dDelta;
  1061.             if (v->dPhase >= 1.0)
  1062.             {
  1063.                 v->dPhase -= 1.0;
  1064. #ifdef USE_BLEP
  1065.                 v->dLastPhase = v->dPhase;
  1066.                 v->dLastDelta = v->dDelta;
  1067. #endif
  1068.                 if (++v->pos >= v->length)
  1069.                 {
  1070.                     v->pos = 0;
  1071.  
  1072.                     // re-fetch Paula register values now
  1073.                     v->length = v->newLength;
  1074.                     v->data = v->newData;
  1075.                 }
  1076.             }
  1077.         }
  1078.     }
  1079.  
  1080.     for (i = 0; i < sampleBlockLength; i++)
  1081.     {
  1082.         dOut[0] = dMixerBufferL[i];
  1083.         dOut[1] = dMixerBufferR[i];
  1084.  
  1085. #ifdef USE_LOWPASS
  1086.         lossyIntegrator(&filterLo, dOut, dOut);
  1087. #endif
  1088.  
  1089. #ifdef USE_HIGHPASS
  1090.         lossyIntegratorHighPass(&filterHi, dOut, dOut);
  1091. #endif
  1092.  
  1093.         //normalize amplitude
  1094.         dOut[0] *= 32768.0 / AMIGA_VOICES;
  1095.         dOut[1] *= 32768.0 / AMIGA_VOICES;
  1096.  
  1097.         smpL = (int32_t)dOut[0];
  1098.         smpR = (int32_t)dOut[1];
  1099.  
  1100.         CLAMP16(smpL);
  1101.         CLAMP16(smpR);
  1102.  
  1103.         *stream++ = (int16_t)smpL;
  1104.         *stream++ = (int16_t)smpR;
  1105.     }
  1106. }
  1107.  
  1108. // these are used to create an equal powered panning
  1109. static double sinApx(double fX)
  1110. {
  1111.     fX = fX * (2.0 - fX);
  1112.     return fX * 1.09742972 + fX * fX * 0.31678383;
  1113. }
  1114.  
  1115. static double cosApx(double fX)
  1116. {
  1117.     fX = (1.0 - fX) * (1.0 + fX);
  1118.     return fX * 1.09742972 + fX * fX * 0.31678383;
  1119. }
  1120. // -------------------------------------------------
  1121.  
  1122. static void calculatePans(uint8_t stereoSeparation)
  1123. {
  1124.     uint8_t scaledPanPos;
  1125.     double p;
  1126.  
  1127.     if (stereoSeparation > 100)
  1128.         stereoSeparation = 100;
  1129.  
  1130.     scaledPanPos = (stereoSeparation * 128) / 100;
  1131.  
  1132.     p = (128 - scaledPanPos) * (1.0 / 256.0);
  1133.     paula[0].dPanL = cosApx(p);
  1134.     paula[0].dPanR = sinApx(p);
  1135.     paula[3].dPanL = cosApx(p);
  1136.     paula[3].dPanR = sinApx(p);
  1137.  
  1138.     p = (128 + scaledPanPos) * (1.0 / 256.0);
  1139.     paula[1].dPanL = cosApx(p);
  1140.     paula[1].dPanR = sinApx(p);
  1141.     paula[2].dPanL = cosApx(p);
  1142.     paula[2].dPanR = sinApx(p);
  1143. }
  1144.  
  1145. void fc14play_FillAudioBuffer(int16_t *buffer, int32_t numSamples)
  1146. {
  1147.     int32_t a, b;
  1148.  
  1149.     a = numSamples;
  1150.     while (a > 0)
  1151.     {
  1152.         if (samplesPerFrameLeft == 0)
  1153.         {
  1154.             if (!musicPaused)
  1155.                 play_music();
  1156.  
  1157.             samplesPerFrameLeft = samplesPerFrame;
  1158.         }
  1159.  
  1160.         b = a;
  1161.         if (b > samplesPerFrameLeft)
  1162.             b = samplesPerFrameLeft;
  1163.  
  1164.         mixAudio(buffer, b);
  1165.         buffer += (uint32_t)b * 2;
  1166.  
  1167.         a -= b;
  1168.         samplesPerFrameLeft -= b;
  1169.     }
  1170.  
  1171.     sampleCounter += numSamples;
  1172. }
  1173.  
  1174. void fc14play_PauseSong(bool flag)
  1175. {
  1176.     musicPaused = flag;
  1177. }
  1178.  
  1179. void fc14play_TogglePause(void)
  1180. {
  1181.     musicPaused ^= 1;
  1182. }
  1183.  
  1184. void fc14play_Close(void)
  1185. {
  1186.     closeMixer();
  1187.  
  1188.     if (dMixerBufferL != NULL)
  1189.     {
  1190.         free(dMixerBufferL);
  1191.         dMixerBufferL = NULL;
  1192.     }
  1193.  
  1194.     if (dMixerBufferR != NULL)
  1195.     {
  1196.         free(dMixerBufferR);
  1197.         dMixerBufferR = NULL;
  1198.     }
  1199.  
  1200.     if (songData != NULL)
  1201.     {
  1202.         free(songData);
  1203.         songData = NULL;
  1204.     }
  1205. }
  1206.  
  1207. void fc14play_SetStereoSep(uint8_t percentage)
  1208. {
  1209.     stereoSep = percentage;
  1210.     if (stereoSep > 100)
  1211.         stereoSep = 100;
  1212.  
  1213.     calculatePans(stereoSep);
  1214. }
  1215.  
  1216. uint32_t fc14play_GetMixerTicks(void)
  1217. {
  1218.     if (audioRate < 1000)
  1219.         return 0;
  1220.  
  1221.     return sampleCounter / (audioRate / 1000);
  1222. }
  1223.  
  1224. bool fc14play_PlaySong(const uint8_t *moduleData, uint32_t dataLength, uint32_t audioFreq)
  1225. {
  1226.     if (audioFreq == 0)
  1227.         audioFreq = 44100;
  1228.  
  1229.     musicPaused = true;
  1230.  
  1231.     fc14play_Close();
  1232.  
  1233.     oldPeriod = 0;
  1234.     oldVoiceDelta = 0.0;
  1235.     sampleCounter = 0;
  1236.  
  1237.     songData = (uint8_t *)malloc(dataLength);
  1238.     if (songData == NULL)
  1239.         return false;
  1240.  
  1241.     memcpy(songData, moduleData, dataLength);
  1242.     if (!init_music(songData))
  1243.     {
  1244.         fc14play_Close();
  1245.         return false;
  1246.     }
  1247.  
  1248.     dMixerBufferL = (double *)malloc(MIX_BUF_SAMPLES * sizeof (double));
  1249.     dMixerBufferR = (double *)malloc(MIX_BUF_SAMPLES * sizeof (double));
  1250.  
  1251.     if (dMixerBufferL == NULL || dMixerBufferR == NULL)
  1252.     {
  1253.         fc14play_Close();
  1254.         return false;
  1255.     }
  1256.  
  1257.     restart_song();
  1258.  
  1259.     // rates below 32kHz will mess up the BLEP synthesis
  1260.     audioFreq = CLAMP(audioFreq, 32000, 96000);
  1261.  
  1262.     if (!openMixer(audioFreq))
  1263.     {
  1264.         fc14play_Close();
  1265.         return false;
  1266.     }
  1267.  
  1268.     audioRate = audioFreq;
  1269.     dAudioRate = (double)audioFreq;
  1270.     dPeriodToDeltaDiv = (double)PAULA_CLK / dAudioRate;
  1271.     soundBufferSize = MIX_BUF_SAMPLES;
  1272.     samplesPerFrame = (uint32_t)round(dAudioRate / 50.0);
  1273.  
  1274. #ifdef USE_LOWPASS
  1275.     // Amiga 500 rev6 RC low-pass filter (~4420.97Hz, 500Hz added to better match A500)
  1276.     calcCoeffLossyIntegrator(dAudioRate, 4420.9706414415377 + 500.0, &filterLo);
  1277. #endif
  1278.  
  1279. #ifdef USE_HIGHPASS
  1280.     // Amiga 500 rev6 RC high-pass filter (~5.12Hz)
  1281.     calcCoeffLossyIntegrator(dAudioRate, 5.1276291562435077, &filterHi);
  1282. #endif
  1283.  
  1284.     memset(paula, 0, sizeof (paula));
  1285.     calculatePans(stereoSep);
  1286.  
  1287. #ifdef USE_BLEP
  1288.     memset(blep, 0, sizeof (blep));
  1289.     memset(blepVol, 0, sizeof (blepVol));
  1290. #endif
  1291.  
  1292. #ifdef USE_LOWPASS
  1293.     clearLossyIntegrator(&filterLo);
  1294. #endif
  1295.  
  1296. #ifdef USE_HIGHPASS
  1297.     clearLossyIntegrator(&filterHi);
  1298. #endif
  1299.  
  1300.     musicPaused = false;
  1301.     return true;
  1302. }
  1303.  
  1304. // the following must be changed if you want to use another audio API than WinMM
  1305.  
  1306. #ifndef WIN32_LEAN_AND_MEAN
  1307. #define WIN32_LEAN_AND_MEAN
  1308. #endif
  1309.  
  1310. #include <windows.h>
  1311. #include <mmsystem.h>
  1312.  
  1313. #define MIX_BUF_NUM 2
  1314.  
  1315. static volatile BOOL audioRunningFlag;
  1316. static uint8_t currBuffer;
  1317. static int16_t *mixBuffer[MIX_BUF_NUM];
  1318. static HANDLE hThread, hAudioSem;
  1319. static WAVEHDR waveBlocks[MIX_BUF_NUM];
  1320. static HWAVEOUT hWave;
  1321.  
  1322. static DWORD WINAPI mixThread(LPVOID lpParam)
  1323. {
  1324.     WAVEHDR *waveBlock;
  1325.  
  1326.     (void)lpParam;
  1327.  
  1328.     SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  1329.  
  1330.     while (audioRunningFlag)
  1331.     {
  1332.         waveBlock = &waveBlocks[currBuffer];
  1333.         fc14play_FillAudioBuffer((int16_t *)waveBlock->lpData, MIX_BUF_SAMPLES);
  1334.         waveOutWrite(hWave, waveBlock, sizeof (WAVEHDR));
  1335.         currBuffer = (currBuffer + 1) % MIX_BUF_NUM;
  1336.  
  1337.         // wait for buffer fill request
  1338.         WaitForSingleObject(hAudioSem, INFINITE);
  1339.     }
  1340.  
  1341.     return 0;
  1342. }
  1343.  
  1344. static void CALLBACK waveProc(HWAVEOUT hWaveOut, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
  1345. {
  1346.     // make compiler happy!
  1347.     (void)hWaveOut;
  1348.     (void)uMsg;
  1349.     (void)dwInstance;
  1350.     (void)dwParam1;
  1351.     (void)dwParam2;
  1352.  
  1353.     if (uMsg == WOM_DONE)
  1354.         ReleaseSemaphore(hAudioSem, 1, NULL);
  1355. }
  1356.  
  1357. static void closeMixer(void)
  1358. {
  1359.     int32_t i;
  1360.  
  1361.     audioRunningFlag = false; // make thread end when it's done
  1362.  
  1363.     if (hAudioSem != NULL)
  1364.         ReleaseSemaphore(hAudioSem, 1, NULL);
  1365.  
  1366.     if (hThread != NULL)
  1367.     {
  1368.         WaitForSingleObject(hThread, INFINITE);
  1369.         CloseHandle(hThread);
  1370.         hThread = NULL;
  1371.     }
  1372.  
  1373.     if (hAudioSem != NULL)
  1374.     {
  1375.         CloseHandle(hAudioSem);
  1376.         hAudioSem = NULL;
  1377.     }
  1378.  
  1379.     if (hWave != NULL)
  1380.     {
  1381.         waveOutReset(hWave);
  1382.  
  1383.         for (i = 0; i < MIX_BUF_NUM; i++)
  1384.         {
  1385.             if (waveBlocks[i].dwUser != 0xFFFF)
  1386.                 waveOutUnprepareHeader(hWave, &waveBlocks[i], sizeof (WAVEHDR));
  1387.         }
  1388.  
  1389.         waveOutClose(hWave);
  1390.         hWave = NULL;
  1391.     }
  1392.  
  1393.     for (i = 0; i < MIX_BUF_NUM; i++)
  1394.     {
  1395.         if (mixBuffer[i] != NULL)
  1396.         {
  1397.             free(mixBuffer[i]);
  1398.             mixBuffer[i] = NULL;
  1399.         }
  1400.     }
  1401. }
  1402.  
  1403. static bool openMixer(uint32_t audioFreq)
  1404. {
  1405.     int32_t i;
  1406.     DWORD threadID;
  1407.     WAVEFORMATEX wfx;
  1408.  
  1409.     // don't unprepare headers on error
  1410.     for (i = 0; i < MIX_BUF_NUM; i++)
  1411.         waveBlocks[i].dwUser = 0xFFFF;
  1412.  
  1413.     closeMixer();
  1414.  
  1415.     ZeroMemory(&wfx, sizeof (wfx));
  1416.     wfx.nSamplesPerSec = audioFreq;
  1417.     wfx.wBitsPerSample = 16;
  1418.     wfx.nChannels = 2;
  1419.     wfx.wFormatTag = WAVE_FORMAT_PCM;
  1420.     wfx.nBlockAlign = wfx.nChannels * (wfx.wBitsPerSample / 8);
  1421.     wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
  1422.  
  1423.     samplesPerFrameLeft = 0;
  1424.     currBuffer = 0;
  1425.  
  1426.     if (waveOutOpen(&hWave, WAVE_MAPPER, &wfx, (DWORD_PTR)&waveProc, 0, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
  1427.         goto omError;
  1428.  
  1429.     // create semaphore for buffer fill requests
  1430.     hAudioSem = CreateSemaphore(NULL, MIX_BUF_NUM - 1, MIX_BUF_NUM, NULL);
  1431.     if (hAudioSem == NULL)
  1432.         goto omError;
  1433.  
  1434.     // allocate WinMM mix buffers
  1435.     for (i = 0; i < MIX_BUF_NUM; i++)
  1436.     {
  1437.         mixBuffer[i] = (int16_t *)calloc(MIX_BUF_SAMPLES, wfx.nBlockAlign);
  1438.         if (mixBuffer[i] == NULL)
  1439.             goto omError;
  1440.     }
  1441.  
  1442.     // initialize WinMM mix headers
  1443.  
  1444.     memset(waveBlocks, 0, sizeof (waveBlocks));
  1445.     for (i = 0; i < MIX_BUF_NUM; i++)
  1446.     {
  1447.         waveBlocks[i].lpData = (LPSTR)mixBuffer[i];
  1448.         waveBlocks[i].dwBufferLength = MIX_BUF_SAMPLES * wfx.nBlockAlign;
  1449.         waveBlocks[i].dwFlags = WHDR_DONE;
  1450.  
  1451.         if (waveOutPrepareHeader(hWave, &waveBlocks[i], sizeof (WAVEHDR)) != MMSYSERR_NOERROR)
  1452.             goto omError;
  1453.     }
  1454.  
  1455.     // create main mixer thread
  1456.     audioRunningFlag = true;
  1457.     hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)mixThread, NULL, 0, &threadID);
  1458.     if (hThread == NULL)
  1459.         goto omError;
  1460.  
  1461.     return TRUE;
  1462.  
  1463. omError:
  1464.     closeMixer();
  1465.     return FALSE;
  1466. }
  1467.  
  1468. // ---------------------------------------------------------------------------
  1469.  
  1470. // 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