Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Audio Data

Discussion in 'Scripting' started by KarneeKarnay, Mar 1, 2013.

  1. KarneeKarnay

    KarneeKarnay

    Joined:
    Sep 3, 2012
    Posts:
    61
    I'm doing an experimentation with my platform game and I want to see if I can move my platforms based on music. I've tried getting the music frequency or channels but there seems to be no change in it.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class music : MonoBehaviour {
    5.    
    6.     private float music_vol_peak;
    7.     private AudioSource audioHolder;
    8.  
    9.  
    10.     // Use this for initialization
    11.     void Start (){
    12.        
    13.         audioHolder = GameObject.FindGameObjectWithTag("Music").GetComponent<AudioSource>();
    14.  
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.        
    20.         music_vol_peak = audioHolder.clip.frequency;
    21.         //Debug.Log(music_vol_peak);
    22.  
    23.     }
    24. }
    25.  
    Thats the code I've been using. Is there anyway to get something like the right and left peak volume channels?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  3. KarneeKarnay

    KarneeKarnay

    Joined:
    Sep 3, 2012
    Posts:
    61
    Thanks for the info. The only problem I get now is this one.

     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Code?
     
  5. KarneeKarnay

    KarneeKarnay

    Joined:
    Sep 3, 2012
    Posts:
    61

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class music : MonoBehaviour {
    5.    
    6.     private AudioClip music_data;
    7.     private AudioSource audioHolder;
    8.  
    9.  
    10.     // Use this for initialization
    11.     void Start (){
    12.        
    13.         audioHolder = GameObject.FindGameObjectWithTag("Music").GetComponent<AudioSource>();
    14.        
    15.         music_data = audioHolder.clip;
    16.        
    17.         float[] samples = new float[audio.clip.samples * audio.clip.channels];
    18.         audio.clip.GetData(samples, 0);
    19.         int i = 0;
    20.         while (i < samples.Length) {
    21.             samples[i] = samples[i] * 0.5F;
    22.             ++i;
    23.         }
    24.         audio.clip.SetData(samples, 0);
    25.        
    26.         Debug.Log(music_data);
    27.  
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.        
    33.        
    34.        
    35.  
    36.     }
    37. }
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    audio.clip has no meaning in your script
    you defined it as "music_data", not audio.clip....
     
  7. KarneeKarnay

    KarneeKarnay

    Joined:
    Sep 3, 2012
    Posts:
    61
    That was the original way I did it but it still came up with the error. I'll try it again and upload the new version if it doesn't work.
     
  8. KarneeKarnay

    KarneeKarnay

    Joined:
    Sep 3, 2012
    Posts:
    61
    Code (csharp):
    1. audioHolder = GameObject.FindGameObjectWithTag("Music").GetComponent<AudioSource>();
    2.        
    3.         music_data = audioHolder.clip;
    4.        
    5.         float[] samples = new float[music_data.samples * music_data.channels];
    6.         music_data.GetData(samples, 0);
    7.         int i = 0;
    8.         while (i < samples.Length) {
    9.             samples[i] = samples[i] * 0.5F;
    10.             ++i;
    11.         }
    12.         music_data.SetData(samples, 0);
    13.        
    14.         //Debug.Log(music_data);
    Same error.
     
  9. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Code is fine
    Its probably your audio clip
    change the import settings for Load Type (cant be compressed in memory) for the clip
    or perhaps use a WAV
     
  10. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    i try adjust volume of one sound with another one

    so getdatamusic[]

    for