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.

168 lines
4.7KB

  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 "rings/drivers/adc.h"
  25. #include "rings/drivers/codec.h"
  26. #include "rings/drivers/debug_pin.h"
  27. #include "rings/drivers/debug_port.h"
  28. #include "rings/drivers/system.h"
  29. #include "rings/drivers/version.h"
  30. #include "rings/dsp/part.h"
  31. #include "rings/dsp/strummer.h"
  32. #include "rings/dsp/string_synth_part.h"
  33. #include "rings/cv_scaler.h"
  34. #include "rings/settings.h"
  35. #include "rings/ui.h"
  36. // #define PROFILE_INTERRUPT 1
  37. using namespace rings;
  38. using namespace stmlib;
  39. uint16_t reverb_buffer[32768] __attribute__ ((section (".ccmdata")));
  40. Codec codec;
  41. CvScaler cv_scaler;
  42. DebugPort debug_port;
  43. Part part;
  44. Settings settings;
  45. StringSynthPart string_synth;
  46. Strummer strummer;
  47. Ui ui;
  48. // Default interrupt handlers.
  49. extern "C" {
  50. int __errno;
  51. void NMI_Handler() { }
  52. void HardFault_Handler() { while (1); }
  53. void MemManage_Handler() { while (1); }
  54. void BusFault_Handler() { while (1); }
  55. void UsageFault_Handler() { while (1); }
  56. void SVC_Handler() { }
  57. void DebugMon_Handler() { }
  58. void PendSV_Handler() { }
  59. void SysTick_Handler() {
  60. ui.Poll();
  61. if (settings.freshly_baked()) {
  62. if (debug_port.readable()) {
  63. uint8_t command = debug_port.Read();
  64. uint8_t response = ui.HandleFactoryTestingRequest(command);
  65. debug_port.Write(response);
  66. }
  67. }
  68. }
  69. }
  70. float in[kMaxBlockSize];
  71. float out[kMaxBlockSize];
  72. float aux[kMaxBlockSize];
  73. const float kNoiseGateThreshold = 0.00003f;
  74. float in_level = 0.0f;
  75. void FillBuffer(Codec::Frame* input, Codec::Frame* output, size_t size) {
  76. #ifdef PROFILE_INTERRUPT
  77. TIC
  78. #endif // PROFILE_INTERRUPT
  79. PerformanceState performance_state;
  80. Patch patch;
  81. cv_scaler.DetectAudioNormalization(input, size);
  82. cv_scaler.Read(&patch, &performance_state);
  83. if (settings.state().easter_egg) {
  84. for (size_t i = 0; i < size; ++i) {
  85. in[i] = static_cast<float>(input[i].r) / 32768.0f;
  86. }
  87. strummer.Process(NULL, size, &performance_state);
  88. string_synth.Process(performance_state, patch, in, out, aux, size);
  89. } else {
  90. // Apply noise gate.
  91. for (size_t i = 0; i < size; ++i) {
  92. float in_sample = static_cast<float>(input[i].r) / 32768.0f;
  93. float error, gain;
  94. error = in_sample * in_sample - in_level;
  95. in_level += error * (error > 0.0f ? 0.1f : 0.0001f);
  96. gain = in_level <= kNoiseGateThreshold
  97. ? (1.0f / kNoiseGateThreshold) * in_level : 1.0f;
  98. in[i] = gain * in_sample;
  99. }
  100. strummer.Process(in, size, &performance_state);
  101. part.Process(performance_state, patch, in, out, aux, size);
  102. }
  103. for (size_t i = 0; i < size; ++i) {
  104. output[i].l = Clip16(static_cast<int32_t>(out[i] * 32768.0f));
  105. output[i].r = Clip16(static_cast<int32_t>(aux[i] * 32768.0f));
  106. }
  107. ui.set_strumming_flag(performance_state.strum);
  108. #ifdef PROFILE_INTERRUPT
  109. TOC
  110. #endif // PROFILE_INTERRUPT
  111. }
  112. void Init() {
  113. System sys;
  114. Version version;
  115. sys.Init(true);
  116. version.Init();
  117. strummer.Init(0.01f, kSampleRate / kMaxBlockSize);
  118. part.Init(reverb_buffer);
  119. string_synth.Init(reverb_buffer);
  120. settings.Init();
  121. cv_scaler.Init(settings.mutable_calibration_data());
  122. ui.Init(&settings, &cv_scaler, &part, &string_synth);
  123. if (!codec.Init(!version.revised(), kSampleRate)) {
  124. ui.Panic();
  125. }
  126. if (!codec.Start(kMaxBlockSize, &FillBuffer)) {
  127. ui.Panic();
  128. }
  129. codec.set_line_input_gain(22);
  130. if (settings.freshly_baked()) {
  131. #ifdef PROFILE_INTERRUPT
  132. DebugPin::Init();
  133. #else
  134. debug_port.Init();
  135. #endif // PROFILE_INTERRUPT
  136. }
  137. sys.StartTimers();
  138. }
  139. int main(void) {
  140. Init();
  141. while (1) {
  142. ui.DoEvents();
  143. }
  144. }