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.

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