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

Author Topic: Reversing samples with loadFromSamples(...), nothing is playing now  (Read 2632 times)

0 Members and 1 Guest are viewing this topic.

lucifercartwright

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
So I'm loading a recorded file ("recording.ogg") which plays fine by default.

However what I'm doing now is I retrieve the samples and reverse them and try to put them in a new buffer.

After doing this and playing from the buffer, I hear nothing now. What's wrong?

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio.hpp>
#include <iostream>
#include <string>

using namespace std;

//g++ test.cpp -o test -DSFML_STATIC -I C:\SFML\include -L C:\SFML\lib -lsfml-graphics-s -lsfml-window-s -lsfml-audio-s -lsfml-system-s -lopenal32 -lvorbisfile -lvorbisenc -lvorbis -logg -lFLAC -ljpeg -lfreetype -lgdi32 -lopengl32 -lwinmm

int main(int argc, char* argv[])
{
    sf::SoundBuffer buffer;
    if (!buffer.loadFromFile("recording.ogg")){
        return EXIT_FAILURE;

    }

    const sf::Int16* samples = buffer.getSamples();

    size_t samples_count = buffer.getSampleCount();
    sf::Int16 newSamples[samples_count];

    //Reversing samples
    for(int i = 0; i <= samples_count; i++){
        newSamples[i] = samples[samples_count-i];
    }

    sf::SoundBuffer newBuffer;

    if(!newBuffer.loadFromSamples(&newSamples[0], samples_count, 2, 44100)){
        cout << "Unable to load from new samples" << endl;
    }

    sf::Sound snd(newBuffer);
    snd.play();
    cin.ignore();

    cout << "Done" << endl;
    return EXIT_SUCCESS;
}

 

EDIT: Apparently I had to use the same number of channels at the original buffer (original only had 1).
Anyone know why I can't just set it to 2? Why is it silent if I set the new buffer to 2 instead of the original (1) ?
« Last Edit: August 06, 2017, 11:52:59 am by lucifercartwright »

Hapax

  • Hero Member
  • *****
  • Posts: 3372
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Reversing samples with loadFromSamples(...), nothing is playing now
« Reply #1 on: August 06, 2017, 12:15:22 pm »
You fixed it then, by sticking to the same number of channels?

Note that if you change the number of channels from one to two, you need twice as many samples. i.e. instead of one sample for the single channel, you need one sample for each of the two channels that are played at the same time.

At a guess, the reason it could sound 'silent' is that since the sample are playing at now twice the speed (two channels), it could be too high pitched for you to hear. Digitally, the actual back/forth waveform may be disrupted and may actually be incredibly low pitched, which may also be too low pitched for you to hear. This is all guesswork since you haven't provided the sample with which the situation occurs.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

lucifercartwright

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Reversing samples with loadFromSamples(...), nothing is playing now
« Reply #2 on: August 06, 2017, 12:50:56 pm »
You fixed it then, by sticking to the same number of channels?

Note that if you change the number of channels from one to two, you need twice as many samples. i.e. instead of one sample for the single channel, you need one sample for each of the two channels that are played at the same time.

At a guess, the reason it could sound 'silent' is that since the sample are playing at now twice the speed (two channels), it could be too high pitched for you to hear. Digitally, the actual back/forth waveform may be disrupted and may actually be incredibly low pitched, which may also be too low pitched for you to hear. This is all guesswork since you haven't provided the sample with which the situation occurs.

But how do you work with 2 channels though exactly if it's a 1D array? Shouldn't there be one array per channel?

Hapax

  • Hero Member
  • *****
  • Posts: 3372
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Reversing samples with loadFromSamples(...), nothing is playing now
« Reply #3 on: August 06, 2017, 03:19:23 pm »
No, they're interleaved. Every two samples is a sample for each of the two channels.
For example, using five channels, the first five samples would be one sample per channel - all played at once. Then, the next set of five samples would be one samples per channel - all played at once. etc..
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

lucifercartwright

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Reversing samples with loadFromSamples(...), nothing is playing now
« Reply #4 on: August 07, 2017, 09:50:25 am »
No, they're interleaved. Every two samples is a sample for each of the two channels.
For example, using five channels, the first five samples would be one sample per channel - all played at once. Then, the next set of five samples would be one samples per channel - all played at once. etc..

I'm confused, can you visualize it for me?

Is it like this, considering we're working with 5 samples per channel and 2 channels?
C# = Channel Number, S# = Sample Number
[C1S1, C1S2, C1S3, C1S4, C1S5, C2S1, C2S2, C2S3, C2S4, C2S5]

?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10936
    • View Profile
    • development blog
    • Email
Re: Reversing samples with loadFromSamples(...), nothing is playing now
« Reply #5 on: August 07, 2017, 12:12:39 pm »
No it's: [C1S1, C2S1, C1S2, C2S2, C1S3, C2S3, C1S4, C2S4, C1S5, C2S5]
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/