28using System.Collections.Generic;
31#if UNITY_EDITOR || UNITY_STANDALONE
32using MYFLT = System.Double;
33#elif UNITY_ANDROID || UNITY_IOS
34using MYFLT = System.Single;
40[RequireComponent(typeof(AudioSource))]
48 [Tooltip(
"The gameObject with the CsoundUnity component to load Audio Channels from")]
59 [Tooltip(
"Audio Output settings")]
65 [SerializeField, HideInInspector]
71 [SerializeField, HideInInspector]
80 #endregion PUBLIC_FIELDS
82 #region PRIVATE_FIELDS
84 [SerializeField, HideInInspector]
87 private MYFLT zerodbfs;
88 private AudioSource audioSource;
91 #endregion PRIVATE_FIELDS
99 Debug.LogError(
"CsoundUnity was not found?");
102 AudioSettings.GetDSPBufferSize(out bufferSize, out numBuffers);
104 audioSource = GetComponent<AudioSource>();
106 Debug.LogError(
"AudioSource was not found?");
108 audioSource.velocityUpdateMode = AudioVelocityUpdateMode.Fixed;
109 audioSource.spatialBlend = 1.0f;
110 audioSource.spatializePostEffects =
true;
119 if (audioSource.clip ==
null)
121 var ac = AudioClip.Create(
"DummyClip", 32, 1, AudioSettings.outputSampleRate,
false);
122 var data =
new float[32];
123 for (var i = 0; i < data.Length; i++)
129 audioSource.clip = ac;
130 audioSource.loop =
true;
152 for (var chan = 0; chan < (int)audioChannels; chan++)
157 this.csoundUnity = csound;
158 this.csoundUnityGameObject = csound.gameObject;
160 this.selectedAudioChannelIndexByChannel =
new int[2];
177 if (csoundUnity) zerodbfs = csoundUnity.
Get0dbfs();
180 void OnAudioFilterRead(
float[] data,
int channels)
182 if (csoundUnity !=
null)
184 ProcessBlock(data, channels);
188 void ProcessBlock(
float[] samples,
int numChannels)
199 if (
string.IsNullOrWhiteSpace(chanToUse))
continue;
204 for (
int i = 0, sampleIndex = 0; i < samples.Length; i += numChannels, sampleIndex++)
206 for (uint channel = 0; channel < numChannels; channel++)
213 samples[i + channel] = samples[i + channel] * (float)(
namedAudioChannelData[0][sampleIndex] / zerodbfs * 0.5f);
216 samples[i + channel] = samples[i + channel] * (float)(
namedAudioChannelData[(
int)channel][sampleIndex] / zerodbfs);
CsoundUnityChild is a component that can output AudioChannels found in the csd of the associated Csou...
int[] selectedAudioChannelIndexByChannel
An array containing the selected audiochannel indexes by channel: MONO = 0, STEREO = 1
List< MYFLT[]> namedAudioChannelData
A list to hold the current audio buffer data for each channel
AudioChannels AudioChannelsSetting
Defines if this CsoundUnityChild will use one (MONO) or two (STEREO) channels. In the case of a MONO ...
void SetAudioChannel(int channel, int audioChannel)
Used after Init(), sets the audioChannel index from the CsoundUnity.availableAudioChannels for each c...
List< string > availableAudioChannels
A list to hold available audioChannels names
GameObject csoundUnityGameObject
The gameObject with the CsoundUnity component to load Audio Channels from
void Init(CsoundUnity csound, AudioChannels audioChannels=AudioChannels.MONO)
Initializes this CsoundUnityChild instance setting the CsoundUnity reference and the audioChannels se...
readonly Dictionary< string, MYFLT[]> namedAudioChannelDataDict
public named audio Channels shown in CsoundUnityChild inspector
MYFLT Get0dbfs()
Get 0 dbfs
List< string > availableAudioChannels
list to hold available audioChannels names
bool IsInitialized
Is Csound initialized?