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

Author Topic: Audio is recording, but not playing, im no idea why this is. [SOLVED]  (Read 3418 times)

0 Members and 1 Guest are viewing this topic.

madpl1239

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Hello everyone, maybe someone help me. Last times im trying make short code: small window application to recording signal from my microphone. Of course all in SFML :P  (window, buttons and audio).
I have problem.  When launch app (compilation at Kubuntu: GCC 7.5, SFML 2.5.1, this same at Window - i compiled too)
recording is ok, but when i press 'play' button i dnot hear anything - i no idea why this is so . 'Record' button is on the left, 'play' button is on the right, 'save' button below. 'Play' button does not change color, and according to the code it should. Im attaching source code bellow:
 
/*
 * main.cpp
 *
 * Recording, playing and save microphone.
 *
 * 08-01-2022 by madpl (aka madpl1239)
 */

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

constexpr int WIDTH = 400;
constexpr int HEIGHT = 300;
constexpr int SAMPLERATE = 44100;

// colors
sf::Color bishop(255, 0, 128, 255);
sf::Color amarant(230, 28, 102, 255);
sf::Color aquamarine(127, 255, 212, 255);
sf::Color chabre(51, 0, 204, 255);

inline sf::RenderWindow win;
inline sf::SoundBuffer buffer;

inline sf::RectangleShape buttonRec(sf::Vector2f(120, 60));
inline sf::RectangleShape buttonPlay(sf::Vector2f(120, 60));
inline sf::RectangleShape buttonSave(sf::Vector2f(120, 60));


class CSound : public sf::Sound
{
public:
        CSound():
                m_switch(false)
        {
        }
       
        virtual ~CSound()
        {
                m_switch = false;
        }

        void play()
        {
                m_switch = true;
                sf::Sound::play();
        }
       
        void stop()
        {
                m_switch = false;
                sf::Sound::stop();
        }
       
        bool isPlaying()
        {
                return m_switch;
        }

private:
        bool m_switch;
};


class CRecorder : public sf::SoundBufferRecorder
{
public:
        CRecorder():
                m_sampleRate(44100),
                m_switch(false)
        {
        }

        virtual ~CRecorder()
        {
                m_sampleRate = 0;
                m_switch = false;
        }

        void start()
        {
                m_switch = true;
                sf::SoundBufferRecorder::start(m_sampleRate);
        }
       
        void stop()
        {
                m_switch = false;
                sf::SoundBufferRecorder::stop();
        }
       
        bool isRecording()
        {
                return m_switch;
        }
       
private:
        int m_sampleRate;      
        bool m_switch;
};


