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

Author Topic: SoundStream Timing  (Read 2739 times)

0 Members and 1 Guest are viewing this topic.

rok

  • Newbie
  • *
  • Posts: 3
    • View Profile
SoundStream Timing
« on: May 22, 2015, 05:17:27 pm »
I want to use SoundStream to play the data I'm giving to it. The audio in this example is loaded from a normal buffer so I get the basics before moving on. I'm timing the swapping of data with Thread.Sleep but I guess it's not the right way to do it. There's some overlapping happening while playing.

Without the "correct" amount of Thread.Sleep the sound is even worse.

1. How should I do the timing of swapping the data so the playback will be as smooth as possible?

2. It's not possible to somehow append the data instead of overwriting right?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML.Audio;
using System.Threading;
 
namespace Stream
{
 
    class MyStream : SoundStream
    {
 
        short[] current_samples;
        public MyStream()
        {
            Initialize(1, 44100);
        }
 
        protected override bool OnGetData(out short[] samples)
        {
            samples = current_samples;
            return true;
        }
 
        protected override void OnSeek(SFML.System.Time timeOffset)
        {
            Console.WriteLine(timeOffset);
        }
 
        public void Split(short[] samples, short[] left, short[] right)
        {
            int c = 0;
            for (int i = 0; i < samples.Length - 1; i += 2)
            {
                left[c] = samples[i];
                right[c] = samples[i + 1];
                c++;
            }
        }
 
        public void SetSample(short[] novi)
        {
            current_samples = novi;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            SoundBuffer buffer = new SoundBuffer("some_music.wav");
 
            short[] sound_left = new short[buffer.Samples.Length / 2];
 
            short[] sound_right = new short[buffer.Samples.Length / 2];
 
            MyStream s = new MyStream();
 
            s.Split(buffer.Samples, sound_left, sound_right);
 
            short[] smallBuffer = new short[4410];
 
            for (int i = 0; i < sound_left.Length - 4410; i += 4410)
            {
                for (int l = 0; l < 4410; l++)
                {
                    smallBuffer[l] = sound_left[i + l];
                }
 
                s.SetSample(smallBuffer);
               
                if (i == 0)
                {
                    s.Play();
                }
 
                Thread.Sleep(100);
            }
        }
    }
}
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: SoundStream Timing
« Reply #1 on: May 22, 2015, 05:36:39 pm »
SoundStream.OnGetData(...) is already called from another thread and the return result is put into a buffer (actually a triple buffer). All you should be doing is generating/loading your samples from within that function. Don't try to do it in the main thread and synchronize it yourself. Not to mention that since the OnGetData(...) function is called from another thread your code isn't thread safe.
« Last Edit: May 22, 2015, 05:40:16 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

rok

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SoundStream Timing
« Reply #2 on: May 22, 2015, 06:19:41 pm »
But how should I change the data inside the function itself (in a for loop)? I just don't get it. ???


protected override bool OnGetData(out short[] samples) {
    short[] smallBuffer = new short[4410];
    // FOR LOOP HERE
    samples = smallBuffer;
    return true;
}
 

Could I get some short example or concept?



Here's how I changed it now but that does still not work.

public MyStream()
        {
            buffer = new SoundBuffer("C:/Users/Rok/Desktop/Reverbi/Sampli/chill2.wav");

            sound_left = new short[buffer.Samples.Length / 2];

            sound_right = new short[buffer.Samples.Length / 2];

            Split(buffer.Samples, sound_left, sound_right);

            Initialize(1, 44100);

        }

        protected override bool OnGetData(out short[] samples)
        {
            short[] smallBuffer = new short[4410];

            for (int i = 0; i < sound_left.Length - 4410; i += 4410)
            {
                if (i > sound_left.Length - 8820)
                {
                    i = 1;
                }

                for (int l = 0; l < 4410; l++)
                {
                    smallBuffer[l] = sound_left[i + l];
                }

                Console.WriteLine(i);

                //SetSample(smallBuffer);

                Console.WriteLine("["+i+"] Offset: " + PlayingOffset.AsSeconds());
            }

            samples = smallBuffer;

            return true;
        }
 
« Last Edit: May 22, 2015, 06:34:38 pm by rok »

rok

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: SoundStream Timing
« Reply #3 on: May 22, 2015, 07:07:42 pm »

Solved it that way somehow. Going to try how it works when I do all the FFT and convolution inside it  :D

 

anything