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

Author Topic: why this code not playing voice of cat ?  (Read 8584 times)

0 Members and 1 Guest are viewing this topic.

Rayan jutt

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #15 on: September 13, 2013, 06:23:03 pm »
no no .. i was not screaming at you guys ... !

Rayan jutt

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #16 on: September 13, 2013, 07:05:57 pm »
now i have made few changes in this code Laurent .. and it's playing the voice of cat ...
but something is going wrong .. when i select cat and press speak it will play voice of cat .. but on second time when i press speak button it will play voice of cat but also displays an error message on console !

#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>

//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
//      {
//              //std::cout << "Hello world" << std::endl;
//      }
//
//             
//             
//}


int main()
{

        sf::SoundBuffer buffer;
        sf::Sound sound;
        //sound.setBuffer(buffer);
        sound.setPitch(1.5f);
        sound.setVolume(80);
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");

       

    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
        button->setPosition(250, 350);
    button->setText("SPEAK");
    button->setSize(300, 40);
        //button->setCallbackId(1);
        //button->bindCallbackEx(function, tgui::Button::LeftMouseClicked);

        tgui::ListBox::Ptr listBox(gui);
        listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
        listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 100);
    listBox->addItem("HORSE");
    listBox->addItem("CAT");
    listBox->addItem("DONKEY");
        listBox->addItem("LION");
        listBox->addItem("GOAT");
        listBox->setCallbackId(2);
        //listBox->getSelectedItem();
        //listBox->bindCallbackEx(function, tgui::ListBox::ItemSelected);
       
        tgui ::Callback callback;
       
        button->bindCallback(tgui::Button::LeftMouseClicked);
        button->setCallbackId(1);
       
       
    while (window.isOpen())
    {
                /////////////////////////////////////////////////////

                while (gui.pollCallback(callback))
                {

                        if(callback.id==1)
                        {
                                if (listBox->getSelectedItem()=="CAT")
                                {
                                        //std::cout<<"heloooo!";
                                        if (!buffer.loadFromFile("Cat Meow.wav"))
                                        {
                                                //sound.setBuffer(buffer);
                                                std::cout << "Error while loading file" << std::endl;
                                        }
                                        else
                                        {
                                                sound.setBuffer(buffer);
                                                sound.play();
                                        }
                                       
                                }
                                else
                                {
                                        std::cout<<"aceesing CAT failed!"<<std::endl;

                                }
                        }
                        else
                        {
                                std::cout<<"id is not valid!!!!"<<std::endl;
                        }
               
        }      




                //////////////////////////////////////////////////////
        sf::Event event;
        while (window.pollEvent(event))
        {
                       
                                if (event.type == sf::Event::Closed)
                                        window.close();
                       
                                // Pass the event to all the widgets (if there would be widgets)
                                gui.handleEvent(event);
                }

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();
               
        }
       

       
       

        //getch();
        return EXIT_SUCCESS;
}





 
« Last Edit: September 13, 2013, 07:16:05 pm by Nabeel Nazir »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: why this code not playing voice of cat ?
« Reply #17 on: September 13, 2013, 07:33:18 pm »
Don't load the audio file every time you want to play it. You should load all your sounds at startup, and then just play them when you need to.
Laurent Gomila - SFML developer

Rayan jutt

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #18 on: September 14, 2013, 09:32:47 am »
now i am loading my all .wav files at the start up ... but when i select any animal from listbox and press speak button this will play the voice of last loaded .wav file (written sequentially in the code) i.e. buffer.loadFromFile("Goat Bah.wav");   

#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>

//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
//      {
//              //std::cout << "Hello world" << std::endl;
//      }
//
//             
//             
//}

int main()
{

        sf::SoundBuffer buffer;
        sf::Sound sound;
       
        //sound.setBuffer(buffer);
        sound.setPitch(1.5f);
        sound.setVolume(80);
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");

       

    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
        button->setPosition(250, 350);
    button->setText("SPEAK");
    button->setSize(300, 40);
       

        tgui::ListBox::Ptr listBox(gui);
        listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
        listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 100);
    listBox->addItem("HORSE");
    listBox->addItem("CAT");
    listBox->addItem("DONKEY");
        listBox->addItem("LION");
        listBox->addItem("GOAT");
        listBox->setCallbackId(2);
       
        tgui ::Callback callback;
       
        button->bindCallback(tgui::Button::LeftMouseClicked);
        button->setCallbackId(1);

        buffer.loadFromFile("Cat Meow.wav");
        buffer.loadFromFile("Lion Roar.wav");
        buffer.loadFromFile("Goat Bah.wav");   
       
       
    while (window.isOpen())
    {
                /////////////////////////////////////////////////////
               
                while (gui.pollCallback(callback))
                {
                       
                        if(callback.id==1)
                        {
                                if (listBox->getSelectedItem()=="CAT")
                                {
                                       
                                        sound.setBuffer(buffer);
                                        sound.play();
                                       
                                }
                               
                                else if (listBox->getSelectedItem()=="LION")
                                {
                                        sound.setBuffer(buffer);
                                        sound.play();
                                       
                                }
                                else if (listBox->getSelectedItem()=="GOAT")
                                {
                                        sound.setBuffer(buffer);
                                        sound.play();
                                }
                               
                        }
                        else
                        {
                                std::cout<<"id is not valid!!!!"<<std::endl;
                        }
               
        }      




                //////////////////////////////////////////////////////
        sf::Event event;
        while (window.pollEvent(event))
        {
                       
                                if (event.type == sf::Event::Closed)
                                        window.close();
                       
                                // Pass the event to all the widgets (if there would be widgets)
                                gui.handleEvent(event);
                }

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();
               
        }
       

       
       

        //getch();
        return EXIT_SUCCESS;
}





 
   

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #19 on: September 14, 2013, 11:32:40 am »
Yes, loading multiple sound files to a single sound buffer just overwrites it repeatedly.  You should have a separate sound buffer for each sound you want to keep loaded, then have just one sound that you change the buffer of when you want it to play a different animal voice.

As a more general point, did you notice your code for cat, lion and goat is identical?  You shouldn't expect it to do something different if it's exactly the same (and none of the objects it refers to are changed anywhere else).

Rayan jutt

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #20 on: September 15, 2013, 10:09:03 am »
Got it :)

 

anything