int main(void)
{
        sf::Clock clk;
       
        CRecorder rec;
        CSound snd;
       
        // buttons definitions
        buttonRec.setOrigin(60, 30);
        buttonRec.setPosition(80, 140);
        buttonRec.setFillColor(aquamarine);
        buttonRec.setOutlineThickness(2);
        buttonRec.setOutlineColor(sf::Color::Black);
               
        buttonPlay.setOrigin(60, 30);
        buttonPlay.setPosition(320, 140);
        buttonPlay.setFillColor(aquamarine);
        buttonPlay.setOutlineThickness(2);
        buttonPlay.setOutlineColor(sf::Color::Black);
       
        buttonSave.setOrigin(60, 30);
        buttonSave.setPosition(200, 240);
        buttonSave.setFillColor(aquamarine);
        buttonSave.setOutlineThickness(2);
        buttonSave.setOutlineColor(sf::Color::Black);
       
        win.create(sf::VideoMode(WIDTH, HEIGHT), "micRecord v0.5",
                          sf::Style::Titlebar | sf::Style::Close);
       
        if(!win.isOpen())
                return -1;
       
        win.setPosition(sf::Vector2i(500, 200));
       
        if(sf::SoundBufferRecorder::isAvailable() == false)
        {
                std::cout << "Sorry, audio capture is not supported
                                                      by your system.\n"
;
               
                return -1;
        }
       
        bool switch_record = false;
        bool switch_play = false;
        bool switch_save = false;
       
        float elapsedTime = 0.0f;
        clk.restart();
       
        sf::Event e;
        while(win.isOpen())
        {
                while(win.pollEvent(e))
                {
                        if(e.type == sf::Event::Closed)
                                win.close();
                       
                        if(e.type == sf::Event::KeyPressed and
                           e.key.code == sf::Keyboard::Escape)
                                win.close();
                       
                        if(e.type == sf::Event::MouseButtonPressed and
                           e.mouseButton.button == sf::Mouse::Left)
                        {
                                sf::Vector2i localMousePos =
                                               sf::Mouse::getPosition(win);

                                if(buttonRec.getGlobalBounds().
                                   contains(localMousePos.x, localMousePos.y))
                                {
                                        switch_record = !switch_record;
                                        switch_play = false;
                                        switch_save = false;
                                }

                                if(buttonPlay.getGlobalBounds().
                                   contains(localMousePos.x, localMousePos.y))
                                {
                                        switch_play = !switch_play;
                                        switch_record = false;
                                        switch_save = false;
                                }

                                if(buttonSave.getGlobalBounds().
                                   contains(localMousePos.x, localMousePos.y))
                                {
                                        switch_save = !switch_save;
                                        switch_record = false;
                                        switch_play = false;
                                }
                        }
                }
               
                if(switch_record)
                {
                        buttonRec.setFillColor(sf::Color::Red);
                       
                        if(!rec.isRecording())
                                rec.start();
                }
                else
                {
                        buttonRec.setFillColor(aquamarine);
                       
                        rec.stop();
                       
                        buffer = rec.getBuffer();
                        snd.setBuffer(buffer);
                        snd.setLoop(false);
                }
               
                if(switch_play == true)
                {
                        buttonPlay.setFillColor(sf::Color::Red);
                        switch_play = false;
                        snd.play();
                }
               
                if(switch_play == false)
                {
                        if(snd.getStatus() == sf::SoundSource::Playing)
                        {
                                buttonPlay.setFillColor(sf::Color::Red);
                        }
                        else
                        {
                                buttonPlay.setFillColor(aquamarine);
                        }
                }
                       
                if(switch_save)
                {
                        buttonSave.setFillColor(amarant);
                        buffer.saveToFile("./rec.ogg");
                       
                         elapsedTime = clk.getElapsedTime().asSeconds();
                        if(elapsedTime > 3)
                        {
                                buttonSave.setFillColor(aquamarine);
                                switch_save = false;
                                clk.restart();
                        }
                }
                else
                        buttonSave.setFillColor(aquamarine);
                       
                win.clear(chabre);
               
                win.draw(buttonRec);
                win.draw(buttonPlay);
                win.draw(buttonSave);
               
                win.display();
        }
               
        std::cout << "\ndone.\n";
       
        return 0;
}
 
« Last Edit: January 23, 2022, 07:53:12 pm by madpl1239 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
Re: Audio is recording, but not playing, im no idea why this is.
« Reply #1 on: January 13, 2022, 11:35:38 am »
With the amount of switches it's a bit hard to follow all the logic.
I suggest to run it either through a debugger and see what's going on or reduce the code to the absolute minimum.

Could be that you're overwriting the buffer, which causes the playback to stop and/or points to an empty buffer
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

madpl1239

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Audio is recording, but not playing, im no idea why this is.
« Reply #2 on: January 13, 2022, 07:52:31 pm »
eXpl0it3r thanks so suggestion. I did a little experiment and added one line of code from while (...) as below:


                if(switch_play)
                {
                        // here is problem: this button not changing color, why?
                        buttonPlay.setFillColor(sf::Color::Red);

                        switch_play = false;
                        snd.play();
                       
                         // when add this line below sound is playing:)))
                         while(snd.get()->getStatus() == sf::SoundSource::
                                       Status::Playing)
                                ;
                }
                else
                { .... }
 

Awesome ;D sound is playing now!!!
But still button play doesn't change color, and according to the code it should. The record button works as expected. And now here's the problem. Why it is: Why one button changes color and the other does not. According to the code, both buttons should work properly. Any propositions, suggestions? Maybe someone know why it is.
« Last Edit: January 13, 2022, 08:01:00 pm by madpl1239 »

kojack

  • Sr. Member
  • ****
  • Posts: 299
  • C++/C# game dev teacher.
    • View Profile
