CSL  6.0
DLine.cpp
Go to the documentation of this file.
1 //
2 // DLine.h -- an Interpolating Delay Line
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5 // This is a typical circular buffer with one writer and multiple readers (tap instances)
6 
7 #include "DLine.h"
8 using namespace csl;
9 
10 DLine :: DLine ( FrameStream &input, float max_delay ) : Processor(input),
11  max_delay_time(max_delay),
12  delay_time(0.5 * max_delay),
13  max_delay_in_frames( (unsigned) (_sampleRate * max_delay + CGestalt::block_size() ) ),
14  interp_type(kTruncate),
15  start_frame(0),
16 // end_frame(0),
17  write_frame(0),
18  ring_buffer(FrameStream::channels(), max_delay_in_frames)
19 {
20 // max_delay_in_frames = _sampleRate * max_delay;
21  ring_buffer.allocate_mono_buffers();
22  ring_buffer.zero_buffers();
23 
24 }
25 
27  ring_buffer.freeMonoBuffers();
28 }
29 
30 bool DLine :: init_delay_time( float dt ) {
31  if (dt > max_delay_time)
32  return false;
33  delay_time = dt;
34  return set_target_delay_time(dt);
35 }
36 
37 bool DLine :: set_target_delay_time( float tdt ) {
38  if (tdt > max_delay_time)
39  return false;
40  target_delay_time = tdt;
41  return true;
42 }
43 
45  interp_type = it;
46  return true;
47 }
48 
49 // Do the work
50 
51 status DLine :: mono_next_buffer(Buffer & inputBuffer, Buffer & outputBuffer, unsigned inBufNum, unsigned outBufNum) {
52  unsigned temp_write_frame = write_frame;
53  sample * rbPtr = ring_buffer._monoBuffers[outBufNum] + temp_write_frame;
54  sample * outPtr = outputBuffer._monoBuffers[outBufNum];
55 /*
56  for (int i=0; i<outputBuffer._numFrames; i++){
57  // ring_buffer._monoBuffers[outBufNum][temp_write_frame] = outputBuffer._monoBuffers[outBufNum][i];
58  *rbPtr++ = *outPtr++
59  if (++temp_write_frame >= ring_buffer._numFrames)
60  temp_write_frame = 0;
61  rbPtr = ring_buffer._monoBuffers[outBufNum];
62  }
63 */
64  sample *tempSamplePtr;
65  outPtr = outputBuffer._monoBuffers[outBufNum];
66  float ramp_factor = 0.0001; // ??
67  rbPtr = ring_buffer._monoBuffers[outBufNum];
68  for (unsigned i = 0; i < outputBuffer._numFrames; i++) {
69  delay_time = (1.0 - ramp_factor) * delay_time + ramp_factor * target_delay_time;
70  float r_index_and_mant = (delay_time * _sampleRate);
71  unsigned index = (unsigned) floor(r_index_and_mant);
72  float mantissa = r_index_and_mant - index;
73  int read_frame = temp_write_frame - index;
74  while (read_frame < 0)
75  read_frame += ring_buffer._numFrames;
76  sample lower_read_value = *(rbPtr + read_frame);
77  tempSamplePtr = ((unsigned)(rbPtr + read_frame + 1)) % ring_buffer._numFrames;
78  sample upper_read_value = *tempSamplePtr;
79  if ( ++temp_write_frame >= ring_buffer._numFrames)
80  temp_write_frame -= ring_buffer._numFrames;
81  *outPtr++ = ((1.0 - mantissa) * lower_read_value) + (mantissa * upper_read_value);
82  }
83  write_frame = temp_write_frame;
84 }
85 
86 // Call inherited version
87 
88 status DLine :: next_buffer(Buffer & inputBuffer, Buffer & outputBuffer){
89  Processor :: pull_input( inputBuffer, outputBuffer );
90  FrameStream :: next_buffer( inputBuffer, outputBuffer );
91 // update_write_frame(outputBuffer._numFrames);
92  write_frame += outputBuffer._numFrames;
93  write_frame %= ring_buffer._numFrames;
94 
95 }
96 
97 
98 
99 
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
~DLine()
Definition: DLine.cpp:26
float max_delay_time
Definition: DLine.h:25
InterpType
Definition: DLine.h:14
The CSL system defaults class.
Definition: CGestalt.h:39
InterpType interp_type
Definition: DLine.h:29
DLine(FrameStream &input, float max_delay)
Definition: DLine.cpp:10
bool init_delay_time(float dt)
Definition: DLine.cpp:30
status next_buffer(Buffer &inputBuffer, Buffer &outputBuffer)
Definition: DLine.cpp:88
bool set_target_delay_time(float tdt)
Definition: DLine.cpp:37
float sample
(could be changed to int, or double)
Definition: CSL_Types.h:191
float target_delay_time
Definition: DLine.h:27
float delay_time
Definition: DLine.h:26
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
status mono_next_buffer(Buffer &inputBuffer, Buffer &outputBuffer, unsigned inBufNum, unsigned outBufNum)
Definition: DLine.cpp:51
Buffer ring_buffer
Definition: DLine.h:23
unsigned write_frame
Definition: DLine.h:32
bool set_interp_type(InterpType)
Definition: DLine.cpp:44