Back to index

libsfml  1.6+dfsg2
Client.cpp
Go to the documentation of this file.
00001 
00003 // Headers
00005 #include <SFML/Audio.hpp>
00006 #include <SFML/Network.hpp>
00007 #include <iostream>
00008 
00009 
00010 const sf::Uint8 AudioData   = 1;
00011 const sf::Uint8 EndOfStream = 2;
00012 
00013 
00018 class NetworkRecorder : public sf::SoundRecorder
00019 {
00020 public :
00021 
00028     NetworkRecorder(sf::SocketTCP Socket) :
00029     mySocket(Socket)
00030     {
00031 
00032     }
00033 
00034 private :
00035 
00040     virtual bool OnProcessSamples(const sf::Int16* Samples, std::size_t SamplesCount)
00041     {
00042         // Pack the audio samples into a network packet
00043         sf::Packet PacketOut;
00044         PacketOut << AudioData;
00045         PacketOut.Append(Samples, SamplesCount * sizeof(sf::Int16));
00046 
00047         // Send the audio packet to the server
00048         return mySocket.Send(PacketOut) == sf::Socket::Done;
00049     }
00050 
00052     // Member data
00054     sf::SocketTCP mySocket; 
00055 };
00056 
00057 
00063 void DoClient(unsigned short Port)
00064 {
00065     // Check that the device can capture audio
00066     if (sf::SoundRecorder::CanCapture() == false)
00067     {
00068         std::cout << "Sorry, audio capture is not supported by your system" << std::endl;
00069         return;
00070     }
00071 
00072     // Ask for server address
00073     sf::IPAddress ServerAddress;
00074     do
00075     {
00076         std::cout << "Type address or name of the server to connect to : ";
00077         std::cin  >> ServerAddress;
00078     }
00079     while (!ServerAddress.IsValid());
00080 
00081     // Create a TCP socket for communicating with server
00082     sf::SocketTCP Socket;
00083 
00084     // Connect to the specified server
00085     if (Socket.Connect(Port, ServerAddress) != sf::Socket::Done)
00086         return;
00087     std::cout << "Connected to server " << ServerAddress << std::endl;
00088 
00089     // Wait for user input...
00090     std::cin.ignore(10000, '\n');
00091     std::cout << "Press enter to start recording audio";
00092     std::cin.ignore(10000, '\n');
00093 
00094     // Create a instance of our custom recorder
00095     NetworkRecorder Recorder(Socket);
00096 
00097     // Start capturing audio data
00098     Recorder.Start(44100);
00099     std::cout << "Recording... press enter to stop";
00100     std::cin.ignore(10000, '\n');
00101     Recorder.Stop();
00102 
00103     // Send a "end-of-stream" packet
00104     sf::Packet PacketOut;
00105     PacketOut << EndOfStream;
00106     Socket.Send(PacketOut);
00107 
00108     // Close the socket when we're done
00109     Socket.Close();
00110 }