A game framework written with osu! in mind.
at master 61 lines 1.9 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System; 5using System.Collections.Generic; 6using System.IO; 7using System.Threading.Tasks; 8using JetBrains.Annotations; 9using osu.Framework.Audio.Mixing; 10using osu.Framework.IO.Stores; 11 12namespace osu.Framework.Audio.Track 13{ 14 internal class TrackStore : AudioCollectionManager<AdjustableAudioComponent>, ITrackStore 15 { 16 private readonly IResourceStore<byte[]> store; 17 private readonly AudioMixer mixer; 18 19 internal TrackStore([NotNull] IResourceStore<byte[]> store, [NotNull] AudioMixer mixer) 20 { 21 this.store = store; 22 this.mixer = mixer; 23 24 (store as ResourceStore<byte[]>)?.AddExtension(@"mp3"); 25 } 26 27 public Track GetVirtual(double length = double.PositiveInfinity) 28 { 29 if (IsDisposed) throw new ObjectDisposedException($"Cannot retrieve items for an already disposed {nameof(TrackStore)}"); 30 31 var track = new TrackVirtual(length); 32 AddItem(track); 33 return track; 34 } 35 36 public Track Get(string name) 37 { 38 if (IsDisposed) throw new ObjectDisposedException($"Cannot retrieve items for an already disposed {nameof(TrackStore)}"); 39 40 if (string.IsNullOrEmpty(name)) return null; 41 42 var dataStream = store.GetStream(name); 43 44 if (dataStream == null) 45 return null; 46 47 TrackBass trackBass = new TrackBass(dataStream); 48 49 mixer.Add(trackBass); 50 AddItem(trackBass); 51 52 return trackBass; 53 } 54 55 public Task<Track> GetAsync(string name) => Task.Run(() => Get(name)); 56 57 public Stream GetStream(string name) => store.GetStream(name); 58 59 public IEnumerable<string> GetAvailableResources() => store.GetAvailableResources(); 60 } 61}