annotate src/portaudio_20140130/bindings/cpp/source/portaudiocpp/Stream.cxx @ 39:7ddb4fc30dac

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