- Home /
Decrease volume of individual sounds over time
Hello,
I'm creating a musical app (a recreation of Brian Eno's Bloom) and I have a question about lowering the volume of sounds that are repeated through a coroutine over time.
Here is a short video of what I've got so far.
How the audio part of the application works:
1) At start I load an Audio Clip array with 24 sounds (The screen itself is divided in 24 rectangles):
audioClip = new AudioClip[]{
(AudioClip)Resources.Load("Sounds/Chroma/C1"),
(AudioClip)Resources.Load("Sounds/Chroma/G1"),
(AudioClip)Resources.Load("Sounds/Chroma/C2"),
(AudioClip)Resources.Load("Sounds/Chroma/D2"),
(AudioClip)Resources.Load("Sounds/Chroma/E2"),
(AudioClip)Resources.Load("Sounds/Chroma/F#2"),
(AudioClip)Resources.Load("Sounds/Chroma/G2"),
(AudioClip)Resources.Load("Sounds/Chroma/A2"),
(AudioClip)Resources.Load("Sounds/Chroma/H2"),
(AudioClip)Resources.Load("Sounds/Chroma/C3"),
(AudioClip)Resources.Load("Sounds/Chroma/D3"),
(AudioClip)Resources.Load("Sounds/Chroma/E3"),
(AudioClip)Resources.Load("Sounds/Chroma/F#3"),
(AudioClip)Resources.Load("Sounds/Chroma/G3"),
(AudioClip)Resources.Load("Sounds/Chroma/A3"),
(AudioClip)Resources.Load("Sounds/Chroma/H3"),
(AudioClip)Resources.Load("Sounds/Chroma/C4"),
(AudioClip)Resources.Load("Sounds/Chroma/D4"),
(AudioClip)Resources.Load("Sounds/Chroma/E4"),
(AudioClip)Resources.Load("Sounds/Chroma/F#4"),
(AudioClip)Resources.Load("Sounds/Chroma/G4"),
(AudioClip)Resources.Load("Sounds/Chroma/A4"),
(AudioClip)Resources.Load("Sounds/Chroma/H4"),
(AudioClip)Resources.Load("Sounds/Chroma/C5")};
2) As suggested by @aldonaletto (link) i created my own PlayClipAtPoint function, because I wanted to be able to adjust the proprieties of the audio source that gets created when a sound is played:
AudioSource PlayClipAt(AudioClip clip, Vector3 pos, float volume, float pitch)
{
GameObject tempGO = new GameObject("TempAudio"); // create the temp object
tempGO.transform.position = pos; // set its position
AudioSource aSource = tempGO.AddComponent<AudioSource>(); // add an audio source
aSource.clip = clip; // define the clip
aSource.volume = volume;
aSource.pitch = pitch;
aSource.outputAudioMixerGroup = audioCenter.outputAudioMixerGroup;
aSource.Play(); // start the sound
Destroy(tempGO, clip.length); // destroy object after clip duration
return aSource; // return the AudioSource reference
}
3) I have a method called startsound() which is inside Update() that checks if the user clicked on a specific rectangle and 1) plays the appropriate sound and 2) starts a coroutine that will repeat that sound a specific number of times:
public void startsound()
{
if (Input.GetMouseButtonDown(0) && screen.interval1.Contains(Input.mousePosition))
{
// Debug.Log("1");
PlayClipAt(audioClip[0], Vector3.zero, 1f, 1f);
StartCoroutine(RepeatSound1(audioClip[0], numbOfTimes));
}
// [...]
}
The coroutine looks like that:
IEnumerator RepeatSound1(AudioClip whichSound, int numOfTimes)
{
for (int i = 0; i < numOfTimes; i++)
{
PlayClipAt(audioClip[0], Vector3.zero, 1f, 1f);
yield return new WaitForSeconds(secondstowait);
}
}
numOfTimes is an int variable that determines how many times a sound gets repeated. secondstowait is a float variable that determines the time in seconds between each repeated sound.
Note that I have 24 if statements and coroutines so that each sound can be played and repeated individually.
As you saw in the video the volume stays the same no matter how many times a sound gets repeated. My question is: how can I decrease the volume of a sound after it has been repeated for a number of times? For example: If numOfTimes is 10 I want the volume to decrease over time so it is very quiet when it reaches the eighth or nineth repetition. In the same time I want the volume of the sounds that get created when clicked to be at the maximum volume.
This is my first project with Unity (no programming experience) and I'm not a native english speaker, so I'm sorry if I haven't used the best code or words :) Thank you so much if you can help me :)