【问题标题】:Save the data from an array that changes every second保存每秒更改的数组中的数据
【发布时间】:2018-10-30 02:58:28
【问题描述】:

我正在做一个在 Unity 中计算 HBR 的应用程序。

因此,当用户单击麦克风按钮时,它开始录制并通过数组中的 GetSpectrumData 命令获取频谱数据。我想要做的是每秒(15秒)在数组中获取光谱并将其保存在另一个中。

代码如下:

using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class Data : MonoBehaviour
{

AudioSource _audio;
public float[] _samples1, arreglo = new float[512];
void Start()
{
    _audio = GetComponent<AudioSource>();
    _audio.clip = Microphone.Start("Built-in Microphone", true, 15, 44100);

}

void Update()
{

    GetSpectrum();

    for (int i = 0; i > 15; i++)
    {
        arreglo.add(_samples1[i]);

    }


}

void GetSpectrum()
{
    _audio.GetSpectrumData(_samples1, 0, FFTWindow.Blackman);


}
}

问题是当我运行它时,我无法识别命令添加。

如果有人可以帮助我,我将不胜感激。非常感谢。

【问题讨论】:

  • 数组没有add 方法。 List&lt;T&gt; 确实有一个 Add 方法。这是你需要的吗?
  • IList 和 ICollection 具有 Add 方法。它是“添加”而不是“添加”。
  • for (int i = 0; i &gt; 15; i++) 是错误的,可能是拼写错误?如果有什么可能应该是for (int i = 0; i &lt; 15; i++)?目前它将立即跳过循环,因为条件 i &gt; 15 从未满足,因为您将 i=0 作为 start ...

标签: c# android ios unity3d audio


【解决方案1】:
using UnityEngine;
using System.Collections.Generic;

[RequireComponent(typeof(AudioSource))]
public class Data : MonoBehaviour
{
    AudioSource _audio;
    public float[] _samples1 = new float[512];
    public List<float> arreglo = new List<float>();

    void Start()
    {
        _audio = GetComponent<AudioSource>();
        _audio.clip = Microphone.Start("Built-in Microphone", true, 15, 44100);
    }

    void Update()
    {
        GetSpectrum();

        // optional if you want to clear old data
        arreglo.Clear();

        // replace 15 with '_samples1.Length' for adding all values
        for (int i = 0; i < 15; i++)
        {
            arreglo.Add(_samples1[i]);
        }
    }

    void GetSpectrum()
    {
        _audio.GetSpectrumData(_samples1, 0, FFTWindow.Blackman);
    }
}

【讨论】:

  • 请说明您所做的更改以及如何解决问题。答案是不要让任何疑问或问题公开。
  • 你还复制了错误的for(int i = 0; i &gt; 15; i++),这毫无意义
  • 我使用 List 而不是数组。而且我认为OP只需要15个元素,所以我没有改变那部分。
  • 但它不会复制 15 个元素......它不会复制任何元素,因为条件 i &gt; 15 从未满足
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 2012-01-17
  • 2016-11-08
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多