MoonWorks-docs/content/Audio/_index.md

77 lines
3.1 KiB
Markdown
Raw Normal View History

2021-01-25 05:09:37 +00:00
---
title: "Audio"
date: 2021-01-24T20:15:28-08:00
2021-01-25 05:17:02 +00:00
weight: 4
2021-01-25 05:09:37 +00:00
---
MoonWorks.Audio is implemented using the [FAudio](https://github.com/FNA-XNA/FAudio) library. The main entry point for audio-related functions is `AudioDevice`. A reference to this class is automatically created and can be retrieved from your `Game` subclass.
2022-04-07 21:21:40 +00:00
There are two main kinds of audio content: static and streaming. MoonWorks supports WAV and Ogg Vorbis files by default, but you can support whatever kind of audio file you want by providing a custom decoder of your choice.
2021-01-25 05:09:37 +00:00
### Static Audio
Static audio is loaded completely into memory, and multiple "instances" of the audio can be created and played. This kind of audio is ideal for short sound effects that may be played simultaneously.
The trade-off is that static audio takes a lot more memory than streaming audio, since the entire sound must be stored in memory in order to be played. This kind of audio is not a good choice for things like long music tracks.
To load and play a static sound, you would do something like this:
```cs
2022-04-07 21:21:40 +00:00
var jumpSoundEffect = StaticSound.LoadWav(AudioDevice, "jump.wav");
2021-01-25 05:09:37 +00:00
var jumpSoundEffectInstance = jumpSoundEffect.CreateInstance();
jumpSoundEffectInstance.Play();
```
2022-04-07 21:21:40 +00:00
Static audio can be loaded from a WAV or Ogg Vorbis file.
2021-01-25 05:09:37 +00:00
### Streaming Audio
Streaming audio is streamed from file storage over its duration, and only a small portion of the audio is stored in memory while it is played. This kind of audio is ideal for recorded dialogue and music.
2022-04-07 21:21:40 +00:00
The trade-off here is that streaming audio needs to be read continuously and decoded from memory, which may be slow. If you stream too much audio at once you may notice audio stuttering.
2021-01-25 05:09:37 +00:00
To load and play streaming sound, you would do something like this:
```cs
var music = StreamingSoundOgg.Load(AudioDevice, "my_music.ogg");
music.Play();
```
2022-04-07 21:21:40 +00:00
By default MoonWorks only provides Ogg Vorbis streaming, if you want to stream other kinds of audio you will need to hook up a decoder function.
2021-01-25 05:09:37 +00:00
### Effects
MoonWorks.Audio sound playback can be modified with several effects, such as panning, reverb, pass filters, and more. To apply these effects, simply set the relevant properties on the sound instance.
```cs
jumpSoundEffectInstance.Reverb = 1.5f;
jumpSoundEffectInstance.Pitch = -0.25f;
jumpSoundEffectInstance.Pan = -0.5f;
jumpSoundEffectInstance.Play();
```
2021-03-17 19:33:37 +00:00
### 3D Audio
MoonWorks has support for 3-dimensional audio.
To use it, you must create an AudioListener, and any sounds that are emitted in 3-dimensional space should use an AudioEmitter.
Emitters and listeners use position, orientation, and velocity to determine audio playback.
Then you can call `Apply3D` on a sound effect instance and provide the emitter and listener.
```cs
var audioEmitter = new AudioEmitter(AudioDevice);
var audioListener = new AudioListener(AudioDevice);
audioEmitter.Position = thingPosition;
audioEmitter.Forward = thingForward;
audioEmitter.Up = thingUp;
2021-03-18 04:07:21 +00:00
audioEmitter.Velocity = thingVelocity;
2021-03-17 19:33:37 +00:00
audioListener.Position = cameraPosition;
audioListener.Forward = cameraForward;
audioListener.Up = cameraUp;
2021-03-18 04:07:21 +00:00
audioListener.Velocity = cameraVelocity;
2021-03-17 19:33:37 +00:00
thingSound.Apply3D(audioListener, audioEmitter);
```