This project has moved and is read-only. For the latest updates, please go here.

WaveViewer loses image of waveform - problem with Position property

Feb 20, 2012 at 12:40 PM
Edited Feb 20, 2012 at 2:06 PM

Hi, this is 5th day that I can't solve this problem. Firstly I thought it was something with C# and .NET But know I lost all my hopes and will to live ;)

I made a class - AudioManager which has ability to play and record sound. It looks something like this:

private DirectSoundOut output = null;
private WaveIn input = null;
private WaveFileWriter waveWriter = null;
private WaveFileReader waveReader = null;

public void LoadFile(string fileName)
      CloseAll(); //just disposes all streams and NAudio objects, like output
      waveReader = new WaveFileReader(fileName);
      output = new DirectSoundOut();
      output.Init(new WaveChannel32(waveReader));

//plays loaded file
public void Play()
      if (_state == State.MONITORING) StopRecord();
      if (output == null) return;
      if (output.PlaybackState != PlaybackState.Playing)
          _state = State.PLAYING;

//stops playing file
public void Stop()
      if (output == null) return;
      if (output.PlaybackState != PlaybackState.Stopped)
          _state = State.STOPPED;

public WaveFileReader GetWaveReader()
      return waveReader;


Recording is fine, so I don't show it here. Anyway, after recording a file, I do some cutting in this file - simply I cut part of the wave file. This works fine also. Then I load new(cutted) file into my AudioManager, and want to show new waveform:

JuhasWaveViewer wv = GetWaveViewer(clickedIndex); //JuhasWaveViewer is standard NAudio.WaveViewer with some new features like colors and cutting

string filename = GetCheckedFileName(); //this filename resides in TAG property of WaveViewer
wv.CutSelection(filename); //cuts some part of file and saves the new file with the same name

am.LoadFile(filename); //loads this new file to my AudioManager

//now I want to draw new waveform. _ws is my private WaveStream
wv.WaveStream = null;
_ws = new RawSourceWaveStream(am.GetWaveReader(), am.GetWaveReader().WaveFormat);
wv.WaveStream = _ws;


(I use _ws just for some debugging, normally I would do just: wv.WaveStream = new RawSourceWaveStream....)
Until now everything works and WaveViewer shows new waveform. I can resize my WaveViewer and everything works fine. But, then I want to play this new file. So I use am.Play() method. It plays - I can hear that this is the cutted file. But now, when I want to resize my waveStream or do anything else with it, it loses waveform and shows just white field with red x-like cross. I was checking the memory and found that _ws is in different place in memory than it was on the start, but wv.WaveStream still points to _ws, so they are still connected. So what is the issue? What am I doing wrong?

I even tried to call Invalidate on waveViewer, but it doesn't work. Please help me


I got into WaveViewer OnPaint method and the problem is connected to Position property. Here is print screen of what debugger says:

("Object reference not set to an instance of an object."). What's wrong with this?


OK, having looked in source, I realised that RawSourceWaveStream is just some strange class that does nothing. I thought it would have a copy of WaveFileReader, not the WaveFileReader. And the Position property calls WaveFileReaders Position property. WHY? I tried something like that:

wv.WaveStream = new WaveFileReader(filename);

instead of:

wv.waveStream = new RawSourceWaveStream(am.GetWaveReader(), am.GetWaveReader().WaveFormat);

But now problem is the file which is opened exlusively by WaveFileReader. Why is that?



Feb 26, 2012 at 4:58 PM

why are you using RawSourceWaveStream for a WAV file? Just use WaveFileReader. The point of it is when you have raw audio data in a stream/file but no WaveFormat info.