public void InitTracks(string songName) { if (!string.IsNullOrWhiteSpace(SongDirectory)) { MapReader = new FileMapReader(SongDirectory + "\\mapping.txt"); bool isMSPInit = false; string[] tracks = MapReader.GetAvailableTracks(songName); string musicDirectory = SongDirectory + "\\"; loopSampleProviders = new List <LoopSampleProvider>(); volumeSampleProviders = new Dictionary <string, VolumeSampleProvider>(); foreach (var track in tracks) { string trackFile = MapReader.GetValue(songName, track); int loopStart = int.Parse(MapReader.GetValue(songName, "loop_start")); int loopEnd = int.Parse(MapReader.GetValue(songName, "loop_end")); int.TryParse(MapReader.GetValue(songName, "dly_" + track), out int startSample); var reader = new CustomVorbisWaveReader(musicDirectory + trackFile); loopSampleProviders.Add(new LoopSampleProvider(reader, loopStart, loopEnd, startSample) { Loop = Loop }); volumeSampleProviders.Add(track, new VolumeSampleProvider(loopSampleProviders.Last()) { Volume = 0.0f }); Console.WriteLine($"Added track \"{track}\" for song \"{songName}\"."); Console.WriteLine(); Console.WriteLine($"Start offset is {startSample}."); Console.WriteLine(); if (!isMSPInit) { mixingSampleProvider = new MixingSampleProvider(loopSampleProviders.First().WaveFormat); isMSPInit = true; } mixingSampleProvider.AddMixerInput(volumeSampleProviders.Last().Value); } currentSong = songName; MapReader.Dispose(); } }
/// <summary> /// Disposes this WaveStream /// </summary> protected override void Dispose(bool disposing) { if (disposing) { if (sourceStream != null) { sourceStream.Dispose(); sourceStream = null; } } else { System.Diagnostics.Debug.Assert(false, "WaveOffsetStream was not Disposed"); } base.Dispose(disposing); }
public CachedSound(Stream audioResource) { using (CustomVorbisWaveReader cvwr = new CustomVorbisWaveReader(audioResource)) { WaveFormat = cvwr.WaveFormat; List <float> wholeFile = new List <float>((int)(cvwr.Length / 4)); float[] buffer = new float[cvwr.WaveFormat.SampleRate * cvwr.WaveFormat.Channels]; int samplesRead; while ((samplesRead = cvwr.Read(buffer, 0, buffer.Length)) > 0) { wholeFile.AddRange(buffer.Take(samplesRead)); } AudioData = wholeFile.ToArray(); } }
/// <summary> /// Creates a new WaveOffsetStream /// </summary> /// <param name="sourceStream">the source stream</param> /// <param name="startSample">the time at which we should start reading from the source stream</param> /// <param name="sourceOffset">amount to trim off the front of the source stream</param> /// <param name="sourceLength">length of time to play from source stream</param> public CustomWaveOffsetStream(CustomVorbisWaveReader sourceStream, int startSample, int sourceOffset, long sourceLength) { //if (sourceStream.WaveFormat.Encoding != WaveFormatEncoding.Pcm) // throw new ArgumentException("Only PCM supported"); // TODO: add support for IEEE float + perhaps some others - // anything with a fixed bytes per sample this.sourceStream = sourceStream; position = 0; bytesPerSample = (sourceStream.WaveFormat.BitsPerSample / 8) * sourceStream.WaveFormat.Channels; StartSample = startSample; SourceOffset = sourceOffset; SourceLength = sourceLength; Console.WriteLine("SourceLength : " + SourceLength); Console.WriteLine("SourceLengthBytes : " + sourceLengthBytes); Console.WriteLine("BytesPerSample : " + bytesPerSample); Console.WriteLine(); }
/// <summary> /// Creates a WaveOffsetStream with default settings (no offset or pre-delay, /// and whole length of source stream) /// </summary> /// <param name="sourceStream">The source stream</param> public CustomWaveOffsetStream(CustomVorbisWaveReader sourceStream) : this(sourceStream, 0, 0, sourceStream.Length) { }