Implement some weird wavefolding

This commit is contained in:
Jan-Henrik 2020-03-15 02:03:23 +01:00
parent d7294bbc12
commit 0a1729b7cd
6 changed files with 201 additions and 42 deletions

View file

@ -48,16 +48,18 @@ const size_t kNumPots = 4;
uint16_t pot_values[kNumPots];
Mode mode;
bool mode_changed = false;
void Process(IOBuffer::Block* block, size_t size)
{
for (size_t i = 0; i < kNumPots; i++) {
uint16_t read_value = AnalogInputs::Read(kNumPots - 1 - i) << 6;
if (read_value != pot_values[i]) { // TODO: apply filtering
if (read_value != pot_values[i] || mode_changed) { // TODO: apply filtering
pot_values[i] = read_value;
envelope.Configure(pot_values, CONTROL_MODE_FULL);
sequencer.Configure(pot_values, CONTROL_MODE_FULL);
tides.Configure(pot_values, CONTROL_MODE_FULL);
mode_changed = false;
}
}
@ -128,6 +130,7 @@ TIMER_0_TICK
{
TickSystemClock();
button_input.Debounce();
Mode prev_mode = mode;
uint8_t mode_value = switchIn1.Read() << 1 | switchIn0.Read();
switch (mode_value) {
case 0:
@ -140,6 +143,8 @@ TIMER_0_TICK
mode = MODE_ADSR;
break;
}
if (prev_mode != mode)
mode_changed = true;
}
TIMER_2_TICK
@ -150,8 +155,7 @@ TIMER_2_TICK
if (!clock_divider_midi) {
uint32_t gate_inputs = gate_input.Read();
if (mode == MODE_ADSR || mode == MODE_SEQ)
gate_inputs |= button_input.pressed();
gate_inputs |= button_input.pressed();
IOBuffer::Slice slice = io_buffer.NextSlice(1);
for (size_t i = 0; i < kNumChannels; ++i) {

View file

@ -171,6 +171,137 @@ const prog_uint16_t lut_res_raised_cosine[] PROGMEM = {
65377, 65414, 65446, 65473, 65495, 65512, 65525, 65532,
65532,
};
const prog_uint16_t lut_res_unipolar_fold[] PROGMEM = {
0, 352, 703, 1054, 1404, 1754, 2102, 2450,
2796, 3141, 3483, 3824, 4163, 4500, 4834, 5165,
5494, 5819, 6141, 6460, 6776, 7088, 7395, 7699,
7999, 8294, 8585, 8871, 9152, 9429, 9700, 9966,
10227, 10482, 10731, 10975, 11213, 11445, 11671, 11891,
12104, 12311, 12512, 12706, 12893, 13073, 13247, 13414,
13574, 13727, 13873, 14012, 14144, 14268, 14386, 14496,
14598, 14694, 14782, 14863, 14937, 15003, 15062, 15113,
15158, 15195, 15225, 15247, 15263, 15271, 15272, 15266,
15253, 15234, 15207, 15174, 15133, 15087, 15033, 14973,
14907, 14834, 14756, 14671, 14580, 14483, 14381, 14273,
14159, 14040, 13915, 13786, 13651, 13512, 13368, 13219,
13066, 12909, 12747, 12582, 12412, 12239, 12063, 11883,
11700, 11513, 11324, 11133, 10939, 10742, 10544, 10343,
10141, 9936, 9731, 9524, 9316, 9107, 8898, 8688,
8477, 8266, 8055, 7845, 7634, 7424, 7215, 7007,
6799, 6593, 6388, 6184, 5983, 5783, 5585, 5389,
5196, 5005, 4816, 4631, 4448, 4269, 4092, 3919,
3750, 3584, 3422, 3264, 3110, 2960, 2814, 2673,
2536, 2403, 2276, 2153, 2035, 1922, 1815, 1712,
1615, 1523, 1436, 1355, 1280, 1210, 1146, 1087,
1035, 988, 947, 912, 883, 860, 843, 832,
827, 828, 835, 848, 867, 893, 924, 962,
1005, 1055, 1110, 1172, 1239, 1312, 1391, 1476,
1567, 1663, 1766, 1873, 1987, 2105, 2229, 2359,
2494, 2634, 2779, 2929, 3084, 3244, 3408, 3578,
3751, 3930, 4112, 4299, 4490, 4686, 4885, 5087,
5294, 5504, 5718, 5935, 6155, 6378, 6604, 6834,
7065, 7300, 7537, 7776, 8017, 8261, 8506, 8753,
9002, 9253, 9504, 9757, 10012, 10267, 10523, 10780,
11037, 11295, 11553, 11812, 12070, 12329, 12587, 12845,
13103, 13360, 13616, 13871, 14126, 14380, 14632, 14883,
15133, 15381, 15627, 15872, 16115, 16356, 16595, 16832,
17067, 17299, 17529, 17756, 17980, 18202, 18422, 18638,
18851, 19061, 19268, 19472, 19673, 19871, 20065, 20255,
20442, 20626, 20806, 20982, 21154, 21323, 21488, 21649,
21807, 21960, 22110, 22255, 22397, 22534, 22668, 22797,
22923, 23044, 23162, 23275, 23384, 23489, 23590, 23687,
23780, 23869, 23954, 24035, 24112, 24185, 24254, 24320,
24381, 24438, 24492, 24542, 24588, 24631, 24669, 24705,
24736, 24764, 24789, 24810, 24828, 24843, 24854, 24862,
24867, 24869, 24868, 24865, 24858, 24848, 24836, 24821,
24803, 24783, 24761, 24736, 24709, 24680, 24648, 24614,
24579, 24541, 24502, 24461, 24418, 24374, 24328, 24281,
24232, 24183, 24131, 24079, 24026, 23972, 23917, 23861,
23804, 23747, 23689, 23631, 23572, 23513, 23454, 23394,
23335, 23275, 23216, 23156, 23097, 23038, 22979, 22920,
22862, 22805, 22748, 22692, 22636, 22581, 22527, 22474,
22422, 22370, 22320, 22271, 22223, 22176, 22130, 22086,
22043, 22001, 21961, 21922, 21884, 21849, 21814, 21782,
21750, 21721, 21693, 21667, 21642, 21620, 21599, 21580,
21562, 21547, 21533, 21521, 21511, 21503, 21497, 21493,
21490, 21490, 21491, 21495, 21500, 21507, 21516, 21527,
21539, 21554, 21571, 21589, 21609, 21631, 21655, 21681,
21708, 21738, 21769, 21801, 21836, 21872, 21910, 21949,
21990, 22033, 22077, 22123, 22171, 22219, 22270, 22321,
22375, 22429, 22485, 22542, 22600, 22660, 22721, 22782,
22845, 22910, 22975, 23041, 23108, 23176, 23245, 23315,
23386, 23457, 23529, 23602, 23676, 23750, 23824, 23900,
23975, 24052, 24128, 24205, 24283, 24361, 24438, 24517,
24595, 24674, 24752, 24831, 24910, 24989, 25068, 25146,
25225, 25304, 25382, 25460, 25538, 25616, 25693, 25770,
25847, 25923, 25999, 26075, 26150, 26224, 26298, 26372,
26445, 26517, 26589, 26660, 26730, 26800, 26869, 26937,
27004, 27071, 27137, 27202, 27266, 27330, 27392, 27454,
27515, 27575, 27634, 27692, 27749, 27805, 27861, 27915,
27968, 28021, 28072, 28123, 28172, 28221, 28268, 28315,
28360, 28404, 28448, 28490, 28532, 28572, 28612, 28650,
28688, 28725, 28760, 28795, 28828, 28861, 28893, 28923,
28953, 28982, 29010, 29037, 29063, 29089, 29113, 29136,
29159, 29181, 29202, 29222, 29241, 29260, 29277, 29294,
29310, 29326, 29341, 29355, 29368, 29380, 29392, 29404,
29414, 29424, 29434, 29443, 29451, 29459, 29466, 29472,
29478, 29484, 29489, 29494, 29498, 29502, 29505, 29508,
29511, 29513, 29515, 29517, 29518, 29519, 29520, 29521,
29521, 29521, 29520, 29520, 29519, 29519, 29518, 29517,
29515, 29514, 29513, 29511, 29510, 29508, 29506, 29505,
29503, 29501, 29499, 29498, 29496, 29494, 29493, 29491,
29490, 29488, 29487, 29486, 29484, 29483, 29483, 29482,
29481, 29481, 29481, 29480, 29480, 29481, 29481, 29482,
29482, 29483, 29485, 29486, 29488, 29490, 29492, 29494,
29496, 29499, 29502, 29505, 29509, 29513, 29517, 29521,
29525, 29530, 29535, 29540, 29546, 29551, 29557, 29564,
29570, 29577, 29584, 29591, 29598, 29606, 29614, 29622,
29631, 29639, 29648, 29657, 29667, 29676, 29686, 29696,
29706, 29717, 29728, 29738, 29749, 29761, 29772, 29784,
29796, 29808, 29820, 29832, 29845, 29857, 29870, 29883,
29896, 29910, 29923, 29937, 29950, 29964, 29978, 29992,
30006, 30021, 30035, 30050, 30064, 30079, 30093, 30108,
30123, 30138, 30153, 30168, 30183, 30198, 30213, 30229,
30244, 30259, 30274, 30290, 30305, 30320, 30336, 30351,
30366, 30382, 30397, 30412, 30428, 30443, 30458, 30473,
30488, 30504, 30519, 30534, 30549, 30564, 30579, 30593,
30608, 30623, 30637, 30652, 30667, 30681, 30695, 30710,
30724, 30738, 30752, 30766, 30780, 30794, 30807, 30821,
30834, 30848, 30861, 30874, 30887, 30900, 30913, 30926,
30939, 30951, 30964, 30976, 30988, 31001, 31013, 31025,
31036, 31048, 31060, 31071, 31083, 31094, 31105, 31117,
31128, 31138, 31149, 31160, 31171, 31181, 31191, 31202,
31212, 31222, 31232, 31242, 31252, 31262, 31271, 31281,
31290, 31299, 31309, 31318, 31327, 31336, 31345, 31354,
31362, 31371, 31380, 31388, 31397, 31405, 31413, 31422,
31430, 31438, 31446, 31454, 31462, 31469, 31477, 31485,
31492, 31500, 31508, 31515, 31522, 31530, 31537, 31544,
31552, 31559, 31566, 31573, 31580, 31587, 31594, 31601,
31608, 31615, 31621, 31628, 31635, 31642, 31648, 31655,
31662, 31668, 31675, 31682, 31688, 31695, 31701, 31708,
31714, 31721, 31727, 31733, 31740, 31746, 31753, 31759,
31765, 31772, 31778, 31785, 31791, 31797, 31804, 31810,
31816, 31823, 31829, 31835, 31842, 31848, 31854, 31861,
31867, 31873, 31880, 31886, 31892, 31898, 31905, 31911,
31917, 31924, 31930, 31937, 31943, 31949, 31956, 31962,
31968, 31975, 31981, 31987, 31994, 32000, 32007, 32013,
32019, 32026, 32032, 32039, 32045, 32052, 32058, 32065,
32071, 32077, 32084, 32090, 32097, 32103, 32110, 32116,
32123, 32129, 32136, 32142, 32149, 32155, 32162, 32168,
32175, 32181, 32188, 32194, 32201, 32207, 32214, 32220,
32227, 32233, 32240, 32246, 32253, 32259, 32266, 32272,
32279, 32285, 32292, 32298, 32305, 32311, 32318, 32324,
32331, 32337, 32344, 32350, 32357, 32363, 32370, 32376,
32382, 32389, 32395, 32402, 32408, 32414, 32421, 32427,
32434, 32440, 32446, 32453, 32459, 32465, 32472, 32478,
32484, 32490, 32497, 32503, 32509, 32516, 32522, 32528,
32534, 32540, 32547, 32553, 32559, 32565, 32571, 32577,
32583, 32590, 32596, 32602, 32608, 32614, 32620, 32626,
32632, 32638, 32644, 32650, 32656, 32662, 32668, 32674,
32680, 32686, 32692, 32697, 32703, 32709, 32715, 32721,
32727, 32732, 32738, 32744, 32750, 32756, 32761, 32767,
32767,
};
const prog_uint16_t* const lookup_table_table[] = {
@ -178,6 +309,7 @@ const prog_uint16_t* const lookup_table_table[] = {
lut_res_env_expo,
lut_res_env_quartic,
lut_res_raised_cosine,
lut_res_unipolar_fold,
};
const prog_uint32_t lut_res_env_increments[] PROGMEM = {
@ -216,39 +348,39 @@ const prog_uint32_t lut_res_env_increments[] PROGMEM = {
219043,
};
const prog_uint32_t lut_res_env_increments_slow[] PROGMEM = {
35046933, 33184343, 31436989, 29796754, 28256163, 26808323, 25446875, 24165949,
22960121, 21824376, 20754076, 19744924, 18792940, 17894435, 17045985, 16244414,
15486770, 14770309, 14092480, 13450909, 12843386, 12267850, 11722382, 11205191,
10714605, 10249065, 9807112, 9387383, 8988604, 8609583, 8249203, 7906418,
7580249, 7269776, 6974139, 6692528, 6424184, 6168395, 5924489, 5691837,
5469847, 5257961, 5055654, 4862432, 4677829, 4501406, 4332750, 4171471,
4017200, 3869589, 3728311, 3593055, 3463528, 3339453, 3220568, 3106625,
2997389, 2892639, 2792164, 2695765, 2603253, 2514449, 2429184, 2347295,
2268632, 2193047, 2120405, 2050573, 1983428, 1918852, 1856732, 1796962,
1739439, 1684069, 1630758, 1579418, 1529968, 1482327, 1436419, 1392173,
1349520, 1308394, 1268733, 1230477, 1193569, 1157955, 1123584, 1090404,
1058371, 1027437, 997560, 968699, 940814, 913868, 887824, 862647,
838306, 814767, 792001, 769979, 748673, 728057, 708104, 688790,
670093, 651989, 634458, 617477, 601028, 585092, 569649, 554684,
540178, 526116, 512482, 499262, 486440, 474003, 461939, 450233,
438875, 427852, 417153, 406766, 396683, 386891, 377383, 368148,
359177, 350462, 341994, 333766, 325770, 317997, 310442, 303096,
295954, 289009, 282255, 275685, 269295, 263077, 257028, 251142,
245413, 239837, 234409, 229126, 223981, 218972, 214094, 209343,
204715, 200207, 195816, 191537, 187368, 183304, 179344, 175484,
171722, 168054, 164477, 160990, 157589, 154272, 151037, 147882,
144803, 141800, 138869, 136010, 133219, 130496, 127837, 125242,
122709, 120236, 117821, 115462, 113160, 110911, 108714, 106568,
104472, 102424, 100423, 98468, 96557, 94690, 92865, 91081,
89338, 87633, 85967, 84338, 82744, 81187, 79663, 78173,
76716, 75290, 73895, 72531, 71196, 69890, 68612, 67362,
66138, 64940, 63767, 62620, 61496, 60396, 59320, 58265,
57233, 56222, 55232, 54262, 53313, 52382, 51471, 50579,
49704, 48847, 48008, 47185, 46379, 45589, 44815, 44056,
43313, 42584, 41869, 41168, 40481, 39808, 39148, 38500,
37865, 37243, 36632, 36033, 35446, 34869, 34304, 33750,
33206, 32672, 32148, 31634, 31130, 30635, 30150, 29673,
29205,
876173, 855969, 836311, 817181, 798563, 780441, 762801, 745627,
728906, 712624, 696767, 681324, 666281, 651626, 637349, 623439,
609883, 596673, 583797, 571247, 559013, 547085, 535455, 524115,
513056, 502270, 491749, 481487, 471475, 461707, 452176, 442875,
433798, 424939, 416292, 407850, 399610, 391564, 383708, 376036,
368544, 361227, 354080, 347098, 340278, 333615, 327104, 320742,
314525, 308449, 302511, 296706, 291032, 285485, 280062, 274760,
269575, 264504, 259546, 254696, 249952, 245312, 240773, 236332,
231987, 227735, 223575, 219504, 215519, 211620, 207803, 204066,
200409, 196828, 193322, 189889, 186528, 183236, 180012, 176855,
173763, 170734, 167767, 164860, 162012, 159222, 156488, 153809,
151184, 148612, 146090, 143619, 141197, 138822, 136495, 134213,
131976, 129782, 127631, 125522, 123454, 121426, 119437, 117486,
115572, 113695, 111853, 110047, 108274, 106535, 104829, 103155,
101512, 99900, 98318, 96765, 95241, 93745, 92277, 90835,
89420, 88031, 86667, 85328, 84013, 82722, 81454, 80209,
78986, 77785, 76605, 75446, 74308, 73190, 72092, 71013,
69953, 68911, 67888, 66883, 65895, 64924, 63969, 63032,
62110, 61204, 60314, 59439, 58578, 57733, 56901, 56084,
55280, 54490, 53713, 52949, 52198, 51460, 50733, 50019,
49316, 48625, 47945, 47277, 46619, 45972, 45336, 44710,
44094, 43488, 42892, 42305, 41728, 41160, 40601, 40051,
39510, 38977, 38453, 37937, 37429, 36929, 36437, 35952,
35475, 35006, 34544, 34089, 33641, 33200, 32765, 32338,
31916, 31502, 31093, 30691, 30295, 29905, 29521, 29143,
28770, 28403, 28041, 27685, 27334, 26988, 26648, 26312,
25981, 25656, 25335, 25019, 24707, 24400, 24097, 23799,
23505, 23216, 22930, 22649, 22372, 22098, 21829, 21564,
21302, 21044, 20789, 20539, 20292, 20048, 19808, 19571,
19337, 19107, 18880, 18656, 18435, 18217, 18003, 17791,
17582, 17376, 17173, 16972, 16775, 16580, 16388, 16198,
16011, 15826, 15644, 15465, 15288, 15113, 14940, 14770,
14602,
};

View file

@ -43,6 +43,7 @@ extern const prog_uint16_t lut_res_env_linear[] PROGMEM;
extern const prog_uint16_t lut_res_env_expo[] PROGMEM;
extern const prog_uint16_t lut_res_env_quartic[] PROGMEM;
extern const prog_uint16_t lut_res_raised_cosine[] PROGMEM;
extern const prog_uint16_t lut_res_unipolar_fold[] PROGMEM;
extern const prog_uint32_t lut_res_env_increments[] PROGMEM;
extern const prog_uint32_t lut_res_env_increments_slow[] PROGMEM;
#define STR_RES_DUMMY 0 // dummy
@ -54,6 +55,8 @@ extern const prog_uint32_t lut_res_env_increments_slow[] PROGMEM;
#define LUT_RES_ENV_QUARTIC_SIZE 257
#define LUT_RES_RAISED_COSINE 3
#define LUT_RES_RAISED_COSINE_SIZE 257
#define LUT_RES_UNIPOLAR_FOLD 4
#define LUT_RES_UNIPOLAR_FOLD_SIZE 1025
#define LUT_RES_ENV_INCREMENTS 0
#define LUT_RES_ENV_INCREMENTS_SIZE 257
#define LUT_RES_ENV_INCREMENTS_SLOW 1

View file

@ -32,6 +32,8 @@ import numpy
SAMPLE_RATE = 20000000.0 / 510 / 16#/ 4 # hz
WAVESHAPER_SIZE = 1024
"""----------------------------------------------------------------------------
LFO and envelope increments.
----------------------------------------------------------------------------"""
@ -61,9 +63,9 @@ lookup_tables_32.append(
('env_increments', values)
)
max_time = 60.0 # seconds
min_time = 0.05
# Tides envelopes
max_time = 60.0 * 2 # seconds
min_time = 2
gamma = 0.175
min_increment = excursion / (max_time * sample_rate)
max_increment = excursion / (min_time * sample_rate)
@ -90,3 +92,11 @@ lookup_tables.append(
raised_cosine = 0.5 - numpy.cos(env_linear * numpy.pi) / 2
lookup_tables.append(('raised_cosine', raised_cosine * 65535.0))
x = numpy.arange(0, WAVESHAPER_SIZE + 1) / float(WAVESHAPER_SIZE)
x[-1] = x[-2]
sine = numpy.sin(8 * numpy.pi * x)
window = numpy.exp(-x * x * 4) ** 2
unipolar_fold = (0.5 * sine + 2 * x) * window + numpy.arctan(4 * x) * (1 - window)
unipolar_fold /= numpy.abs(unipolar_fold).max()
lookup_tables.append(('unipolar_fold', numpy.round(32767 * unipolar_fold)))

View file

@ -81,6 +81,16 @@ void Tides::Process(
uint16_t t = x + ((y - x) * static_cast<uint32_t>((phase_ >> 8) & 0xffff) >> 16);
value_ = a + ((b - a) * (t >> 1) >> 15);
int32_t wf_balance = smoothness_ >> 1;
int32_t wf_gain = 2048 + \
(static_cast<int32_t>(smoothness_ >> 1) * (65535 - 2048) >> 15);
int32_t original = value_;
int32_t m = pgm_read_word(lut_res_unipolar_fold + ((original * wf_gain + (1U << 31)) >> 22));
int32_t n = pgm_read_word(lut_res_unipolar_fold + ((original * wf_gain + (1U << 31)) >> 22) + 1);
int32_t folded = m + ((n - m) * static_cast<int32_t>(((original * wf_gain) >> 6) & 0xffff) >> 16);
value_ = original + ((folded - original) * wf_balance >> 15);
value_ = value_ << 1;
phase_ += phase_increment_;
*out++ = value_;
}

View file

@ -47,8 +47,8 @@ class Tides {
void Configure(uint16_t* parameter, ControlMode control_mode)
{
set_ar_loop(parameter[0], parameter[1]);
set_ad_loop(parameter[0], parameter[1]);
smoothness_ = parameter[3];
uint8_t waveform_mode = quant.Process(parameter[2] / 65536.0f, 5);
switch (waveform_mode) {
@ -338,7 +338,7 @@ class Tides {
uint16_t sustain_point_;
uint16_t loop_start_;
uint16_t loop_end_;
uint16_t smoothness_;
bool hard_reset_;
HysteresisQuantizer quant;
DISALLOW_COPY_AND_ASSIGN(Tides);