Author Topic: nBASS, C# and CreateSample  (Read 2507 times)

CakeDeath

  • Guest
nBASS, C# and CreateSample
« on: 28 Mar '03 - 19:51 »
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

Code: [Select]

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.

Code: [Select]

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

  • Administrator
  • Posts: 22829
Re: nBASS, C# and CreateSample
« Reply #1 on: 29 Mar '03 - 14:12 »
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...
Code: [Select]
     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

  • Guest
Re: nBASS, C# and CreateSample
« Reply #2 on: 29 Mar '03 - 18:24 »
Aha. I've just tried your code, and that is the problem.

Your test needs a slight tweak to compile;

Code: [Select]

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


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