private bool ConnectSpeakers(PropagatingAudioSource host) { if (!IsConnected(host)) { PropagatingSoundGateway[] gateways = RoomToNeighbourPositions[host.HostRoom]; PropagatingSpeaker[] speakers = new PropagatingSpeaker[gateways.Length]; for (int i = 0; i < speakers.Length; i++) { PropagatingSpeaker speaker = GetNextFree(speakers.Length); if (speaker == null) { FreeSpeakers(speakers); return(false); } else { speaker.Dampening = gateways[i].Dampening; speakers[i] = speaker; } } // All required speakers are available - position them correctly for (int i = 0; i < speakers.Length; i++) { speakers[i].audioSource.transform.position = gateways[i].SoundTarget; SpeakerToOrigin[speakers[i]] = host; } ConnectedSpeakers[host] = speakers; } return(true); }
public void Disconnect(PropagatingAudioSource host) { if (ConnectedSpeakers.ContainsKey(host)) { PropagatingSpeaker[] speakers = ConnectedSpeakers[host]; FreeSpeakers(speakers); } }
public void ForwardPlayOneShot(PropagatingAudioSource host, AudioClip clip, float volume) { if (ConnectSpeakers(host)) { PropagatingSpeaker[] speakers = ConnectedSpeakers[host]; foreach (PropagatingSpeaker speaker in speakers) { speaker.audioSource.PlayOneShot(clip, volume * speaker.Dampening); } } }
public void UpdateClip(PropagatingAudioSource host, AudioClip clip) { if (IsConnected(host)) { PropagatingSpeaker[] speakers = ConnectedSpeakers[host]; foreach (PropagatingSpeaker speaker in speakers) { speaker.audioSource.clip = clip; } } }
public void UpdateVolume(PropagatingAudioSource host, float volume) { if (IsConnected(host)) { PropagatingSpeaker[] speakers = ConnectedSpeakers[host]; foreach (PropagatingSpeaker speaker in speakers) { speaker.audioSource.volume = volume * speaker.Dampening; } } }
public void ForwardStop(PropagatingAudioSource host) { if (IsConnected(host)) { PropagatingSpeaker[] speakers = ConnectedSpeakers[host]; foreach (PropagatingSpeaker speaker in speakers) { speaker.audioSource.Stop(); } FreeSpeakers(speakers); } }
public void ForwardPlay(PropagatingAudioSource host, AudioClip clip, float volume, bool loop) { if (ConnectSpeakers(host)) { PropagatingSpeaker[] speakers = ConnectedSpeakers[host]; foreach (PropagatingSpeaker speaker in speakers) { speaker.audioSource.clip = clip; speaker.audioSource.volume = volume * speaker.Dampening; speaker.audioSource.loop = loop; speaker.audioSource.Play(); } } }
private void FreeSpeaker(PropagatingSpeaker speaker) { if (SpeakerToOrigin.ContainsKey(speaker)) { PropagatingAudioSource origin = SpeakerToOrigin[speaker]; SpeakerToOrigin.Remove(speaker); if (ConnectedSpeakers.ContainsKey(origin)) { ConnectedSpeakers.Remove(origin); } } speaker.Connected = false; }
private bool GarbageCollect(int required) { int collected = 0; for (int i = 0; i < audioSourcePool.Length; i++) { if (!audioSourcePool[i].IsPlaying) { PropagatingSpeaker registered = audioSourcePool[i]; if (SpeakerToOrigin.ContainsKey(registered)) { PropagatingAudioSource origin = SpeakerToOrigin[registered]; FreeSpeakers(ConnectedSpeakers[origin]); collected++; } } } return(collected >= required); }
private bool IsConnected(PropagatingAudioSource host) => ConnectedSpeakers.ContainsKey(host);