Guest User

Untitled

a guest
Jul 5th, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ReadMusic : MonoBehaviour
  6. {
  7. public SoundFXHandler soundFXHandler;
  8. public MusicHandler inputMusic;
  9.  
  10. private List<AudioSource> audioQueue = new List<AudioSource>();
  11. private List<AudioSource> clipsPlaying = new List<AudioSource>();
  12. private List<AudioSource> clipsPlayed = new List<AudioSource>();
  13.  
  14. void Start()
  15. {
  16. soundFXHandler = FindObjectOfType<SoundFXHandler>();
  17. PrepSong();
  18. PrepSong();
  19. Debug.Log("Measure to go to: " + inputMusic.Measure);
  20. Debug.Log("Test: " + (inputMusic.Measure - 1));
  21. }
  22.  
  23. void Update()
  24. {
  25. playUpTo(2);
  26. }
  27.  
  28. public void PrepSong()
  29. {
  30. foreach (MusicSection section in inputMusic.Sections)
  31. {
  32. if (audioQueue.Count > 0)
  33. foreach (AudioSource audioSource in audioQueue)
  34. if (audioSource.clip == section.ReadAudio())
  35. return;
  36.  
  37. audioQueue.Add(soundFXHandler.GetSound2DRaw(section.ReadAudio()));
  38. }
  39. }
  40.  
  41. public void playUpTo(int measure)
  42. {
  43. foreach (AudioSource audio in clipsPlaying)
  44. if(!audio.isPlaying)
  45. audio.Play();
  46.  
  47. if (audioQueue.Count == 0)
  48. PrepSong();
  49.  
  50.  
  51. if (clipsPlayed.Count >= inputMusic.Measure - 1)
  52. {
  53. Debug.Log("looping song");
  54. // Loop song
  55. }
  56. else
  57. {
  58. // Play song
  59. if (clipsPlaying.Count < 1) // Checks if clips playing list is empty
  60. foreach (AudioSource source in audioQueue)
  61. if (clipsPlayed.IndexOf(source) < 0)
  62. {
  63. Debug.Log("Breaking at " + audioQueue.IndexOf(source)); // Outputs the clip that is needed to be played
  64. audioQueue.Remove(source);
  65. clipsPlaying.Add(source);
  66. break;
  67. }
  68. else
  69. {
  70. Debug.Log("Continuing...");
  71. continue; // potentially not needed
  72. }
  73. else
  74. if (clipsPlaying[0].time >= (clipsPlaying[0].clip.length - 0.035f))
  75. Debug.Log("Attempting to add another clip");
  76.  
  77. foreach (MusicSection musicSection in inputMusic.Sections)
  78. if (musicSection.ReadAudio() == audioQueue[0].clip)
  79. 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
  80. {
  81. Debug.Log("something happened");
  82. if (clipsPlayed.Count < inputMusic.Measure)
  83. {
  84. Debug.Log("Next Clip");
  85.  
  86. if(clipsPlaying.IndexOf(audioQueue[0]) < 0)
  87. clipsPlaying.Add(audioQueue[0]);
  88.  
  89. if(clipsPlayed.IndexOf(clipsPlaying[0]) < 0)
  90. {
  91. clipsPlayed.Add(clipsPlaying[0]);
  92. clipsPlaying.RemoveAt(0);
  93. }
  94. }
  95. else
  96. {
  97. Debug.Log("Looping Clip");
  98. }
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment