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

Stream that stores a mono sample buffer and scaling factors. Plays as a stereo stream. More...

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

Public Member Functions

 NormalizedMonoClip (float[] monoSamples, double leftFactor, double rightFactor)
 
 NormalizedMonoClip (float[] monoSamples, double presentationLevel)
 
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 void Reset ()
 Sets this internal state of this stream to the initial state 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 TotalSamples => 2 * Samples.Length
 
override int ChannelSamples => Samples.Length
 
override int Channels => 2
 
- Data Fields inherited from BGC.Audio.BGCAudioClip
float Duration => ChannelSamples / SamplingRate
 
override float SamplingRate => 44100f
 

Protected Member Functions

override void _Initialize ()
 

Properties

int Position [get, set]
 
float [] Samples [get]
 
- 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

int RemainingChannelSamples => Math.Max(0, Samples.Length - Position)
 
int RemainingTotalSamples => 2 * RemainingChannelSamples
 
bool factorsInitialized = false
 
float leftFactor
 
float rightFactor
 
readonly double presentationLevel
 
IEnumerable< double > _channelRMS = null
 

Additional Inherited Members

- Protected Attributes inherited from BGC.Audio.BGCStream
bool initialized = false
 

Detailed Description

Stream that stores a mono sample buffer and scaling factors. Plays as a stereo stream.

Definition at line 11 of file NormalizedMonoClip.cs.

Constructor & Destructor Documentation

◆ NormalizedMonoClip() [1/2]

BGC.Audio.NormalizedMonoClip.NormalizedMonoClip ( float []  monoSamples,
double  leftFactor,
double  rightFactor 
)
inline

Definition at line 30 of file NormalizedMonoClip.cs.

31  {
32  Samples = monoSamples;
33  this.leftFactor = (float)leftFactor;
34  this.rightFactor = (float)rightFactor;
35  factorsInitialized = true;
36  }

◆ NormalizedMonoClip() [2/2]

BGC.Audio.NormalizedMonoClip.NormalizedMonoClip ( float []  monoSamples,
double  presentationLevel 
)
inline

Definition at line 38 of file NormalizedMonoClip.cs.

39  {
40  Samples = monoSamples;
42  factorsInitialized = false;
43  }

Member Function Documentation

◆ _Initialize()

override void BGC.Audio.NormalizedMonoClip._Initialize ( )
inlineprotectedvirtual

Reimplemented from BGC.Audio.BGCStream.

Definition at line 45 of file NormalizedMonoClip.cs.

References BGC.Audio.Normalization.GetMonoRMSScalingFactors().

46  {
47  if (!factorsInitialized)
48  {
49  factorsInitialized = true;
50 
51  Normalization.GetMonoRMSScalingFactors(
52  monoSamples: Samples,
53  desiredLevel: presentationLevel,
54  scalingFactorL: out double tempLeftFactor,
55  scalingFactorR: out double tempRightFactor);
56 
57  leftFactor = (float)tempLeftFactor;
58  rightFactor = (float)tempRightFactor;
59  }
60  }
Here is the call graph for this function:

◆ GetChannelRMS()

override IEnumerable<double> BGC.Audio.NormalizedMonoClip.GetChannelRMS ( )
inlinevirtual

The RMS amplitude of each channel

Implements BGC.Audio.BGCStream.

Definition at line 87 of file NormalizedMonoClip.cs.

88  {
89  if (_channelRMS == null)
90  {
91  double rms = 0.0;
92 
93  for (int i = 0; i < ChannelSamples; i++)
94  {
95  rms += Samples[i] * Samples[i];
96  }
97 
98  rms = Math.Sqrt(rms / ChannelSamples);
99 
100  _channelRMS = new double[2] { leftFactor * rms, rightFactor * rms };
101  }
102 
103  return _channelRMS;
104  }
IEnumerable< double > _channelRMS

◆ Read()

override int BGC.Audio.NormalizedMonoClip.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 62 of file NormalizedMonoClip.cs.

References BGC.Mathematics.GeneralMath.Clamp().

63  {
64  if (!initialized)
65  {
66  Initialize();
67  }
68 
69  int copyLength = Math.Min(RemainingChannelSamples, count / 2);
70 
71  for (int i = 0; i < copyLength; i++)
72  {
73  data[offset + 2 * i] = leftFactor * Samples[Position + i];
74  data[offset + 2 * i + 1] = rightFactor * Samples[Position + i];
75  }
76 
77  Position += copyLength;
78 
79  return 2 * copyLength;
80  }
void Initialize()
Perform any calculations necessary to prepare the Stream
Definition: BGCStream.cs:18
Here is the call graph for this function:

◆ Reset()

override void BGC.Audio.NormalizedMonoClip.Reset ( )
virtual

Sets this internal state of this stream to the initial state

Implements BGC.Audio.BGCStream.

◆ Seek()

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

Seek to the indicated position in the stream

Implements BGC.Audio.BGCStream.

Field Documentation

◆ _channelRMS

IEnumerable<double> BGC.Audio.NormalizedMonoClip._channelRMS = null
private

Definition at line 86 of file NormalizedMonoClip.cs.

◆ Channels

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

Definition at line 20 of file NormalizedMonoClip.cs.

◆ ChannelSamples

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

Definition at line 16 of file NormalizedMonoClip.cs.

◆ factorsInitialized

bool BGC.Audio.NormalizedMonoClip.factorsInitialized = false
private

Definition at line 25 of file NormalizedMonoClip.cs.

◆ leftFactor

float BGC.Audio.NormalizedMonoClip.leftFactor
private

Definition at line 26 of file NormalizedMonoClip.cs.

◆ presentationLevel

readonly double BGC.Audio.NormalizedMonoClip.presentationLevel
private

Definition at line 28 of file NormalizedMonoClip.cs.

◆ RemainingChannelSamples

int BGC.Audio.NormalizedMonoClip.RemainingChannelSamples => Math.Max(0, Samples.Length - Position)
private

Definition at line 22 of file NormalizedMonoClip.cs.

◆ RemainingTotalSamples

int BGC.Audio.NormalizedMonoClip.RemainingTotalSamples => 2 * RemainingChannelSamples
private

Definition at line 23 of file NormalizedMonoClip.cs.

◆ rightFactor

float BGC.Audio.NormalizedMonoClip.rightFactor
private

Definition at line 27 of file NormalizedMonoClip.cs.

◆ TotalSamples

override int BGC.Audio.NormalizedMonoClip.TotalSamples => 2 * Samples.Length

Definition at line 15 of file NormalizedMonoClip.cs.

Property Documentation

◆ Position

int BGC.Audio.NormalizedMonoClip.Position
getset

Definition at line 13 of file NormalizedMonoClip.cs.

◆ Samples

float [] BGC.Audio.NormalizedMonoClip.Samples
get

Definition at line 18 of file NormalizedMonoClip.cs.


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