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