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

Author Topic: How are Samples, ChannelCount and SampleRate connected?  (Read 2993 times)

0 Members and 1 Guest are viewing this topic.

Dread_Boy

  • Newbie
  • *
  • Posts: 2
    • View Profile
How are Samples, ChannelCount and SampleRate connected?
« on: April 05, 2015, 09:45:25 am »
I'm having a problem and I can't find a solution. Long story short, I have 9 seconds long .wav sound with 2 channels and 48000 SampleRate. Then I try to do convolution (which is correct from tests I did) and save these new Samples in new SoundBuffer and later in Sound and play again. Firstly I was using these code:

song = new Sound(new SoundBuffer(y, song.SoundBuffer.ChannelCount, song.SoundBuffer.SampleRate));

and as you can see, I just copied old ChannelCount and SampleRate. It didn't work, SampleRate of new SoundBuffer was 0. Then I found out that if I do this:

song = new Sound(new SoundBuffer(y, 1, song.SoundBuffer.SampleRate));

I can play that new Sound but speed is half of that of original. Lastly, I changed the code to this:

song = new Sound(new SoundBuffer(y, 1, song.SoundBuffer.SampleRate * 2));

and now everything's fine, the Sound can be played and length is as it should be. But I still don't know the actual solution to my problem. What if I want to do different convolutions for different channels, how can I recognise those channels in Samples array? And how do I need to save these modified Samples in new array in order to play them as SoundBuffer with 2 channels?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
AW: How are Samples, ChannelCount and SampleRate connected?
« Reply #1 on: April 05, 2015, 11:21:42 am »
First don't use new. If you need dynamically allocated memory use RAII. If you come from Java or a like, read up on class instancing, you don't need the new keyword for that.

Second you need to understand a bit how audio sampling works in general.
If you have two channels you'll have two samples one for each channel. If you want to extract one channel, you need to go over the sample data and only pick every other sample. If you want to do a mix down you best google on how to add/multiple samples to get a good result and then go over the data and apply it to all samples in pairs.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: AW: How are Samples, ChannelCount and SampleRate connected?
« Reply #2 on: April 05, 2015, 01:30:36 pm »
First don't use new. If you need dynamically allocated memory use RAII. If you come from Java or a like, read up on class instancing, you don't need the new keyword for that.
That is C# code, the new is fine ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Dread_Boy

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: AW: How are Samples, ChannelCount and SampleRate connected?
« Reply #3 on: April 05, 2015, 05:42:09 pm »
If you want to extract one channel, you need to go over the sample data and only pick every other sample.

Great, so if I for example want to create stereo sound where only left speaker is loud and right is quiet my Samples array would be

{1879, 0, 3751, 0, 5608, 0, 7444, 0, 9250, 0} //only a lot longer ofc

and when I want to play the same thing on both speakers, I would create

{1879, 1879, 3751, 3751, 5608, 5608, 7444, 7444, 9250, 9250}

Thank you for help!

 

anything