2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Audio.hpp>
|
2023-04-24 20:13:52 +08:00
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
#include <SFML/Network.hpp>
|
2022-07-05 00:20:58 +08:00
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
2022-09-04 04:15:24 +08:00
|
|
|
const std::uint8_t clientAudioData = 1;
|
|
|
|
const std::uint8_t clientEndOfStream = 2;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Specialization of audio recorder for sending recorded audio
|
|
|
|
/// data through the network
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
class NetworkRecorder : public sf::SoundRecorder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Constructor
|
|
|
|
///
|
|
|
|
/// \param host Remote host to which send the recording data
|
|
|
|
/// \param port Port of the remote host
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2022-07-05 00:20:58 +08:00
|
|
|
NetworkRecorder(const sf::IpAddress& host, unsigned short port) : m_host(host), m_port(port)
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-02 20:49:00 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Destructor
|
|
|
|
///
|
|
|
|
/// \see SoundRecorder::~SoundRecorder()
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2021-12-09 08:37:49 +08:00
|
|
|
~NetworkRecorder() override
|
2015-10-02 20:49:00 +08:00
|
|
|
{
|
|
|
|
// Make sure to stop the recording thread
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
private:
|
|
|
|
////////////////////////////////////////////////////////////
|
2015-10-02 20:49:00 +08:00
|
|
|
/// \see SoundRecorder::onStart
|
2014-09-30 22:07:25 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2021-12-09 08:37:49 +08:00
|
|
|
bool onStart() override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2022-06-13 02:55:00 +08:00
|
|
|
if (m_socket.connect(m_host, m_port) == sf::Socket::Status::Done)
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
std::cout << "Connected to server " << m_host << std::endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2015-10-02 20:49:00 +08:00
|
|
|
/// \see SoundRecorder::onProcessSamples
|
2014-09-30 22:07:25 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2022-09-04 07:36:41 +08:00
|
|
|
bool onProcessSamples(const std::int16_t* samples, std::size_t sampleCount) override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// Pack the audio samples into a network packet
|
|
|
|
sf::Packet packet;
|
2021-04-14 09:59:13 +08:00
|
|
|
packet << clientAudioData;
|
2022-09-04 07:36:41 +08:00
|
|
|
packet.append(samples, sampleCount * sizeof(std::int16_t));
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Send the audio packet to the server
|
2022-06-13 02:55:00 +08:00
|
|
|
return m_socket.send(packet) == sf::Socket::Status::Done;
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
2015-10-02 20:49:00 +08:00
|
|
|
/// \see SoundRecorder::onStop
|
2014-09-30 22:07:25 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2021-12-09 08:37:49 +08:00
|
|
|
void onStop() override
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// Send a "end-of-stream" packet
|
|
|
|
sf::Packet packet;
|
2021-04-14 09:59:13 +08:00
|
|
|
packet << clientEndOfStream;
|
2021-12-10 00:17:38 +08:00
|
|
|
|
2022-06-13 02:55:00 +08:00
|
|
|
if (m_socket.send(packet) != sf::Socket::Status::Done)
|
2021-12-10 00:17:38 +08:00
|
|
|
{
|
|
|
|
std::cerr << "Failed to send end-of-stream packet" << std::endl;
|
|
|
|
}
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Close the socket
|
|
|
|
m_socket.disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Member data
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
sf::IpAddress m_host; ///< Address of the remote host
|
|
|
|
unsigned short m_port; ///< Remote port
|
|
|
|
sf::TcpSocket m_socket; ///< Socket used to communicate with the server
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Create a client, connect it to a running server and
|
|
|
|
/// start sending him audio data
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
void doClient(unsigned short port)
|
|
|
|
{
|
|
|
|
// Check that the device can capture audio
|
2015-10-02 20:49:00 +08:00
|
|
|
if (!sf::SoundRecorder::isAvailable())
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
std::cout << "Sorry, audio capture is not supported by your system" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for server address
|
2022-06-27 18:02:35 +08:00
|
|
|
std::optional<sf::IpAddress> server;
|
2014-09-30 22:07:25 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
std::cout << "Type address or name of the server to connect to: ";
|
2022-07-05 00:20:58 +08:00
|
|
|
std::cin >> server;
|
|
|
|
} while (!server.has_value());
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Create an instance of our custom recorder
|
2022-06-27 18:02:35 +08:00
|
|
|
NetworkRecorder recorder(server.value(), port);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Wait for user input...
|
|
|
|
std::cin.ignore(10000, '\n');
|
|
|
|
std::cout << "Press enter to start recording audio";
|
|
|
|
std::cin.ignore(10000, '\n');
|
|
|
|
|
|
|
|
// Start capturing audio data
|
2021-12-14 21:04:15 +08:00
|
|
|
if (!recorder.start(44100))
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to start recorder" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
std::cout << "Recording... press enter to stop";
|
|
|
|
std::cin.ignore(10000, '\n');
|
|
|
|
recorder.stop();
|
|
|
|
}
|