1
Audio / Re: Audio is recording, but not playing, im no idea why this is.
« 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.
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;
}
* 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;
}