![]() |
Ryle Radio 1.1.2
An open-source "radio" system for Unity, allowing tracks, tuning, broadcasters, and more!
|
The main scene component for a radio that plays it through an AudioSource. More...
Public Types | |
| enum | MultiplePlayersSelector { Youngest , Oldest , Random } |
| The method by which a RadioTrackPlayer is chosen from this output. Really only matters when you're playing the same track repeatedly causing overlaps. More... | |
Public Member Functions | |
| override void | Init () |
| Initializes the output itself, and creates all required every required RadioTrackPlayer. | |
| RadioTrackPlayer | PlayOneShot (string _id) |
| Plays a track as a one-shot. A one-shot destroys itself when its track ends. | |
| RadioTrackPlayer | PlayLoop (string _id) |
| Plays a track as a loop. A loop restarts when the track ends, then continues to play. | |
| bool | TryGetPlayer (string _trackID, out RadioTrackPlayer _player, bool _createNew=false, MultiplePlayersSelector _multiplePlayerSelector=MultiplePlayersSelector.Youngest) |
| Gets an active RadioTrackPlayer from this output. | |
| Public Member Functions inherited from RyleRadio.Components.Base.RadioComponent | |
| void | Init () |
| Initialises this component. | |
Protected Member Functions | |
| virtual void | Update () |
| Updates cachedPos. | |
| void | LocalInit () |
| Initializes the RadioData- this needs to be separated from Init() as it would be recursive otherwise. | |
| void | PlayerCreation (RadioTrackPlayer _player) |
| Sets up and stores a new RadioTrackPlayer and alerts any RadioObserver of its creation. | |
| virtual void | OnAudioFilterRead (float[] _data, int _channels) |
| Assigns a set of samples from the radio to play from the AudioSource- this preserves the settings on the Source, e.g: volume, 3D. This is the main driving method for the radio's playback. | |
| void | GetOutput (ref float[] _data, int _channels) |
| Gets a set of samples from the radio, and push it along to the next samples- that is, get some samples from the radio, and move it on. | |
| void | FillCacheClip () |
| Generate and store the next chunk of audio that will be played on WebGL. This is called just before the next chunk is generated. | |
Protected Attributes | |
| float | tune |
| The current tune value of this output- akin to the frequency of a real radio. Controls what tracks can be heard through tune power. Never modify this directly except for in the inspector, use Tune instead. | |
| List< RadioTrackPlayer > | players = new() |
| The players used by this output. | |
| Vector3 | cachedPos |
| The position of this object as of the last frame update. We can't access transform.position from the audio thread, so we cache it here. | |
| Protected Attributes inherited from RyleRadio.Components.Base.RadioComponent | |
| RadioData | data |
| The RadioData (aka just radio) that this component is linked to. | |
Properties | |
| List< RadioObserver > | Observers = new() [get, private set] |
| Every RadioObserver associated with this output. | |
| Action< float > | OnTune = new(_ => { }) [get, set] |
| Event called whenever Tune is changed. | |
| Action | OnInit = new(() => { }) [get, set] |
| Event called when this output is finished initializing- mainly useful for WebGL when we use AwaitLoadingThenInit. | |
| float | Tune [get, set] |
| The tune clamped to the full range. | |
| float | Tune01 [get] |
| Tune scaled to [0 - 1], useful for UI | |
| float | DisplayTune [get] |
| Tune with limited decimal points- looks better when displayed, more like an actual radio | |
| bool | Loaded [get] |
| Public accessor for loaded. | |
| Properties inherited from RyleRadio.Components.Base.RadioComponent | |
| RadioData | Data [get] |
| Read-only accessor for data. | |
Private Member Functions | |
| void | ExecOnTune () |
| Called when tune is modified in the inspector. | |
| void | FixedUpdate () |
| Updates audio playback in WebGL, does nothing on other platforms. | |
| void | OnDestroy () |
| IEnumerator | AwaitLoadingThenInit () |
| Waits for all AudioClips used in this radio to be loaded before initializing the radio. This is used as WebGL will cause errors if a clip is used before it's fully loaded. | |
| void | StartPlayers () |
| Creates every RadioTrackPlayer that this output needs for playback. | |
Private Attributes | |
| float | baseSampleRate |
| The normal sample rate of this output, applied to each RadioTrackPlayer. | |
| Action | playEvents = () => { } |
| Called at the end of every audio cycle so that we don't interrupt threads when manipulating RadioTrackPlayers. | |
| AudioClip | cacheClipSecond |
| The AudioClip used internally to emulate audio streaming on WebGL- half of this is played at a time, and the other half is generated in advance. | |
| AudioClip | cacheClipFirst |
| int | totalCacheChunkLength = 0 |
| The total number of samples in each half of the cache clip. | |
| float | cacheClipChunkDuration = .04f |
| The size of half the cache clip used for WebGL playback- this is the amount of audio the cache clip stores in advance during playback before switching to the other half of the clip. | |
| AudioSource | source |
| The AudioSource attached to this Output. This is assigned automatically. | |
| bool | isInFirstHalf = false |
| Whether or not the cache clip playback is in the second half of the clip. | |
| bool | loaded = false |
| Whether or not the clips in this radio have been loaded. | |
| float[] | cacheClipSamples |
The main scene component for a radio that plays it through an AudioSource.
See RadioTrackPlayer as well for more info on how playback works
Definition at line 21 of file RadioOutput.cs.
The method by which a RadioTrackPlayer is chosen from this output. Really only matters when you're playing the same track repeatedly causing overlaps.
| Enumerator | |
|---|---|
| Youngest | Selects the youngest player. |
| Oldest | Selects the oldest player. |
| Random | Selects a random player (probably useless but funny to have) |
Definition at line 26 of file RadioOutput.cs.
|
private |
Waits for all AudioClips used in this radio to be loaded before initializing the radio. This is used as WebGL will cause errors if a clip is used before it's fully loaded.
Definition at line 221 of file RadioOutput.cs.
|
private |
Called when tune is modified in the inspector.
Definition at line 151 of file RadioOutput.cs.
|
protected |
Generate and store the next chunk of audio that will be played on WebGL. This is called just before the next chunk is generated.
We need to use this method as WebGL doesn't support audio streaming. Because this system also requires audio to be modified in realtime, we can't simply generate a couple seconds of audio and just play that- we need to modify that audio each frame depending on what the player does- e.g: tuning The methodology for this system is as follows:
This is obviously not perfect, and the main issue we face here is a constant popping sound when switching back and forth between clips- unfortunately, as it stands, this is a required tradeoff due to WebGL's audio limitations. If there's a smoother way to get this functionality working, please do give it a try or let me know :)
Definition at line 595 of file RadioOutput.cs.
Referenced by FixedUpdate(), and Init().
|
private |
Updates audio playback in WebGL, does nothing on other platforms.
See: FillCacheClip
Definition at line 172 of file RadioOutput.cs.
|
protected |
Gets a set of samples from the radio, and push it along to the next samples- that is, get some samples from the radio, and move it on.
| _data | An array (usually empty) that the samples will be written to. The length of the array is the number of samples used recorded |
| _channels | The number of channels being played- usually this should be at one, I'm not confident in its functionality otherwise |
Definition at line 537 of file RadioOutput.cs.
Referenced by FillCacheClip(), and OnAudioFilterRead().
| override void RyleRadio.Components.RadioOutput.Init | ( | ) |
Initializes the output itself, and creates all required every required RadioTrackPlayer.
Definition at line 277 of file RadioOutput.cs.
|
protected |
Initializes the RadioData- this needs to be separated from Init() as it would be recursive otherwise.
Definition at line 266 of file RadioOutput.cs.
Referenced by AwaitLoadingThenInit().
|
protectedvirtual |
Assigns a set of samples from the radio to play from the AudioSource- this preserves the settings on the Source, e.g: volume, 3D. This is the main driving method for the radio's playback.
The method itself appears to have been initially introduced so devs could create custom audio filters, but it just so happens we can use it for direct output of samples too!
WebGL can't use this method, as it doesn't support audio streaming. I've implemented a "streaming emulation" system creating and storing clips repeatedly, but it's a bit rough around the edges and causes some popping artefacts. Please try to avoid WebGL with this package when possible, or use a pop removal filter at runtime. See FillCacheClip for a bit more info :)
See: GetOutput
| _data | Whatever other audio is playing from the AudioSource- preferably nothing |
| _channels | The number of channels the AudioSource is using- the radio itself is limited to one channel, but still outputs as two- they'll just be identical. |
Definition at line 525 of file RadioOutput.cs.
|
private |
Definition at line 205 of file RadioOutput.cs.
|
protected |
Sets up and stores a new RadioTrackPlayer and alerts any RadioObserver of its creation.
| _player | The new player to set up |
Definition at line 321 of file RadioOutput.cs.
Referenced by PlayLoop(), PlayOneShot(), and StartPlayers().
| RadioTrackPlayer RyleRadio.Components.RadioOutput.PlayLoop | ( | string | _id | ) |
Plays a track as a loop. A loop restarts when the track ends, then continues to play.
| _id |
Definition at line 419 of file RadioOutput.cs.
| RadioTrackPlayer RyleRadio.Components.RadioOutput.PlayOneShot | ( | string | _id | ) |
Plays a track as a one-shot. A one-shot destroys itself when its track ends.
| _id |
Definition at line 384 of file RadioOutput.cs.
Referenced by TryGetPlayer().
|
private |
Creates every RadioTrackPlayer that this output needs for playback.
Definition at line 362 of file RadioOutput.cs.
Referenced by Init().
| bool RyleRadio.Components.RadioOutput.TryGetPlayer | ( | string | _trackID, |
| out RadioTrackPlayer | _player, | ||
| bool | _createNew = false, | ||
| MultiplePlayersSelector | _multiplePlayerSelector = MultiplePlayersSelector::Youngest ) |
Gets an active RadioTrackPlayer from this output.
| _trackID | The ID of the track used by the player |
| _player | Output parameter containing the found player |
| _createNew | Whether or not a new player should be created if one can't be found. Players created this way are always one-shots |
| _multiplePlayerSelector | How a player is selected when multiple are present for the same track |
Definition at line 452 of file RadioOutput.cs.
|
protectedvirtual |
Updates cachedPos.
Definition at line 160 of file RadioOutput.cs.
|
private |
The normal sample rate of this output, applied to each RadioTrackPlayer.
Definition at line 53 of file RadioOutput.cs.
Referenced by Init(), PlayLoop(), PlayOneShot(), and StartPlayers().
|
private |
The size of half the cache clip used for WebGL playback- this is the amount of audio the cache clip stores in advance during playback before switching to the other half of the clip.
Definition at line 75 of file RadioOutput.cs.
Referenced by Init().
|
private |
Definition at line 65 of file RadioOutput.cs.
|
private |
Definition at line 92 of file RadioOutput.cs.
|
private |
The AudioClip used internally to emulate audio streaming on WebGL- half of this is played at a time, and the other half is generated in advance.
Definition at line 64 of file RadioOutput.cs.
Referenced by FillCacheClip(), and Init().
|
protected |
The position of this object as of the last frame update. We can't access transform.position from the audio thread, so we cache it here.
Definition at line 48 of file RadioOutput.cs.
Referenced by GetOutput(), and Update().
|
private |
Whether or not the cache clip playback is in the second half of the clip.
Definition at line 85 of file RadioOutput.cs.
Referenced by FillCacheClip(), and FixedUpdate().
|
private |
Whether or not the clips in this radio have been loaded.
Definition at line 90 of file RadioOutput.cs.
Referenced by AwaitLoadingThenInit(), and FixedUpdate().
|
protected |
The players used by this output.
Definition at line 43 of file RadioOutput.cs.
Referenced by GetOutput(), PlayerCreation(), PlayOneShot(), and TryGetPlayer().
|
private |
Called at the end of every audio cycle so that we don't interrupt threads when manipulating RadioTrackPlayers.
Definition at line 58 of file RadioOutput.cs.
Referenced by GetOutput(), PlayLoop(), and PlayOneShot().
|
private |
The AudioSource attached to this Output. This is assigned automatically.
Definition at line 80 of file RadioOutput.cs.
Referenced by FillCacheClip().
|
private |
The total number of samples in each half of the cache clip.
Definition at line 70 of file RadioOutput.cs.
Referenced by FillCacheClip(), FixedUpdate(), and Init().
|
protected |
The current tune value of this output- akin to the frequency of a real radio. Controls what tracks can be heard through tune power. Never modify this directly except for in the inspector, use Tune instead.
Definition at line 38 of file RadioOutput.cs.
Referenced by ExecOnTune(), and Init().
|
get |
Tune with limited decimal points- looks better when displayed, more like an actual radio
Definition at line 137 of file RadioOutput.cs.
|
get |
Public accessor for loaded.
Definition at line 145 of file RadioOutput.cs.
|
getprivate set |
Every RadioObserver associated with this output.
Definition at line 98 of file RadioOutput.cs.
Referenced by PlayerCreation().
|
getset |
Event called when this output is finished initializing- mainly useful for WebGL when we use AwaitLoadingThenInit.
Definition at line 108 of file RadioOutput.cs.
|
getset |
Event called whenever Tune is changed.
Definition at line 103 of file RadioOutput.cs.
Referenced by ExecOnTune(), and Init().
|
getset |
The tune clamped to the full range.
Definition at line 113 of file RadioOutput.cs.
Referenced by GetOutput().
|
get |
Tune scaled to [0 - 1], useful for UI
Definition at line 129 of file RadioOutput.cs.