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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - madpl1239

Pages: [1]
1
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;
}
 

Pages: [1]
anything