Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ReadMusic : MonoBehaviour
- {
- public SoundFXHandler soundFXHandler;
- public MusicHandler inputMusic;
- private List<AudioSource> audioQueue = new List<AudioSource>();
- private List<AudioSource> clipsPlaying = new List<AudioSource>();
- private List<AudioSource> clipsPlayed = new List<AudioSource>();
- void Start()
- {
- soundFXHandler = FindObjectOfType<SoundFXHandler>();
- PrepSong();
- PrepSong();
- Debug.Log("Measure to go to: " + inputMusic.Measure);
- Debug.Log("Test: " + (inputMusic.Measure - 1));
- }
- void Update()
- {
- playUpTo(2);
- }
- public void PrepSong()
- {
- foreach (MusicSection section in inputMusic.Sections)
- {
- if (audioQueue.Count > 0)
- foreach (AudioSource audioSource in audioQueue)
- if (audioSource.clip == section.ReadAudio())
- return;
- audioQueue.Add(soundFXHandler.GetSound2DRaw(section.ReadAudio()));
- }
- }
- public void playUpTo(int measure)
- {
- foreach (AudioSource audio in clipsPlaying)
- if(!audio.isPlaying)
- audio.Play();
- if (audioQueue.Count == 0)
- PrepSong();
- if (clipsPlayed.Count >= inputMusic.Measure - 1)
- {
- Debug.Log("looping song");
- // Loop song
- }
- else
- {
- // Play song
- if (clipsPlaying.Count < 1) // Checks if clips playing list is empty
- foreach (AudioSource source in audioQueue)
- if (clipsPlayed.IndexOf(source) < 0)
- {
- Debug.Log("Breaking at " + audioQueue.IndexOf(source)); // Outputs the clip that is needed to be played
- audioQueue.Remove(source);
- clipsPlaying.Add(source);
- break;
- }
- else
- {
- Debug.Log("Continuing...");
- continue; // potentially not needed
- }
- else
- if (clipsPlaying[0].time >= (clipsPlaying[0].clip.length - 0.035f))
- Debug.Log("Attempting to add another clip");
- foreach (MusicSection musicSection in inputMusic.Sections)
- if (musicSection.ReadAudio() == audioQueue[0].clip)
- if (clipsPlaying[0].time >= (clipsPlaying[0].clip.length - 0.035f)) // -0.035f to get a low margin of error but allowing us to check when clip is over. We then need to subtract the ms of the next section
- {
- Debug.Log("something happened");
- if (clipsPlayed.Count < inputMusic.Measure)
- {
- Debug.Log("Next Clip");
- if(clipsPlaying.IndexOf(audioQueue[0]) < 0)
- clipsPlaying.Add(audioQueue[0]);
- if(clipsPlayed.IndexOf(clipsPlaying[0]) < 0)
- {
- clipsPlayed.Add(clipsPlaying[0]);
- clipsPlaying.RemoveAt(0);
- }
- }
- else
- {
- Debug.Log("Looping Clip");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment