-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAdvancedProceduralSoundExample.cs
More file actions
127 lines (108 loc) · 3.4 KB
/
AdvancedProceduralSoundExample.cs
File metadata and controls
127 lines (108 loc) · 3.4 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//A more advanced example of generating a sine wave, applying a tremolo effect to it,
//and have it play in 3D space.
using System;
using MiniAudioEx.Core.StandardAPI;
using MiniAudioEx.DSP.Effects;
using MiniAudioEx.DSP.Generators;
using MiniAudioEx.Native;
namespace MiniAudioExExamples
{
public class AdvancedProceduralSoundExample
{
private AudioApp application;
private AudioSource source;
private AudioListener listener;
private SineGenerator sineGenerator;
private TremoloEffect tremoloEffect;
private double timer;
public AdvancedProceduralSoundExample()
{
application = new AudioApp(44100, 2);
application.Loaded += OnLoaded;
application.Update += OnUpdate;
}
public void Run()
{
application.Run();
}
private void OnLoaded()
{
source = new AudioSource();
source.DopplerFactor = 0.1f;
source.Position = new Vector3f(0, 0, 0);
source.MinDistance = 1.0f;
source.MaxDistance = 200.0f;
source.AttenuationModel = AttenuationModel.Exponential;
//Simply set Spatial to false to disable any 3D effects on the source
source.Spatial = true;
listener = new AudioListener();
listener.Position = new Vector3f(0, 0, 3);
sineGenerator = new SineGenerator(440);
tremoloEffect = new TremoloEffect(8);
source.AddGenerator(sineGenerator);
source.AddEffect(tremoloEffect);
source.Play();
}
private void OnUpdate(float deltaTime)
{
float direction = (float)Math.Sin(timer * 0.5);
source.Position = new Vector3f(20, 0, 0) * direction;
source.Velocity = source.GetCalculatedVelocity();
timer += deltaTime;
}
}
public class TremoloEffect : IAudioEffect
{
private WaveCalculator calculator;
private long tickTimer;
private float frequency;
public TremoloEffect(float frequency)
{
this.frequency = frequency;
tickTimer = 0;
calculator = new WaveCalculator(WaveType.Sine);
}
public void OnProcess(NativeArray<float> framesIn, uint frameCountIn, NativeArray<float> framesOut, ref uint frameCountOut, uint channels)
{
float sample = 0;
float phase = 0;
for (int i = 0; i < framesOut.Length; i += (int)channels)
{
phase = (float)(2 * Math.PI * frequency * tickTimer / AudioContext.SampleRate);
sample = calculator.GetValue(phase);
framesOut[i] = framesIn[i] * sample;
if (channels == 2)
framesOut[i + 1] = framesIn[i + 1] * sample;
tickTimer++;
}
}
public void OnDestroy() { }
}
public class SineGenerator : IAudioGenerator
{
private WaveCalculator calculator;
private long tickTimer;
private float frequency;
public SineGenerator(float frequency)
{
this.frequency = frequency;
tickTimer = 0;
calculator = new WaveCalculator(WaveType.Sine);
}
public void OnGenerate(NativeArray<float> framesOut, ulong frameCount, int channels)
{
float sample = 0;
float phase = 0;
for (int i = 0; i < framesOut.Length; i += channels)
{
phase = (float)(2 * Math.PI * frequency * tickTimer / AudioContext.SampleRate);
sample = calculator.GetValue(phase);
framesOut[i] = sample;
if (channels == 2)
framesOut[i + 1] = sample;
tickTimer++;
}
}
public void OnDestroy() {}
}
}