SFML community forums

Bindings - other languages => DotNet => Topic started by: trajan on October 01, 2012, 11:11:23 pm

Title: How to combine multiple sound buffer in real-time with C#?
Post by: trajan on October 01, 2012, 11:11:23 pm
Sample project:
User write to music note in textbox and push "Play"...

Process begin:
Parse music note (it does not matter)
e.g.
 re re mi mi do re mi (textbox)

SFML.Audio.SoundBuffer SndBf_re = new SoundBuffer(soundLocation + "re.wav");
SFML.Audio.SoundBuffer SndBf_mi = new SoundBuffer(soundLocation + "mi.wav");
SFML.Audio.SoundBuffer SndBf_do = new SoundBuffer(soundLocation + "do.wav");
 

How to:

SFML.Audio.Sound UserNote = new Sound(SndBf_re+SndBf_re+SndBf_mi+SndBf_mi+SndBf_do+SndBf_re+SndBf_mi);
UserNote.Play();
 

Process ended

sorry my bad English.
Title: Re: How to combine multiple sound buffer in real-time with C#?
Post by: eXpl0it3r on October 01, 2012, 11:27:42 pm
I'm not sure if anyone gets what the heck you want to do/where the problem is... ???
There are tutorials and examples on how to use SFML if it's just about the basics. ;)
Title: Re: How to combine multiple sound buffer in real-time with C#?
Post by: Laurent on October 01, 2012, 11:48:35 pm
Concatenate the sample arrays of eacy sound buffer into a new bigger sound buffer. But that's only if you want to save the combined music. If you just want to play it, then simply play one sound buffer after the other.
Title: Re: How to combine multiple sound buffer in real-time with C#?
Post by: trajan on October 02, 2012, 02:59:27 pm
Thank you everyone, following code nicely working -need little corrections-.
string soundLocation = AppDomain.CurrentDomain.BaseDirectory + "notes\\";

int totalAddedLen = 0;
Int16[] newBigMusic = new Int16[10000000];

public Form1()
{
      InitializeComponent();
      LoadAndPlay();
}

private void tryProductBuffer(Int16[] Buff)
{
      Array.Copy(Buff, 0, newBigMusic, totalAddedLen, Buff.Length);
      totalAddedLen = totalAddedLen + Buff.Length;
}

public void LoadAndPlay()
{
      SoundBuffer do_a = new SoundBuffer(soundLocation + "do_a.wav");
      SoundBuffer re = new SoundBuffer(soundLocation + "re.wav");
      SoundBuffer mi = new SoundBuffer(soundLocation + "mi.wav");
      SoundBuffer fa = new SoundBuffer(soundLocation + "fa.wav");
      SoundBuffer sol = new SoundBuffer(soundLocation + "sol.wav");
      SoundBuffer la = new SoundBuffer(soundLocation + "la.wav");
      SoundBuffer si = new SoundBuffer(soundLocation + "si.wav");
      SoundBuffer do_b = new SoundBuffer(soundLocation + "do_b.wav");
           
      /*
            User enter textbox -> do do re re do re mi
            string[0]->
            if(do_a)
            {
                  tryProductBuffer(do_a.Samples);
            }
            if(re)
            {
                  tryProductBuffer(re.Samples);
            }
             ...
      */

     
      Array.Resize(ref newBigMusic, totalAddedLen);
      SoundBuffer AllBuf = new SoundBuffer(newBigMusic, 2, 44100);
      AllBuf.SaveToFile("newBigMusic.wav");
      Sound Q = new Sound(AllBuf);
      // Q.Pitch = 3;
      Q.Play();
}