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

Author Topic: How to split channels  (Read 3269 times)

0 Members and 1 Guest are viewing this topic.

Filippo

  • Newbie
  • *
  • Posts: 2
    • View Profile
How to split channels
« on: April 25, 2018, 06:49:39 pm »
Hi!

I am loading a WAV file with sf::InputSoundFile in C++ and I want to extract the samples for each channel separately. As far as I know the method sf::InputSoundFile::read(Int16 *samples, Uint64 maxCount) writes an array of interleaved samples to samples. Each sample is then made up by 16 bits, the first 8 belonging to a channel, and the remaining 8 belonging to the other. My silly question is: should I consider each 8-bits part as an Int8 or as a Uint8? I am very much confused.

A code snippet that performs the conversion from interleaved to separate channels would be also appreciated!  ;D

What follows is my attempt to do this conversion.

sf::Int16 samples[1024];
sf::Int16 mono[1024];
file.read(samplesData, 1024);
if (file.getChannelCount() == 2){
  for(int k = 0; k < 1024; ++k){
    sf::Int16 right = samples[k] & 0x00FF;
    sf::Int16 left = (samples[k] & 0xFF00) >> 8;
    mono[k] = (right+left)/2; // mean of the two channels
  }
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: How to split channels
« Reply #1 on: April 25, 2018, 07:02:48 pm »
writes an array of interleaved samples to samples.
This is true.

Each sample is then made up by 16 bits, the first 8 belonging to a channel, and the remaining 8 belonging to the other.
I have no idea where you heard that and it totally contradicts your previous statement. :D

Interleaved means, the first sample is from channel 1, the second sample is from channel 2, the third sample is from channel 1, the forth sample is from channel 2 and so on... (if you have stereo).

Hope that helps. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Filippo

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How to split channels
« Reply #2 on: April 25, 2018, 07:21:40 pm »
Quote
I have no idea where you heard that
Actually, this is the bad source, though probably I misread it ;)
https://stackoverflow.com/questions/48804523/is-it-possible-to-split-left-and-right-channel-from-audio-sample-in-sfml

Quote
Hope that helps.
It really helps a lot!! Thank you!! I've been stuck for hours thinking about how to split those 16 bits.  :o

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to split channels
« Reply #3 on: April 28, 2018, 05:24:25 pm »
Since the stackexchange question also asks how to split the channels, I posted an answer (and example) there.  It should help you :)

https://stackoverflow.com/a/50078065/6158263
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything