| 1 |
#include "portaudiocpp/StreamParameters.hxx" |
| 2 |
|
| 3 |
#include <cstddef> |
| 4 |
|
| 5 |
#include "portaudiocpp/Device.hxx" |
| 6 |
|
| 7 |
namespace portaudio |
| 8 |
{ |
| 9 |
// ----------------------------------------------------------------------------------- |
| 10 |
|
| 11 |
////// |
| 12 |
/// Default constructor; does nothing. |
| 13 |
////// |
| 14 |
StreamParameters::StreamParameters() |
| 15 |
{ |
| 16 |
} |
| 17 |
|
| 18 |
////// |
| 19 |
/// Sets up the all parameters needed to open either a half-duplex or full-duplex Stream. |
| 20 |
/// |
| 21 |
/// @param inputParameters The parameters for the input direction of the to-be opened |
| 22 |
/// Stream or DirectionSpecificStreamParameters::null() for an output-only Stream. |
| 23 |
/// @param outputParameters The parameters for the output direction of the to-be opened |
| 24 |
/// Stream or DirectionSpecificStreamParameters::null() for an input-only Stream. |
| 25 |
/// @param sampleRate The to-be opened Stream's sample rate in Hz. |
| 26 |
/// @param framesPerBuffer The number of frames per buffer for a CallbackStream, or |
| 27 |
/// the preferred buffer granularity for a BlockingStream. |
| 28 |
/// @param flags The flags for the to-be opened Stream; default paNoFlag. |
| 29 |
////// |
| 30 |
StreamParameters::StreamParameters(const DirectionSpecificStreamParameters &inputParameters, |
| 31 |
const DirectionSpecificStreamParameters &outputParameters, double sampleRate, unsigned long framesPerBuffer, |
| 32 |
PaStreamFlags flags) : inputParameters_(inputParameters), outputParameters_(outputParameters), |
| 33 |
sampleRate_(sampleRate), framesPerBuffer_(framesPerBuffer), flags_(flags) |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
// ----------------------------------------------------------------------------------- |
| 38 |
|
| 39 |
////// |
| 40 |
/// Sets the requested sample rate. If this sample rate isn't supported by the hardware, the |
| 41 |
/// Stream will fail to open. The real-life sample rate used might differ slightly due to |
| 42 |
/// imperfections in the sound card hardware; use Stream::sampleRate() to retrieve the |
| 43 |
/// best known estimate for this value. |
| 44 |
////// |
| 45 |
void StreamParameters::setSampleRate(double sampleRate) |
| 46 |
{ |
| 47 |
sampleRate_ = sampleRate; |
| 48 |
} |
| 49 |
|
| 50 |
////// |
| 51 |
/// Either the number of frames per buffer for a CallbackStream, or |
| 52 |
/// the preferred buffer granularity for a BlockingStream. See PortAudio |
| 53 |
/// documentation. |
| 54 |
////// |
| 55 |
void StreamParameters::setFramesPerBuffer(unsigned long framesPerBuffer) |
| 56 |
{ |
| 57 |
framesPerBuffer_ = framesPerBuffer; |
| 58 |
} |
| 59 |
|
| 60 |
////// |
| 61 |
/// Sets the specified flag or does nothing when the flag is already set. Doesn't |
| 62 |
/// `unset' any previously existing flags (use clearFlags() for that). |
| 63 |
////// |
| 64 |
void StreamParameters::setFlag(PaStreamFlags flag) |
| 65 |
{ |
| 66 |
flags_ |= flag; |
| 67 |
} |
| 68 |
|
| 69 |
////// |
| 70 |
/// Unsets the specified flag or does nothing if the flag isn't set. Doesn't affect |
| 71 |
/// any other flags. |
| 72 |
////// |
| 73 |
void StreamParameters::unsetFlag(PaStreamFlags flag) |
| 74 |
{ |
| 75 |
flags_ &= ~flag; |
| 76 |
} |
| 77 |
|
| 78 |
////// |
| 79 |
/// Clears or `unsets' all set flags. |
| 80 |
////// |
| 81 |
void StreamParameters::clearFlags() |
| 82 |
{ |
| 83 |
flags_ = paNoFlag; |
| 84 |
} |
| 85 |
|
| 86 |
// ----------------------------------------------------------------------------------- |
| 87 |
|
| 88 |
void StreamParameters::setInputParameters(const DirectionSpecificStreamParameters ¶meters) |
| 89 |
{ |
| 90 |
inputParameters_ = parameters; |
| 91 |
} |
| 92 |
|
| 93 |
void StreamParameters::setOutputParameters(const DirectionSpecificStreamParameters ¶meters) |
| 94 |
{ |
| 95 |
outputParameters_ = parameters; |
| 96 |
} |
| 97 |
|
| 98 |
// ----------------------------------------------------------------------------------- |
| 99 |
|
| 100 |
bool StreamParameters::isSupported() const |
| 101 |
{ |
| 102 |
return (Pa_IsFormatSupported(inputParameters_.paStreamParameters(), |
| 103 |
outputParameters_.paStreamParameters(), sampleRate_) == paFormatIsSupported); |
| 104 |
} |
| 105 |
|
| 106 |
// ----------------------------------------------------------------------------------- |
| 107 |
|
| 108 |
double StreamParameters::sampleRate() const |
| 109 |
{ |
| 110 |
return sampleRate_; |
| 111 |
} |
| 112 |
|
| 113 |
unsigned long StreamParameters::framesPerBuffer() const |
| 114 |
{ |
| 115 |
return framesPerBuffer_; |
| 116 |
} |
| 117 |
|
| 118 |
////// |
| 119 |
/// Returns all currently set flags as a binary combined |
| 120 |
/// integer value (PaStreamFlags). Use isFlagSet() to |
| 121 |
/// avoid dealing with the bitmasks. |
| 122 |
////// |
| 123 |
PaStreamFlags StreamParameters::flags() const |
| 124 |
{ |
| 125 |
return flags_; |
| 126 |
} |
| 127 |
|
| 128 |
////// |
| 129 |
/// Returns true if the specified flag is currently set |
| 130 |
/// or false if it isn't. |
| 131 |
////// |
| 132 |
bool StreamParameters::isFlagSet(PaStreamFlags flag) const |
| 133 |
{ |
| 134 |
return ((flags_ & flag) != 0); |
| 135 |
} |
| 136 |
|
| 137 |
// ----------------------------------------------------------------------------------- |
| 138 |
|
| 139 |
DirectionSpecificStreamParameters &StreamParameters::inputParameters() |
| 140 |
{ |
| 141 |
return inputParameters_; |
| 142 |
} |
| 143 |
|
| 144 |
const DirectionSpecificStreamParameters &StreamParameters::inputParameters() const |
| 145 |
{ |
| 146 |
return inputParameters_; |
| 147 |
} |
| 148 |
|
| 149 |
DirectionSpecificStreamParameters &StreamParameters::outputParameters() |
| 150 |
{ |
| 151 |
return outputParameters_; |
| 152 |
} |
| 153 |
|
| 154 |
const DirectionSpecificStreamParameters &StreamParameters::outputParameters() const |
| 155 |
{ |
| 156 |
return outputParameters_; |
| 157 |
} |
| 158 |
|
| 159 |
// ----------------------------------------------------------------------------------- |
| 160 |
} // namespace portaudio |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|