forked from hughpyle/inguz-DSPUtil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delayer.cs
90 lines (80 loc) · 2.24 KB
/
Delayer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.Text;
// Copyright (c) 2009 by Hugh Pyle, inguzaudio.com
namespace DSPUtil
{
public class Delayer : SoundObj
{
private int _delay;
private CircularBuffer _buff;
private bool _tail;
/// <summary>
/// Create a delay processor
/// </summary>
public Delayer()
{
Delay = 0;
_tail = true;
}
/// <summary>
/// Create a delay processor
/// </summary>
/// <param name="returnTail">True to return all the samples (false to truncate the delay-tail)</param>
public Delayer(bool returnTail)
{
Delay = 0;
_tail = returnTail;
}
public override void Reset()
{
Delay = _delay;
base.Reset();
}
/// <summary>
/// Get an iterator for samples
/// </summary>
public override IEnumerator<ISample> Samples
{
get
{
if (_input == null)
{
yield break;
}
_buff.Input = _input;
// Return the delayed
foreach (ISample sample in _buff)
{
ISample s = _buff[-_delay];
yield return s;
}
if (_tail)
{
for (int j = 0; j < _delay; j++)
{
yield return _buff[j-_delay];
}
}
}
}
/// <summary>
/// Time difference, in samples, to apply between left and right channels.
/// Positive values delay the right channel relative to the left channel, and vice versa.
/// </summary>
public int Delay
{
get { return _delay; }
set
{
int d = value;
if (_buff == null || _delay < d)
{
// Recreate the cache (even if it means losing data)
_buff = new CircularBuffer(_input, (uint)(d+1));
}
_delay = d;
}
}
}
}