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 /
  • Help Room /
avatar image
0
Question by sling5hot · Sep 12, 2017 at 12:15 PM · c#arrayfor-looplogicgetspectrumdata

how to start counting from the last index the script stop on?

i'm making an audio Visualization script.
i want to split the samples into 4 bands for example, but i can't figure out how.
i know i need to have a variable that's hold the last index the loop stop on it but how to make it start from this variable.
i have wrote a script but its not doing the job.
and all the index in _Bands have the same value(Which is what I don't want).
_Band its a list and its size change according to the enum" _NumberOfBand".
_Samples its an array hold 64 samples.
here a part of it:

         void MakeBands()
         {
             int _Count = _Samples.Length / (int)_NumberOfBands;
             int _index = 0;
             
             for (int i = 0; i < (int)_NumberOfBands; i++)
             {
                 float _sum = 0;
                 
                 for ( int i2 = _index; i2 < _Count; i2++)
                 {
                     _sum += _Samples[i2];
                     
                 }
 
                 _Count = _Count + (int)_NumberOfBands;
                 _Bands[i] = _sum;
             }
 
         }

Comment
Add comment · Show 1
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 sling5hot · Sep 12, 2017 at 06:38 PM 0
Share

i've edited the script but still not giving the correct sum

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Krunoslav · Sep 12, 2017 at 04:16 PM

You can just add a variable end and add it to i2.

 void MakeBands()
          {
              int _Count = _Samples.Length / (int)_NumberOfBands;       
              float _end = 0;
              for (int i = 0; i < (int)_NumberOfBands; i++)
              {
                  float _sum = 0;
                  for (int i2=0;i2<_Count;i2++)
                  {
                      _sum += _Samples[i2+_end];
                  }
                  _end = i2;
                  _Bands[i]=_sum;
              }
    
          }


Comment
Add comment · Show 2 · 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 sling5hot · Sep 12, 2017 at 04:47 PM 0
Share

but i2 cannot be used out of the for loop

avatar image sling5hot · Sep 12, 2017 at 05:09 PM 0
Share

@$$anonymous$$runoslav
you know i'll just post the full script:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 namespace Audio_Visualization
 {
     public enum NumberOfBands {One_Band=1,Tow_Band=2,Four_Band=4,Eight_Band=8};
     [RequireComponent(typeof(AudioSource))]
     [RequireComponent(typeof(Canvas))]
     public class AudioVisualization : $$anonymous$$onoBehaviour
     {
         public Image _Bar;
         public Vector2 _BarsSize = new Vector2(25,100);
         [Range(1.0f, 10f)]
         public float _DistanceBetweenBars=1.0f;
         public NumberOfBands _NumberOfBands;
 
         private float[] _Samples = new float[64];
         private List<float> _Bands;
         private List<Image> _VisualBars;
         private RectTransform _Canvas;
         private RectTransform _BarSize;
         private AudioSource _AudioSource;
 
         void Awake()
         {
             for (int i = 0; i < (int)_NumberOfBands; i++)
             {
                 _Bands.Add(0);
                 _VisualBars.Add(_Bar);
             }
         }
 
         void Start()
         {
             
             _AudioSource = GetComponent<AudioSource>();
 
             _BarSize = _Bar.GetComponent<RectTransform>();
 
             _BarSize.sizeDelta = _BarsSize;
 
             _Canvas = GetComponent<RectTransform>();
 
             _Canvas.sizeDelta = new Vector2((_BarsSize.x + _DistanceBetweenBars) * (int)_NumberOfBands, _BarsSize.y);
 
             
         }
          void Update()
         {
             GetSpect();
             $$anonymous$$akeBands();
             
         }
         void GetSpect()
         {
                // _AudioSource.GetSpectrumData(_Samples,0, FFTWindow.Blackman);   
                for(int i=0;i<64;i++)
             {
                 
                 _Samples[i] = (float)i;
             }
         }
         void $$anonymous$$akeBands()
         {
             int _Count = _Samples.Length / (int)_NumberOfBands;
             int _index = 0;
             
             for (int i = 0; i < (int)_NumberOfBands; i++)
             {
                 float _sum = 0;
                 
                 for ( int i2 = _index; i2 < _Count; i2++)
                 {
                     _sum += _Samples[i2];
                     
                 }
 
                 _Count = _Count + (int)_NumberOfBands;
                 _Bands[i] = _sum;
             }
 
         }
     }
 }
avatar image
1

Answer by sling5hot · Sep 12, 2017 at 06:49 PM

ok i figure it out by my self, thank you @Krunoslav for your help.

         void MakeBands()
         {
             int _Count = _Samples.Length / (int)_NumberOfBands;
             int _index = 0;
             
             for (int i = 0; i < (int)_NumberOfBands; i++)
             {
                 float _sum = 0;
                 
                 for ( int i2 = _index; i2 < _Count; i2++)
                 {
                     _sum += _Samples[i2];
                     _index = i2;
                     
                 }
 
                 _Count = _Count + (int)_NumberOfBands;
                 _Bands[i] = _sum;
                 
             }
 
         }
     }
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 Krunoslav · Sep 13, 2017 at 09:59 AM 0
Share

It is true. It should works if you define i2 before the loop, I think.

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

70 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

Related Questions

Just another line renderer question. Line renderer vector positions in an array of game objects. My line only draws between 2 positions? 1 Answer

Nested for loop isnt working 1 Answer

if ALL items in array are something 1 Answer

Why arrays don't set on void Start() ? 1 Answer

Destroy question if its already asked 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