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