Repository navigation
Audio setup with WAV file loading #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
d704a54
Audio setup with WAV file loading
nrbyte 1bb6680
Log errors too
nrbyte 083702b
Ran formatter
nrbyte b705c2b
Log error
nrbyte 12cbba6
Fix syntax error
nrbyte ed72e69
Audio setup with WAV file loading
nrbyte 749cd27
Log errors too
nrbyte 2acd571
Ran formatter
nrbyte 8da3490
Log error
nrbyte 5fa95a2
Fix syntax error
nrbyte ad5e86f
Convert AudioSource into a Component
nrbyte a2ec1ce
Refactor audio around ECS + ResourceManager
nrbyte 477c4c6
Don't make an instance of AudioManager
nrbyte 973c54b
Clean up a working Audio subsystem
nrbyte 3ef0220
Fix formatting issues
nrbyte 995e1f5
Merge branch 'main' of https://github.com/Thrill12/rasdaq into audio
Thrill12 93cce1d
Merge branch 'audio' of https://github.com/Thrill12/rasdaq into audio
Thrill12 53b96e7
update
Thrill12 86da701
audiosource play button
Thrill12 1acf5d9
stop and internalize funcs
Thrill12 1155644
add stop to example
Thrill12 40d0ca2
format
Thrill12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using rasdaq; | ||
| using rasdaq.Audio; | ||
| using rasdaq.Core.ECS; | ||
| using rasdaq.Graphics; | ||
| using rasdaq.Resources; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| using OpenTK.Audio.OpenAL; | ||
| using rasdaq.Logging; | ||
|
|
||
| namespace rasdaq.Audio; | ||
|
|
||
| public class Audio : IDisposable | ||
| { | ||
| internal int Handle; | ||
|
|
||
| public Audio() | ||
| { | ||
| Handle = AL.GenBuffer(); | ||
| // Check for errors | ||
| ALError error = AL.GetError(); | ||
| if (error != ALError.NoError) | ||
| { | ||
| string err = $"OpenAL error while trying to create audio buffer: {error}"; | ||
|
|
||
| Log.Error(err); | ||
| throw new Exception(err); | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| AL.DeleteBuffer(Handle); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using OpenTK.Audio.OpenAL; | ||
| using rasdaq.Logging; | ||
| using rasdaq.Resources; | ||
|
|
||
| namespace rasdaq.Audio; | ||
|
|
||
| public class AudioLoader : IResourceLoader | ||
| { | ||
| public object Load(string path) | ||
| { | ||
| return AudioManager.Instance.LoadAudio(path); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| using OpenTK.Audio.OpenAL; | ||
| using rasdaq.Logging; | ||
|
|
||
| namespace rasdaq.Audio; | ||
|
|
||
| public class AudioManager | ||
| { | ||
| public static AudioManager Instance | ||
| { | ||
| get | ||
| { | ||
| if (_instance == null) | ||
| { | ||
| _instance = new(); | ||
| } | ||
|
|
||
| return _instance; | ||
| } | ||
| } | ||
|
|
||
| private static AudioManager _instance; | ||
|
|
||
| private static void CheckALCError(ALDevice device) | ||
| { | ||
| AlcError error = ALC.GetError(device); | ||
| if (error != AlcError.NoError) | ||
| { | ||
| string err = $"OpenAL error: {error}"; | ||
| Log.Error(err); | ||
| throw new Exception(err); | ||
| } | ||
| } | ||
|
|
||
| public static void CheckALError() | ||
| { | ||
| ALError error = AL.GetError(); | ||
| if (error != ALError.NoError) | ||
| { | ||
| string err = $"OpenAL error: {error}"; | ||
| Log.Error(err); | ||
| throw new Exception(err); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initialize Audio for the Application, using the default audio device. | ||
| /// </summary> | ||
| /// <exception cref="Exception"></exception> | ||
| public AudioManager() | ||
| { | ||
| // Open the default audio device. | ||
| ALDevice device = ALC.OpenDevice(null); | ||
| if (device == ALDevice.Null) | ||
| { | ||
| string err = "Failed to open the default audio device."; | ||
| Log.Error(err); | ||
| throw new Exception(err); | ||
| } | ||
|
|
||
| // Create a context for the device. | ||
| ALContext context = ALC.CreateContext( | ||
| device, | ||
| new ALContextAttributes(null, null, null, null, null) | ||
| ); | ||
| if (context == ALContext.Null) | ||
| { | ||
| ALC.CloseDevice(device); | ||
| string err = "Failed to create an OpenAL context."; | ||
| Log.Error(err); | ||
| throw new Exception(err); | ||
| } | ||
|
|
||
| // Check if we have any errors. | ||
| AlcError error = ALC.GetError(device); | ||
| CheckALCError(device); | ||
| // If no errors, make the context current. | ||
| ALC.MakeContextCurrent(context); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Load a WAV file that contains PCM data only. | ||
| /// </summary> | ||
| /// <param name="path">Path to WAV file</param> | ||
| /// <returns>A integer handle referring to the loaded audio</returns> | ||
| /// <exception cref="Exception"></exception> | ||
| internal Audio LoadAudio(string path) | ||
| { | ||
| // Create an OpenAL buffer for the data to go in. | ||
| Audio audio = new Audio(); | ||
|
|
||
| // Load the WAV file | ||
| WAVLoader.WAVData wavData = WAVLoader.LoadWav(path); | ||
|
|
||
| // Discern the format of the audio data based on number of channels and bits per sample. | ||
| ALFormat format; | ||
| if (wavData.Format.NumChannels == 1) | ||
| { | ||
| if (wavData.Format.BitsPerSample == 8) | ||
| { | ||
| format = ALFormat.Mono8; | ||
| } | ||
| else if (wavData.Format.BitsPerSample == 16) | ||
| { | ||
| format = ALFormat.Mono16; | ||
| } | ||
| else | ||
| { | ||
| string err = $"Unsupported bits per sample: {wavData.Format.BitsPerSample}"; | ||
| Log.Error(err); | ||
| throw new Exception(err); | ||
| } | ||
| } | ||
| else if (wavData.Format.NumChannels == 2) | ||
| { | ||
| if (wavData.Format.BitsPerSample == 8) | ||
| { | ||
| format = ALFormat.Stereo8; | ||
| } | ||
| else if (wavData.Format.BitsPerSample == 16) | ||
| { | ||
| format = ALFormat.Stereo16; | ||
| } | ||
| else | ||
| { | ||
| string err = $"Unsupported bits per sample: {wavData.Format.BitsPerSample}"; | ||
| Log.Error(err); | ||
| throw new Exception(err); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| string err = $"Unsupported number of channels: {wavData.Format.NumChannels}"; | ||
| Log.Error(err); | ||
| throw new Exception(err); | ||
| } | ||
|
|
||
| // Load the audio data into the OpenAL buffer. | ||
| AL.BufferData(audio.Handle, format, wavData.Data.Data, (int)wavData.Format.SampleRate); | ||
|
|
||
| // Check if we have any errors from trying to load the audio data. | ||
| CheckALError(); | ||
|
|
||
| return audio; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Add a loaded audio file to an audio source, so that it can be played. | ||
| /// </summary> | ||
| /// <param name="audio">Audio file</param> | ||
| /// <param name="audioSource">Audio source</param> | ||
| internal void AttachAudioToSource(Audio audio, AudioSource audioSource) | ||
| { | ||
| AL.Source(audioSource.Handle, ALSourcei.Buffer, audio.Handle); | ||
| CheckALError(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Play back an audio source that has audio attached to it. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| internal void PlaySource(AudioSource audioSource) | ||
| { | ||
| AL.SourcePlay(audioSource.Handle); | ||
| CheckALError(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Stop playback of an audio source. | ||
| /// </summary> | ||
| /// <param name="source"></param> | ||
| internal void StopSource(AudioSource audioSource) | ||
| { | ||
| AL.SourceStop(audioSource.Handle); | ||
| CheckALError(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.