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;
}
}
i've edited the script but still not giving the correct sum
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;
}
}
@$$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;
}
}
}
}
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;
}
}
}
It is true. It should works if you define i2 before the loop, I think.
Your answer
Follow this Question
Related Questions
Nested for loop isnt working 1 Answer
if ALL items in array are something 1 Answer