Chris@4
|
1 #include "portaudiocpp/Stream.hxx"
|
Chris@4
|
2
|
Chris@4
|
3 #include <cstddef>
|
Chris@4
|
4
|
Chris@4
|
5 #include "portaudiocpp/Exception.hxx"
|
Chris@4
|
6 #include "portaudiocpp/System.hxx"
|
Chris@4
|
7
|
Chris@4
|
8 namespace portaudio
|
Chris@4
|
9 {
|
Chris@4
|
10
|
Chris@4
|
11 // -----------------------------------------------------------------------------------
|
Chris@4
|
12
|
Chris@4
|
13 Stream::Stream() : stream_(NULL)
|
Chris@4
|
14 {
|
Chris@4
|
15 }
|
Chris@4
|
16
|
Chris@4
|
17 Stream::~Stream()
|
Chris@4
|
18 {
|
Chris@4
|
19 // (can't call close here,
|
Chris@4
|
20 // the derived class should atleast call
|
Chris@4
|
21 // close() in it's deconstructor)
|
Chris@4
|
22 }
|
Chris@4
|
23
|
Chris@4
|
24 // -----------------------------------------------------------------------------------
|
Chris@4
|
25
|
Chris@4
|
26 //////
|
Chris@4
|
27 /// Closes the Stream if it's open, else does nothing.
|
Chris@4
|
28 //////
|
Chris@4
|
29 void Stream::close()
|
Chris@4
|
30 {
|
Chris@4
|
31 if (isOpen() && System::exists())
|
Chris@4
|
32 {
|
Chris@4
|
33 PaError err = Pa_CloseStream(stream_);
|
Chris@4
|
34 stream_ = NULL;
|
Chris@4
|
35
|
Chris@4
|
36 if (err != paNoError)
|
Chris@4
|
37 throw PaException(err);
|
Chris@4
|
38 }
|
Chris@4
|
39 }
|
Chris@4
|
40
|
Chris@4
|
41 //////
|
Chris@4
|
42 /// Returns true if the Stream is open.
|
Chris@4
|
43 //////
|
Chris@4
|
44 bool Stream::isOpen() const
|
Chris@4
|
45 {
|
Chris@4
|
46 return (stream_ != NULL);
|
Chris@4
|
47 }
|
Chris@4
|
48
|
Chris@4
|
49 // -----------------------------------------------------------------------------------
|
Chris@4
|
50
|
Chris@4
|
51 void Stream::setStreamFinishedCallback(PaStreamFinishedCallback *callback)
|
Chris@4
|
52 {
|
Chris@4
|
53 PaError err = Pa_SetStreamFinishedCallback(stream_, callback);
|
Chris@4
|
54
|
Chris@4
|
55 if (err != paNoError)
|
Chris@4
|
56 throw PaException(err);
|
Chris@4
|
57 }
|
Chris@4
|
58
|
Chris@4
|
59 // -----------------------------------------------------------------------------------
|
Chris@4
|
60
|
Chris@4
|
61 void Stream::start()
|
Chris@4
|
62 {
|
Chris@4
|
63 PaError err = Pa_StartStream(stream_);
|
Chris@4
|
64
|
Chris@4
|
65 if (err != paNoError)
|
Chris@4
|
66 throw PaException(err);
|
Chris@4
|
67 }
|
Chris@4
|
68
|
Chris@4
|
69 void Stream::stop()
|
Chris@4
|
70 {
|
Chris@4
|
71 PaError err = Pa_StopStream(stream_);
|
Chris@4
|
72
|
Chris@4
|
73 if (err != paNoError)
|
Chris@4
|
74 throw PaException(err);
|
Chris@4
|
75 }
|
Chris@4
|
76
|
Chris@4
|
77 void Stream::abort()
|
Chris@4
|
78 {
|
Chris@4
|
79 PaError err = Pa_AbortStream(stream_);
|
Chris@4
|
80
|
Chris@4
|
81 if (err != paNoError)
|
Chris@4
|
82 throw PaException(err);
|
Chris@4
|
83 }
|
Chris@4
|
84
|
Chris@4
|
85 bool Stream::isStopped() const
|
Chris@4
|
86 {
|
Chris@4
|
87 PaError ret = Pa_IsStreamStopped(stream_);
|
Chris@4
|
88
|
Chris@4
|
89 if (ret < 0)
|
Chris@4
|
90 throw PaException(ret);
|
Chris@4
|
91
|
Chris@4
|
92 return (ret == 1);
|
Chris@4
|
93 }
|
Chris@4
|
94
|
Chris@4
|
95 bool Stream::isActive() const
|
Chris@4
|
96 {
|
Chris@4
|
97 PaError ret = Pa_IsStreamActive(stream_);
|
Chris@4
|
98
|
Chris@4
|
99 if (ret < 0)
|
Chris@4
|
100 throw PaException(ret);
|
Chris@4
|
101
|
Chris@4
|
102 return (ret == 1);
|
Chris@4
|
103 }
|
Chris@4
|
104
|
Chris@4
|
105 // -----------------------------------------------------------------------------------
|
Chris@4
|
106
|
Chris@4
|
107 //////
|
Chris@4
|
108 /// Returns the best known input latency for the Stream. This value may differ from the
|
Chris@4
|
109 /// suggested input latency set in the StreamParameters. Includes all sources of
|
Chris@4
|
110 /// latency known to PortAudio such as internal buffering, and Host API reported latency.
|
Chris@4
|
111 /// Doesn't include any estimates of unknown latency.
|
Chris@4
|
112 //////
|
Chris@4
|
113 PaTime Stream::inputLatency() const
|
Chris@4
|
114 {
|
Chris@4
|
115 const PaStreamInfo *info = Pa_GetStreamInfo(stream_);
|
Chris@4
|
116 if (info == NULL)
|
Chris@4
|
117 {
|
Chris@4
|
118 throw PaException(paInternalError);
|
Chris@4
|
119 return PaTime(0.0);
|
Chris@4
|
120 }
|
Chris@4
|
121
|
Chris@4
|
122 return info->inputLatency;
|
Chris@4
|
123 }
|
Chris@4
|
124
|
Chris@4
|
125 //////
|
Chris@4
|
126 /// Returns the best known output latency for the Stream. This value may differ from the
|
Chris@4
|
127 /// suggested output latency set in the StreamParameters. Includes all sources of
|
Chris@4
|
128 /// latency known to PortAudio such as internal buffering, and Host API reported latency.
|
Chris@4
|
129 /// Doesn't include any estimates of unknown latency.
|
Chris@4
|
130 //////
|
Chris@4
|
131 PaTime Stream::outputLatency() const
|
Chris@4
|
132 {
|
Chris@4
|
133 const PaStreamInfo *info = Pa_GetStreamInfo(stream_);
|
Chris@4
|
134 if (info == NULL)
|
Chris@4
|
135 {
|
Chris@4
|
136 throw PaException(paInternalError);
|
Chris@4
|
137 return PaTime(0.0);
|
Chris@4
|
138 }
|
Chris@4
|
139
|
Chris@4
|
140 return info->outputLatency;
|
Chris@4
|
141 }
|
Chris@4
|
142
|
Chris@4
|
143 //////
|
Chris@4
|
144 /// Returns the sample rate of the Stream. Usually this will be the
|
Chris@4
|
145 /// best known estimate of the used sample rate. For instance when opening a
|
Chris@4
|
146 /// Stream setting 44100.0 Hz in the StreamParameters, the actual sample
|
Chris@4
|
147 /// rate might be something like 44103.2 Hz (due to imperfections in the
|
Chris@4
|
148 /// sound card hardware).
|
Chris@4
|
149 //////
|
Chris@4
|
150 double Stream::sampleRate() const
|
Chris@4
|
151 {
|
Chris@4
|
152 const PaStreamInfo *info = Pa_GetStreamInfo(stream_);
|
Chris@4
|
153 if (info == NULL)
|
Chris@4
|
154 {
|
Chris@4
|
155 throw PaException(paInternalError);
|
Chris@4
|
156 return 0.0;
|
Chris@4
|
157 }
|
Chris@4
|
158
|
Chris@4
|
159 return info->sampleRate;
|
Chris@4
|
160 }
|
Chris@4
|
161
|
Chris@4
|
162 // -----------------------------------------------------------------------------------
|
Chris@4
|
163
|
Chris@4
|
164 PaTime Stream::time() const
|
Chris@4
|
165 {
|
Chris@4
|
166 return Pa_GetStreamTime(stream_);
|
Chris@4
|
167 }
|
Chris@4
|
168
|
Chris@4
|
169 // -----------------------------------------------------------------------------------
|
Chris@4
|
170
|
Chris@4
|
171 //////
|
Chris@4
|
172 /// Accessor (const) for PortAudio PaStream pointer, useful for interfacing with
|
Chris@4
|
173 /// PortAudio add-ons such as PortMixer for instance. Normally accessing this
|
Chris@4
|
174 /// pointer should not be needed as PortAudioCpp aims to provide all of PortAudio's
|
Chris@4
|
175 /// functionality.
|
Chris@4
|
176 //////
|
Chris@4
|
177 const PaStream *Stream::paStream() const
|
Chris@4
|
178 {
|
Chris@4
|
179 return stream_;
|
Chris@4
|
180 }
|
Chris@4
|
181
|
Chris@4
|
182 //////
|
Chris@4
|
183 /// Accessor (non-const) for PortAudio PaStream pointer, useful for interfacing with
|
Chris@4
|
184 /// PortAudio add-ons such as PortMixer for instance. Normally accessing this
|
Chris@4
|
185 /// pointer should not be needed as PortAudioCpp aims to provide all of PortAudio's
|
Chris@4
|
186 /// functionality.
|
Chris@4
|
187 //////
|
Chris@4
|
188 PaStream *Stream::paStream()
|
Chris@4
|
189 {
|
Chris@4
|
190 return stream_;
|
Chris@4
|
191 }
|
Chris@4
|
192
|
Chris@4
|
193 // -----------------------------------------------------------------------------------
|
Chris@4
|
194
|
Chris@4
|
195 } // namespace portaudio
|