Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 PJEstrada006 · Mar 25, 2015 at 03:13 AM · audiosourcemusicpitch

Forming chords & playing multiple sounds simultaneously

Hello,

I´m developing a simple piano on unity that will play the sound corresponding to the keys the user touches. I´m new to managing sounds in unity and currently all I know is I can play a sound using the audioclip.play()

However I would like to be able to play chords, which means I have to play multiple sound at the same time. Can anyone tell me how can I accomplish this?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AlwaysSunny · Mar 25, 2015 at 03:19 AM

Your original question was honestly too easily researched to have passed moderation, but you raise a really interesting topic which I can address. I edited your question so others might find this explanation.

Not sure if there's anything new in Unity 5 regarding this. In previous versions, the answer to playing concurrent sounds is definitely:

Use multiple audio sources. You need one AudioSource component for each concurrent sound you want to play.

When they build keyboards (at least in the sub-thousand-dollar range) they have perhaps two dozen samples of each instrument at various keys. When the pianist hits a key, the "nearest" sample is played with an adjusted pitch which corresponds to that key. (High quality keyboards probably use individual samples per key or at least use more sophisticated methods).

I've actually done this before in Unity - not a piano, but playing instrument samples at a specific pitch. It works just fine. There's a formula for determining how much the pitch needs to change to achieve a certain key. If you know the key of your sample, you can arrive at any "nearby" key and maintain a good natural sound. I don't remember the formula, but it was easy to find.

So your piano or whatever you expose to the user could be as complex or simple as you like, but if it were me, I'd have a manager class which responds to key presses in the following way:

A key, when pressed, requests a note be played at its corresponding pitch.
The script responsible accepts this pitch information and creates an AudioSource from a prefab.
The prefab gets loaded with the nearest sample, and corrected for pitch.
The sample is played until the key is released or it attenuates naturally...
...at which point you can destroy or recycle the prefab instance.

There's a lot of wiggle room with how you handle this. It can be as complex as you like, but this is the simplest, cleanest approach I can think of.

Best,

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How would I go about using music from StreamingAssets folder (imported by the player) 0 Answers

C sharp cant play Audio 2 Answers

Creating Audio Source for different pitch 0 Answers

does unity 5 pitch shifter audio effect change playback speed? 1 Answer

Musical beat not in perfect time with tempo. WaitForSeconds not accurate. 2 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