15 static struct timeval then, now;
16 static long timeVals, thisSec, timeSum;
23 unsigned waitTime = (unsigned) (1000000.0f * (
float) bufSize / (float) CGestalt::sample_rate());
30 if (WSAStartup(MAKEWORD(1,1),&localWSA)!= 0)
31 perror(
"Couldn't do WSAStartup");
34 for (
int i = 0; i < 2; i++) {
46 perror(
"RFS: sem_open");
50 for (
unsigned i = 0; i < 2; i++) {
60 perror(
"Close mSocket");
71 perror(
"RemoteStream: sem_open");
74 logMsg(
"Open RemoteStream output socket");
78 perror(
"RemoteStream: socket");
81 if ((he = gethostbyname(serverName)) == NULL) {
82 perror(
"\tRemoteStream: gethostbyname");
88 mServerAddr.sin_addr = *((
struct in_addr *) he->h_addr);
89 logMsg(
"RemoteStream open output socket to %s:%d (= %d) (sz = %d)",
97 unsigned char * bString;
102 for (
int i = 0; i < 2; i++) {
116 perror(
"\tRemoteStream connect");
121 perror(
"\tRemoteStream intro packet write");
127 perror(
"RemoteStream connect");
131 command = packetHeader->
magic;
133 logMsg(
kLogError,
"RemoteStream: bad request magic number: %x\n", command);
136 logMsg(
"RemoteStream received server confirmation");
147 perror(
"RemoteStream: close _sock");
153 unsigned char * out, * buf;
154 unsigned frames = outputBuffer.mNumFrames;
159 if ((mCurrentFrame + frames) <= mBufferSize) {
161 toCopy = frames *
sizeof(
sample);
164 toCopy = (mBufferSize - mCurrentFrame) *
sizeof(
sample);
167 for (
unsigned i = 0; i < outputBuffer.mNumChannels; i++) {
168 out = (
unsigned char *) outputBuffer.buffer(i];
170 + (i * mBufferSize *
sizeof(
sample));
171 buf = ((
unsigned char *) (mIoBuffers[mCurrentBuffer])) + offset;
172 memcpy(out, buf, toCopy);
174 mCurrentFrame += frames;
175 if (mCurrentFrame >= mBufferSize) {
176 mCurrentBuffer = 1 - mCurrentBuffer;
178 sem_post(mServerSemaphore);
181 toCopy = (frames *
sizeof(
sample)) - toCopy;
182 for (
unsigned i = 0; i < outputBuffer.mNumChannels; i++) {
183 out = (
unsigned char *) outputBuffer.buffer(i];
185 buf = ((
unsigned char *) (mIoBuffers[mCurrentBuffer])) + offset;
186 memcpy(out, buf, toCopy);
188 mCurrentFrame = toCopy /
sizeof(
sample);
198 unsigned command, packet_size;
199 unsigned _numChannels = rfs->channels();
200 unsigned which, _bufferSize;
201 unsigned char * b_str;
202 CSL_RFS_MSG * pkt_header;
203 int sock = rfs->get_sock();
205 thisSec = timeVals = timeSum = 0L;
208 logMsg(
"\tRemoteStream starting read_loop");
210 sem_wait(rfs->get_semaphore());
212 which = rfs->get_buffer_switch();
213 _bufferSize = rfs->get_buffer_size();
214 b_str = (
unsigned char *) ((rfs->get_io_buffers())[1 - which]);
215 pkt_header = (CSL_RFS_MSG *) b_str;
217 pkt_header->frames = _bufferSize;
218 pkt_header->channels = _numChannels;
221 #endif // Send a request packet
222 if (write(sock, (
char *) b_str, RFS_PACKET_SIZE) != (
int) RFS_PACKET_SIZE) {
227 packet_size = RFS_RESPONSE_PACKET_SIZE;
228 bytes_xfer = read(sock, (
char *) b_str, packet_size);
229 if (bytes_xfer != (
int) packet_size) {
236 rfs->print_time_statistics(&now, &then, &thisSec, &timeSum, &timeVals);
237 #endif // Now process the response buffer
238 command = pkt_header->magic;
239 if ((command & 0xffffff00) != RFS_PACKET_MAGIC) {
240 logMsg(
kLogError,
"RemoteStream: bad response magic number: %x", command);
243 memcpy(& command, & b_str[packet_size - 4], 4);
244 if (command != RFS_PACKET_MAGIC) {
258 pthread_attr_init(&attr);
259 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
260 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
262 ret = pthread_create(&tid, &attr, pfnThreadProc, pvParam);
void logMsg(const char *format,...)
These are the public logging messages.
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
void * RS_Read_Loop(void *inst)
void nextBuffer(Buffer &outputBuffer)
Get a buffer of frames.
void * RS_read_loop(void *inst)
struct sockaddr_in mServerAddr
Socket addresses for the remote server and for me.
RemoteStream(char *clientName, unsigned short clientPort, unsigned ch, unsigned bufSize)
Constructor.
#define RS_RESPONSE_PACKET_SIZE
int initSockets(char *serverName, unsigned short serverPort)
float sample
(could be changed to int, or double)
unsigned mNumChannels
my "expected" number of output channels
unsigned mBufferSize
the size of the input ring buffer (in FRAMES)
virtual int connectToServer()
sample * mIoBuffers[2]
My IO buffers (2 for dbl-buffering; mBufferSize frames in size)
sem_t * mServerSemaphore
Semaphore to trigger call to server for samples.
int CSL_CreateThread(THREAD_START_ROUTINE pfnThreadProc, void *pvParam)
unsigned mCurrentBuffer
current IO buffer in use
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
CSL_RS_MSG mHeader
The request packet header.
void *(* THREAD_START_ROUTINE)(void *)
#define CSL_CMD_NEXT_BUFFER
unsigned mCurrentFrame
current position in buffer
#define CSL_CMD_SET_CLIENT
void error(int num, const char *m, const char *path)
int mSocket
The socket I send to.
Base class of CSL exceptions (written upper-case). Has a string message.