This project has moved. For the latest updates, please go here.

Playing from MemoryStream

Jun 11, 2013 at 3:28 PM
Hi, I need help with playing from MemoryStream
I'm working on recorder that can continue record to existing file. For this I use general stream for WaveOut and WaveIn. Everything work fine except one moment:
when I start playing the file and changing Current Time Position then from time to time instead of normal sound I have scream or loud noise (I don't know how to describe it on English :) ).

I created the file like this:
using (var fileWaveStream = new MediaFoundationReader(FileName))
                    {
                        var buffer = new byte[fileWaveStream.Length];
                        var readBytes = fileWaveStream.Read(buffer, 0,
                        buffer.Length);
                        AudioMemoryStream.Write(buffer, 0, readBytes);
                        AudioStream = new RawSourceWaveStream(
                                       AudioMemoryStream,    
                                       fileWaveStream.WaveFormat);
                     }
What is wrong with such approach? Maybe I need to use something instead of RawSourceWaveStream?
But I need to have MemoryStream as I'm reading from it and also writing WaveIn data to the end of that file.
Coordinator
Jun 12, 2013 at 1:40 PM
the loud noise when you reposition is probably you repositioning by something that is not a multiple of BlockAlign. Also, you can't write to a memory stream while reading from it. There will be a conflict over what the current Position is.
Jun 12, 2013 at 9:22 PM
Edited Jun 12, 2013 at 9:32 PM
the loud noise when you reposition is probably you repositioning by something that is not a multiple of BlockAlign
No, it isn't related, I have such issue when change stream time, and I have such issue from time to time (One time I have noise and in another time after scroling to the same time position everything works fine).
Also, you can't write to a memory stream while reading from it
Yes, I understand it, I don't do this. I described playing and recording because this explains why I use Memory Stream (this is importand because I don't have this issue when playing using MediaFoundationReader or Mp3FileReader etc.)
Jul 29, 2013 at 9:21 PM
Hi Mark, sorry for the late response, have a LOT of todo items :(
I discovered my problem and want to share this with you and other guys (who knows, maybe others will have the same):

In my function which change current time I had this one
_audioStream.CurrentTime = TimeSpan.FromSeconds(positionInSeconds);
where _audioStream is instance of RawSourceWaveStream. And if after this operation our stream position was odd number (and in mono we have 2 bytes for sample, so not odd number indicate that we move stream position to the middle of some sample) than we will have very loudness noise.

My fix is to add this lines after changing of stream position
if (_audioMemoryStream.Position % 2 != 0)
{
    _audioMemoryStream.Seek(1, SeekOrigin.Current);
}
It's tested only for mono audio and I believe that general fix is to move position considering BlockAlogn (from WaveFormat).