forked from dzamkov/MD-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioSignal.cs
83 lines (74 loc) · 2.3 KB
/
AudioSignal.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
using System;
using System.Collections.Generic;
namespace MD
{
/// <summary>
/// A representation of a signal that allows it to be played as audio.
/// </summary>
public abstract class AudioSignal<TSample>
where TSample : IAudioSample
{
/// <summary>
/// Gets the amount of samples played in a second for this signal.
/// </summary>
public abstract double SampleRate { get; }
/// <summary>
/// Gets a multi-channel discrete signal for the audio.
/// </summary>
public abstract MultiDiscreteSignal<TSample> Samples { get; }
/// <summary>
/// Gets a multi-channel discrete signal of double values for the audio.
/// </summary>
public virtual MultiDiscreteSignal<double> DoubleSamples
{
get
{
// Default implementation of this is horribly inefficent
return new _DoubleSignal<MultiDiscreteSignal<TSample>>(this.Samples);
}
}
private class _DoubleSignal<TSource> : MultiDiscreteSignal<double>
where TSource : MultiDiscreteSignal<TSample>
{
public _DoubleSignal(TSource Source)
{
this._Source = Source;
}
public override int Channels
{
get
{
return this._Source.Channels;
}
}
public override int Size
{
get
{
return this._Source.Size;
}
}
public override double[] Read(int Index)
{
TSample[] samps = this._Source.Read(Index);
double[] dsamps = new double[samps.Length];
for (int t = 0; t < dsamps.Length; t++)
{
dsamps[t] = samps[t].Value;
}
return dsamps;
}
private TSource _Source;
}
}
/// <summary>
/// Represents a single sample of audio.
/// </summary>
public interface IAudioSample
{
/// <summary>
/// Get the double (most precise) value of this sample, from -1.0 to 1.0.
/// </summary>
double Value { get; }
}
}