|
| 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 | +} |
0 commit comments