2009-01-29 00:18:34 +08:00
|
|
|
module client;
|
|
|
|
|
|
|
|
import util;
|
|
|
|
|
|
|
|
|
|
|
|
// Specialization of audio recorder for sending recorded audio
|
|
|
|
// data through the network
|
|
|
|
class NetworkRecorder : SoundRecorder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2010-03-04 10:23:27 +08:00
|
|
|
// Constructor
|
|
|
|
this(SocketTCP Socket)
|
|
|
|
{
|
|
|
|
mySocket = Socket;
|
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-04 10:23:27 +08:00
|
|
|
~this()
|
|
|
|
{
|
|
|
|
delete mySocket;
|
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
protected:
|
2010-03-04 10:23:27 +08:00
|
|
|
override bool onStart()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-04 10:23:27 +08:00
|
|
|
override void onStop()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override bool onProcessSamples(short[] samples)
|
|
|
|
{
|
|
|
|
// Pack the audio samples into a network packet
|
|
|
|
Packet PacketOut = new Packet();
|
|
|
|
PacketOut.set(AudioData);
|
|
|
|
PacketOut.append((cast(byte*)samples.ptr)[0..samples.length * short.sizeof]);
|
|
|
|
// Send the audio packet to the server
|
|
|
|
return mySocket.send(PacketOut) == SocketStatus.DONE;
|
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-04 10:23:27 +08:00
|
|
|
SocketTCP mySocket; ///< Socket used to communicate with the server
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void runClient(IPAddress adr, int port)
|
|
|
|
{
|
2010-03-04 10:23:27 +08:00
|
|
|
// Create a TCP socket for communicating with server
|
|
|
|
SocketTCP Socket = new SocketTCP();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-04 10:23:27 +08:00
|
|
|
// Connect to the specified server
|
|
|
|
if (!Socket.connect(port, adr))
|
|
|
|
return;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-04 10:23:27 +08:00
|
|
|
// Wait for user input...
|
|
|
|
Cout("Press enter to start recording audio").newline;
|
|
|
|
Cin.get();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-04 10:23:27 +08:00
|
|
|
// Create a instance of our custom recorder
|
|
|
|
NetworkRecorder Recorder = new NetworkRecorder(Socket);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-04 10:23:27 +08:00
|
|
|
// Start capturing audio data
|
|
|
|
Recorder.start(44100);
|
|
|
|
Cout("Press enter to stop recording audio").newline;
|
|
|
|
Cin.get();
|
|
|
|
Recorder.stop();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-04 10:23:27 +08:00
|
|
|
// Send a "end-of-stream" packet
|
|
|
|
Packet PacketOut = new Packet();
|
|
|
|
PacketOut.set(EndOfStream);
|
|
|
|
Socket.send(PacketOut);
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|