1 // Copyright 2017 Emilie Gillet.
2 //
3 // Author: Emilie Gillet (emilie.o.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 
29 #ifndef STAGES_IO_BUFFER_H_
30 #define STAGES_IO_BUFFER_H_
31 
32 #include "stmlib/stmlib.h"
33 #include "stmlib/utils/gate_flags.h"
34 
35 #include <algorithm>
36 
37 namespace stages {
38 
39 const size_t kNumBlocks = 2;
40 const size_t kBlockSize = 8;
41 const size_t kNumChannels = 6;
42 
43 class IOBuffer {
44  public:
45   struct Block {
46     float cv_slider[kNumChannels];
47     float pot[kNumChannels];
48     bool input_patched[kNumChannels];
49 
50     stmlib::GateFlags input[kNumChannels][kBlockSize];
51     uint16_t output[kNumChannels][kBlockSize];
52   };
53 
54   struct Slice {
55     Block* block;
56     size_t frame_index;
57   };
58 
59   typedef void ProcessFn(Block* block, size_t size);
60 
IOBuffer()61   IOBuffer() { }
~IOBuffer()62   ~IOBuffer() { }
63 
Init()64   void Init() {
65     io_block_ = 0;
66     render_block_ = kNumBlocks / 2;
67     io_frame_ = 0;
68   }
69 
Process(ProcessFn * fn)70   inline void Process(ProcessFn* fn) {
71     while (render_block_ != io_block_) {
72       (*fn)(&block_[render_block_], kBlockSize);
73       render_block_ = (render_block_ + 1) % kNumBlocks;
74     }
75   }
76 
NextSlice(size_t size)77   inline Slice NextSlice(size_t size) {
78     Slice s;
79     s.block = &block_[io_block_];
80     s.frame_index = io_frame_;
81     io_frame_ += size;
82     if (io_frame_ >= kBlockSize) {
83       io_frame_ -= kBlockSize;
84       io_block_ = (io_block_ + 1) % kNumBlocks;
85     }
86     return s;
87   }
88 
new_block()89   inline bool new_block() const {
90     return io_frame_ == 0;
91   }
92 
93  private:
94   Block block_[kNumBlocks];
95 
96   size_t io_frame_;
97   volatile size_t io_block_;
98   volatile size_t render_block_;
99 
100   DISALLOW_COPY_AND_ASSIGN(IOBuffer);
101 };
102 
103 }  // namespace stages
104 
105 #endif  // STAGES_IO_BUFFER_H_
106