Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Extending SoundStream/C#, Lack of examples? Not working?  (Read 2712 times)

0 Members and 1 Guest are viewing this topic.

sfml-ah

  • Newbie
  • *
  • Posts: 3
    • View Profile
Extending SoundStream/C#, Lack of examples? Not working?
« on: March 01, 2011, 02:38:30 pm »
I'm working on DSP-related processing where I need to process the data received (Samples) from the DSP as a Sound Stream i.e. to play the data for starters...
The following is an attempt to achieve such functionality:
Code: [Select]

   public class AudioStream : SoundStream {

        private List<short> myBuffer;
        private int offset;
        private int myBufferSize;

        public AudioStream( int myBufferSize )
        {
            this.offset = 0;
            this.myBufferSize = myBufferSize;
            myBuffer = new List<short>();
        }

        protected override bool OnGetData(out short[] samples)
        {
            samples = null;
            if (offset + myBufferSize >= myBuffer.Count)
                return false;
            samples = new short[myBuffer.Count - offset];
            myBuffer.CopyTo(samples, offset);
            offset += myBuffer.Count - offset;
            return true;
        }

        protected override void OnSeek(float timeOffset)
        {
            offset =  Convert.ToInt16(timeOffset);
        }

        public void setData(short[] samples) {
            myBuffer.AddRange(samples);
        }
        public void clearBuffer()
        {
            myBuffer.Clear();
            offset = 0;
        }
    }

///Usage
if(audioStream.Status == SoundStatus.Stopped)
       audioStream.clearBuffer();
audioStream.setData(sdata);
if (audioStream.Status != SoundStatus.Playing)
      audioStream.Play();

Problem: The above usage part causes the AudioStream to Stop playing, and if the clearBuffer() is removed it replays old data.
Question: Where did I went wrong? and if so, how to correct/make it work?
Note: the data is received via a function....
Thanks in advance for your help...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Extending SoundStream/C#, Lack of examples? Not working?
« Reply #1 on: March 01, 2011, 02:46:40 pm »
Quote
The above usage part causes the AudioStream to Stop playing

When does it stop? The code above only shows how your stream starts, we don't know what happens next.

Quote
and if the clearBuffer() is removed it replays old data

This is normal, since you always give the same samples to play in OnGetData.

Your code also doesn't show how your stream is updated while playing, which is probably the most important thing.
Laurent Gomila - SFML developer

sfml-ah

  • Newbie
  • *
  • Posts: 3
    • View Profile
Temporary Solution.....
« Reply #2 on: March 02, 2011, 04:54:27 pm »
Thanks for the fast reply. You have a good Community/Support.
Below you will find the required information:

- The code below shows how the data is being updated inside the stream. The Code is inside an event handler that is called when new data or sdata is received: (sdata is 2048 = myBufferSize)
Code: [Select]

if (audioStream.Status == SFML.Audio.SoundStatus.Stopped)
{
audioStream.clearBuffer();
}
audioStream.setData(sdata); //sdata = short[2048]
if (audioStream.Status != SFML.Audio.SoundStatus.Playing)
{
audioStream.Play();
}


The problem is that the data required to be played is supplied through a function and as soon as the data supply stops for a short term the SoundStream switchs to Stopped Status because of the false return value of the OnGetData. So I need to keep the SoundStream in Playing Status even though the data supply stops for a short term.
Supplying empty/zero array causes ticks in the played audio, thus a temporary solution is realized using the EventWaitHandle class.
The functionality required is similar to the Bass Audio Library Stream Capability, where the data is just "pushed" into the stream resulting in a continuous audio output even if the data supply stops for a short term. But of-course then I have a bigger problem with licensing.

The code below corresponds to the temporary solution:
Code: [Select]

public class AudioStream : SoundStream
    {

        private int myBufferSize;
        private uint channelCount;
        private uint sampleRate;

        private short[] tempData;

        private static EventWaitHandle ewh;
        private bool play;

        public AudioStream(int myBufferSize, uint channelCount, uint sampleRate)
        {
            this.myBufferSize = myBufferSize;
            this.channelCount = channelCount;
            this.sampleRate = sampleRate;
            Initialize(this.channelCount, this.sampleRate);
            tempData = new short[myBufferSize];
            ewh = new EventWaitHandle(false, EventResetMode.AutoReset);
            play = true;
        }

        protected override bool OnGetData(out short[] samples)
        {
            ewh.WaitOne();
            samples = tempData;
            if (!play)
                return false;
            return true;
        }

        protected override void OnSeek(float timeOffset)
        {
            /////
        }

        public void setData(short[] samples)
        {
            samples.CopyTo(tempData, 0);
            ewh.Set();
        }

        public void unblock() {
            play = false;
            ewh.Set();
        }


Note: I switch the Audio Library of the whole project from PortAudio to SFML because, I can achieve the same functionality with less pain !!!!
And I'm using the 2.0 version. Any how-to for the OnSeek function is appreciated.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Extending SoundStream/C#, Lack of examples? Not working?
« Reply #3 on: March 02, 2011, 06:41:47 pm »
That's right, SFML streams are designed so that they stop when they run out of samples. You're supposed to feed them continuously. But even if your stream stops, why is this a problem?

Quote
Any how-to for the OnSeek function is appreciated

It depends on your data source. There are some situations where this function cannot be implemented. For example, if you can't seek your source (the thing that collects samples and calls setData), you will probably not be able to implement it.
Laurent Gomila - SFML developer

sfml-ah

  • Newbie
  • *
  • Posts: 3
    • View Profile
When the Stream Stops.....
« Reply #4 on: March 02, 2011, 11:47:53 pm »
Thanks again for the fast reply.

When the stream stops, I try to check for the status after supplying new data and change it to Playing Status by calling Play().

This action causes the resulting Audio Output to have a tick-sound at the very start of each call to Play() ( where the data supply stops for a short term ), which in turn confuses the Noise Reduction Group.

So using the temporary solution mentioned previously, allows me to avoid calling Play(), thus no tick-sound in the Audio Output.


Note: The received Audio Data (sdata) corresponds to RF with LSB, USB, FM, or AM modulation for now. And you're right concerning the OnSeek() function. And why is OnStart() removed in 2.0?


Thanks in advance for your help.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Extending SoundStream/C#, Lack of examples? Not working?
« Reply #5 on: March 03, 2011, 08:05:25 am »
Quote
This action causes the resulting Audio Output to have a tick-sound at the very start of each call to Play() ( where the data supply stops for a short term ), which in turn confuses the Noise Reduction Group.

Even with SFML 2.0?
When you start the stream, it calls OnGetData 3 times (it fills all its internal buffers), so make sure that you have enough data before calling Play(). However I can see how this behaviour is not suited to your situation, and there's probably something to improve in the SoundStream class.

Quote
And why is OnStart() removed in 2.0?

OnStart is now OnSeek(0) ;)
Laurent Gomila - SFML developer

 

anything