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.

148 lines
4.3KB

  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. // #define PROFILE_INTERRUPT 1
  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. #ifdef PROFILE_INTERRUPT
  70. TIC
  71. #endif // PROFILE_INTERRUPT
  72. PerformanceState s;
  73. cv_scaler.Read(part.mutable_patch(), &s);
  74. s.gate |= ui.gate();
  75. for (size_t i = 0; i < n; ++i) {
  76. float blow_in_sample = static_cast<float>(input[i].r) / 32768.0f;
  77. float strike_in_sample = static_cast<float>(input[i].l) / 32768.0f;
  78. float error, gain;
  79. error = strike_in_sample * strike_in_sample - strike_in_level;
  80. strike_in_level += error * (error > 0.0f ? 0.1f : 0.0001f);
  81. gain = strike_in_level <= kNoiseGateThreshold
  82. ? (1.0f / kNoiseGateThreshold) * strike_in_level : 1.0f;
  83. strike_in[i] = gain * strike_in_sample;
  84. error = blow_in_sample * blow_in_sample - blow_in_level;
  85. blow_in_level += error * (error > 0.0f ? 0.1f : 0.0001f);
  86. gain = blow_in_level <= kNoiseGateThreshold
  87. ? (1.0f / kNoiseGateThreshold) * blow_in_level : 1.0f;
  88. blow_in[i] = gain * blow_in_sample;
  89. }
  90. part.Process(s, blow_in, strike_in, out, aux, n);
  91. for (size_t i = 0; i < n; ++i) {
  92. output[i].r = SoftConvert(out[i]);
  93. output[i].l = SoftConvert(aux[i]);
  94. }
  95. #ifdef PROFILE_INTERRUPT
  96. TOC
  97. #endif // PROFILE_INTERRUPT
  98. }
  99. void Init() {
  100. System sys;
  101. sys.Init(true);
  102. // Init and seed the random parameters and generators with the serial number.
  103. part.Init(reverb_buffer);
  104. part.Seed((uint32_t*)(0x1fff7a10), 3);
  105. cv_scaler.Init();
  106. ui.Init(&part, &cv_scaler);
  107. if (!codec.Init(32000, CODEC_PROTOCOL_PHILIPS, CODEC_FORMAT_16_BIT)) {
  108. ui.Panic();
  109. }
  110. if (!codec.Start(&FillBuffer)) {
  111. ui.Panic();
  112. }
  113. if (cv_scaler.freshly_baked()) {
  114. #ifdef PROFILE_INTERRUPT
  115. DebugPin::Init();
  116. #else
  117. debug_port.Init();
  118. #endif // PROFILE_INTERRUPT
  119. }
  120. sys.StartTimers();
  121. }
  122. int main(void) {
  123. Init();
  124. while (1) {
  125. ui.DoEvents();
  126. }
  127. }