Skip to content

Commit 639a234

Browse files
authored
Add OpenAL Wave Player Demo (#474)
1 parent 40fa176 commit 639a234

File tree

118 files changed

+409
-198
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+409
-198
lines changed

Silk.NET.sln

Lines changed: 209 additions & 185 deletions
Large diffs are not rendered by default.

examples/Directory.Build.props renamed to examples/CSharp/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project>
1+
<Project>
22
<PropertyGroup>
33
<AssemblyName>Tutorial</AssemblyName>
44
<LangVersion>9</LangVersion>
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
using System;
2+
using System.Buffers.Binary;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Runtime.CompilerServices;
6+
using System.Text;
7+
using Silk.NET.OpenAL;
8+
9+
namespace WavePlayer
10+
{
11+
class Program
12+
{
13+
static unsafe void Main(string[] args)
14+
{
15+
if (args.Length != 1)
16+
{
17+
Console.WriteLine("Exactly one argument should be given: the path to the .wav file that should be played.");
18+
return;
19+
}
20+
var filePath = args[0];
21+
ReadOnlySpan<byte> file = File.ReadAllBytes(filePath);
22+
int index = 0;
23+
if (file[index++] != 'R' || file[index++] != 'I' || file[index++] != 'F' || file[index++] != 'F')
24+
{
25+
Console.WriteLine("Given file is not in RIFF format");
26+
return;
27+
}
28+
29+
var chunkSize = BinaryPrimitives.ReadInt32LittleEndian(file.Slice(index, 4));
30+
index += 4;
31+
32+
if (file[index++] != 'W' || file[index++] != 'A' || file[index++] != 'V' || file[index++] != 'E')
33+
{
34+
Console.WriteLine("Given file is not in WAVE format");
35+
return;
36+
}
37+
38+
short numChannels = -1;
39+
int sampleRate = -1;
40+
int byteRate = -1;
41+
short blockAlign = -1;
42+
short bitsPerSample = -1;
43+
BufferFormat format = 0;
44+
45+
var alc = ALContext.GetApi();
46+
var al = AL.GetApi();
47+
var device = alc.OpenDevice("");
48+
if (device == null)
49+
{
50+
Console.WriteLine("Could not create device");
51+
return;
52+
}
53+
54+
var context = alc.CreateContext(device, null);
55+
alc.MakeContextCurrent(context);
56+
57+
al.GetError();
58+
59+
var source = al.GenSource();
60+
var buffer = al.GenBuffer();
61+
al.SetSourceProperty(source, SourceBoolean.Looping, true);
62+
63+
64+
while (index + 4 < file.Length)
65+
{
66+
var identifier = "" + (char) file[index++] + (char) file[index++] + (char) file[index++] + (char) file[index++];
67+
var size = BinaryPrimitives.ReadInt32LittleEndian(file.Slice(index, 4));
68+
index += 4;
69+
if (identifier == "fmt ")
70+
{
71+
if (size != 16)
72+
{
73+
Console.WriteLine($"Unknown Audio Format with subchunk1 size {size}");
74+
}
75+
else
76+
{
77+
var audioFormat = BinaryPrimitives.ReadInt16LittleEndian(file.Slice(index, 2));
78+
index += 2;
79+
if (audioFormat != 1)
80+
{
81+
Console.WriteLine($"Unknown Audio Format with ID {audioFormat}");
82+
}
83+
else
84+
{
85+
numChannels = BinaryPrimitives.ReadInt16LittleEndian(file.Slice(index, 2));
86+
index += 2;
87+
sampleRate = BinaryPrimitives.ReadInt32LittleEndian(file.Slice(index, 4));
88+
index += 4;
89+
byteRate = BinaryPrimitives.ReadInt32LittleEndian(file.Slice(index, 4));
90+
index += 4;
91+
blockAlign = BinaryPrimitives.ReadInt16LittleEndian(file.Slice(index, 2));
92+
index += 2;
93+
bitsPerSample = BinaryPrimitives.ReadInt16LittleEndian(file.Slice(index, 2));
94+
index += 2;
95+
96+
if (numChannels == 1)
97+
{
98+
if (bitsPerSample == 8)
99+
format = BufferFormat.Mono8;
100+
else if (bitsPerSample == 16)
101+
format = BufferFormat.Mono16;
102+
else
103+
{
104+
Console.WriteLine($"Can't Play mono {bitsPerSample} sound.");
105+
}
106+
}
107+
else if (numChannels == 2)
108+
{
109+
if (bitsPerSample == 8)
110+
format = BufferFormat.Stereo8;
111+
else if (bitsPerSample == 16)
112+
format = BufferFormat.Stereo16;
113+
else
114+
{
115+
Console.WriteLine($"Can't Play stereo {bitsPerSample} sound.");
116+
}
117+
}
118+
else
119+
{
120+
Console.WriteLine($"Can't play audio with {numChannels} sound");
121+
}
122+
}
123+
}
124+
}
125+
else if (identifier == "data")
126+
{
127+
var data = file.Slice(44, size);
128+
index += size;
129+
130+
fixed(byte* pData = data)
131+
al.BufferData(buffer, format, pData, size, sampleRate);
132+
Console.WriteLine($"Read {size} bytes Data");
133+
}
134+
else if (identifier == "JUNK")
135+
{
136+
// this exists to align things
137+
index += size;
138+
}
139+
else if (identifier == "iXML")
140+
{
141+
var v = file.Slice(index, size);
142+
var str = Encoding.ASCII.GetString(v);
143+
Console.WriteLine($"iXML Chunk: {str}");
144+
index += size;
145+
}
146+
else
147+
{
148+
Console.WriteLine($"Unknown Section: {identifier}");
149+
index += size;
150+
}
151+
}
152+
153+
Console.WriteLine
154+
(
155+
$"Success. Detected RIFF-WAVE audio file, PCM encoding. {numChannels} Channels, {sampleRate} Sample Rate, {byteRate} Byte Rate, {blockAlign} Block Align, {bitsPerSample} Bits per Sample"
156+
);
157+
158+
al.SetSourceProperty(source, SourceInteger.Buffer, buffer);
159+
al.SourcePlay(source);
160+
161+
Console.WriteLine("Press Enter to Exit...");
162+
Console.ReadLine();
163+
164+
al.SourceStop(source);
165+
166+
al.DeleteSource(source);
167+
al.DeleteBuffer(buffer);
168+
alc.DestroyContext(context);
169+
alc.CloseDevice(device);
170+
al.Dispose();
171+
alc.Dispose();
172+
}
173+
}
174+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\..\..\src\OpenAL\Silk.NET.OpenAL\Silk.NET.OpenAL.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

examples/CSharp/Tutorial 1.1 - Hello Window/Tutorial 1.1 - Hello Window.csproj renamed to examples/CSharp/OpenGL Tutorials/Tutorial 1.1 - Hello Window/Tutorial 1.1 - Hello Window.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<ProjectReference Include="..\..\..\src\Core\Silk.NET\Silk.NET.csproj" />
10+
<ProjectReference Include="..\..\..\..\src\Core\Silk.NET\Silk.NET.csproj" />
1111
</ItemGroup>
1212

1313
</Project>

examples/CSharp/Tutorial 1.2 - Hello quad/Tutorial 1.2 - Hello quad.csproj renamed to examples/CSharp/OpenGL Tutorials/Tutorial 1.2 - Hello quad/Tutorial 1.2 - Hello quad.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<ProjectReference Include="..\..\..\src\Core\Silk.NET\Silk.NET.csproj" />
11+
<ProjectReference Include="..\..\..\..\src\Core\Silk.NET\Silk.NET.csproj" />
1212
</ItemGroup>
1313

1414
</Project>

0 commit comments

Comments
 (0)