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 syhrmr · Feb 26, 2018 at 05:00 AM · c#audioscript.sine-wave

Envelope in OnAudioFilterRead method

Hi all, I'm trying to make a sine wave in OnAudioFilterRead() method to generate it using an equation a = sin(2 phi frequency * sample length / sampling rate). But I have two problem with this,

  1. I want to limit the samples length to 100.000 samples how to limit it in this method?

  2. How to implement an envelope so my waveform has attack, decay, sustain, and release?

Below is simple code that I've implemented.

 private double samplingRate = 48000;
 private double frequency = 440;
     
 private void OnAudioFilterRead (float[] data, int channels) {
                 increment = 2.0 * Mathf.PI * frequency / samplingRate;
                
                 for (int i = 0; i < data.Length; i += channels) {
                     phase         += increment;
                     data[i]     = Mathf.Sin(phase);
          
                     if (channels == 2)                data[i + 1] = data[i];
                     if (phase > (Mathf.PI * 2))        phase         = 0.0;
                 }
                
 }

Thank you

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 Bunny83 · Feb 26, 2018 at 12:37 PM

Well, first of all currently you do not create a sine wave as you directly use the phase as data. This would result in a positive only sawtooth wave that would heavily clipping since it goes up to 2PI (6.28). So obviously to get a sine wave you either want to use Mathf.Sin and pass your phase as parameter, or you want to use a "complex phasor" by representing your phase and increment as complex number. If the frequency doesn't change often a complex phasor would probably be better for performance as you only need to use Sin and Cos once when you change the frequency. If you need a complex number type i've once created this one.


An envelope is a bit more complicated. See this post here for a good starting point.


ps: If you're not familiar with complex numbers you can do something like this:

 Complex phase = new Complex(1, 0);
 float m_Frequency;
 Complex increment;
 
 public float Frequency
 {
     get { return m_Frequency;}
     set
     {
         if (value != m_Frequency)
         {
             m_Frequency = value;
             float angle = (m_Frequency * 2.0 * Mathf.PI ) / samplingFq;
             increment = new Complex(Mathf.Cos(angle), Mathf.Sin(angle));
         }
     }
 }
 
 private void OnAudioFilterRead (float[] data, int channels)
 {
     if (channels == 2)
     {
         for (int i = 0; i < data.Length; i += 2)
         {
             phase *= increment;
             data[i] = data[i+1] = phase.fReal;
         }
     }
     else
     {
         for (int i = 0; i < data.Length; i++)
         {
             phase *= increment;
             data[i] = phase.fReal;
         }
     }
     double norm = (3d - phase.real * phase.real - phase.img * phase.img) * 0.5d;
     phase *= norm;
 }

As you can see we just need to calculate sin and cos once when we change the frequency. From now on we can simply "spin" the complex number. Note that the "normalization" at the end is an easy way to avoid drift over time. You could do it within the loops but usually it should be stable for several thousand complex rotations. Note that i splitted the loop into two seperate loops. Doing the if check every iteration is bad for performance. Branch prediction is pretty good nowadays, however it's best to just avoid it in time critical code ^^.

Comment
Add comment · Show 1 · 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
avatar image syhrmr · Feb 27, 2018 at 07:02 AM 0
Share

So, how do I limit the length of wave since my code generate continuous wave, like in $$anonymous$$atlab you can create a wave with sample length from 1 to 100.000 samples?

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

97 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 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

Distribute terrain in zones 3 Answers

Why the audio sound effects of the main menu are not hearing like in mute ? 0 Answers

How do I add to channels or sine waves? 0 Answers

Script to trigger all audio playing to stop, and play a different audio? 1 Answer

Please help with playing music for button click 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