using SharpDX.XAudio2; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using Windows.Storage; using SharpDX.Multimedia; namespace DrumKit.Repository { class SoundRepository { #region Properties public StorageFolder RepositoryLocation { get; private set; } public Dictionary LoadedSounds { get; private set; } #endregion #region Constructor /// /// Creates a new instance of sound repository. /// public SoundRepository(StorageFolder where) { this.RepositoryLocation = null; this.LoadedSounds = new Dictionary(); this.Initialize(where); } #endregion #region Initialize /// /// Initializes the repository set to one location. /// private void Initialize(StorageFolder where) { // Set up location this.RepositoryLocation = where; Log.Write("[SoundRepository] Location set: {0}", where.Path); } #endregion /// /// Loads a sound into memory. /// /// The id of the drum which will hold the sound. /// A sound source. private async Task LoadSound(string drumid, SoundSource source) { // Get file StorageFile file = await IOHelper.GetFileRelativeAsync(RepositoryLocation, source.Source); // Open file var stream = await file.OpenReadAsync(); var iostream = stream.AsStream(); var soundStream = new SoundStream(iostream); // Read data var buffer = new AudioBuffer() { Stream = soundStream, AudioBytes = (int)soundStream.Length, Flags = BufferFlags.EndOfStream }; iostream.Dispose(); // Create sound object Sound sound = new Sound(); sound.Buffer = buffer; sound.DecodedPacketsInfo = soundStream.DecodedPacketsInfo; sound.WaveFormat = soundStream.Format; // Add sound to dictionary this.LoadedSounds.Add(String.Format("{0}#{1}", drumid, source.Intensity), sound); } /// /// Loads all the sounds associated with a drum to memory. /// /// The drum. public async Task LoadSounds(Drum drum) { // Load each sound foreach (var i in drum.Sounds) await this.LoadSound(drum.Id, i); } /// /// Unloads all the sounds associated with a drum from memory. /// /// The drum public void UnloadSounds(Drum drum) { // Unload each sound foreach (var i in drum.Sounds) { string key = String.Format("{0}#{1}", drum.Id, i.Intensity); if (this.LoadedSounds.ContainsKey(key)) this.LoadedSounds.Remove(key); } } /// /// Gets a loaded sound from the dictionary. /// /// ID of the drum. /// Sound intensity. public Sound? GetLoadedSound(string drumid, int intensity) { Sound sound; string key = String.Format("{0}#{1}", drumid, intensity); // Try to get sound if (!this.LoadedSounds.TryGetValue(key, out sound)) return null; // OK return sound; } /// /// Cleans the currently used resources. /// public void Dispose() { this.LoadedSounds.Clear(); } } }