| 1 |  | 
|---|
| 2 | //////////////////////////////////////////////////////////// | 
|---|
| 3 | // Headers | 
|---|
| 4 | //////////////////////////////////////////////////////////// | 
|---|
| 5 | #include <SFML/Audio.hpp> | 
|---|
| 6 | #include <SFML/Network.hpp> | 
|---|
| 7 | #include <iostream> | 
|---|
| 8 |  | 
|---|
| 9 |  | 
|---|
| 10 | const sf::Uint8 audioData   = 1; | 
|---|
| 11 | const sf::Uint8 endOfStream = 2; | 
|---|
| 12 |  | 
|---|
| 13 |  | 
|---|
| 14 | //////////////////////////////////////////////////////////// | 
|---|
| 15 | /// Specialization of audio recorder for sending recorded audio | 
|---|
| 16 | /// data through the network | 
|---|
| 17 | //////////////////////////////////////////////////////////// | 
|---|
| 18 | class NetworkRecorder : public sf::SoundRecorder | 
|---|
| 19 | { | 
|---|
| 20 | public: | 
|---|
| 21 |  | 
|---|
| 22 | //////////////////////////////////////////////////////////// | 
|---|
| 23 | /// Constructor | 
|---|
| 24 | /// | 
|---|
| 25 | /// \param host Remote host to which send the recording data | 
|---|
| 26 | /// \param port Port of the remote host | 
|---|
| 27 | /// | 
|---|
| 28 | //////////////////////////////////////////////////////////// | 
|---|
| 29 | NetworkRecorder(const sf::IpAddress& host, unsigned short port) : | 
|---|
| 30 | m_host(host), | 
|---|
| 31 | m_port(port) | 
|---|
| 32 | { | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | //////////////////////////////////////////////////////////// | 
|---|
| 36 | /// Destructor | 
|---|
| 37 | /// | 
|---|
| 38 | /// \see SoundRecorder::~SoundRecorder() | 
|---|
| 39 | /// | 
|---|
| 40 | //////////////////////////////////////////////////////////// | 
|---|
| 41 | ~NetworkRecorder() | 
|---|
| 42 | { | 
|---|
| 43 | // Make sure to stop the recording thread | 
|---|
| 44 | stop(); | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | private: | 
|---|
| 48 |  | 
|---|
| 49 | //////////////////////////////////////////////////////////// | 
|---|
| 50 | /// \see SoundRecorder::onStart | 
|---|
| 51 | /// | 
|---|
| 52 | //////////////////////////////////////////////////////////// | 
|---|
| 53 | virtual bool onStart() | 
|---|
| 54 | { | 
|---|
| 55 | if (m_socket.connect(m_host, m_port) == sf::Socket::Done) | 
|---|
| 56 | { | 
|---|
| 57 | std::cout << "Connected to server "<< m_host << std::endl; | 
|---|
| 58 | return true; | 
|---|
| 59 | } | 
|---|
| 60 | else | 
|---|
| 61 | { | 
|---|
| 62 | return false; | 
|---|
| 63 | } | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | //////////////////////////////////////////////////////////// | 
|---|
| 67 | /// \see SoundRecorder::onProcessSamples | 
|---|
| 68 | /// | 
|---|
| 69 | //////////////////////////////////////////////////////////// | 
|---|
| 70 | virtual bool onProcessSamples(const sf::Int16* samples, std::size_t sampleCount) | 
|---|
| 71 | { | 
|---|
| 72 | // Pack the audio samples into a network packet | 
|---|
| 73 | sf::Packet packet; | 
|---|
| 74 | packet << audioData; | 
|---|
| 75 | packet.append(samples, sampleCount * sizeof(sf::Int16)); | 
|---|
| 76 |  | 
|---|
| 77 | // Send the audio packet to the server | 
|---|
| 78 | return m_socket.send(packet) == sf::Socket::Done; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | //////////////////////////////////////////////////////////// | 
|---|
| 82 | /// \see SoundRecorder::onStop | 
|---|
| 83 | /// | 
|---|
| 84 | //////////////////////////////////////////////////////////// | 
|---|
| 85 | virtual void onStop() | 
|---|
| 86 | { | 
|---|
| 87 | // Send a "end-of-stream" packet | 
|---|
| 88 | sf::Packet packet; | 
|---|
| 89 | packet << endOfStream; | 
|---|
| 90 | m_socket.send(packet); | 
|---|
| 91 |  | 
|---|
| 92 | // Close the socket | 
|---|
| 93 | m_socket.disconnect(); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | //////////////////////////////////////////////////////////// | 
|---|
| 97 | // Member data | 
|---|
| 98 | //////////////////////////////////////////////////////////// | 
|---|
| 99 | sf::IpAddress  m_host;   ///< Address of the remote host | 
|---|
| 100 | unsigned short m_port;   ///< Remote port | 
|---|
| 101 | sf::TcpSocket  m_socket; ///< Socket used to communicate with the server | 
|---|
| 102 | }; | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | //////////////////////////////////////////////////////////// | 
|---|
| 106 | /// Create a client, connect it to a running server and | 
|---|
| 107 | /// start sending him audio data | 
|---|
| 108 | /// | 
|---|
| 109 | //////////////////////////////////////////////////////////// | 
|---|
| 110 | void doClient(unsigned short port) | 
|---|
| 111 | { | 
|---|
| 112 | // Check that the device can capture audio | 
|---|
| 113 | if (!sf::SoundRecorder::isAvailable()) | 
|---|
| 114 | { | 
|---|
| 115 | std::cout << "Sorry, audio capture is not supported by your system"<< std::endl; | 
|---|
| 116 | return; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | // Ask for server address | 
|---|
| 120 | sf::IpAddress server; | 
|---|
| 121 | do | 
|---|
| 122 | { | 
|---|
| 123 | std::cout << "Type address or name of the server to connect to: "; | 
|---|
| 124 | std::cin  >> server; | 
|---|
| 125 | } | 
|---|
| 126 | while (server == sf::IpAddress::None); | 
|---|
| 127 |  | 
|---|
| 128 | // Create an instance of our custom recorder | 
|---|
| 129 | NetworkRecorder recorder(server, port); | 
|---|
| 130 |  | 
|---|
| 131 | // Wait for user input... | 
|---|
| 132 | std::cin.ignore(10000, '\n'); | 
|---|
| 133 | std::cout << "Press enter to start recording audio"; | 
|---|
| 134 | std::cin.ignore(10000, '\n'); | 
|---|
| 135 |  | 
|---|
| 136 | // Start capturing audio data | 
|---|
| 137 | recorder.start(44100); | 
|---|
| 138 | std::cout << "Recording... press enter to stop"; | 
|---|
| 139 | std::cin.ignore(10000, '\n'); | 
|---|
| 140 | recorder.stop(); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|