-
Notifications
You must be signed in to change notification settings - Fork 175
/
InetWvIn.h
158 lines (133 loc) · 5.53 KB
/
InetWvIn.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef STK_INETWVIN_H
#define STK_INETWVIN_H
#include "WvIn.h"
#include "TcpServer.h"
#include "UdpSocket.h"
#include "Thread.h"
#include "Mutex.h"
namespace stk {
/***************************************************/
/*! \class InetWvIn
\brief STK internet streaming input class.
This Wvin subclass reads streamed audio data over a network via a
TCP or UDP socket connection. The data is assumed in big-endian,
or network, byte order. Only a single socket connection is
supported.
InetWvIn supports multi-channel data. It is important to
distinguish the tick() method that computes a single frame (and
returns only the specified sample of a multi-channel frame) from
the overloaded one that takes an StkFrames object for
multi-channel and/or multi-frame data.
This class implements a socket server. When using the TCP
protocol, the server "listens" for a single remote connection
within the InetWvIn::start() function. For the UDP protocol, no
attempt is made to verify packet delivery or order. The default
data type for the incoming stream is signed 16-bit integers,
though any of the defined StkFormats are permissible.
by Perry R. Cook and Gary P. Scavone, 1995--2023.
*/
/***************************************************/
typedef struct {
bool finished;
void *object;
} ThreadInfo;
class InetWvIn : public WvIn
{
public:
//! Default constructor.
/*!
An StkError will be thrown if an error occurs while initializing the input thread.
*/
InetWvIn( unsigned long bufferFrames = 1024, unsigned int nBuffers = 8 );
//! Class destructor.
~InetWvIn();
//! Wait for a (new) socket connection with specified protocol, port, data channels and format.
/*!
For the UDP protocol, this function will create a socket
instance and return. For the TCP protocol, this function will
block until a connection is established. An StkError will be
thrown if a socket error occurs or an invalid function argument is
provided.
*/
void listen( int port = 2006, unsigned int nChannels = 1,
Stk::StkFormat format = STK_SINT16,
Socket::ProtocolType protocol = Socket::PROTO_TCP );
//! Returns true is an input connection exists or input data remains in the queue.
/*!
This method will not return false after an input connection has been closed until
all buffered input data has been read out.
*/
bool isConnected( void );
//! Return the specified channel value of the last computed frame.
/*!
For multi-channel files, use the lastFrame() function to get
all values from the last computed frame. If no connection exists,
the returned value is 0.0. The \c channel argument must be less
than the number of channels in the data stream (the first channel
is specified by 0). However, range checking is only performed if
_STK_DEBUG_ is defined during compilation, in which case an
out-of-range value will trigger an StkError exception.
*/
StkFloat lastOut( unsigned int channel = 0 );
//! Compute a sample frame and return the specified \c channel value.
/*!
For multi-channel files, use the lastFrame() function to get
all values from the computed frame. If no connection exists, the
returned value is 0.0 (and a warning will be issued if _STK_DEBUG_
is defined during compilation). The \c channel argument must be
less than the number of channels in the data stream (the first
channel is specified by 0). However, range checking is only
performed if _STK_DEBUG_ is defined during compilation, in which
case an out-of-range value will trigger an StkError exception.
*/
StkFloat tick( unsigned int channel = 0 );
//! Fill the StkFrames object with computed sample frames, starting at the specified channel and return the same reference.
/*!
The \c channel argument plus the number of channels specified
in the listen() function must be less than the number of channels
in the StkFrames argument (the first channel is specified by 0).
However, this is only checked if _STK_DEBUG_ is defined during
compilation, in which case an incompatibility will trigger an
StkError exception. If no connection exists, the function does
nothing (a warning will be issued if _STK_DEBUG_ is defined during
compilation).
*/
StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
// Called by the thread routine to receive data via the socket connection
// and fill the socket buffer. This is not intended for general use but
// must be public for access from the thread.
void receive( void );
protected:
// Read buffered socket data into the data buffer ... will block if none available.
int readData( void );
Socket *soket_;
Thread thread_;
Mutex mutex_;
char *buffer_;
unsigned long bufferFrames_;
unsigned long bufferBytes_;
unsigned long bytesFilled_;
unsigned int nBuffers_;
unsigned long writePoint_;
unsigned long readPoint_;
long bufferCounter_;
int dataBytes_;
bool connected_;
int fd_;
ThreadInfo threadInfo_;
Stk::StkFormat dataType_;
};
inline StkFloat InetWvIn :: lastOut( unsigned int channel )
{
#if defined(_STK_DEBUG_)
if ( channel >= data_.channels() ) {
oStream_ << "InetWvIn::lastOut(): channel argument and data stream are incompatible!";
handleError( StkError::FUNCTION_ARGUMENT );
}
#endif
// If no connection and we've output all samples in the queue, return.
if ( !connected_ && bytesFilled_ == 0 && bufferCounter_ == 0 ) return 0.0;
return lastFrame_[channel];
}
} // stk namespace
#endif