summaryrefslogtreecommitdiff
path: root/3rdparty/portaudio/bindings/cpp/source/portaudiocpp/Stream.cxx
blob: ba16e0335f994f01dee558e049c1549b8d8f787e (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "portaudiocpp/Stream.hxx"

#include <cstddef>

#include "portaudiocpp/Exception.hxx"
#include "portaudiocpp/System.hxx"

namespace portaudio
{

	// -----------------------------------------------------------------------------------

	Stream::Stream() : stream_(NULL)
	{
	}

	Stream::~Stream()
	{
		// (can't call close here, 
		// the derived class should atleast call 
		// close() in it's deconstructor)
	}

	// -----------------------------------------------------------------------------------

	//////
	/// Closes the Stream if it's open, else does nothing.
	//////
	void Stream::close()
	{
		if (isOpen() && System::exists())
		{
			PaError err = Pa_CloseStream(stream_);
			stream_ = NULL;

			if (err != paNoError)
				throw PaException(err);
		}
	}

	//////
	/// Returns true if the Stream is open.
	//////
	bool Stream::isOpen() const
	{
		return (stream_ != NULL);
	}

	// -----------------------------------------------------------------------------------

	void Stream::setStreamFinishedCallback(PaStreamFinishedCallback *callback)
	{
		PaError err = Pa_SetStreamFinishedCallback(stream_, callback);

		if (err != paNoError)
			throw PaException(err);
	}

	// -----------------------------------------------------------------------------------

	void Stream::start()
	{
		PaError err = Pa_StartStream(stream_);

		if (err != paNoError)
			throw PaException(err);
	}

	void Stream::stop()
	{
		PaError err = Pa_StopStream(stream_);

		if (err != paNoError)
			throw PaException(err);
	}

	void Stream::abort()
	{
		PaError err = Pa_AbortStream(stream_);

		if (err != paNoError)
			throw PaException(err);
	}

	bool Stream::isStopped() const
	{
		PaError ret = Pa_IsStreamStopped(stream_);

		if (ret < 0)
			throw PaException(ret);

		return (ret == 1);
	}

	bool Stream::isActive() const
	{
		PaError ret = Pa_IsStreamActive(stream_);

		if (ret < 0)
			throw PaException(ret);

		return (ret == 1);
	}

	// -----------------------------------------------------------------------------------

	//////
	/// Returns the best known input latency for the Stream. This value may differ from the 
	/// suggested input latency set in the StreamParameters. Includes all sources of 
	/// latency known to PortAudio such as internal buffering, and Host API reported latency. 
	/// Doesn't include any estimates of unknown latency.
	//////
	PaTime Stream::inputLatency() const
	{
		const PaStreamInfo *info = Pa_GetStreamInfo(stream_);
		if (info == NULL)
		{
			throw PaException(paInternalError);
			return PaTime(0.0);
		}

		return info->inputLatency;
	}

	//////
	/// Returns the best known output latency for the Stream. This value may differ from the 
	/// suggested output latency set in the StreamParameters. Includes all sources of 
	/// latency known to PortAudio such as internal buffering, and Host API reported latency. 
	/// Doesn't include any estimates of unknown latency.
	//////
	PaTime Stream::outputLatency() const
	{
		const PaStreamInfo *info = Pa_GetStreamInfo(stream_);
		if (info == NULL)
		{
			throw PaException(paInternalError);
			return PaTime(0.0);
		}

		return info->outputLatency;
	}

	//////
	/// Returns the sample rate of the Stream. Usually this will be the 
	/// best known estimate of the used sample rate. For instance when opening a 
	/// Stream setting 44100.0 Hz in the StreamParameters, the actual sample 
	/// rate might be something like 44103.2 Hz (due to imperfections in the 
	/// sound card hardware).
	//////
	double Stream::sampleRate() const
	{
		const PaStreamInfo *info = Pa_GetStreamInfo(stream_);
		if (info == NULL)
		{
			throw PaException(paInternalError);
			return 0.0;
		}

		return info->sampleRate;
	}

	// -----------------------------------------------------------------------------------

	PaTime Stream::time() const
	{
		return Pa_GetStreamTime(stream_);
	}

	// -----------------------------------------------------------------------------------

	//////
	/// Accessor (const) for PortAudio PaStream pointer, useful for interfacing with 
	/// PortAudio add-ons such as PortMixer for instance. Normally accessing this 
	/// pointer should not be needed as PortAudioCpp aims to provide all of PortAudio's 
	/// functionality.
	//////
	const PaStream *Stream::paStream() const
	{
		return stream_;
	}

	//////
	/// Accessor (non-const) for PortAudio PaStream pointer, useful for interfacing with 
	/// PortAudio add-ons such as PortMixer for instance. Normally accessing this 
	/// pointer should not be needed as PortAudioCpp aims to provide all of PortAudio's 
	/// functionality.
	//////
	PaStream *Stream::paStream()
	{
		return stream_;
	}

	// -----------------------------------------------------------------------------------

} // namespace portaudio