Re: Audio is recording, but not playing, im no idea why this is.
« Reply #3 on: January 13, 2022, 09:34:43 pm »
Looks like your while loop is a busy loop that won't finish until the sound source has completed. This means all rendering has stopped, the colour change a few lines above did happen but it's never getting a chance to appear on screen.
Once the while loop ends, rendering resumes but the button has now returned to it's not playing colour.

madpl1239

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Audio is recording, but not playing, im no idea why this is.
« Reply #4 on: January 13, 2022, 10:00:11 pm »
thanks to kojack for the suggestions. It seems that it may be as you say. In my search for bedbugs, I go in this direction. Thanks one time yet. Maybe someone have any other proposition?

madpl1239

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Audio is recording, but not playing, im no idea why this is.
« Reply #5 on: January 23, 2022, 07:30:57 pm »
I resolved my problem. It was that when I pressed the button, the SF :: Sound class snd object played the sound in a loop. and therefore the button did not change color. As the eXploit3r noted, the rendering loop stopped. Display and draw methods waited, but did not execute! In order for the play button to change color when pressed with the mouse, the draw and display methods had to be called immediately after entering if(switch_play).
Problem was solved. Now all that's left for me to do is add graphics to the background of the window and to the buttons. Thanks Kojak and eXploit3r for helping me.

/*
 * main.cpp
 *
 * Recording, playing and save microphone.
 *
 * 08-01-2022 by madpl (aka madpl1239)
 */

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

constexpr int WIDTH = 400;
constexpr int HEIGHT = 300;
constexpr int SAMPLERATE = 44100;

// colors
sf::Color bishop(255, 0, 128, 255);
sf::Color amarant(230, 28, 102, 255);
sf::Color aquamarine(127, 255, 212, 255);
sf::Color chabre(51, 0, 204, 255);

inline sf::RenderWindow win;
inline sf::SoundBuffer buffer;

inline sf::RectangleShape buttonRec(sf::Vector2f(120, 60));
inline sf::RectangleShape buttonPlay(sf::Vector2f(120, 60));
inline sf::RectangleShape buttonSave(sf::Vector2f(120, 60));


class CSound
{
public:
        CSound():
                m_sound(new sf::Sound()),
                m_switch(false)
        {}
       
        virtual ~CSound()
        {
                if(m_sound != nullptr)
                        delete m_sound;
               
                m_switch = false;
        }

        void play()
        {
                m_switch = true;
                m_sound->play();
               
                while(m_sound->getStatus() ==
                             sf::SoundSource::Status::Playing)
                        ;
        }
       
        void stop()
        {
                m_switch = false;
               
                 if(m_sound->getStatus() !=
                               sf::SoundSource::Status::Playing)
                        m_sound->stop();
        }
       
        bool isPlaying()
        {
                return m_switch;
        }
       
        sf::Sound* get()
        {
                return m_sound;
        }

private:
        sf::Sound* m_sound;
        bool m_switch;
};


class CRecorder
{
public:
        CRecorder():
                m_recorder(new sf::SoundBufferRecorder()),
                m_sampleRate(SAMPLERATE),
                m_switch(false)
        {}

        virtual ~CRecorder()
        {
                if(m_recorder != nullptr)
                        delete m_recorder;
               
                m_sampleRate = 0;
                m_switch = false;
        }

        void start()
        {
                m_switch = true;
                m_recorder->start(m_sampleRate);
        }
       
        void stop()
        {
                m_switch = false;
                m_recorder->stop();
        }
       
        bool isRecording()
        {
                return m_switch;
        }
       
        sf::SoundBufferRecorder* get()
        {
                return m_recorder;
        }
       
private:
        sf::SoundBufferRecorder* m_recorder;
        int m_sampleRate;      
        bool m_switch;
};


inline sf::Clock clk;
inline static CRecorder rec;
inline static CSound snd;


