The BASS_STREAMPROC_END flag tells BASS that there is no more data to come, so you shouldn't set that if there will be more data coming.
If you're using the same "MemoryStream" for reading and writing, you will need to synchronize access to it so that the STREAMPROC and DSPPROC functions aren't ever trying to read and write at the same time. I believe .Net provides a "lock" statement that you can use for that, perhaps something like this...
private void DSPCallback(int Handle, int Channel, IntPtr Buffer, int Length, IntPtr User) {
byte[] Buf = new byte[Length];
Marshal.Copy(Buffer, Buf, 0, Length);
lock (this.RawStorage) {
this.RawStorage.Position = writepos;
this.RawStorage.Write(Buf , 0, length);
writepos = this.RawStorage.Position;
}
}
...
private int StreamRead(int Handle, IntPtr Buffer, int Length, IntPtr User) {
if (this.RawStorage != null) {
byte[] Data = new byte[Length];
int BytesRead;
lock (this.RawStorage) {
this.RawStorage.Position = readpos;
BytesRead = this.RawStorage.Read(Data, 0, Length);
}
readpos += BytesRead;
Marshal.Copy(Data, 0, Buffer, Length);
return BytesRead;
}
else {
return 0;
}
}