BGC Tools
Public Member Functions | Data Fields | Properties | Private Attributes
BGC.Audio.CrossFadingRefClip Class Reference

Holds a reference to a large underlying sample buffer. Loops and crossfades with itself. Used for managing extra-large TENoise sample. More...

Inheritance diagram for BGC.Audio.CrossFadingRefClip:
Inheritance graph
[legend]
Collaboration diagram for BGC.Audio.CrossFadingRefClip:
Collaboration graph
[legend]

Public Member Functions

 CrossFadingRefClip (float[] samples, double leftFactor, double rightFactor, int initialPosition=-1)
 
override void Reset ()
 Sets this internal state of this stream to the initial state More...
 
override int Read (float[] data, int offset, int count)
 Copy count samples into the Data buffer, starting at offset. More...
 
override void Seek (int position)
 Seek to the indicated position in the stream More...
 
override IEnumerable< double > GetChannelRMS ()
 The RMS amplitude of each channel More...
 
- Public Member Functions inherited from BGC.Audio.BGCStream
void Initialize ()
 Perform any calculations necessary to prepare the Stream More...
 

Data Fields

override int Channels => 2
 
override int TotalSamples => Channels * Samples.Length
 
override int ChannelSamples => Samples.Length
 
- Data Fields inherited from BGC.Audio.BGCAudioClip
float Duration => ChannelSamples / SamplingRate
 
override float SamplingRate => 44100f
 

Properties

float [] Samples [get]
 
int Position [get, private set]
 
- Properties inherited from BGC.Audio.BGCStream
abstract int Channels [get]
 
abstract int TotalSamples [get]
 
abstract int ChannelSamples [get]
 
abstract float SamplingRate [get]
 
- Properties inherited from BGC.Audio.IBGCStream
int Channels [get]
 The number of underlying Channels of this BGCStream More...
 
int TotalSamples [get]
 The total number of Samples of this BGCStream More...
 
int ChannelSamples [get]
 The number of Samples of each channel for this BGCStream More...
 
float SamplingRate [get]
 The sampling rate of the Stream More...
 

Private Attributes

readonly float leftFactor = 0f
 
readonly float rightFactor = 0f
 
const float FADE_DURATION = 1.0f
 
readonly float [] window = null
 
int initialWindowStart = -1
 
readonly int startCrossFade
 

Additional Inherited Members

- Protected Member Functions inherited from BGC.Audio.BGCStream
virtual void _Initialize ()
 
- Protected Attributes inherited from BGC.Audio.BGCStream
bool initialized = false
 

Detailed Description

Holds a reference to a large underlying sample buffer. Loops and crossfades with itself. Used for managing extra-large TENoise sample.

Definition at line 12 of file CrossFadingRefClip.cs.

Constructor & Destructor Documentation

◆ CrossFadingRefClip()

BGC.Audio.CrossFadingRefClip.CrossFadingRefClip ( float []  samples,
double  leftFactor,
double  rightFactor,
int  initialPosition = -1 
)
inline

Definition at line 33 of file CrossFadingRefClip.cs.

References BGC.Mathematics.CustomRandom.Next().

38  {
39  Samples = samples;
40 
41  this.leftFactor = (float)leftFactor;
42  this.rightFactor = (float)rightFactor;
43 
44  window = new float[(int)(SamplingRate * FADE_DURATION)];
45 
46  double sineArgument = Math.PI / (2 * window.Length - 1);
47 
48  for (int i = 0; i < window.Length; i++)
49  {
50  window[i] = (float)Math.Sin(i * sineArgument);
51  }
52 
53  startCrossFade = Samples.Length - window.Length;
54 
55  if (initialPosition == -1)
56  {
57  //Start at least 2 window lengths before end
58  initialPosition = CustomRandom.Next(0, Samples.Length - (2 * window.Length + 2));
59  }
60 
61  Position = initialPosition;
63  }
override float SamplingRate
Definition: BGCAudioClip.cs:7
static int Next()
Returns a random integer
Definition: CustomRandom.cs:54
Here is the call graph for this function:

Member Function Documentation

◆ GetChannelRMS()

override IEnumerable<double> BGC.Audio.CrossFadingRefClip.GetChannelRMS ( )
virtual

The RMS amplitude of each channel

Implements BGC.Audio.BGCStream.

◆ Read()

override int BGC.Audio.CrossFadingRefClip.Read ( float []  data,
int  offset,
int  count 
)
inlinevirtual

Copy count samples into the Data buffer, starting at offset.

Returns
The number of samples copied

Implements BGC.Audio.BGCStream.

Definition at line 72 of file CrossFadingRefClip.cs.

References BGC.Mathematics.GeneralMath.Clamp().

