2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Audio.hpp>
|
|
|
|
#include <SFML/Network.hpp>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
2009-07-12 06:17:24 +08:00
|
|
|
const sf::Uint8 audioData = 1;
|
|
|
|
const sf::Uint8 endOfStream = 2;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Specialization of audio recorder for sending recorded audio
|
|
|
|
/// data through the network
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
class NetworkRecorder : public sf::SoundRecorder
|
|
|
|
{
|
|
|
|
public :
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Constructor
|
|
|
|
///
|
2010-03-23 17:39:43 +08:00
|
|
|
/// \param host Remote host to which send the recording data
|
|
|
|
/// \param port Port of the remote host
|
2009-01-29 00:18:34 +08:00
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2010-03-23 17:39:43 +08:00
|
|
|
NetworkRecorder(const sf::IpAddress& host, unsigned short port) :
|
|
|
|
myHost(host),
|
|
|
|
myPort(port)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private :
|
|
|
|
|
2010-03-23 17:39:43 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// /see SoundRecorder::OnStart
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
virtual bool OnStart()
|
|
|
|
{
|
|
|
|
if (mySocket.Connect(myHost, myPort) == sf::Socket::Done)
|
|
|
|
{
|
|
|
|
std::cout << "Connected to server " << myHost << std::endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// /see SoundRecorder::ProcessSamples
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2011-12-26 06:42:43 +08:00
|
|
|
virtual bool OnProcessSamples(const sf::Int16* samples, std::size_t sampleCount)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Pack the audio samples into a network packet
|
2009-07-12 06:17:24 +08:00
|
|
|
sf::Packet packet;
|
|
|
|
packet << audioData;
|
2011-12-26 06:42:43 +08:00
|
|
|
packet.Append(samples, sampleCount * sizeof(sf::Int16));
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Send the audio packet to the server
|
2009-07-12 06:17:24 +08:00
|
|
|
return mySocket.Send(packet) == sf::Socket::Done;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
2010-03-23 17:39:43 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// /see SoundRecorder::OnStop
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
virtual void OnStop()
|
|
|
|
{
|
|
|
|
// Send a "end-of-stream" packet
|
|
|
|
sf::Packet packet;
|
|
|
|
packet << endOfStream;
|
|
|
|
mySocket.Send(packet);
|
|
|
|
|
|
|
|
// Close the socket
|
|
|
|
mySocket.Disconnect();
|
|
|
|
}
|
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Member data
|
|
|
|
////////////////////////////////////////////////////////////
|
2010-03-23 17:39:43 +08:00
|
|
|
sf::IpAddress myHost; ///< Address of the remote host
|
|
|
|
unsigned short myPort; ///< Remote port
|
|
|
|
sf::TcpSocket mySocket; ///< Socket used to communicate with the server
|
2009-01-29 00:18:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Create a client, connect it to a running server and
|
|
|
|
/// start sending him audio data
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-07-12 06:17:24 +08:00
|
|
|
void DoClient(unsigned short port)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Check that the device can capture audio
|
2009-11-03 17:04:40 +08:00
|
|
|
if (sf::SoundRecorder::IsAvailable() == false)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
std::cout << "Sorry, audio capture is not supported by your system" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for server address
|
2010-03-23 17:39:43 +08:00
|
|
|
sf::IpAddress server;
|
2009-01-29 00:18:34 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
std::cout << "Type address or name of the server to connect to : ";
|
2010-03-23 17:39:43 +08:00
|
|
|
std::cin >> server;
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
2010-03-23 17:39:43 +08:00
|
|
|
while (server == sf::IpAddress::None);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-23 17:39:43 +08:00
|
|
|
// Create an instance of our custom recorder
|
|
|
|
NetworkRecorder recorder(server, port);
|
2009-01-29 00:18:34 +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
|
2009-07-12 06:17:24 +08:00
|
|
|
recorder.Start(44100);
|
2009-01-29 00:18:34 +08:00
|
|
|
std::cout << "Recording... press enter to stop";
|
|
|
|
std::cin.ignore(10000, '\n');
|
2009-07-12 06:17:24 +08:00
|
|
|
recorder.Stop();
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|