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.

110 lines
2.9KB

  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. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // I/O Buffering.
  28. #ifndef MARBLES_IO_BUFFER_H_
  29. #define MARBLES_IO_BUFFER_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/utils/gate_flags.h"
  32. #include <algorithm>
  33. namespace marbles {
  34. const size_t kNumBlocks = 2;
  35. const size_t kBlockSize = 5;
  36. const size_t kNumInputs = 2;
  37. const size_t kNumCvOutputs = 4;
  38. const size_t kNumGateOutputs = 3;
  39. const size_t kNumParameters = 8;
  40. class IOBuffer {
  41. public:
  42. struct Block {
  43. uint16_t adc_value[kNumParameters * 2];
  44. bool input_patched[kNumInputs];
  45. stmlib::GateFlags input[kNumInputs][kBlockSize];
  46. uint16_t cv_output[kNumCvOutputs][kBlockSize];
  47. bool gate_output[kNumGateOutputs][kBlockSize + 2];
  48. };
  49. struct Slice {
  50. Block* block;
  51. size_t frame_index;
  52. };
  53. typedef void ProcessFn(Block* block, size_t size);
  54. IOBuffer() { }
  55. ~IOBuffer() { }
  56. void Init() {
  57. io_block_ = 0;
  58. render_block_ = kNumBlocks / 2;
  59. io_frame_ = 0;
  60. }
  61. inline void Process(ProcessFn* fn) {
  62. while (render_block_ != io_block_) {
  63. (*fn)(&block_[render_block_], kBlockSize);
  64. render_block_ = (render_block_ + 1) % kNumBlocks;
  65. }
  66. }
  67. inline Slice NextSlice(size_t size) {
  68. Slice s;
  69. s.block = &block_[io_block_];
  70. s.frame_index = io_frame_;
  71. io_frame_ += size;
  72. if (io_frame_ >= kBlockSize) {
  73. io_frame_ -= kBlockSize;
  74. io_block_ = (io_block_ + 1) % kNumBlocks;
  75. }
  76. return s;
  77. }
  78. inline bool new_block() const {
  79. return io_frame_ == 0;
  80. }
  81. private:
  82. Block block_[kNumBlocks];
  83. size_t io_frame_;
  84. volatile size_t io_block_;
  85. volatile size_t render_block_;
  86. DISALLOW_COPY_AND_ASSIGN(IOBuffer);
  87. };
  88. } // namespace marbles
  89. #endif // MARBLES_IO_BUFFER_H_