73  {
74  //Split into regions
75  int remainingCount = count;
76 
77  while (remainingCount > 0)
78  {
79  if (initialWindowStart != -1)
80  {
81  //Initial window period
82 
83  //Windowing region
84 
85  int initialWindowingEndSample = initialWindowStart + window.Length;
86 
87  int copyLength = Math.Min(initialWindowingEndSample - Position, remainingCount / 2);
88 
89  for (int i = 0; i < copyLength; i++)
90  {
91  float sample = window[Position - initialWindowStart + i] * Samples[Position + i];
92  data[offset + 2 * i] = leftFactor * sample;
93  data[offset + 2 * i + 1] = rightFactor * sample;
94  }
95 
96  //Advance Indices
97  Position += copyLength;
98  offset += 2 * copyLength;
99  remainingCount -= 2 * copyLength;
100 
101  if (Position == initialWindowingEndSample)
102  {
103  //Done with initial window
104  initialWindowStart = -1;
105  }
106 
107  }
108  else if (Position < startCrossFade)
109  {
110  //Region 1 - No windowing Needed
111  int copyLength = Math.Min(startCrossFade - Position, remainingCount / 2);
112 
113  for (int i = 0; i < copyLength; i++)
114  {
115  data[offset + 2 * i] = leftFactor * Samples[Position + i];
116  data[offset + 2 * i + 1] = rightFactor * Samples[Position + i];
117  }
118 
119  //Advance Indices
120  Position += copyLength;
121  offset += 2 * copyLength;
122  remainingCount -= 2 * copyLength;
123  }
124  else
125  {
126  //Region 2 - Cross-windowing
127  int copyLength = Math.Min(Samples.Length - Position, remainingCount / 2);
128 
129  for (int i = 0; i < copyLength; i++)
130  {
131  float outSample = window[Samples.Length - Position - i - 1] * Samples[Position + i];
132  float inSample = window[Position + i - startCrossFade] * Samples[Position + i - startCrossFade];
133  data[offset + 2 * i] = leftFactor * (outSample + inSample);
134  data[offset + 2 * i + 1] = rightFactor * (outSample + inSample);
135  }
136 
137  //Advance Indices
138  Position += copyLength;
139  offset += 2 * copyLength;
140  remainingCount -= 2 * copyLength;
141 
142  if (Position >= Samples.Length)
143  {
144  Position = window.Length;
145  }
146  }
147  }
148 
149  return count;
150  }
Here is the call graph for this function:

◆ Reset()

override void BGC.Audio.CrossFadingRefClip.Reset ( )
inlinevirtual

Sets this internal state of this stream to the initial state

Implements BGC.Audio.BGCStream.

Definition at line 65 of file CrossFadingRefClip.cs.

References BGC.Mathematics.CustomRandom.Next().

66  {
67  //Start at least 2 window lengths before end
68  Position = CustomRandom.Next(0, Samples.Length - (2 * window.Length + 2));
70  }
static int Next()
Returns a random integer
Definition: CustomRandom.cs:54
Here is the call graph for this function:

◆ Seek()

override void BGC.Audio.CrossFadingRefClip.Seek ( int  position)
virtual

Seek to the indicated position in the stream

Implements BGC.Audio.BGCStream.

Field Documentation

◆ Channels

override int BGC.Audio.CrossFadingRefClip.Channels => 2

Definition at line 15 of file CrossFadingRefClip.cs.

◆ ChannelSamples

override int BGC.Audio.CrossFadingRefClip.ChannelSamples => Samples.Length

Definition at line 18 of file CrossFadingRefClip.cs.

◆ FADE_DURATION

const float BGC.Audio.CrossFadingRefClip.FADE_DURATION = 1.0f
private

Definition at line 27 of file CrossFadingRefClip.cs.

◆ initialWindowStart

int BGC.Audio.CrossFadingRefClip.initialWindowStart = -1
private

Definition at line 30 of file CrossFadingRefClip.cs.

◆ leftFactor

readonly float BGC.Audio.CrossFadingRefClip.leftFactor = 0f
private

Definition at line 24 of file CrossFadingRefClip.cs.

◆ rightFactor

readonly float BGC.Audio.CrossFadingRefClip.rightFactor = 0f
private

Definition at line 25 of file CrossFadingRefClip.cs.

◆ startCrossFade

readonly int BGC.Audio.CrossFadingRefClip.startCrossFade
private

Definition at line 31 of file CrossFadingRefClip.cs.

◆ TotalSamples

override int BGC.Audio.CrossFadingRefClip.TotalSamples => Channels * Samples.Length

Definition at line 17 of file CrossFadingRefClip.cs.

◆ window

readonly float [] BGC.Audio.CrossFadingRefClip.window = null
private

Definition at line 29 of file CrossFadingRefClip.cs.

Property Documentation

◆ Position

int BGC.Audio.CrossFadingRefClip.Position
getprivate set

Definition at line 22 of file CrossFadingRefClip.cs.

◆ Samples

float [] BGC.Audio.CrossFadingRefClip.Samples
get

Definition at line 20 of file CrossFadingRefClip.cs.


The documentation for this class was generated from the following file: