Changed internal naming convention (local variables now start with a lower case character)
Removed the AudioResource class git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1166 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
parent
7cc00085d8
commit
45b150648d
245 changed files with 7865 additions and 8065 deletions
|
@ -7,8 +7,8 @@
|
|||
#include <iostream>
|
||||
|
||||
|
||||
const sf::Uint8 AudioData = 1;
|
||||
const sf::Uint8 EndOfStream = 2;
|
||||
const sf::Uint8 audioData = 1;
|
||||
const sf::Uint8 endOfStream = 2;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
@ -25,8 +25,8 @@ public :
|
|||
/// \param Socket : Socket that holds the connection with the server
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
NetworkRecorder(sf::SocketTCP Socket) :
|
||||
mySocket(Socket)
|
||||
NetworkRecorder(sf::SocketTCP socket) :
|
||||
mySocket(socket)
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -37,15 +37,15 @@ private :
|
|||
/// /see SoundRecorder::ProcessSamples
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
virtual bool OnProcessSamples(const sf::Int16* Samples, std::size_t SamplesCount)
|
||||
virtual bool OnProcessSamples(const sf::Int16* samples, std::size_t samplesCount)
|
||||
{
|
||||
// Pack the audio samples into a network packet
|
||||
sf::Packet PacketOut;
|
||||
PacketOut << AudioData;
|
||||
PacketOut.Append(Samples, SamplesCount * sizeof(sf::Int16));
|
||||
sf::Packet packet;
|
||||
packet << audioData;
|
||||
packet.Append(samples, samplesCount * sizeof(sf::Int16));
|
||||
|
||||
// Send the audio packet to the server
|
||||
return mySocket.Send(PacketOut) == sf::Socket::Done;
|
||||
return mySocket.Send(packet) == sf::Socket::Done;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
@ -60,7 +60,7 @@ private :
|
|||
/// start sending him audio data
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void DoClient(unsigned short Port)
|
||||
void DoClient(unsigned short port)
|
||||
{
|
||||
// Check that the device can capture audio
|
||||
if (sf::SoundRecorder::CanCapture() == false)
|
||||
|
@ -70,21 +70,21 @@ void DoClient(unsigned short Port)
|
|||
}
|
||||
|
||||
// Ask for server address
|
||||
sf::IPAddress ServerAddress;
|
||||
sf::IPAddress serverAddress;
|
||||
do
|
||||
{
|
||||
std::cout << "Type address or name of the server to connect to : ";
|
||||
std::cin >> ServerAddress;
|
||||
std::cin >> serverAddress;
|
||||
}
|
||||
while (!ServerAddress.IsValid());
|
||||
while (!serverAddress.IsValid());
|
||||
|
||||
// Create a TCP socket for communicating with server
|
||||
sf::SocketTCP Socket;
|
||||
sf::SocketTCP socket;
|
||||
|
||||
// Connect to the specified server
|
||||
if (Socket.Connect(Port, ServerAddress) != sf::Socket::Done)
|
||||
if (socket.Connect(port, serverAddress) != sf::Socket::Done)
|
||||
return;
|
||||
std::cout << "Connected to server " << ServerAddress << std::endl;
|
||||
std::cout << "Connected to server " << serverAddress << std::endl;
|
||||
|
||||
// Wait for user input...
|
||||
std::cin.ignore(10000, '\n');
|
||||
|
@ -92,19 +92,19 @@ void DoClient(unsigned short Port)
|
|||
std::cin.ignore(10000, '\n');
|
||||
|
||||
// Create a instance of our custom recorder
|
||||
NetworkRecorder Recorder(Socket);
|
||||
NetworkRecorder recorder(socket);
|
||||
|
||||
// Start capturing audio data
|
||||
Recorder.Start(44100);
|
||||
recorder.Start(44100);
|
||||
std::cout << "Recording... press enter to stop";
|
||||
std::cin.ignore(10000, '\n');
|
||||
Recorder.Stop();
|
||||
recorder.Stop();
|
||||
|
||||
// Send a "end-of-stream" packet
|
||||
sf::Packet PacketOut;
|
||||
PacketOut << EndOfStream;
|
||||
Socket.Send(PacketOut);
|
||||
sf::Packet packet;
|
||||
packet << endOfStream;
|
||||
socket.Send(packet);
|
||||
|
||||
// Close the socket when we're done
|
||||
Socket.Close();
|
||||
socket.Close();
|
||||
}
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
#include <iostream>
|
||||
|
||||
|
||||
const sf::Uint8 AudioData = 1;
|
||||
const sf::Uint8 EndOfStream = 2;
|
||||
const sf::Uint8 audioData = 1;
|
||||
const sf::Uint8 endOfStream = 2;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
@ -47,19 +47,19 @@ public :
|
|||
/// Run the server, stream audio data from the client
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void Start(unsigned short Port)
|
||||
void Start(unsigned short port)
|
||||
{
|
||||
if (!myHasFinished)
|
||||
{
|
||||
// Listen to the given port for incoming connections
|
||||
if (!myListener.Listen(Port))
|
||||
if (!myListener.Listen(port))
|
||||
return;
|
||||
std::cout << "Server is listening to port " << Port << ", waiting for connections... " << std::endl;
|
||||
std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;
|
||||
|
||||
// Wait for a connection
|
||||
sf::IPAddress ClientAddress;
|
||||
myListener.Accept(myClient, &ClientAddress);
|
||||
std::cout << "Client connected : " << ClientAddress << std::endl;
|
||||
sf::IPAddress clientAddress;
|
||||
myListener.Accept(myClient, &clientAddress);
|
||||
std::cout << "Client connected : " << clientAddress << std::endl;
|
||||
|
||||
// Start playback
|
||||
Play();
|
||||
|
@ -80,7 +80,7 @@ private :
|
|||
/// /see SoundStream::OnGetData
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
virtual bool OnGetData(sf::SoundStream::Chunk& Data)
|
||||
virtual bool OnGetData(sf::SoundStream::Chunk& data)
|
||||
{
|
||||
// We have reached the end of the buffer and all audio data have been played : we can stop playback
|
||||
if ((myOffset >= mySamples.size()) && myHasFinished)
|
||||
|
@ -93,13 +93,13 @@ private :
|
|||
// Copy samples into a local buffer to avoid synchronization problems
|
||||
// (don't forget that we run in two separate threads)
|
||||
{
|
||||
sf::Lock Lock(myMutex);
|
||||
sf::Lock lock(myMutex);
|
||||
myTempBuffer.assign(mySamples.begin() + myOffset, mySamples.end());
|
||||
}
|
||||
|
||||
// Fill audio data to pass to the stream
|
||||
Data.Samples = &myTempBuffer[0];
|
||||
Data.NbSamples = myTempBuffer.size();
|
||||
data.Samples = &myTempBuffer[0];
|
||||
data.NbSamples = myTempBuffer.size();
|
||||
|
||||
// Update the playing offset
|
||||
myOffset += myTempBuffer.size();
|
||||
|
@ -111,9 +111,9 @@ private :
|
|||
/// /see SoundStream::OnSeek
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
virtual void OnSeek(float TimeOffset)
|
||||
virtual void OnSeek(float timeOffset)
|
||||
{
|
||||
myOffset = static_cast<std::size_t>(TimeOffset * GetSampleRate() * GetChannelsCount());
|
||||
myOffset = static_cast<std::size_t>(timeOffset * GetSampleRate() * GetChannelsCount());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
@ -125,28 +125,28 @@ private :
|
|||
while (!myHasFinished)
|
||||
{
|
||||
// Get waiting audio data from the network
|
||||
sf::Packet PacketIn;
|
||||
if (myClient.Receive(PacketIn) != sf::Socket::Done)
|
||||
sf::Packet packet;
|
||||
if (myClient.Receive(packet) != sf::Socket::Done)
|
||||
break;
|
||||
|
||||
// Extract the message ID
|
||||
sf::Uint8 Id;
|
||||
PacketIn >> Id;
|
||||
sf::Uint8 id;
|
||||
packet >> id;
|
||||
|
||||
if (Id == AudioData)
|
||||
if (id == audioData)
|
||||
{
|
||||
// Extract audio samples from the packet, and append it to our samples buffer
|
||||
const sf::Int16* Samples = reinterpret_cast<const sf::Int16*>(PacketIn.GetData() + 1);
|
||||
std::size_t NbSamples = (PacketIn.GetDataSize() - 1) / sizeof(sf::Int16);
|
||||
const sf::Int16* samples = reinterpret_cast<const sf::Int16*>(packet.GetData() + 1);
|
||||
std::size_t nbSamples = (packet.GetDataSize() - 1) / sizeof(sf::Int16);
|
||||
|
||||
// Don't forget that the other thread can access the samples array at any time
|
||||
// (so we protect any operation on it with the mutex)
|
||||
{
|
||||
sf::Lock Lock(myMutex);
|
||||
std::copy(Samples, Samples + NbSamples, std::back_inserter(mySamples));
|
||||
sf::Lock lock(myMutex);
|
||||
std::copy(samples, samples + nbSamples, std::back_inserter(mySamples));
|
||||
}
|
||||
}
|
||||
else if (Id == EndOfStream)
|
||||
else if (id == endOfStream)
|
||||
{
|
||||
// End of stream reached : we stop receiving audio data
|
||||
std::cout << "Audio data has been 100% received !" << std::endl;
|
||||
|
@ -179,14 +179,14 @@ private :
|
|||
/// a connected client
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void DoServer(unsigned short Port)
|
||||
void DoServer(unsigned short port)
|
||||
{
|
||||
// Build an audio stream to play sound data as it is received through the network
|
||||
NetworkAudioStream AudioStream;
|
||||
AudioStream.Start(Port);
|
||||
NetworkAudioStream audioStream;
|
||||
audioStream.Start(port);
|
||||
|
||||
// Loop until the sound playback is finished
|
||||
while (AudioStream.GetStatus() != sf::SoundStream::Stopped)
|
||||
while (audioStream.GetStatus() != sf::SoundStream::Stopped)
|
||||
{
|
||||
// Leave some CPU time for other threads
|
||||
sf::Sleep(0.1f);
|
||||
|
@ -199,10 +199,10 @@ void DoServer(unsigned short Port)
|
|||
std::cin.ignore(10000, '\n');
|
||||
|
||||
// Replay the sound (just to make sure replaying the received data is OK)
|
||||
AudioStream.Play();
|
||||
audioStream.Play();
|
||||
|
||||
// Loop until the sound playback is finished
|
||||
while (AudioStream.GetStatus() != sf::SoundStream::Stopped)
|
||||
while (audioStream.GetStatus() != sf::SoundStream::Stopped)
|
||||
{
|
||||
// Leave some CPU time for other threads
|
||||
sf::Sleep(0.1f);
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
|
@ -11,8 +11,8 @@
|
|||
// Function prototypes
|
||||
// (I'm too lazy to put them into separate headers...)
|
||||
////////////////////////////////////////////////////////////
|
||||
void DoClient(unsigned short Port);
|
||||
void DoServer(unsigned short Port);
|
||||
void DoClient(unsigned short port);
|
||||
void DoServer(unsigned short port);
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
@ -24,22 +24,22 @@ void DoServer(unsigned short Port);
|
|||
int main()
|
||||
{
|
||||
// Choose a random port for opening sockets (ports < 1024 are reserved)
|
||||
const unsigned short Port = 2435;
|
||||
const unsigned short port = 2435;
|
||||
|
||||
// Client or server ?
|
||||
char Who;
|
||||
char who;
|
||||
std::cout << "Do you want to be a server ('s') or a client ('c') ? ";
|
||||
std::cin >> Who;
|
||||
std::cin >> who;
|
||||
|
||||
if (Who == 's')
|
||||
if (who == 's')
|
||||
{
|
||||
// Run as a server
|
||||
DoServer(Port);
|
||||
DoServer(port);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Run as a client
|
||||
DoClient(Port);
|
||||
DoClient(port);
|
||||
}
|
||||
|
||||
// Wait until the user presses 'enter' key
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue