forked from sipsorcery-org/sipsorcery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioScopeOpenGL.cs
More file actions
193 lines (158 loc) · 7.61 KB
/
AudioScopeOpenGL.cs
File metadata and controls
193 lines (158 loc) · 7.61 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//-----------------------------------------------------------------------------
// Filename: AudioScopeOpenGL.cs
//
// Description: Initialise the OpenGL portion of the Audio Scope demo.
// Author(s):
// Aaron Clauson (aaron@sipsorcery.com)
//
// History:
// 29 Feb 2020 Aaron Clauson Created, Dublin, Ireland.
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
using System;
using System.IO;
using SharpGL;
using SharpGL.Shaders;
using SharpGL.VertexBuffers;
namespace AudioScope
{
public class AudioScopeOpenGL
{
private const string VERTEX_SHADER_PATH = "shaders/line/line.vert";
private const string FRAGMENT_SHADER_PATH = "shaders/line/line.frag";
private const string GEOMETRY_SHADER_PATH = "shaders/line/line.geom";
private const string CLEAR_VERTEX_SHADER_PATH = "shaders/clear/clear.vert";
private const string CLEAR_FRAGMENT_SHADER_PATH = "shaders/clear/clear.frag";
/// <summary>
/// Length of each vector element passed to main GL program.
/// - X coordinate,
/// - Y coordinate,
/// - Angle,
/// - Output.
/// </summary>
private const int MAIN_DATA_STRIDE = 4;
/// <summary>
/// Length of each vector element passed to main GL program.
/// - X coordinate,
/// - Y coordinate,
/// </summary>
private const int CLEAR_DATA_STRIDE = 2;
private AudioScope _audioScope;
private SharpGL.Shaders.ShaderProgram? _prog;
private SharpGL.Shaders.ShaderProgram? _clearProg;
private float[]? _clearRectangle;
public AudioScopeOpenGL(AudioScope audioScope)
{
_audioScope = audioScope;
}
public void Initialise(OpenGL gl)
{
// Load the main program.
string? fragmentShaderCode = null;
using (StreamReader sr = new StreamReader(FRAGMENT_SHADER_PATH))
{
fragmentShaderCode = sr.ReadToEnd();
}
string? vertexShaderCode = null;
using (StreamReader sr = new StreamReader(VERTEX_SHADER_PATH))
{
vertexShaderCode = sr.ReadToEnd();
}
_prog = new SharpGL.Shaders.ShaderProgram();
_prog.Create(gl, vertexShaderCode, fragmentShaderCode, null);
string? geometryShaderCode = null;
if (File.Exists(GEOMETRY_SHADER_PATH))
{
using (StreamReader sr = new StreamReader(GEOMETRY_SHADER_PATH))
{
geometryShaderCode = sr.ReadToEnd();
}
Shader geometryShader = new Shader();
geometryShader.Create(gl, OpenGL.GL_GEOMETRY_SHADER, geometryShaderCode);
gl.AttachShader(_prog.ShaderProgramObject, geometryShader.ShaderObject);
}
gl.LinkProgram(_prog.ShaderProgramObject);
// Now that we've compiled and linked the shader, check it's link status.If it's not linked properly, we're
// going to throw an exception.
if (_prog.GetLinkStatus(gl) == false)
{
throw new SharpGL.Shaders.ShaderCompilationException($"Failed to link shader program with ID {_prog.ShaderProgramObject}.", _prog.GetInfoLog(gl));
}
// Load clear program.
string? clearFragShaderCode = null;
using (StreamReader sr = new StreamReader(CLEAR_FRAGMENT_SHADER_PATH))
{
clearFragShaderCode = sr.ReadToEnd();
}
string? clearVertexShaderCode = null;
using (StreamReader sr = new StreamReader(CLEAR_VERTEX_SHADER_PATH))
{
clearVertexShaderCode = sr.ReadToEnd();
}
_clearProg = new SharpGL.Shaders.ShaderProgram();
_clearProg.Create(gl, clearVertexShaderCode, clearFragShaderCode, null);
gl.LinkProgram(_clearProg.ShaderProgramObject);
// Now that we've compiled and linked the shader, check it's link status. If it's not linked properly, we're
// going to throw an exception.
if (_clearProg.GetLinkStatus(gl) == false)
{
throw new SharpGL.Shaders.ShaderCompilationException($"Failed to link the clear shader program with ID {_clearProg.ShaderProgramObject}.", _clearProg.GetInfoLog(gl));
}
_clearRectangle = new float[] { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f };
}
public void Draw(OpenGL gl, int width, int height)
{
// Uncomment lne below to change the background color.
//gl.ClearColor(1.0f, 0.0f, 1.0f, 1.0f);
gl.Viewport(0, 0, width, height);
gl.Ortho2D(0, width, 0, height);
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
if (_prog == null)
{
throw new ApplicationException("The main program must be assigned for the audio scope to work properly.");
}
int windowID = gl.GetUniformLocation(_prog.ShaderProgramObject, "window");
int nID = gl.GetUniformLocation(_prog.ShaderProgramObject, "n");
int baseHueID = gl.GetUniformLocation(_prog.ShaderProgramObject, "base_hue");
int colorizeID = gl.GetUniformLocation(_prog.ShaderProgramObject, "colorize");
int thicknessID = gl.GetUniformLocation(_prog.ShaderProgramObject, "thickness");
int minThicknessID = gl.GetUniformLocation(_prog.ShaderProgramObject, "min_thickness");
int thinningID = gl.GetUniformLocation(_prog.ShaderProgramObject, "thinning");
int decayID = gl.GetUniformLocation(_prog.ShaderProgramObject, "decay");
int desaturationID = gl.GetUniformLocation(_prog.ShaderProgramObject, "desaturation");
gl.Uniform2(windowID, (float)width, (float)height);
gl.Uniform1(nID, 5);
gl.Uniform1(thicknessID, 5.0f);
gl.Uniform1(minThicknessID, 1.5f);
gl.Uniform1(thinningID, 0.05f);
gl.Uniform1(baseHueID, 0.0f);
gl.Uniform1(colorizeID, 1);
gl.Uniform1(decayID, 0.3f);
gl.Uniform1(desaturationID, 0.1f);
// Run the clear program.
if (_clearProg == null)
{
throw new ApplicationException("The clear program must be assigned for teh audio scope to work properly.");
}
gl.UseProgram(_clearProg.ShaderProgramObject);
VertexBuffer clearVertexBuffer = new VertexBuffer();
clearVertexBuffer.Create(gl);
clearVertexBuffer.Bind(gl);
clearVertexBuffer.SetData(gl, 0, _clearRectangle, false, CLEAR_DATA_STRIDE);
// Attempt to get an available audio sample.
var data = _audioScope.GetSample();
if (data != null)
{
// Run the main program.
gl.UseProgram(_prog.ShaderProgramObject);
VertexBuffer vertexBuffer = new VertexBuffer();
vertexBuffer.Create(gl);
vertexBuffer.Bind(gl);
vertexBuffer.SetData(gl, 0, data, false, MAIN_DATA_STRIDE);
gl.DrawArrays(OpenGL.GL_LINE_STRIP_ADJACENCY, 0, data.Length);
}
}
}
}