• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by alessandro_f · Jan 18, 2016 at 09:52 AM · audiosoundaudioclipaudio sourceplayclipatpoint

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 :)

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Audio: -3db automatic attenuation on any audio playing? 0 Answers

Stop audiosource clip from playing 1 Answer

Question about audio (AudioSource). My ingame sound doesn't sound like the original audio file? 3 Answers

Why is Unity3d strictly using PCM Buffer size with 32bit floating in AudioClip? 1 Answer

Is OnAudioFilterRead Still the Preferred Method for Procedural Audio? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges