private void OnTimeQuantumEvent(TimeQuantumEvent timeQuantumEvent) { _soundsToStop.Clear(); foreach (var kvPair in _soundControllers) { kvPair.Value.IncrementTimeQuantumCounter(); if (kvPair.Value.IsTimeToStop()) { _soundsToStop.Add(kvPair.Key); } } foreach (int soundId in _soundsToStop) { AbstractSoundController soundController = _soundControllers[soundId]; _soundControllers.Remove(soundId); soundController.Stop(); } PlayableSound playableSound; while ((playableSound = _queue.GetNextForTimeQuantum(timeQuantumEvent.timeQuantumNumber)) != null) { AbstractSoundController soundController = _instrument.PlayNote( playableSound.pitch, playableSound.volume, playableSound.durationTimeQuanta); if (soundController != null) { _soundControllers[_soundCounter++] = soundController; } } }
private void OnTimeQuantumEvent(TimeQuantumEvent timeQuantumEvent) { foreach (var kv in _sounds) { Sound sound = kv.Value; if (!sound.gameObject.activeSelf) { sound.gameObject.SetActive(true); } int beatGridIndex = sound.playableSound.startTimeQuantumNumber - timeQuantumEvent.timeQuantumNumber; float x = beatGridIndex * _timeQuantumStep; Pitch pitch = sound.playableSound.pitch; int pitchGridIndex = (int)pitch.note + NOTE_GRID_OFFSET + (pitch.octave + OCTAVE_GRID_OFFSET) * Defines.SEMITONES_COUNT; pitchGridIndex += _pitchGridIndexOffset; float y = pitchGridIndex * _pitchStep; sound.transform.localPosition = new Vector3(x, y, Z_DEPTH); if (beatGridIndex + sound.playableSound.durationTimeQuanta < -PLAYED_SOUNDS_TIME_TO_LIVE * _beatManager.timeQuantaPerBeat) { _soundsToRemove.Add(sound.playableSound.id); } if (beatGridIndex <= 0 && Mathf.Abs(beatGridIndex) < sound.playableSound.durationTimeQuanta) { sound.SetColor(_playingSoundColor); } else { sound.SetColor(_defaultSoundColor); } onSoundUpdated?.Invoke(sound); } foreach (int id in _soundsToRemove) { Destroy(_sounds[id].gameObject); _sounds.Remove(id); onSoundRemoved?.Invoke(id); } _soundsToRemove.Clear(); }
private void FireTimeQuantumEvent() { TimeQuantumEvent timeQuantumEvent = new TimeQuantumEvent(_timeQuantumCounter); _timeQuantumEventListeners.ForEach(listener => listener.action.Invoke(timeQuantumEvent)); }