annotate src/portaudio_20161030/bindings/cpp/source/portaudiocpp/Stream.cxx @ 81:7029a4916348

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