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 Zild · Apr 16, 2015 at 09:51 AM · distortion

Using multiple AudioClips with data created using SetData causes distortion

I am trying to create a music generation app (the user writes a short score and the app then plays the corresponding notes), where each separate note is handled as a separate AudioSource with its AudioClip created dynamically by setting the clip's data according to a sine wave function using AudioClip.SetData.

For individual notes or two notes together this works fine, but if I have three or more notes playing at the same I get severe distortion.

In case the function that sets the data is taking too much processing power I tested it with all the data pre-set, but the problem still occurs so I presume it is something else.

I also tried playing three music mp3 files instead of my dynamically-generated notes - this worked fine with no distortion.

Any suggestions what I may be doing wrong or what I should look out for?

 using UnityEngine;
 using System.Collections;
 
 public class KeyScript : MonoBehaviour {
 
     public float Duration = 1;
     public int KeyPitch = 48;
 
     public Note Note;
     private int sampleRate;
     private AudioSource source;
 
     // Use this for initialization
     void Start () {
         sampleRate = AudioSettings.outputSampleRate;
         source = GetComponent<AudioSource>();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnMouseDown () {
         Note = new Note(Scale.pitches[KeyPitch], Duration, sampleRate, source);
         Note.Play ();
     }
 }
 
 public class Note
 {
     private Pitch pitch;
     private float length;
     private int sampleRate;
     private AudioClip clip;
     private AudioSource source;
 
     public Note(Pitch Pitch, float Length, int SampleRate, AudioSource Source)
     {
         this.pitch = Pitch;
         this.length = Length;
         this.sampleRate = SampleRate;
         this.source = Source;
         this.clip = AudioClip.Create ("Procedural Tone", (int)(sampleRate * length), 1, sampleRate, false);
 
         int count = 0;
         float[] data = new float[(int)(sampleRate * length)];
         while (count < data.Length)
             {
             data[count] = Mathf.Sign(Mathf.Sin(2 * Mathf.PI * pitch.Frequency * count / sampleRate));
             count++;
             }
 
         clip.SetData (data, 0);
         source.clip = clip;
     }
 
     public void Play()
     {
         source.Play ();
     }
 }
 
 public class Pitch
 {
     private string name;
     private float frequency;
     
     public float Frequency
     {
         get { return frequency;}
     }
     
     public Pitch(string Name, float Frequency)
     {
         this.name = Name;
         this.frequency = Frequency;
     }
     
     public override string ToString()
     {
         return name;
     }
 }
 
 public static class Scale
 {
     public static Pitch[] pitches =
     {
         new Pitch("C0", 16.35f),
         new Pitch("C#0/Db0", 17.32f),
         new Pitch("D0", 18.35f),
         new Pitch("D#0/Eb0", 19.45f),
         new Pitch("E0", 20.6f),
         new Pitch("F0", 21.83f),
         new Pitch("F#0/Gb0", 23.12f),
         new Pitch("G0", 24.5f),
         new Pitch("G#0/Ab0", 25.96f),
         new Pitch("A0", 27.5f),
         new Pitch("A#0/Bb0", 29.14f),
         new Pitch("B0", 30.87f),
         new Pitch("C1", 32.7f),
         new Pitch("C#1/Db1", 34.65f),
         new Pitch("D1", 36.71f),
         new Pitch("D#1/Eb1", 38.89f),
         new Pitch("E1", 41.2f),
         new Pitch("F1", 43.65f),
         new Pitch("F#1/Gb1", 46.25f),
         new Pitch("G1", 49f),
         new Pitch("G#1/Ab1", 51.91f),
         new Pitch("A1", 55f),
         new Pitch("A#1/Bb1", 58.27f),
         new Pitch("B1", 61.74f),
         new Pitch("C2", 65.41f),
         new Pitch("C#2/Db2", 69.3f),
         new Pitch("D2", 73.42f),
         new Pitch("D#2/Eb2", 77.78f),
         new Pitch("E2", 82.41f),
         new Pitch("F2", 87.31f),
         new Pitch("F#2/Gb2", 92.5f),
         new Pitch("G2", 98f),
         new Pitch("G#2/Ab2", 103.83f),
         new Pitch("A2", 110f),
         new Pitch("A#2/Bb2", 116.54f),
         new Pitch("B2", 123.47f),
         new Pitch("C3", 130.81f),
         new Pitch("C#3/Db3", 138.59f),
         new Pitch("D3", 146.83f),
         new Pitch("D#3/Eb3", 155.56f),
         new Pitch("E3", 164.81f),
         new Pitch("F3", 174.61f),
         new Pitch("F#3/Gb3", 185f),
         new Pitch("G3", 196f),
         new Pitch("G#3/Ab3", 207.65f),
         new Pitch("A3", 220f),
         new Pitch("A#3/Bb3", 233.08f),
         new Pitch("B3", 246.94f),
         new Pitch("C4", 261.63f),
         new Pitch("C#4/Db4", 277.18f),
         new Pitch("D4", 293.66f),
         new Pitch("D#4/Eb4", 311.13f),
         new Pitch("E4", 329.63f),
         new Pitch("F4", 349.23f),
         new Pitch("F#4/Gb4", 369.99f),
         new Pitch("G4", 392f),
         new Pitch("G#4/Ab4", 415.3f),
         new Pitch("A4", 440f),
         new Pitch("A#4/Bb4", 466.16f),
         new Pitch("B4", 493.88f),
         new Pitch("C5", 523.25f),
         new Pitch("C#5/Db5", 554.37f),
         new Pitch("D5", 587.33f),
         new Pitch("D#5/Eb5", 622.25f),
         new Pitch("E5", 659.25f),
         new Pitch("F5", 698.46f),
         new Pitch("F#5/Gb5", 739.99f),
         new Pitch("G5", 783.99f),
         new Pitch("G#5/Ab5", 830.61f),
         new Pitch("A5", 880f),
         new Pitch("A#5/Bb5", 932.33f),
         new Pitch("B5", 987.77f),
         new Pitch("C6", 1046.5f),
         new Pitch("C#6/Db6", 1108.73f),
         new Pitch("D6", 1174.66f),
         new Pitch("D#6/Eb6", 1244.51f),
         new Pitch("E6", 1318.51f),
         new Pitch("F6", 1396.91f),
         new Pitch("F#6/Gb6", 1479.98f),
         new Pitch("G6", 1567.98f),
         new Pitch("G#6/Ab6", 1661.22f),
         new Pitch("A6", 1760f),
         new Pitch("A#6/Bb6", 1864.66f),
         new Pitch("B6", 1975.53f),
         new Pitch("C7", 2093f),
         new Pitch("C#7/Db7", 2217.46f),
         new Pitch("D7", 2349.32f),
         new Pitch("D#7/Eb7", 2489.02f),
         new Pitch("E7", 2637.02f),
         new Pitch("F7", 2793.83f),
         new Pitch("F#7/Gb7", 2959.96f),
         new Pitch("G7", 3135.96f),
         new Pitch("G#7/Ab7", 3322.44f),
         new Pitch("A7", 3520f),
         new Pitch("A#7/Bb7", 3729.31f),
         new Pitch("B7", 3951.07f),
         new Pitch("C8", 4186.01f),
         new Pitch("C#8/Db8", 4434.92f),
         new Pitch("D8", 4698.63f),
         new Pitch("D#8/Eb8", 4978.03f),
         new Pitch("E8", 5274.04f),
         new Pitch("F8", 5587.65f),
         new Pitch("F#8/Gb8", 5919.91f),
         new Pitch("G8", 6271.93f),
         new Pitch("G#8/Ab8", 6644.88f),
         new Pitch("A8", 7040f),
         new Pitch("A#8/Bb8", 7458.62f),
         new Pitch("B8", 7902.13f)
     };
 }
Comment
Add comment · Show 3
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 Zild · Apr 16, 2015 at 07:48 AM 0
Share

As an aside, am I the only one having difficulty creating more than one tag for questions in Google Chrome?

avatar image Zild · Apr 16, 2015 at 02:06 PM 0
Share

After I type the first one and select it from the auto-complete selection, it puts the first tag in a box and then lets me start typing again to the right (in the remaining area of the text entry box). But whenever I select a second tag it just replaces the first one...

avatar image SaraCecilia · Apr 17, 2015 at 12:40 PM 0
Share

Okay, thanks. A workaround is ins$$anonymous$$d of clicking on the tag to add more, just press Enter. It's a bug we're looking into for a future update.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Zild · Apr 16, 2015 at 02:32 PM

Well this is embarrassing... It looks like the problem is due to the sheer amount of noise I am trying to put out. If I reduce the volume of the notes (either by reducing the amplitude of the signal data or by reducing the volume of the AudioSource controls) I can reduce or entirely remove the distortion.

I guess at the original levels they were saturating the AudioListener or something...

(Like most programming problems, this became obvious as soon as I stepped out of the house...)

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

I need no perspective distortion for some part of screen 0 Answers

Android Splash Screen Stretches 1 Answer

Blurred texture on range 1 Answer

How to measure fisheye lens distortion? 0 Answers

Extremely Annoying Unity Distortion Problem 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