Ejemplo n.º 1
0
 /// <summary>
 /// Reads bytes from this wave stream
 /// </summary>
 /// <param name="destBuffer">The destination buffer</param>
 /// <param name="offset">Offset into the destination buffer</param>
 /// <param name="numBytes">Number of bytes read</param>
 /// <returns>Number of bytes read.</returns>
 public override int Read(byte[] destBuffer, int offset, int numBytes)
 {
     lock (lockObject)
     {
         int bytesWritten = 0;
         // 1. fill with silence
         if (position < audioStartPosition)
         {
             bytesWritten = (int)Math.Min(numBytes, audioStartPosition - position);
             for (int n = 0; n < bytesWritten; n++)
             {
                 destBuffer[n + offset] = 0;
             }
         }
         if (bytesWritten < numBytes)
         {
             // don't read too far into source stream
             int sourceBytesRequired = (int)Math.Min(
                 numBytes - bytesWritten,
                 sourceLengthBytes + sourceOffsetBytes - (sourceStream.Position * bytesPerSample));
             int read = sourceStream.Read(destBuffer, bytesWritten + offset, sourceBytesRequired);
             bytesWritten += read;
         }
         // 3. Fill out with zeroes
         for (int n = bytesWritten; n < numBytes; n++)
         {
             destBuffer[offset + n] = 0;
         }
         position += numBytes;
         return(numBytes);
     }
 }
Ejemplo n.º 2
0
        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();
            }
        }