Tone Gen

Has clicking problem in between each cycle.
mail@pastecode.io avatar
unknown
plain_text
10 days ago
3.6 kB
7
Indexable
Never
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

[RequireComponent(typeof(AudioSource))]  // Has clicking
public class ToneGenerator : MonoBehaviour
{
    public Slider volumeSlider;
    public Text frequencyText;

    private float currentFrequency = 100f;
    private const float minFrequency = 1f;
    private const float maxFrequency = 10000f;
    private const float frequencyStep = 1f;
    private const float frequencyStep5 = 5f;
    private const float frequencyStep10 = 10f;

    private AudioSource audioSource;
    private bool generatingTone = false;

    void Start()
    {
        audioSource = GetComponent<AudioSource>();

        // Set initial volume
        if (volumeSlider != null)
        {
            volumeSlider.onValueChanged.AddListener(ChangeVolume);
            volumeSlider.value = 0.5f; // Set initial volume to half
        }

        // Set initial frequency
        UpdateFrequencyText();
		StartTone();
    }

    public void ChangeVolume(float value)
    {
        audioSource.volume = value;
    }

    public void IncreaseFrequency()
    {
        currentFrequency += frequencyStep;
        if (currentFrequency > maxFrequency)
            currentFrequency = maxFrequency;
        UpdateFrequencyText();
    }

    public void DecreaseFrequency()
    {
        currentFrequency -= frequencyStep;
        if (currentFrequency < minFrequency)
            currentFrequency = minFrequency;
        UpdateFrequencyText();
    }

    public void IncreaseFrequencyBy5()
    {
        currentFrequency += frequencyStep5;
        if (currentFrequency > maxFrequency)
            currentFrequency = maxFrequency;
        UpdateFrequencyText();
    }

    public void DecreaseFrequencyBy5()
    {
        currentFrequency -= frequencyStep5;
        if (currentFrequency < minFrequency)
            currentFrequency = minFrequency;
        UpdateFrequencyText();
    }

    public void IncreaseFrequencyBy10()
    {
        currentFrequency += frequencyStep10;
        if (currentFrequency > maxFrequency)
            currentFrequency = maxFrequency;
        UpdateFrequencyText();
    }

    public void DecreaseFrequencyBy10()
    {
        currentFrequency -= frequencyStep10;
        if (currentFrequency < minFrequency)
            currentFrequency = minFrequency;
        UpdateFrequencyText();
    }

    private void UpdateFrequencyText()
    {
        if (frequencyText != null)
            frequencyText.text = "Frequency: " + currentFrequency.ToString("F0") + " Hz";
    }

    public void StartTone()
    {
        if (!generatingTone)
        {
            generatingTone = true;
            StartCoroutine(GenerateTone());
        }
    }

    public void StopTone()
    {
        generatingTone = false;
    }

    private IEnumerator GenerateTone()
    {
        float[] samples = new float[44100]; // Assuming sample rate of 44100 Hz

        while (generatingTone)
        {
            float increment = currentFrequency * 2 * Mathf.PI / 44100;
            float angle = 0f;

            for (int i = 0; i < samples.Length; i++)
            {
                samples[i] = Mathf.Sin(angle);
                angle += increment;
            }

            audioSource.clip = AudioClip.Create("Tone", samples.Length, 1, 44100, false);
            audioSource.clip.SetData(samples, 0);
            audioSource.Play();

            yield return new WaitForSeconds(samples.Length / 44100f); // Wait for the duration of the audio clip
        }
    }
}
Leave a Comment