int main(void)
{
        // buttons definitions
        buttonRec.setOrigin(60, 30);
        buttonRec.setPosition(80, 140);
        buttonRec.setFillColor(aquamarine);
        buttonRec.setOutlineThickness(2);
        buttonRec.setOutlineColor(sf::Color::Black);
               
        buttonPlay.setOrigin(60, 30);
        buttonPlay.setPosition(320, 140);
        buttonPlay.setFillColor(aquamarine);
        buttonPlay.setOutlineThickness(2);
        buttonPlay.setOutlineColor(sf::Color::Black);
       
        buttonSave.setOrigin(60, 30);
        buttonSave.setPosition(200, 240);
        buttonSave.setFillColor(aquamarine);
        buttonSave.setOutlineThickness(2);
        buttonSave.setOutlineColor(sf::Color::Black);
       
        win.create(sf::VideoMode(WIDTH, HEIGHT), "micRecord v0.5",
                                      sf::Style::Titlebar | sf::Style::Close);
       
        if(not win.isOpen())
                return -1;
       
        win.setPosition(sf::Vector2i(500, 200));
        win.setVerticalSyncEnabled(true);
        win.setKeyRepeatEnabled(false);
       
        if(sf::SoundBufferRecorder::isAvailable() == false)
        {
                std::cout << "Sorry, audio capture is not supported by your
                                                                 system.\n"
;
               
                return -1;
        }
       
        sf::Mouse::setPosition(sf::Vector2i(600, 300), win);
       
        bool switch_record = false;
        bool switch_play = false;
        bool switch_save = false;
       
        float elapsedTime = 0.0f;
        clk.restart();
       
        sf::Event e;
        while(win.isOpen())
        {
                while(win.pollEvent(e))
                {
                        if(e.type == sf::Event::Closed)
                                win.close();
                       
                        if(e.type == sf::Event::KeyPressed and
                                e.key.code == sf::Keyboard::Escape)
                                win.close();
                       
                        if(e.type == sf::Event::MouseButtonPressed and
                                e.mouseButton.button == sf::Mouse::Left)
                        {
                                sf::Vector2i localMousePos =
                                                  sf::Mouse::getPosition(win);
                                if(buttonRec.getGlobalBounds().contains(
                                              localMousePos.x, localMousePos.y))
                                {
                                        switch_record = !switch_record;
                                        switch_play = false;
                                        switch_save = false;
                                }
                                       
                                if(buttonPlay.getGlobalBounds().contains(
                                              localMousePos.x, localMousePos.y))
                                {
                                        switch_play = !switch_play;
                                        switch_record = false;
                                        switch_save = false;
                                       
                                        std::cout << "buttonPlay pressed\n";
                                }
                                       
                                if(buttonSave.getGlobalBounds().contains(
                                              localMousePos.x, localMousePos.y))
                                {
                                        switch_save = !switch_save;
                                        switch_record = false;
                                        switch_play = false;
                                }
                        }
                }
               
                if(switch_record)
                {
                        if(not rec.isRecording())
                                rec.start();
                       
                        buttonRec.setFillColor(sf::Color::Red);
                }
                else
                {
                        if(rec.isRecording())
                        {
                                rec.stop();
                       
                                buffer = rec.get()->getBuffer();
                                snd.get()->setBuffer(buffer);
                                snd.get()->setLoop(false);
                        }
                       
                        buttonRec.setFillColor(aquamarine);
                }
               
                if(switch_play)
                {
                        buttonPlay.setFillColor(sf::Color::Red);
                        win.draw(buttonPlay);
                        win.display();
                       
                        switch_play = false;
                       
                        if(not snd.isPlaying())
                                snd.play(); // tu zatrzymuje się renderowanie
                }
                else
                {
                        if(snd.get()->getStatus() !=
                                                sf::SoundSource::Status::Playing)
                        {
                                if(snd.isPlaying())
                                        snd.stop();
                               
                                buttonPlay.setFillColor(aquamarine);
                                win.draw(buttonPlay);
                                win.display();
                        }
                }
                       
                if(switch_save)
                {
                        buttonSave.setFillColor(sf::Color::Red);
                        buffer.saveToFile("./rec.ogg");
                        clk.restart();
                       
                        switch_save = false;
                }
                else
                {
                        elapsedTime = clk.getElapsedTime().asSeconds();
                        if(elapsedTime > 2)
                                buttonSave.setFillColor(aquamarine);
                }
                       
                win.clear(chabre);
               
                win.draw(buttonRec);
                win.draw(buttonPlay);
                win.draw(buttonSave);
               
                win.display();
        }
               
        std::cout << "\ndone.\n";
       
        return 0;
}
 
« Last Edit: January 23, 2022, 07:50:58 pm by madpl1239 »

 

anything