main.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <malloc.h>
  5. #include <Windows.h>
  6. #include <MMSystem.h>
  7. #include "binary.h"
  8. static inline uint8_t THREEQUARTERS(uint8_t x)
  9. {
  10. return (x >> 2) + (x >> 1);
  11. }
  12. const int notes[37] = { 134, 142, 150, 159, 169, 179, 189, 201, 213, 225, 239, 253, 268, 284, 301, 319, 338, 358, 379, 401, 425, 451, 477, 506, 536, 568, 601, 637, 675, 715, 758, 803, 851, 901, 955, 1011, 1072 };
  13. const uint8_t arpnotes[] = {
  14. 8, 10, 12, 14, 15, 17, 19, 20, 21, 22, 24, 26, 27, 29,
  15. };
  16. const uint16_t arpeggio[][2] = {
  17. { 0x24, 0x6A },
  18. { 0x46, 0x9C },
  19. { 0x13, 0x59 },
  20. { 0x02, 0x47 },
  21. { 0x24, 0x59 },
  22. { 0x24, 0x58 },
  23. { 0x57, 0xAD },
  24. { 0x35, 0x9B }
  25. };
  26. #define ARPSIZE 76
  27. #if 0
  28. uint8_t arpseq1[4][8] = {
  29. { 0, 0, 1, 2, 0, 0, 6, 2, },
  30. { 0, 0, 1, 2, 0, 0, 1, 7, },
  31. { 0, 0, 1, 2, 0, 0, 1, 2, },
  32. { 3, 3, 2, 2, 0, 0, 4, 5, },
  33. };
  34. #else
  35. const uint8_t arpseq1[4][4] = {
  36. { 0x00, 0x12, 0x00, 0x62 },
  37. { 0x00, 0x12, 0x00, 0x17 },
  38. { 0x00, 0x12, 0x00, 0x12 },
  39. { 0x33, 0x22, 0x00, 0x45 },
  40. };
  41. #endif
  42. const int arpseq2[] = { 0, 1, 0, 1, 0, 1, 0, 2, 3, 3 };
  43. const uint32_t arptiming = B32(00001100,00110000,11111011,00001100);
  44. const int bassbeat[8] = { 0, 0, 1, 0, 0, 1, 0, 1 };
  45. const int bassline[] = {
  46. 12, 12, 15, 10, 12, 12, 17, 10, 12, 12, 15, 7, 8, 8, 3, 7,
  47. 8, 8, 10, 10, 12, 12, 5, 5, 8, 8, 10, 10,
  48. };
  49. const uint8_t leadnotes[] = {
  50. 0xFF, 0, 3, 5, 7, 8, 10, 12, 14, 15, 17, 19, 20, 22, 24, 26, 27
  51. };
  52. const uint8_t leadtimes[] = {
  53. 1, 2, 3, 4, 5, 6, 28, 14
  54. };
  55. const uint8_t leaddata[] = {
  56. 0x67, 0x24, 0x20, 0x27, 0x20, 0x28, 0x89, 0x0, 0x28, 0x20, 0x27, 0x20, 0x28, 0x89, 0x0, 0x28,
  57. 0x20, 0x27, 0x20, 0x28, 0x86, 0x0, 0x44, 0x0, 0x63, 0x24, 0x62, 0xA1, 0xE0, 0xE0, 0xE0, 0xE0,
  58. 0x20, 0x29, 0x20, 0x2A, 0x8B, 0x0, 0x4D, 0x0, 0x6E, 0x2F, 0x70, 0xAE, 0xE0, 0xE0, 0xE0, 0xE0,
  59. 0x20, 0x29, 0x20, 0x2A, 0x8B, 0x0, 0x4D, 0x0, 0x6E, 0x2F, 0x6E, 0xAC, 0xE0, 0xE0, 0xE0, 0xE0,
  60. 0x65, 0x22, 0x20, 0x65, 0x26, 0x87, 0x0, 0x68, 0x69, 0x2B, 0xAA, 0xC0, 0x67, 0x24, 0x20, 0x67,
  61. 0x28, 0x89, 0x0, 0x68, 0x69, 0x2B, 0xAA, 0xC0, 0x65, 0x22, 0x20, 0x65, 0x26, 0xA7, 0x28, 0x20,
  62. 0x69, 0x2B, 0xAA, 0x29, 0x20, 0x68, 0x29, 0xAA, 0x2B, 0x20, 0x69, 0x28, 0x69, 0x67,
  63. };
  64. const uint8_t leadseq[] = { 0, 1, 0, 2, 0, 1, 0, 3, 4, 5, 6 };
  65. #define LEADSIZE 174
  66. struct leadvoice_s {
  67. uint8_t ptr, timer;
  68. uint16_t osc;
  69. } leads[3] = {
  70. { LEADSIZE, 0, 0 },
  71. { LEADSIZE, 0, 1601 },
  72. { LEADSIZE, 0, 3571 },
  73. };
  74. uint8_t boosts;
  75. static unsigned char voice_lead(unsigned long i, int voice_nr)
  76. {
  77. #define leadptr leads[voice_nr].ptr
  78. #define lead_osc leads[voice_nr].osc
  79. #define leadtimer leads[voice_nr].timer
  80. if (leadptr == LEADSIZE)
  81. {
  82. if (i == (0x40000 + 0x400 * voice_nr))
  83. {
  84. leadptr = -1;
  85. leadtimer = 1;
  86. }
  87. else
  88. return 0;
  89. }
  90. if (0 == (i & 0x0FF))
  91. boosts &= ~(1 << voice_nr);
  92. if (0 == (i & 0x1FF))
  93. leadtimer--;
  94. if (0 == leadtimer)
  95. {
  96. leadptr++;
  97. leadtimer = leadtimes[leaddata[(leadseq[leadptr >> 4] << 4) | (leadptr & 0xF)] >> 5];
  98. boosts |= 1 << voice_nr;
  99. }
  100. uint8_t melody = leadnotes[leaddata[(leadseq[leadptr >> 4] << 4) | (leadptr & 0xF)] & 0x1F];
  101. lead_osc += notes[melody];
  102. uint8_t sample = ((lead_osc >> 6) & 0x7F) + ((lead_osc >> 6) & 0x3F);
  103. return (melody == 0xFF) ? 0 : ((boosts & (1 << voice_nr)) ? sample : THREEQUARTERS(sample));
  104. }
  105. static inline unsigned char voice_arp(unsigned long i)
  106. {
  107. static uint16_t arp_osc = 0;
  108. uint8_t arpptr = i >> 13;
  109. uint8_t arpptr2 = arpseq1[arpseq2[arpptr >> 3]][(arpptr >> 1) & 3];
  110. if (!(arpptr & 1))
  111. arpptr2 >>= 4;
  112. arpptr = arpeggio[arpptr2 & 0xF][(i >> 8) & 1];
  113. if (!(i & 0x80))
  114. arpptr >>= 4;
  115. int note = notes[arpnotes[arpptr & 0xF]];
  116. arp_osc += note;
  117. return ((arptiming & (1 << (31 - (i >> 9)))) && (arp_osc & (1 << 12)) && ((i >> 13) > 15)) ? 0 : 140;
  118. }
  119. static inline unsigned char voice_bass(unsigned long i)
  120. {
  121. static uint16_t bassosc = 0, flangeosc = 0;
  122. uint8_t bassptr = (i >> 13) & 0xF;
  123. if (i >> 19)
  124. bassptr |= 0x10;
  125. int note = notes[bassline[bassptr]];
  126. if (bassbeat[(i >> 10) & 7])
  127. note <<= 1;
  128. bassosc += note;
  129. flangeosc += note + 1;
  130. unsigned char ret = ((bassosc >> 8) & 0x7F) + ((flangeosc >> 8) & 0x7F);
  131. return ((i >> 6) & 0xF) == 0xF ? 0 : ret;
  132. }
  133. static inline uint8_t next_sample()
  134. {
  135. static unsigned long i = 0;//x40000;
  136. uint8_t ret = (voice_lead(i, 0) >> 1) + THREEQUARTERS(voice_lead(i, 1) >> 2) + (voice_lead(i, 2) >> 3) + (voice_bass(i) >> 2) + (voice_arp(i) >> 2);
  137. i++;
  138. if ((i >> 13) == ARPSIZE)
  139. i = 16 << 13;
  140. return ret;
  141. }
  142. void fill(uint8_t *data)
  143. {
  144. static uint8_t max = 0;
  145. for (int j = 0; j < 4096; j++)
  146. {
  147. data[j] = next_sample();
  148. if (data[j] > max)
  149. {
  150. max = data[j];
  151. printf("%x\n", max);
  152. }
  153. }
  154. }
  155. static const WAVEFORMATEX fmt = {
  156. WAVE_FORMAT_PCM, 1, 8000, 8000, 1, 8, 0
  157. };
  158. int main(int argc, char *argv[])
  159. {
  160. HWAVEOUT out;
  161. HRESULT rc = waveOutOpen(&out, WAVE_MAPPER, &fmt, NULL, NULL, CALLBACK_NULL);
  162. if (rc != MMSYSERR_NOERROR)
  163. {
  164. printf("error %d on open\n");
  165. exit(1);
  166. }
  167. WAVEHDR bufs[2] = {
  168. { (char *)malloc(4096), 4096 },
  169. { (char *)malloc(4096), 4096 },
  170. };
  171. int i = 0;
  172. fill((uint8_t *)bufs[i].lpData);
  173. bufs[i].dwFlags = WHDR_PREPARED;
  174. waveOutPrepareHeader(out, bufs + i, sizeof(WAVEHDR));
  175. waveOutWrite(out, bufs + i, sizeof(WAVEHDR));
  176. i ^= 1;
  177. while (!(GetAsyncKeyState(VK_ESCAPE) & 1))
  178. {
  179. fill((uint8_t *)bufs[i].lpData);
  180. bufs[i].dwFlags = WHDR_PREPARED;
  181. waveOutPrepareHeader(out, bufs + i, sizeof(WAVEHDR));
  182. waveOutWrite(out, bufs + i, sizeof(WAVEHDR));
  183. i ^= 1;
  184. while (waveOutUnprepareHeader(out, bufs + i, sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);
  185. }
  186. }