Noticias

¡RECUERDA QUE SI ERES UN NUEVO USUARIO, DEBES PRESENTARTE PARA PODER PUBLICAR MENSAJES! | TENEMOS CANAL OFICIAL DE TELEGRAM: t.me/unity3dspain

Ecualizador grafico

Iniciado por Jose134, Julio 31, 2015, 09:11:24 PM

Tema anterior - Siguiente tema
[quote author=iRobb date=1438695661" data-ipsquote="" data-cite="iRobb" class="ipsQuote">Bueno primero arreglemos el bucle que como bien decias hay que incrementar i con el valor de barsamples y no se nos superpongan.[/quote]Que tal haciendo esto?
							

Vale. Que ocurre con las barras? Que defectos ves (estoy en el tablet no con el pc).

Pues me pasa como antes. Solo se mueven las dos primeras barras. Las variables tienen estos valores:he puesto en barSamples 32 porque 4096 samples que recojo entre 128 barras que he creado segun la calculadora el resultado es 32. Entonces cada barra representa 32 frecuencias distintas (a no ser que funcione de un modo distinto pero segun lo poco que he ido deduciendo deberia ser asi) 

Voy al PC. Pero diria que el bucle está mal. Lo miro.
Buff. Vamos mal. Hay que reorganizar ya que entiendo que barsamples son las fft y barnumbers las barras que se muestran. Voy a poner mi código.

    public float heightMultiplier;
public float maxHeight;
public float minHeight;
public int barsNumber; //Numero de barras que se crean
[Range (1,4096)] public int barSamples; //Numero de samples que muestra cada barra
public float heightSpeed;


private AudioSource source;
private float[] samples; //Las samples recogidas con GetSpectrumData
private GameObject[] bars; //Las barras
 

Mi código sobre el tuyo modificado:using UnityEngine;
using System.Collections;public class Visualizer : MonoBehaviour {
    public float heightMultiplier;
    public float maxHeight;
    public float minHeight;
    public int barsNumber;
    [Range(1, 4096)]
    public int barSamples;    private AudioSource source;
    private float[] samples;
    private float[] samplesPacked;
    private GameObject[] bars;    void Awake() {
        this.source = GetComponent<AudioSource>();
        this.samples = new float[this.barSamples];
        this.samplesPacked = new float[this.barsNumber];
        this.bars = new GameObject[samples.Length];        for (int i = 0; i < this.barsNumber; i++) {
            this.bars = GameObject.CreatePrimitive(PrimitiveType.Cube);
            this.bars.transform.position = new Vector3(i, 0, 0);
            this.bars.transform.parent = transform;
            this.bars.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);        }
    }    void Update() {
        samples = source.GetSpectrumData(samples.Length, 0, FFTWindow.BlackmanHarris);
        //samples = AudioListener.GetSpectrumData(samples.Length, 0, FFTWindow.BlackmanHarris);        int rBars = this.barSamples / this.barsNumber;
        int index = 0;        for (int i = 0; i < this.barSamples; i += rBars, index++) {
            this.samplesPacked[index] = 0f;
            for (int j = 0; j < this.barsNumber; j++) {
                this.samplesPacked[index] += this.samples[j + index];
            }
        }
        for (int k = 0; k < this.barsNumber; k++) {
            Vector3 v1 = new Vector3(0.1f, this.samplesPacked[k] * heightMultiplier, 0.1f);
            float speed = Time.deltaTime * 10f;            this.bars[k].transform.localScale = Vector3.Lerp(this.bars[k].transform.localScale, v1, speed);
        }
    }
}
Sorry, esta línea del for está mal, es:                this.samplesPacked[index] += this.samples[j + i]; 
Height M = 20bar number = 32bar samples = 4096
Cuando lo tengas mezclado con el tuyo me dices. Verás que ya tiene una pinta estupenda
[quote author=Jose134" data-ipsquote-contapp="forums" data-ipsquote-contenttype="forums" data-ipsquote-contentclass="forums_Topic" data-ipsquote-contentid="15868" data-ipsquote-contentcommentid="101216">
    public float heightMultiplier;public float maxHeight;
public float minHeight;
public int barsNumber; //Numero de barras que se crean
[Range (1,4096)] public int barSamples; //Numero de samples que muestra cada barra
public float heightSpeed;


private AudioSource source;
private float[] samples; //Las samples recogidas con GetSpectrumData
private GameObject[] bars; //Las barras
 [/quote]barSamples es todo el sampleado de todas las frecuencias en un momento dado, que es lo que devuelve la fftbarsNumber es el número de barras a mostrar, que serán el empaquetado de frecuencias de barSamples.

El codigo se me ha quedado asi
using UnityEngine;
using System.Collections;

public class Visualizer : MonoBehaviour {

public float heightMultiplier;
public float maxHeight;
public float minHeight;
public int barsNumber;
[Range (1,4096)] public int barSamples;
public float heightSpeed;


private AudioSource source;
private float[] samples;
private float[] packedSamples;
private GameObject[] bars;

void Awake () {
   source = GetComponent<AudioSource>();
   samples = new float[4096];
   packedSamples = new float[barsNumber];
   bars = new GameObject[barsNumber];

   CreateBars();
}

private void CreateBars () {
   for(int i = 0; i < barsNumber; i++){
      bars = GameObject.CreatePrimitive(PrimitiveType.Cube);   
      bars.transform.position = new Vector3(i,0,0);
      bars.transform.parent = transform;
      bars.name = "" + i;
   }
}

void Update () {
   samples = source.GetSpectrumData(samples.Length, 0, FFTWindow.BlackmanHarris);

   int rBars = barSamples / barsNumber;
   int index = 0;

   for(int i = 0; i < barSamples; i+= rBars, index++){
      packedSamples[index] = 0f;
      for(int j = 0; j < barsNumber; j++){
         packedSamples[index] += samples[j + i];
      }

      /*
      float yValue = Mathf.Clamp(samplesAdd * heightMultiplier, minHeight, maxHeight);
      bars.transform.localScale = new Vector3(1,yValue,1);
      */
   }

   for (int k = 0; k < barsNumber; k++) {
      Vector3 v1 = new Vector3(1f, packedSamples[k] * heightMultiplier, 1f);
      float speed = Time.deltaTime * heightSpeed;
      
      bars[k].transform.localScale = Vector3.Lerp(bars[k].transform.localScale, v1, speed);
   }
}
}
Pero las barras siguen sin funcionar como me gustaria La primera barra siempre esta muy alta las siguientes se mueven un poco y las ultimas apenas pasan de 0.2 de altura por cierto los graves son las barras de la izquierda o de la derecha?

Claro, esto es normal. Piensa que cada valor de frecuencia es la mitad de su anterior, o lo que es lo mismo, cada frecuencia es el doble de la anterior. Por tanto, es las frecuencias más altas (la barra altísima), el valor es muy grande y en las primeras muuuy pequeño. Como normalizamos esto, pues con el logaritmo. Al resultado de la suma de los valores packed, aplícale Mathf.Log. Y serán todas enormes, por tanto tendrás que modificar el heightmodifier.

Y al verlas moverse, seguro que descubres cuáles son las bajas y cuáles las altas.

no se que pasa , pero se lee interesante, llegando a casa me mentere tambien en este tema  


INFINITAMENTE MEJOR Peeeero no para de salirme este error en la consola:transform.localScale assign attempt for '14' is not valid. Input localScale is { 1.000000, -Infinity, 1.000000 }.
UnityEngine.Transform:set_localScale(Vector3)
Visualizer:Update() (at Assets/Scripting/Visualizer.cs:56) Solo que no siempre pone 14 son numeros desde el 0 hasta el 31

Que cosa más rara. Mi código sin este error es:using UnityEngine;
using System.Collections;public class Visualizer : MonoBehaviour {
    public float heightMultiplier;
    public float maxHeight;
    public float minHeight;
    public int barsNumber;
    [Range(1, 4096)]
    public int barSamples;
    public float barSpeed;    private AudioSource source;
    private float[] samples;
    private float[] samplesPacked;
    private GameObject[] bars;    private Vector3[] pBars;
    private Vector3[] sBars;    private Vector3 barScale;    void Awake() {
        this.source = GetComponent<AudioSource>();
        this.samples = new float[this.barSamples];
        this.samplesPacked = new float[this.barsNumber];
        this.bars = new GameObject[samples.Length];        this.pBars = new Vector3[this.barsNumber];
        this.sBars = new Vector3[this.barsNumber];        this.barScale = new Vector3(0.5f, 0.01f, 0.1f);        for (int i = 0; i < this.barsNumber; i++) {
            this.bars = GameObject.CreatePrimitive(PrimitiveType.Cube);
            this.bars.transform.position = new Vector3(i, 0, 0);
            this.bars.transform.parent = transform;
            this.bars.transform.localScale = this.barScale;            this.pBars = bars.transform.localPosition;
            this.sBars = bars.transform.localScale;
        }
    }    void Update() {
        //samples = source.GetSpectrumData(samples.Length, 0, FFTWindow.BlackmanHarris);
        samples = AudioListener.GetSpectrumData(samples.Length, 0, FFTWindow.BlackmanHarris);        int rBars = this.barSamples / this.barsNumber;
        int index = 0;        for (int i = 0; i < this.barSamples; i += rBars, index++) {
            this.samplesPacked[index] = 0f;
            for (int j = 0; j < this.barsNumber; j++) {
                this.samplesPacked[index] += this.samples[j + i];
            }
            this.samplesPacked[index] = Mathf.Log(this.samplesPacked[index]);
        }        for (int k = 0; k < this.barsNumber; k++) {
            Vector3 v1 = this.barScale + new Vector3(0f, this.samplesPacked[k] * heightMultiplier, 0f);
            //Vector3 v2 = new Vector3(0.1f, this.samplesPacked[k] * heightMultiplier / 2f, 0.1f);
            float speed = Time.deltaTime * this.barSpeed;            this.bars[k].transform.localScale = Vector3.Lerp(this.sBars[k], v1, speed);
            //this.bars[k].transform.localPosition = Vector3.Lerp(this.bars[k].transform.localPosition, this.pBars[k] + v2, speed);
        }
    }
} Los params:HM = 6BarsN = 32BarsS = 4096BarSpeed = 15 
Ah yo de mientras ayer cambié el lerp de localscale. Para que sea más dinámico, lo hago partir del scale inicial, no del que tenga en ese momento. Así lo obligo a ir de 0f de escala a la escala del valor.
El mío se mueve de la leche. Muy chulo.

Agosto 05, 2015, 12:16:09 PM #28 Ultima modificación: Agosto 05, 2015, 12:17:53 PM por Oak Human Flesh
Si no les molesta, me quiero sumar como tester ,

Bye Bye



Claro, bienvenido. Cuántos más seamos más nos divertiremos.
A ver si entre todos lo hacemos mucho mejor.
[quote author=Oak Human Flesh" data-ipsquote-contapp="forums" data-ipsquote-contenttype="forums" data-ipsquote-contentclass="forums_Topic" data-ipsquote-contentid="15868" data-ipsquote-contentcommentid="101253">Si no les molesta, me quiero sumar como tester , [/quote]Si quieres ver la app completa que hice con la base de audio que conozco, ve a http://unityspain.com/topic/15839-xqualizer-09a/ 

Etiquetas: