protected override void OnCreate() { this._samples = new AudioSampleSet(GetAudioSamples()); this._prefab = this.EntityManager.CreateEntity(typeof(PlaysOneShotAudioSample), typeof(Prefab)); this.EntityManager.SetName(this._prefab, "One shot audio"); this.EntityManager.SetSharedComponentData(this._prefab, new PlaysOneShotAudioSample(this._samples)); }
protected override void OnCreate() { if (!WaveHelper.TryReadWav( Path.Combine(Application.dataPath, "Music", "short.wav"), //Path.Combine(Application.dataPath, "Music", "bensound-summer.wav"), out var music )) { return; } // try read uses temporary allocation so we resample with more persistant allocation this._music = new AudioSampleSet(music.Initialize(AudioSettings.outputSampleRate)); music.Dispose(); this.EntityManager.SetSharedComponentData( this.EntityManager.CreateEntity(typeof(ChangesMusicTrack)), new ChangesMusicTrack(this._music) ); }
public void ChangeMusicTrack(AudioSampleSet sample) { var random = sample.RandomSample(); if (!random.HasValue) { return; } // swap the current and previous var current = this._previous; this._previous = this._current; this._current = current; using (var block = this._graph.CreateCommandBlock()) { block.TriggerAudioSample( this._current.node, random.Value.ToReader(true), Categories.Music ); block.SetAttenuation( this._current.connection, 1f, AudioSettings.outputSampleRate * 15 ); block.SetAttenuation( this._previous.connection, 0f, AudioSettings.outputSampleRate * 4 ); } }
protected override void OnDestroy() { Debug.Log($"{nameof(ExampleMusicSystem)}.{nameof(this.OnDestroy)}"); this._music?.Dispose(); this._music = null; }
protected override void OnDestroy() { Debug.Log($"{nameof(ExampleOneShotSystem)}.{nameof(this.OnDestroy)}"); this._samples?.Dispose(); this._samples = null; }
public ChangesMusicTrack(AudioSampleSet samples) { this.samples = samples; }
public static AudioSampleData?RandomSample(this AudioSampleSet set) => set.samples.Length != 0 ? set.samples[UnityEngine.Random.Range(0, set.samples.Length)] : (AudioSampleData?)null;
public PlaysOneShotAudioSample(AudioSampleSet samples) { this.samples = samples; }