Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question I am working on project and need help

Discussion in 'Audio & Video' started by LukaLevecBorovsak2010, Dec 3, 2023.

  1. LukaLevecBorovsak2010

    LukaLevecBorovsak2010

    Joined:
    Dec 2, 2023
    Posts:
    1
    i want to make so line is drawn based on live bD and Hz from microphone and that you can sellect colors and it will not effect line drawn before only line drawn after but unity is showing me these problems:
    Posnetek zaslona 2023-12-03 100847.png Posnetek zaslona 2023-12-03 100857.png that is the code :
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class NewBehaviourScript : MonoBehaviour
    {
    private AudioSource audioSource;
    private float[] samples;
    private LineRenderer lineRenderer;
    private Color currentColor = Color.red; // Default line color
    void Start()
    {
    audioSource = GetComponent<AudioSource>();
    lineRenderer = GetComponent<LineRenderer>();
    if (audioSource == null)
    {
    Debug.LogError("Audio Source not found!");
    return;
    }
    if (lineRenderer == null)
    {
    Debug.LogError("Line Renderer not found!");
    return;
    }
    lineRenderer.startWidth = 0.1f;
    lineRenderer.endWidth = 0.1f;
    lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
    lineRenderer.startColor = currentColor;
    lineRenderer.endColor = currentColor;
    samples = new float[audioSource.clip.samples * audioSource.clip.channels];
    }
    void Update()
    {
    if (audioSource == null || lineRenderer == null)
    return;
    audioSource.clip.GetData(samples, 0);
    float db = CalculateDecibelLevel(samples);
    float hz = CalculateFrequency(samples, audioSource.clip);
    DrawLineBasedOnData(db, hz);
    }
    float CalculateDecibelLevel(float[] samples)
    {
    // Implementation of loudness (dB) calculation based on the data from the microphone
    // ...
    return 0f; // Returns the calculated volume (dB)
    }
    float CalculateFrequency(float[] samples, AudioClip clip)
    {
    // Implementation of calculation of frequency (Hz) according to the data from the microphone
    // ...
    return 0f; // Returns the calculated frequency (Hz)
    }
    void DrawLineBasedOnData(float db, float hz)
    }
    and if anyone could please help me fix it
    thank you
     

    Attached Files: