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

Author Topic: No sound is played. (When loading with samples)  (Read 4401 times)

0 Members and 1 Guest are viewing this topic.

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
No sound is played. (When loading with samples)
« on: January 27, 2016, 09:01:12 pm »
Hi!
I want to play an ultra sound but no sound is played.

Actually I'm on ubuntu 14.04 64 bits version.

Here is the code.

#include <SFML/Audio.hpp>
int main(int argc, char* argv[]) {
    std::vector<sf::Int16> samples = {10000};
    sf::SoundBuffer buffer;
    buffer.loadFromSamples(samples.data(),samples.size(),1,44100);
    sf::Sound sound;
    sound.setBuffer(buffer);
    sound.setLoop(true);
    sound.play();
    while (true) {}

    return 0;
}
 

I've turn down the frequency to listen if it works.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: No sound is played. (When loading with samples)
« Reply #1 on: January 27, 2016, 09:29:30 pm »
Are you able to hear ultrasounds?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: No sound is played. (When loading with samples)
« Reply #2 on: January 28, 2016, 02:06:23 am »
std::vector<sf::Int16> samples = {10000};
This is a vector with only one element.

This single value is then looped causing a flat waveform - silence.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: No sound is played. (When loading with samples)
« Reply #3 on: January 28, 2016, 10:35:37 am »
Ok so how many elements should I put to hear the sound ?
 

PS : I've put 10 000 elements in the vector but I can't heart anything.

Code: [Select]
#include <SFML/Audio.hpp>
int main(int argc, char* argv[]) {
    std::vector<sf::Int16> samples;
    for (unsigned int i = 0; i < 1000000; i++)
        samples.push_back(1000);
    sf::SoundBuffer buffer;
    buffer.loadFromSamples(samples.data(),samples.size(),1,44100);
    sf::Sound sound;
    sound.setBuffer(buffer);
    sound.setLoop(true);
    sound.play();
    while (true) {}

    return 0;
}
« Last Edit: January 28, 2016, 10:42:46 am by Lolilolight »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: No sound is played. (When loading with samples)
« Reply #4 on: January 28, 2016, 10:56:45 am »
As already stated by Hapax, a flat waveform (constant value) is silence. To create a sound, you must generate a waveform (sine function) with the desired frequency and amplitude.

Don't hesitate to document yourself on sampled audio and waveforms, you won't get anywhere without some basic knowledge about what you're doing.
Laurent Gomila - SFML developer

 

anything