• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

My AudioClip wont loop even though loop is true 1 Answer

Need help with AudioSources 1 Answer

2d PlayAudioOneShot inconsistent behavior 0 Answers

Can't get PlayOneShot() or Play() to work 1 Answer

WWW Audio and PlayOneShot: Playing the same AudioClip Twice Cuts off First Instance 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges