Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by wijesijp · Jan 20, 2017 at 03:06 PM · audiosourceaudioclipplayoneshotdsp

Play a segment in a audio file

I am working on a music game which requires me to play small audio clips very quickly. At the moment I play the clips using PlayOneshot.

I am thinking of putting all the clips in to a single audio clip and jump to different positions in that clip.

Is this a good idea?

So I tried the following as a test. I go to the 90 beat at the beginning. And each beat I jump to a random location.

But the output is jumble of sounds can’t make out anything.

Can anyone suggest a solution ?

     void Start ()
     {
 
         source = gameObject.GetComponent<AudioSource>();
         source.clip = clip;
         samplePeriod = 60f * clip.frequency / 110.0f;
         nextBeatSample = clip.frequency;
 
 
         numberOfBests = (int)(clip.length*clip.frequency/samplePeriod);
         source.time = (float)(clip.length * 90 / numberOfBests);
         source.Play();
 
         StartCoroutine(BeatCheck());
 
 
     }
 
     private IEnumerator BeatCheck()
     {
         while (source.isPlaying)
         {
             currentSample = (double)AudioSettings.dspTime * clip.frequency;
 
             if (currentSample >= nextBeatSample)
             {
                 nextBeatSample += samplePeriod;
                 source.time = (float)(clip.length * Random.Range(numberOfBests/4, numberOfBests/2) / numberOfBests);
             }
             yield return new WaitForSeconds(loopTime / 1000f);
         }       
     }

Comment
Add comment · Show 5
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
avatar image wijesijp · Jan 26, 2017 at 05:29 AM 0
Share

bump..........

avatar image FortisVenaliter · Jan 26, 2017 at 08:06 PM 0
Share

...why exactly did you combine the audio into one big file to start with?

avatar image wijesijp FortisVenaliter · Jan 27, 2017 at 03:21 AM 0
Share

i thought it would be more efficient to jump to different locations of the file ins$$anonymous$$d of playing different files.

I have around 100 small files

avatar image FortisVenaliter wijesijp · Jan 27, 2017 at 08:37 PM 1
Share

I mean, it might be a tiny bit more efficient in the finished project... maybe.

But is it really worth the trouble? That introduces so many new points of failure, at least in my opinion, that the costs outweigh the benefits.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Loui_Studios · Jan 28, 2017 at 03:22 PM

I see you're already using AudioSource.time.

You say that you jump to a random point in the clip every beat and the sounds are jumbled?

Either your timing between beats is off, and the sound clips are switching too fast, or your positioning is off, and you're playing the wrong part of the file.

Try rewriting the timer, and if that doesn't work, double check to make sure you're playing the right part of the track.

If that doesn't work, I recommend splitting the one big AudioClip into lots of small ones. Put all of the small AudioClips into an AudioClip array, and play a random clip each beat. It's the easiest way!

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Playing many audioclips with PlayOneShot causes all sound to cut out. 0 Answers

Why PlayOneShot is not working? C# 1 Answer

Sound wont play when triggered (javascript) 1 Answer

My AudioClip wont loop even though loop is true 1 Answer

Need help with AudioSources 1 Answer


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