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.

136 lines
4.1KB

  1. // Copyright 2014 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 "elements/drivers/cv_adc.h"
  25. #include "elements/drivers/codec.h"
  26. #include "elements/drivers/debug_pin.h"
  27. #include "elements/drivers/debug_port.h"
  28. #include "elements/drivers/pots_adc.h"
  29. #include "elements/drivers/system.h"
  30. #include "elements/dsp/part.h"
  31. #include "elements/cv_scaler.h"
  32. #include "elements/ui.h"
  33. #include "stmlib/dsp/dsp.h"
  34. using namespace elements;
  35. using namespace stmlib;
  36. Codec codec;
  37. CvScaler cv_scaler;
  38. DebugPort debug_port;
  39. Part part;
  40. Ui ui;
  41. uint16_t reverb_buffer[32768] __attribute__ ((section (".ccmdata")));
  42. // Default interrupt handlers.
  43. extern "C" {
  44. void NMI_Handler() { }
  45. void HardFault_Handler() { while (1); }
  46. void MemManage_Handler() { while (1); }
  47. void BusFault_Handler() { while (1); }
  48. void UsageFault_Handler() { while (1); }
  49. void SVC_Handler() { }
  50. void DebugMon_Handler() { }
  51. void PendSV_Handler() { }
  52. void SysTick_Handler() {
  53. ui.Poll();
  54. if (debug_port.readable()) {
  55. uint8_t command = debug_port.Read();
  56. uint8_t response = ui.HandleFactoryTestingRequest(command);
  57. debug_port.Write(response);
  58. }
  59. }
  60. }
  61. float blow_in[kAudioChunkSize];
  62. float strike_in[kAudioChunkSize];
  63. float out[kAudioChunkSize];
  64. float aux[kAudioChunkSize];
  65. const float kNoiseGateThreshold = 0.0001f;
  66. float strike_in_level = 0.0f;
  67. float blow_in_level = 0.0f;
  68. void FillBuffer(Codec::Frame* input, Codec::Frame* output, size_t n) {
  69. PerformanceState s;
  70. cv_scaler.Read(part.mutable_patch(), &s);
  71. s.gate |= ui.gate();
  72. for (size_t i = 0; i < n; ++i) {
  73. float blow_in_sample = static_cast<float>(input[i].r) / 32768.0f;
  74. float strike_in_sample = static_cast<float>(input[i].l) / 32768.0f;
  75. float error, gain;
  76. error = strike_in_sample * strike_in_sample - strike_in_level;
  77. strike_in_level += error * (error > 0.0f ? 0.1f : 0.0001f);
  78. gain = strike_in_level <= kNoiseGateThreshold
  79. ? (1.0f / kNoiseGateThreshold) * strike_in_level : 1.0f;
  80. strike_in[i] = gain * strike_in_sample;
  81. error = blow_in_sample * blow_in_sample - blow_in_level;
  82. blow_in_level += error * (error > 0.0f ? 0.1f : 0.0001f);
  83. gain = blow_in_level <= kNoiseGateThreshold
  84. ? (1.0f / kNoiseGateThreshold) * blow_in_level : 1.0f;
  85. blow_in[i] = gain * blow_in_sample;
  86. }
  87. part.Process(s, blow_in, strike_in, out, aux, n);
  88. for (size_t i = 0; i < n; ++i) {
  89. output[i].r = SoftConvert(out[i]);
  90. output[i].l = SoftConvert(aux[i]);
  91. }
  92. }
  93. void Init() {
  94. System sys;
  95. sys.Init(true);
  96. // Init and seed the random parameters and generators with the serial number.
  97. part.Init(reverb_buffer);
  98. part.Seed((uint32_t*)(0x1fff7a10), 3);
  99. cv_scaler.Init();
  100. ui.Init(&part, &cv_scaler);
  101. if (!codec.Init(32000, CODEC_PROTOCOL_PHILIPS, CODEC_FORMAT_16_BIT)) {
  102. ui.Panic();
  103. }
  104. if (!codec.Start(&FillBuffer)) {
  105. ui.Panic();
  106. }
  107. // DebugPin::Init();
  108. debug_port.Init();
  109. sys.StartTimers();
  110. }
  111. int main(void) {
  112. Init();
  113. while (1) {
  114. ui.DoEvents();
  115. }
  116. }