nBASS, C# and CreateSample

Started by CakeDeath, 28 Mar '03 - 19:51

CakeDeath

Hi, I hope this is the right place to ask...

I'm trying to create a tone in C# using nBASS. I can fill up an array with samples, and when I call

bass.CreateSample(data, 44100, 1, SampleInfoFlags.Mono);

Everything works fine, and I get 2 seconds of tone. (So my array of samples is OK).

But I created the sample to be stereo. It has interleaved samples for left channel/right channel.

bass.CreateSample(data, 44100, 1, SampleInfoFlags.Default);

Should give me 1 second of stereo tone (I think), but it doesn't.
It gives me 1 second of tone, 1 second of white noise.

So... does it only create mono samples? Is it expecting non-interleaved? Where is the white noise coming from?

4 hours later, I just have to ask for Help please!

Ian @ un4seen

Looks to me like there's a small bug in the CreateSample function (BASSInternal.cs). It should check for stereo, and adjust the "length" parameter accordingly (number of samples, not shorts). Try changing it to this...
     public Sample CreateSample(short[] data, int freq, int max, SampleInfoFlags flags)
      {
         if (this.disposed)
            throw new ObjectDisposedException("BASS");

// changes start
         int samlength=data.Length;
         if (!(flags&SampleInfoFlags.Mono)) samlength/=2;
         IntPtr memloc = _CreateSample(samlength, freq, max, (int) flags);
// changes end

         Marshal.Copy(data, 0, memloc, data.Length);
         if (memloc == IntPtr.Zero) throw new BASSException();
         IntPtr sample = _CreateSampleDone(); /// ???????????
         if (sample == IntPtr.Zero) throw new BASSException();
         return new Sample( sample );
      }
I don't use .Net/C# myself, so this may not be exactly correct, but you get the idea :)

CakeDeath

Aha. I've just tried your code, and that is the problem.

Your test needs a slight tweak to compile;

if (0==((int)SampleInfoFlags.Mono & (int)flags)) samlength/=2;

But now it works great.
Thank you very much. :)