forked from hughpyle/inguz-DSPUtil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Skewer.cs
113 lines (105 loc) · 3.12 KB
/
Skewer.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Text;
// Copyright (c) 2006, 2007 by Hugh Pyle, inguzaudio.com
namespace DSPUtil
{
public class Skewer : SoundObj
{
private int _skew;
private int _s;
private double[] _data;
private bool _tail;
/// <summary>
/// Create a skew processor for a stereo input
/// </summary>
/// <param name="returnTail">True to return all the samples (false to truncate the skew-tail)</param>
public Skewer(bool returnTail)
{
Skew = 0;
_tail = returnTail;
}
public override void Reset()
{
Skew = _skew;
base.Reset();
}
/// <summary>
/// Get an iterator for samples
/// </summary>
public override IEnumerator<ISample> Samples
{
get
{
if (_input == null)
{
yield break;
}
// Return skewed sample. Positive skew means delay the right-hand channel.
// eg if skew=1,
// output(t=0) = input(t=0,nul)
// output(t=1) = input(t=1,t=0) ...etc...
// To do this keep a circular buffer _data[] of the channel which is being delayed
// (right if _skew>0, left if _skew<0)
// with ptr n
int n = 0;
foreach (ISample sample in Input)
{
yield return _nxt(ref n, sample);
}
if (_tail)
{
for (int j = 0; j < _s; j++)
{
yield return _nxt(ref n, new Sample(_nc));
}
}
}
}
ISample _nxt(ref int n, ISample s)
{
if (_s == 0 || s.NumChannels != 2)
{
return s;
}
else
{
double L = s[0];
double R = s[1];
if (_skew < 0)
{
_data[n % _s] = L;
n = (n + 1) % _s;
L = _data[n];
}
else
{
_data[n % _s] = R;
n = (n + 1) % _s;
R = _data[n];
}
s[0] = L;
s[1] = R;
return s;
}
}
/// <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 Skew
{
get { return _skew; }
set
{
_skew = value;
_s = Math.Abs(value) + 1;
if (_data==null || _data.Length < _s)
{
// Recreate the skewer cache (even if it means losing data)
_data = new double[_s];
}
}
}
}
}