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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - rok

Pages: [1]
1
Audio / 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);
            }
        }
    }
}
 

Pages: [1]