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

Author Topic: [solved] No sound plays.  (Read 4236 times)

0 Members and 3 Guests are viewing this topic.

spccrow

  • Newbie
  • *
  • Posts: 3
    • View Profile
[solved] No sound plays.
« on: August 24, 2012, 02:07:12 pm »
I am trying to extend the sinewave playing example from the wiki into a dtmf generator. Here is the header file Dtmf.hpp

#include <SFML/Audio.hpp>
#include <cmath>
#include <iostream>


class Dtmf
{
    private:
        sf::Int16* raw;
        unsigned _sample_rate;
        unsigned _amp;
        double freqs[2];
        sf::SoundBuffer* buffer;
        sf::Sound* sound;
        void genBuffer();
    public:
        Dtmf(double, double,unsigned,unsigned);
        ~Dtmf();
        unsigned getSampleRate();
        double getFreq(int);
        void play();
};

The implementation of Dtmf


#include "Dtmf.hpp"

Dtmf::Dtmf(double freq1, double freq2, unsigned sample_rate=44100, unsigned amp=15)
{
    _sample_rate = sample_rate;
    _amp = amp;
    freqs[0] = freq1;
    freqs[1] = freq2;
    raw = new sf::Int16[_sample_rate];
    buffer = new sf::SoundBuffer;
    sound = new sf::Sound;
    genBuffer();
}

Dtmf::~Dtmf()
{
    delete raw;
    delete buffer;
    delete sound;
}

unsigned Dtmf::getSampleRate()
{
    return _sample_rate;
}

double Dtmf::getFreq(int index)
{
    return freqs[index];
}

void Dtmf::genBuffer()
{
    const double TWO_PI = 6.28318;
    double f1inc = freqs[0] / _sample_rate;
    double f2inc = freqs[1] / _sample_rate;
    unsigned i;
    double f1=0;
    double f2=0;
    for(i=0;i<_sample_rate;i++)
    {
        raw[i] = _amp * ( sin( f1 * TWO_PI ) + sin( f2 * TWO_PI ) );
        f1 += f1inc; f2 += f2inc;
    }
    if(!buffer->loadFromSamples(raw,_sample_rate,1,_sample_rate))
    {
        std::cerr << "Error loading buffer" << std::endl;
        return;
    }
    sound->setBuffer(*buffer);
    sound->setLoop(true);
}

void Dtmf::play()
{
    sound->play();
    sf::sleep(sf::milliseconds(500));
    sound->stop();
}
 

And test.cpp

#include "Dtmf.hpp"

int main()
{
    Dtmf dialTone = Dtmf(350., 440.,44100,15);
    std::cout << dialTone.getSampleRate() << std::endl;
    std::cout << "Freqs :" << dialTone.getFreq(0) << " and " << dialTone.getFreq(1) << std::endl;
    dialTone.play();
}

 

The code compiles and links just fine, however no sound is generated.

Here is a working example of what I'm trying to accomplish without the OOP encapsulation.
#include <SFML/Audio.hpp>
#include <cmath>
#include <iostream>

int main() {
        const unsigned SAMPLES = 44100;
        const unsigned SAMPLE_RATE = 44100;
        const unsigned AMPLITUDE = 15000;
       
        sf::Int16 raw[SAMPLES];

        const double TWO_PI = 6.28318;
        const double xincrement = 440./44100;
        const double yincrement = 350./44100;
        double x = 0;
        double y = 0;
        for (unsigned i = 0; i < SAMPLES; i++) {
                raw[i] = AMPLITUDE * ( sin( x * TWO_PI ) + sin( y * TWO_PI ) );
                x += xincrement;
                y += yincrement;
        }
       
        sf::SoundBuffer Buffer;
        if (!Buffer.loadFromSamples(raw, SAMPLES, 1, SAMPLE_RATE)) {
                std::cerr << "Loading failed!" << std::endl;
                return 1;
        }

        sf::Sound Sound;
        Sound.setBuffer(Buffer);
        Sound.setLoop(true);
        Sound.play();
        while (1) {
        sf::sleep(sf::milliseconds(100));
        }
        return 0;
}
 

Any hints or suggestions on correcting the problem are appreciated.

This is the SConstruct file that I am using for compilation
Code: [Select]
env = Environment()
target = 'test'
sources = ['test.cpp','Dtmf.cpp']
libs = ['sfml-audio','sfml-system']
object_list = []
for source in sources:
    object_list.extend(Object(source,CCFLAGS='-g'))
env.Program(target,object_list,LIBS = libs,CCFLAGS='-g')
« Last Edit: August 25, 2012, 12:24:16 am by spccrow »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: No sound plays.
« Reply #1 on: August 24, 2012, 04:28:47 pm »
You only play 500 ms of sound, is it enough to hear anything? ;)
Laurent Gomila - SFML developer

spccrow

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: No sound plays.
« Reply #2 on: August 24, 2012, 04:55:46 pm »
Laurent, thank you for the speedy reply. Even adjusting the length of the tone to a more noticeable length, i.e. 5 seconds, none is played.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: No sound plays.
« Reply #3 on: August 24, 2012, 07:27:07 pm »
Well, you have a working version of your code, so it's not really hard to compare them and find that your amplitude is set to 15 whereas it should be 15000. It took me 5 minutes ;)

Another error is that you're copying instances of your class, but don't handle the copy correctly (your class should have a copy constructor, the auto-generated one won't do the job).
Laurent Gomila - SFML developer

spccrow

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: No sound plays.
« Reply #4 on: August 25, 2012, 12:23:48 am »
Changing this line fixed it all.

    Dtmf dialTone(350., 440.,44100,15000);

I feel kinda slow now...
Thank you for taking the time to fix my oversights.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [solved] No sound plays.
« Reply #5 on: August 25, 2012, 12:05:43 pm »
You should still implement a copy constructor and assignment operator, or forbid copies (by declaring both methods private and leaving them undefined, or by deriving from sf::NonCopyable). Otherwise, you can easily run into problems again.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything