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