Chris@4: #include "portaudiocpp/Stream.hxx" Chris@4: Chris@4: #include Chris@4: Chris@4: #include "portaudiocpp/Exception.hxx" Chris@4: #include "portaudiocpp/System.hxx" Chris@4: Chris@4: namespace portaudio Chris@4: { Chris@4: Chris@4: // ----------------------------------------------------------------------------------- Chris@4: Chris@4: Stream::Stream() : stream_(NULL) Chris@4: { Chris@4: } Chris@4: Chris@4: Stream::~Stream() Chris@4: { Chris@4: // (can't call close here, Chris@4: // the derived class should atleast call Chris@4: // close() in it's deconstructor) Chris@4: } Chris@4: Chris@4: // ----------------------------------------------------------------------------------- Chris@4: Chris@4: ////// Chris@4: /// Closes the Stream if it's open, else does nothing. Chris@4: ////// Chris@4: void Stream::close() Chris@4: { Chris@4: if (isOpen() && System::exists()) Chris@4: { Chris@4: PaError err = Pa_CloseStream(stream_); Chris@4: stream_ = NULL; Chris@4: Chris@4: if (err != paNoError) Chris@4: throw PaException(err); Chris@4: } Chris@4: } Chris@4: Chris@4: ////// Chris@4: /// Returns true if the Stream is open. Chris@4: ////// Chris@4: bool Stream::isOpen() const Chris@4: { Chris@4: return (stream_ != NULL); Chris@4: } Chris@4: Chris@4: // ----------------------------------------------------------------------------------- Chris@4: Chris@4: void Stream::setStreamFinishedCallback(PaStreamFinishedCallback *callback) Chris@4: { Chris@4: PaError err = Pa_SetStreamFinishedCallback(stream_, callback); Chris@4: Chris@4: if (err != paNoError) Chris@4: throw PaException(err); Chris@4: } Chris@4: Chris@4: // ----------------------------------------------------------------------------------- Chris@4: Chris@4: void Stream::start() Chris@4: { Chris@4: PaError err = Pa_StartStream(stream_); Chris@4: Chris@4: if (err != paNoError) Chris@4: throw PaException(err); Chris@4: } Chris@4: Chris@4: void Stream::stop() Chris@4: { Chris@4: PaError err = Pa_StopStream(stream_); Chris@4: Chris@4: if (err != paNoError) Chris@4: throw PaException(err); Chris@4: } Chris@4: Chris@4: void Stream::abort() Chris@4: { Chris@4: PaError err = Pa_AbortStream(stream_); Chris@4: Chris@4: if (err != paNoError) Chris@4: throw PaException(err); Chris@4: } Chris@4: Chris@4: bool Stream::isStopped() const Chris@4: { Chris@4: PaError ret = Pa_IsStreamStopped(stream_); Chris@4: Chris@4: if (ret < 0) Chris@4: throw PaException(ret); Chris@4: Chris@4: return (ret == 1); Chris@4: } Chris@4: Chris@4: bool Stream::isActive() const Chris@4: { Chris@4: PaError ret = Pa_IsStreamActive(stream_); Chris@4: Chris@4: if (ret < 0) Chris@4: throw PaException(ret); Chris@4: Chris@4: return (ret == 1); Chris@4: } Chris@4: Chris@4: // ----------------------------------------------------------------------------------- Chris@4: Chris@4: ////// Chris@4: /// Returns the best known input latency for the Stream. This value may differ from the Chris@4: /// suggested input latency set in the StreamParameters. Includes all sources of Chris@4: /// latency known to PortAudio such as internal buffering, and Host API reported latency. Chris@4: /// Doesn't include any estimates of unknown latency. Chris@4: ////// Chris@4: PaTime Stream::inputLatency() const Chris@4: { Chris@4: const PaStreamInfo *info = Pa_GetStreamInfo(stream_); Chris@4: if (info == NULL) Chris@4: { Chris@4: throw PaException(paInternalError); Chris@4: return PaTime(0.0); Chris@4: } Chris@4: Chris@4: return info->inputLatency; Chris@4: } Chris@4: Chris@4: ////// Chris@4: /// Returns the best known output latency for the Stream. This value may differ from the Chris@4: /// suggested output latency set in the StreamParameters. Includes all sources of Chris@4: /// latency known to PortAudio such as internal buffering, and Host API reported latency. Chris@4: /// Doesn't include any estimates of unknown latency. Chris@4: ////// Chris@4: PaTime Stream::outputLatency() const Chris@4: { Chris@4: const PaStreamInfo *info = Pa_GetStreamInfo(stream_); Chris@4: if (info == NULL) Chris@4: { Chris@4: throw PaException(paInternalError); Chris@4: return PaTime(0.0); Chris@4: } Chris@4: Chris@4: return info->outputLatency; Chris@4: } Chris@4: Chris@4: ////// Chris@4: /// Returns the sample rate of the Stream. Usually this will be the Chris@4: /// best known estimate of the used sample rate. For instance when opening a Chris@4: /// Stream setting 44100.0 Hz in the StreamParameters, the actual sample Chris@4: /// rate might be something like 44103.2 Hz (due to imperfections in the Chris@4: /// sound card hardware). Chris@4: ////// Chris@4: double Stream::sampleRate() const Chris@4: { Chris@4: const PaStreamInfo *info = Pa_GetStreamInfo(stream_); Chris@4: if (info == NULL) Chris@4: { Chris@4: throw PaException(paInternalError); Chris@4: return 0.0; Chris@4: } Chris@4: Chris@4: return info->sampleRate; Chris@4: } Chris@4: Chris@4: // ----------------------------------------------------------------------------------- Chris@4: Chris@4: PaTime Stream::time() const Chris@4: { Chris@4: return Pa_GetStreamTime(stream_); Chris@4: } Chris@4: Chris@4: // ----------------------------------------------------------------------------------- Chris@4: Chris@4: ////// Chris@4: /// Accessor (const) for PortAudio PaStream pointer, useful for interfacing with Chris@4: /// PortAudio add-ons such as PortMixer for instance. Normally accessing this Chris@4: /// pointer should not be needed as PortAudioCpp aims to provide all of PortAudio's Chris@4: /// functionality. Chris@4: ////// Chris@4: const PaStream *Stream::paStream() const Chris@4: { Chris@4: return stream_; Chris@4: } Chris@4: Chris@4: ////// Chris@4: /// Accessor (non-const) for PortAudio PaStream pointer, useful for interfacing with Chris@4: /// PortAudio add-ons such as PortMixer for instance. Normally accessing this Chris@4: /// pointer should not be needed as PortAudioCpp aims to provide all of PortAudio's Chris@4: /// functionality. Chris@4: ////// Chris@4: PaStream *Stream::paStream() Chris@4: { Chris@4: return stream_; Chris@4: } Chris@4: Chris@4: // ----------------------------------------------------------------------------------- Chris@4: Chris@4: } // namespace portaudio