Ryle Radio 1.0.0
An open-source "radio" system for Unity, allowing tracks, tuning, broadcasters, and more!
Loading...
Searching...
No Matches
StationRadioTrackWrapper.cs
1using NaughtyAttributes;
2using System;
3using System.Linq;
4using UnityEngine;
5
6#if UNITY_EDITOR
7using UnityEditor;
8#endif
9
10namespace RyleRadio.Tracks
11{
12 /// <summary>
13 /// A smaller, separate version of \ref RadioTrackWrapper for use in a \ref StationRadioTrack
14 ///
15 /// This is how child tracks are contained inside stations- very similar to how normal tracks themselves are stored in \ref RadioData
16 ///
17 /// We could probably use an interface in future for both this and RadioTrackWrapper, but I fear we won't ever use that interface again and it would just obfuscate functionality
18 /// If it'd be useful, though, I'm happy to implement
19 /// </summary>
20 [System.Serializable]
22 {
23 /// <summary>
24 /// The ID of this track
25 /// </summary>
26 [SerializeField] private string id;
27
28 /// <summary>
29 /// The additional volume of this track. See \ref RadioTrackWrapper.gain
30 /// </summary>
31 [Range(0, 500)] public float gain = 100;
32
33 /// <summary>
34 /// The amount of time at the start and end of the track for which there's silence. This allows you to spread tracks apart a little bit
35 /// </summary>
36 public Vector2 startAndEndRests = Vector2.zero;
37
38 /// <summary>
39 /// The current eventType of this track as chosen in the editor. Displayed as a dropdown of \ref RadioTrack `DISPLAY_NAME`s
40 /// </summary>
41 [SerializeField, AllowNesting, OnValueChanged("CreateTrackLocal"), Dropdown("TrackNames")]
42 private string trackType = "None";
43
44 /// <summary>
45 /// The track contained in this wrapper.<br>
46 /// Note that this is an \ref IStationTrack and not an \ref IRadioTrack like in the normal \ref RadioTrackWrapper
47 /// Some custom track types might not be usable in a station (e.g a station within a station)- hence we need to separate those that can be in one from those that can't
48 /// </summary>
49 [SerializeReference]
51
52#if !SKIP_IN_DOXYGEN
53 // the ID of this track
54 public string ID => id;
55
56 // the sample rate of this track
57 public float SampleRate => track.SampleRate;
58
59 // the number of samples in this track
60 public int SampleCount
61 {
62 get
63 {
64 // adds the rests to the number of samples in this track
65 return track.SampleCount + (int)((startAndEndRests.x + startAndEndRests.y) * track.SampleRate);
66 }
67 }
68#endif
69
70 /// <summary>
71 /// The gain value scaled down to ones- e.g \ref gain at 200 is \ref Gain at 2
72 /// </summary>
73 public float Gain => gain / 100f;
74
75#if !SKIP_IN_DOXYGEN
76 private static Type[] trackTypes;
77#endif
78 /// <summary>
79 /// A list of each eventType of track that this wrapper can contain- this is anything that inherits from \ref IStationTrack
80 /// <br><br><b>See also: </b> \ref RadioUtils.FindDerivedTypes()
81 /// </summary>
82 private static Type[] TrackTypes
83 {
84 get
85 {
86 // just like RadioTrackWrapper, get all available track types dynamically
87 trackTypes ??= RadioUtils.FindDerivedTypes(typeof(IStationTrack));
88
89 return trackTypes;
90 }
91 }
92
93#if !SKIP_IN_DOXYGEN
94 private static string[] trackNames;
95#endif
96 /// <summary>
97 /// A list of the names of each eventType in \ref TrackTypes
98 /// This is what's displayed in the inspector for the user to choose from
99 /// </summary>
100 private static string[] TrackNames
101 {
102 get
103 {
104 // convert available track types to their class names, as usual
105 trackNames ??= TrackTypes
106 .Select(t => (string)t.GetField("DISPLAY_NAME").GetValue(null))
107 .ToArray();
108
109 return trackNames;
110 }
111 }
112
113#if UNITY_EDITOR
114 /// <summary>
115 /// If this wrapper contains a \ref ClipRadioTrack , this becomes a reference to the track's clip. Used when forcing sample rates in \ref RadioDataEditor.ForceSampleRate()
116 /// </summary>
117 public AudioClip EditorChildClip => (track is ClipRadioTrack clipTrack) ? clipTrack.clip : null;
118#endif
119
120 /// <summary>
121 /// Creates an empty wrapper for a station
122 /// </summary>
123 /// <param name="_track">The default track eventType to use</param>
125 {
126 track = _track;
127 gain = 100;
128
129 // ensure this wrapper is not empty
131 }
132
133#if UNITY_EDITOR
134 /// <summary>
135 /// Updates track types and names on script reload to detect any new types
136 /// </summary>
137 [InitializeOnLoadMethod]
138 public static void OnReload()
139 {
140 trackTypes = null;
141 trackNames = null;
142 }
143#endif
144
145 /// <summary>
146 /// Gets a new track to be used in a wrapper- note that this is marked as `static`<br>
147 /// This is mainly used in the editor to update the track eventType when it's selected on \ref trackType
148 /// </summary>
149 /// <param name="_name">The name of the track eventType to create</param>
150 /// <returns>A new track with the given eventType</returns>
151 public static IStationTrack CreateTrackEditor(string _name)
152 {
153 // get the index of the chosen track eventType
154 int index = Array.IndexOf(TrackNames, _name);
155
156 // if you somehow have an invalid track eventType, don't create anything
157 if (index < 0)
158 return null;
159
160 // create the track generically
161 // more info in RadioTrackWrapper.CreateTrackEditor
162 IStationTrack outTrack = (IStationTrack)Activator.CreateInstance(TrackTypes[index]);
163 outTrack.IsInStation = true;
164
165 // return the track
166 return outTrack;
167 }
168
169 /// <summary>
170 /// Initialize the track stored in this wrapper
171 /// </summary>
172 public void Init()
173 {
174 track.Init();
175 }
176
177 /// <summary>
178 /// Creates a new track in this wrapper. This is called when \ref trackType is updated
179 /// </summary>
180 public void CreateTrackLocal()
181 {
183 }
184
185 /// <summary>
186 /// Gets a sample from the contained track.
187 /// </summary>
188 /// <param name="_sampleIndex">Index of the sample to get</param>
189 /// <returns>A sample from the contained track</returns>
190 public float GetSample(int _sampleIndex)
191 {
192 // if the track has a rest at the beginning at that sample index, return a silent sample
193 if (_sampleIndex < startAndEndRests.x)
194 return 0;
195 // same thing but for a rest at the end of the track
196 else if (_sampleIndex > (track.SampleCount + startAndEndRests.x))
197 return 0;
198
199 // otherwise, return the sample from the track
200 return track.GetSample(_sampleIndex);
201 }
202 }
203
204}
Various utilities used throughout the project.
Definition RadioUtils.cs:28
static Type[] FindDerivedTypes(Type _baseType)
Gets all types derived from a given one. Does not include:Interfaces Generic classes Abstract classes
Definition RadioUtils.cs:72
A eventType of RadioTrack that plays from a chosen AudioClip object.
static Type[] TrackTypes
A list of each eventType of track that this wrapper can contain- this is anything that inherits from ...
void Init()
Initialize the track stored in this wrapper.
string trackType
The current eventType of this track as chosen in the editor. Displayed as a dropdown of RadioTrack DI...
void CreateTrackLocal()
Creates a new track in this wrapper. This is called when trackType is updated.
float GetSample(int _sampleIndex)
Gets a sample from the contained track.
Vector2 startAndEndRests
The amount of time at the start and end of the track for which there's silence. This allows you to sp...
float Gain
The gain value scaled down to ones- e.g gain at 200 is Gain at 2.
static IStationTrack CreateTrackEditor(string _name)
Gets a new track to be used in a wrapper- note that this is marked as static This is mainly used in ...
float gain
The additional volume of this track. See RadioTrackWrapper.gain.
StationRadioTrackWrapper(IStationTrack _track)
Creates an empty wrapper for a station.
IStationTrack track
The track contained in this wrapper. Note that this is an IStationTrack and not an IRadioTrack like ...
static string[] TrackNames
A list of the names of each eventType in TrackTypes This is what's displayed in the inspector for the...
A RadioTrack that can be played as part of a station.
Tracks to be used on a radio- includes base classes.
Definition RadioUtils.cs:20