You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

448 lines
13KB

  1. // Copyright 2015 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. #include <stm32f4xx_conf.h>
  25. #include "marbles/drivers/clock_inputs.h"
  26. #include "marbles/drivers/dac.h"
  27. #include "marbles/drivers/debug_pin.h"
  28. #include "marbles/drivers/debug_port.h"
  29. #include "marbles/drivers/gate_outputs.h"
  30. #include "marbles/drivers/rng.h"
  31. #include "marbles/drivers/system.h"
  32. #include "marbles/ramp/ramp_extractor.h"
  33. #include "marbles/random/random_generator.h"
  34. #include "marbles/random/random_stream.h"
  35. #include "marbles/random/t_generator.h"
  36. #include "marbles/random/x_y_generator.h"
  37. #include "marbles/clock_self_patching_detector.h"
  38. #include "marbles/cv_reader.h"
  39. #include "marbles/io_buffer.h"
  40. #include "marbles/note_filter.h"
  41. #include "marbles/resources.h"
  42. #include "marbles/scale_recorder.h"
  43. #include "marbles/settings.h"
  44. #include "marbles/ui.h"
  45. #include "stmlib/dsp/dsp.h"
  46. #include "stmlib/dsp/hysteresis_quantizer.h"
  47. #include "stmlib/dsp/units.h"
  48. #define PROFILE_INTERRUPT 0
  49. #define PROFILE_RENDER 0
  50. using namespace marbles;
  51. using namespace std;
  52. using namespace stmlib;
  53. const bool test_adc_noise = false;
  54. const int kSampleRate = 32000;
  55. const int kGateDelay = 2;
  56. ClockInputs clock_inputs;
  57. ClockSelfPatchingDetector self_patching_detector[kNumGateOutputs];
  58. CvReader cv_reader;
  59. Dac dac;
  60. DebugPort debug_port;
  61. GateOutputs gate_outputs;
  62. HysteresisQuantizer deja_vu_length_quantizer;
  63. IOBuffer io_buffer;
  64. NoteFilter note_filter;
  65. Rng rng;
  66. ScaleRecorder scale_recorder;
  67. Settings settings;
  68. Ui ui;
  69. RandomGenerator random_generator;
  70. RandomStream random_stream;
  71. TGenerator t_generator;
  72. XYGenerator xy_generator;
  73. // Default interrupt handlers.
  74. extern "C" {
  75. int __errno;
  76. void NMI_Handler() { }
  77. void HardFault_Handler() { while (1); }
  78. void MemManage_Handler() { while (1); }
  79. void BusFault_Handler() { while (1); }
  80. void UsageFault_Handler() { while (1); }
  81. void SVC_Handler() { }
  82. void DebugMon_Handler() { }
  83. void PendSV_Handler() { }
  84. void SysTick_Handler() {
  85. IWDG_ReloadCounter();
  86. ui.Poll();
  87. if (settings.freshly_baked()) {
  88. if (debug_port.readable()) {
  89. uint8_t command = debug_port.Read();
  90. uint8_t response = ui.HandleFactoryTestingRequest(command);
  91. debug_port.Write(response);
  92. }
  93. }
  94. }
  95. }
  96. IOBuffer::Slice FillBuffer(size_t size) {
  97. if (PROFILE_INTERRUPT) {
  98. TIC;
  99. }
  100. IOBuffer::Slice s = io_buffer.NextSlice(size);
  101. gate_outputs.Write(s);
  102. clock_inputs.Read(s, size);
  103. if (io_buffer.new_block()) {
  104. cv_reader.Copy(&s.block->adc_value[0]);
  105. clock_inputs.ReadNormalization(s.block);
  106. }
  107. if (rng.readable()) {
  108. random_stream.Write(rng.data());
  109. }
  110. if (PROFILE_INTERRUPT) {
  111. TOC;
  112. }
  113. return s;
  114. }
  115. inline uint16_t DacCode(int index, float voltage) {
  116. CONSTRAIN(voltage, -5.0f, 5.0f);
  117. const float scale = settings.calibration_data().dac_scale[index];
  118. const float offset = settings.calibration_data().dac_offset[index];
  119. return ClipU16(static_cast<int32_t>(voltage * scale + offset));
  120. }
  121. void ProcessTest(IOBuffer::Block* block, size_t size) {
  122. float parameters[kNumParameters];
  123. static float phase;
  124. cv_reader.Process(&block->adc_value[0], parameters);
  125. for (size_t i = 0; i < size; ++i) {
  126. phase += 100.0f / static_cast<float>(kSampleRate);
  127. if (phase >= 1.0f) {
  128. phase -= 1.0f;
  129. }
  130. block->cv_output[0][i] = DacCode(
  131. 0, 4.0 * Interpolate(lut_sine, phase, 256.0f));
  132. block->cv_output[1][i] = DacCode(
  133. 1, -8.0f * phase + 4.0f);
  134. block->cv_output[2][i] = DacCode(
  135. 2, (phase < 0.5f ? phase : 1.0f - phase) * 16.0f - 4.0f);
  136. block->cv_output[3][i] = DacCode(
  137. 3, phase < 0.5f ? -4.0f : 4.0f);
  138. for (int j = 0; j < 4; ++j) {
  139. uint16_t dac_code = ui.output_test_forced_dac_code(j);
  140. if (dac_code) {
  141. block->cv_output[j][i] = dac_code;
  142. }
  143. }
  144. block->gate_output[0][i] = block->input_patched[0]
  145. ? block->input[0][i]
  146. : phase < 0.2f;
  147. block->gate_output[1][i] = phase < 0.5f;
  148. block->gate_output[2][i] = block->input_patched[1]
  149. ? block->input[1][i]
  150. : phase < 0.8f;
  151. }
  152. }
  153. Ratio y_divider_ratios[] = {
  154. { 1, 64 },
  155. { 1, 48 },
  156. { 1, 32 },
  157. { 1, 24 },
  158. { 1, 16 },
  159. { 1, 12 },
  160. { 1, 8 },
  161. { 1, 6 },
  162. { 1, 4 },
  163. { 1, 3 },
  164. { 1, 2 },
  165. { 1, 1 },
  166. };
  167. int loop_length[] = {
  168. 1,
  169. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  170. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  171. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  172. 5, 5, 5, 5,
  173. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  174. 7, 7,
  175. 8, 8, 8, 8, 8, 8, 8, 8, 8,
  176. 10, 10, 10,
  177. 12, 12, 12, 12, 12, 12, 12,
  178. 14,
  179. 16
  180. };
  181. float parameters[kNumParameters];
  182. float ramp_buffer[kBlockSize * 4];
  183. bool gates[kBlockSize * 2];
  184. float voltages[kBlockSize * 4];
  185. Ramps ramps;
  186. GroupSettings x, y;
  187. bool gate_delay_tail[kNumGateOutputs][kGateDelay];
  188. float SineOscillator(float voltage) {
  189. static float phase = 0.0f;
  190. CONSTRAIN(voltage, -5.0f, 5.0f);
  191. float frequency = stmlib::SemitonesToRatio(voltage * 12.0f) * 220.0f / kSampleRate;
  192. phase += frequency;
  193. if (phase >= 1.0f) {
  194. phase -= 1.0f;
  195. }
  196. return 5.0f * Interpolate(lut_sine, phase, 256.0f);
  197. }
  198. void Process(IOBuffer::Block* block, size_t size) {
  199. if (PROFILE_RENDER) {
  200. TIC;
  201. }
  202. // Filter CV values (3.5%)
  203. cv_reader.Process(&block->adc_value[0], parameters);
  204. float deja_vu = parameters[ADC_CHANNEL_DEJA_VU_AMOUNT];
  205. // Deadband near 12 o'clock for the deja vu parameter.
  206. const float d = fabsf(deja_vu - 0.5f);
  207. if (d > 0.03f) {
  208. ui.set_deja_vu_lock(false);
  209. } else if (d < 0.02f) {
  210. ui.set_deja_vu_lock(true);
  211. }
  212. if (deja_vu < 0.47f) {
  213. deja_vu *= 1.06382978723f;
  214. } else if (deja_vu > 0.53f) {
  215. deja_vu = 0.5f + (deja_vu - 0.53f) * 1.06382978723f;
  216. } else {
  217. deja_vu = 0.5f;
  218. }
  219. GateFlags* t_clock = block->input[0];
  220. GateFlags* xy_clock = block->input[1];
  221. // Determine the clock source for the XY section (2%)
  222. ClockSource xy_clock_source = CLOCK_SOURCE_INTERNAL_T1_T2_T3;
  223. if (block->input_patched[1]) {
  224. xy_clock_source = CLOCK_SOURCE_EXTERNAL;
  225. size_t best_score = 8;
  226. for (size_t i = 0; i < kNumGateOutputs; ++i) {
  227. size_t score = self_patching_detector[i].Process(block, size);
  228. if (score >= best_score) {
  229. xy_clock_source = ClockSource(CLOCK_SOURCE_INTERNAL_T1 + i);
  230. best_score = score;
  231. }
  232. }
  233. }
  234. // Generate gates for T-section (16%).
  235. ramps.master = &ramp_buffer[0];
  236. ramps.external = &ramp_buffer[kBlockSize];
  237. ramps.slave[0] = &ramp_buffer[kBlockSize * 2];
  238. ramps.slave[1] = &ramp_buffer[kBlockSize * 3];
  239. const State& state = settings.state();
  240. int deja_vu_length = deja_vu_length_quantizer.Lookup(
  241. loop_length,
  242. parameters[ADC_CHANNEL_DEJA_VU_LENGTH],
  243. sizeof(loop_length) / sizeof(int));
  244. t_generator.set_model(TGeneratorModel(state.t_model));
  245. t_generator.set_range(TGeneratorRange(state.t_range));
  246. t_generator.set_rate(parameters[ADC_CHANNEL_T_RATE]);
  247. t_generator.set_bias(parameters[ADC_CHANNEL_T_BIAS]);
  248. t_generator.set_jitter(parameters[ADC_CHANNEL_T_JITTER]);
  249. t_generator.set_deja_vu(state.t_deja_vu ? deja_vu : 0.0f);
  250. t_generator.set_length(deja_vu_length);
  251. t_generator.set_pulse_width_mean(float(state.t_pulse_width_mean) / 256.0f);
  252. t_generator.set_pulse_width_std(float(state.t_pulse_width_std) / 256.0f);
  253. t_generator.Process(
  254. block->input_patched[0],
  255. t_clock,
  256. ramps,
  257. gates,
  258. size);
  259. // Generate voltages for X-section (40%).
  260. float note_cv_1 = cv_reader.channel(ADC_CHANNEL_X_SPREAD).scaled_raw_cv();
  261. float note_cv_2 = cv_reader.channel(ADC_CHANNEL_X_SPREAD_2).scaled_raw_cv();
  262. float note_cv = 0.5f * (note_cv_1 + note_cv_2);
  263. float u = note_filter.Process(0.5f * (note_cv + 1.0f));
  264. if (test_adc_noise) {
  265. static float note_lp = 0.0f;
  266. float note = note_cv_1;
  267. ONE_POLE(note_lp, note, 0.0001f);
  268. float cents = (note - note_lp) * 1200.0f * 5.0f;
  269. fill(&voltages[0], &voltages[4 * size], cents);
  270. } else if (ui.recording_scale()) {
  271. float voltage = (u - 0.5f) * 10.0f;
  272. for (size_t i = 0; i < size; ++i) {
  273. GateFlags gate = block->input_patched[1]
  274. ? block->input[1][i]
  275. : GATE_FLAG_LOW;
  276. if (gate & GATE_FLAG_RISING) {
  277. scale_recorder.NewNote(voltage);
  278. }
  279. if (gate & GATE_FLAG_HIGH) {
  280. scale_recorder.UpdateVoltage(voltage);
  281. }
  282. if (gate & GATE_FLAG_FALLING) {
  283. scale_recorder.AcceptNote();
  284. }
  285. }
  286. fill(&voltages[0], &voltages[4 * size], voltage);
  287. } else {
  288. x.control_mode = ControlMode(state.x_control_mode);
  289. x.voltage_range = VoltageRange(state.x_range % 3);
  290. x.register_mode = state.x_register_mode;
  291. x.register_value = u;
  292. cv_reader.set_attenuverter(
  293. ADC_CHANNEL_X_SPREAD, state.x_register_mode ? 0.5f : 1.0f);
  294. x.spread = parameters[ADC_CHANNEL_X_SPREAD];
  295. x.bias = parameters[ADC_CHANNEL_X_BIAS];
  296. x.steps = parameters[ADC_CHANNEL_X_STEPS];
  297. x.deja_vu = state.x_deja_vu ? deja_vu : 0.0f;
  298. x.length = deja_vu_length;
  299. x.ratio.p = 1;
  300. x.ratio.q = 1;
  301. y.control_mode = CONTROL_MODE_IDENTICAL;
  302. y.voltage_range = VoltageRange(state.y_range);
  303. y.register_mode = false;
  304. y.register_value = 0.0f;
  305. y.spread = float(state.y_spread) / 256.0f;
  306. y.bias = float(state.y_bias) / 256.0f;
  307. y.steps = float(state.y_steps) / 256.0f;
  308. y.deja_vu = 0.0f;
  309. y.length = 1;
  310. y.ratio = y_divider_ratios[
  311. static_cast<uint16_t>(state.y_divider) * 12 >> 8];
  312. if (settings.dirty_scale_index() != -1) {
  313. int i = settings.dirty_scale_index();
  314. xy_generator.LoadScale(i, settings.persistent_data().scale[i]);
  315. settings.set_dirty_scale_index(-1);
  316. }
  317. y.scale_index = x.scale_index = state.x_scale;
  318. xy_generator.Process(
  319. xy_clock_source,
  320. x,
  321. y,
  322. xy_clock,
  323. ramps,
  324. voltages,
  325. size);
  326. }
  327. const float* v = voltages;
  328. const bool* g = gates;
  329. for (size_t i = 0; i < size; ++i) {
  330. block->cv_output[1][i] = DacCode(1, *v++);
  331. block->cv_output[2][i] = DacCode(2, *v++);
  332. block->cv_output[3][i] = DacCode(3, *v++);
  333. block->cv_output[0][i] = DacCode(0, *v++);
  334. block->gate_output[0][i + kGateDelay] = *g++;
  335. block->gate_output[1][i + kGateDelay] = ramps.master[i] < 0.5f;
  336. block->gate_output[2][i + kGateDelay] = *g++;
  337. }
  338. for (size_t i = 0; i < kNumGateOutputs; ++i) {
  339. for (size_t j = 0; j < kGateDelay; ++j) {
  340. block->gate_output[i][j] = gate_delay_tail[i][j];
  341. gate_delay_tail[i][j] = block->gate_output[i][size + j];
  342. }
  343. }
  344. if (PROFILE_RENDER) {
  345. TOC;
  346. }
  347. }
  348. void Init() {
  349. System sys;
  350. sys.Init(true);
  351. settings.Init();
  352. clock_inputs.Init();
  353. dac.Init(kSampleRate, 1);
  354. rng.Init();
  355. note_filter.Init();
  356. gate_outputs.Init();
  357. io_buffer.Init();
  358. deja_vu_length_quantizer.Init();
  359. cv_reader.Init(settings.mutable_calibration_data());
  360. scale_recorder.Init();
  361. ui.Init(&settings, &cv_reader, &scale_recorder, &clock_inputs);
  362. if (settings.freshly_baked()) {
  363. settings.ProgramOptionBytes();
  364. if (PROFILE_INTERRUPT || PROFILE_RENDER) {
  365. DebugPin::Init();
  366. } else {
  367. debug_port.Init();
  368. }
  369. }
  370. random_generator.Init(1);
  371. random_stream.Init(&random_generator);
  372. t_generator.Init(&random_stream, static_cast<float>(kSampleRate));
  373. xy_generator.Init(&random_stream, static_cast<float>(kSampleRate));
  374. for (size_t i = 0; i < kNumScales; ++i) {
  375. xy_generator.LoadScale(i, settings.persistent_data().scale[i]);
  376. }
  377. for (size_t i = 0; i < kNumGateOutputs; ++i) {
  378. self_patching_detector[i].Init(i);
  379. }
  380. sys.StartTimers();
  381. dac.Start(&FillBuffer);
  382. }
  383. int main(void) {
  384. Init();
  385. while (1) {
  386. ui.DoEvents();
  387. io_buffer.Process(ui.output_test_mode() ? &ProcessTest : &Process);
  388. }
  389. }