Ryle Radio 1.0.0
An open-source "radio" system for Unity, allowing tracks, tuning, broadcasters, and more!
Loading...
Searching...
No Matches
ClipRadioTrack.cs
1using UnityEngine;
2
3namespace RyleRadio.Tracks
4{
5
6 /// <summary>
7 /// A eventType of RadioTrack that plays from a chosen AudioClip object
8 /// </summary>
9 [System.Serializable]
11 {
12 /// <summary>
13 /// The display name of this track in the editor. Required in \ref RadioTrack
14 /// </summary>
15 public const string DISPLAY_NAME = "Audio Clip";
16
17 /// <summary>
18 /// The clip that this track plays
19 /// </summary>
20 public AudioClip clip;
21
22 /// <summary>
23 /// The individual samples of this clip, as it needs to be played sample-by-sample (a limitation of Unity's AudioClip)
24 /// </summary>
25 protected float[] Samples { get; set; }
26
27 /// <summary>
28 /// Whether or not this is in a \ref StationRadioTrack
29 /// Required by \ref IStationTrack
30 /// </summary>
31 public bool IsInStation { get; set; }
32
33
34 /// <summary>
35 /// Initializes this track. This needs to be called every time the clip is changed
36 /// </summary>
37 public override void Init()
38 {
40
41 SampleCount = Samples.Length; // assign clip values based on chosen clip
42 SampleRate = clip.frequency;
43 }
44
45 /// <summary>
46 /// Reads the clip into the \ref Samples array, and combines its channels into one.
47 ///
48 /// We need to flatten the clip into one channel to play it from a \ref RadioOutput as the Output is only using one channel. Theoretically, we could expand the Output to use multiple channels, but given this would be the only track eventType to do this it's probably not worth the significant effort.
49 /// <br>For the moment, we'll treat Outputs as AM radios (which are mono in real life)
50 /// </summary>
52 {
53 float[] allSamples = new float[clip.samples * clip.channels]; // all samples from both channels
54 Samples = new float[clip.samples]; // samples combined to one channel
55
56 // if the clip is invalid for some reason, tell the user
57 if (!clip.GetData(allSamples, 0))
58 {
59 Debug.LogError("Cannot access clip data from track " + clip.name);
60 return;
61 }
62
63 // for each sample (all channels)
64 for (int sample = 0; sample < clip.samples; sample++)
65 {
66 float combined = 0;
67
68 // for each channel in this sample, combine the audio
69 for (int channel = 0; channel < clip.channels; channel++)
70 combined += allSamples[(sample * clip.channels) + channel];
71
72 combined /= clip.channels; // find the average of the channels' audio
73 Samples[sample] = combined; // make the mono sample the average
74 }
75 }
76
77 /// <summary>
78 /// Gets a sample from the clip
79 /// </summary>
80 /// <param name="_sampleIndex">The index of the sample</param>
81 /// <returns>A sample from the clip at the given index</returns>
82 public override float GetSample(int _sampleIndex)
83 {
84 // we already have all the samples, so we just get the one at the given index
85 return Samples[_sampleIndex];
86 }
87 }
88
89}
A eventType of RadioTrack that plays from a chosen AudioClip object.
bool IsInStation
Whether or not this is in a StationRadioTrack Required by IStationTrack.
override void Init()
Initializes this track. This needs to be called every time the clip is changed.
AudioClip clip
The clip that this track plays.
void ReadClipAndForceToMono()
Reads the clip into the Samples array, and combines its channels into one.
override float GetSample(int _sampleIndex)
Gets a sample from the clip.
const string DISPLAY_NAME
The display name of this track in the editor. Required in RadioTrack.
float[] Samples
The individual samples of this clip, as it needs to be played sample-by-sample (a limitation of Unity...
A track to play as part of a radio. These are the fundamental objects that define the content of the ...
Definition RadioTrack.cs:20
float SampleRate
The sample rate of this track.
Definition RadioTrack.cs:27
virtual int SampleCount
The number of samples in this track.
Definition RadioTrack.cs:33
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