Stream – Giavapps ProAudio

pa_stream_open_default() Opens a stream with default values. This is the simplest and quickest way to open a stream but usually, you would like to use pa_stream_open() for having more control over the stream. The function returns an ErrorCode if fails.

InputDeviceIndex: default input device.
InputChannelCount: 1 input channel (mono).
InputLatency: default input latency.
OutputDeviceIndex: default output device.
OutputChannelCount: 2 output channels (stereo).
OutputLatency: default output latency.
SampleRate: 44100 samples per second.
FramesPerBuffer: 441 frames per buffer.
StreamFlags: default stream flags.

 

pa_stream_open(InputDeviceIndex, InputChannelCount, InputLatency, OutputDeviceIndex, OutputChannelCount, OutputLatency, SampleRate, FramesPerBuffer, StreamFlags) Opens a stream for either input, output or both. The function returns an ErrorCode if fails.

InputDeviceIndex: input device index (or -1 for an output-only stream).
InputChannelCount: the number of channels of sound to be delivered to the stream callback. It can range from 1 to the value of PA_DEVICE_INPUT_CHANNELS returned by the pa_device_info() function.
InputLatency: the desired latency in seconds. Actual latency values for an open stream may be retrieved using pa_stream_info() function.
OutputDeviceIndex: output device index (or -1 for an input-only stream).
OutputChannelCount: the number of channels of sound to be delivered to the stream callback. It can range from 1 to the value of PA_DEVICE_OUTPUT_CHANNELS returned by the pa_device_info() function.
OutputLatency: the desired latency in seconds. Actual latency values for an open stream may be retrieved using pa_stream_info() function.
SampleRate: sample rate.
FramesPerBuffer: frames per buffer i.e. the number of sample frames that Giavapps ProAudio will request from the callback.
StreamFlags: stream flags.

StreamFlags argument can be one or more of the following values:

PA_STREAM_NO_FLAG no flag used.

PA_STREAM_CLIP_OFF Disable default clipping of out of range samples.

PA_STREAM_DITHER_OFF Disable default dithering.

 

pa_stream_close() Closes an audio stream. If the audio stream is active it discards any pending buffers as if pa_stream_abort() had been called.

 

pa_stream_supported(InputDeviceIndex, InputChannelCount, InputLatency, OutputDeviceIndex, OutputChannelCount, OutputLatency, SampleRate) Determine whether it would be possible to open a stream with the specified parameters. Returns false if the format is supported, and an ErrorCode indicating why the format is not supported otherwise.

InputDeviceIndex: input device index (or -1 for an output-only stream).
InputChannelCount: the number of channels of sound to be delivered to the stream callback. It can range from 1 to the value of PA_DEVICE_INPUT_CHANNELS returned by the pa_device_info() function.
InputLatency: the desired latency in seconds. Actual latency values for an open stream may be retrieved using pa_stream_info() function.
OutputDeviceIndex: output device index (or -1 for an input-only stream).
OutputChannelCount: the number of channels of sound to be delivered to the stream callback. It can range from 1 to the value of PA_DEVICE_OUTPUT_CHANNELS returned by the pa_device_info() function.
OutputLatency: the desired latency in seconds. Actual latency values for an open stream may be retrieved using pa_stream_info() function.
SampleRate: the sample rate of the stream in Hertz (Samples Per Second).

 

pa_stream_info(InfoFlag) Retrieves information about the opened stream.

InfoFlag: info flag.

InfoFlag argument can be one of the following values:

PA_STREAM_INFO_INPUT_LATENCY the input latency of the stream in seconds. It may differ significantly from the value passed to pa_stream_open(). The value of this field will be 0 for output-only streams.

PA_STREAM_INFO_OUTPUT_LATENCY the output latency of the stream in seconds. It may differ significantly from the value passed to pa_stream_open(). The value of this field will be 0 for input-only streams.

PA_STREAM_INFO_SAMPLE_RATE the sample rate of the stream in Hertz (Samples Per Second).

 

pa_stream_start() starts audio processing.

 

pa_stream_stop() Terminates audio processing. It waits until all pending audio buffers have been played before it returns.

 

pa_stream_abort() Terminates audio processing immediately without waiting for pending buffers to complete.

 

pa_stream_record(Enable) Enables or disables recording for Input Stream Buffer. Recording starts only when the Stream is active. PLEASE NOTE: you must process and free data stored inside the Input Stream Buffer with pa_stream_sample_get() to retrieve data about samples and pa_stream_sample_delete() or pa_stream_sample_clear() to free memory otherwise you may cause a memory leak.

Enable: enables (true) or disables (false) recording.

 

pa_stream_recording() Returns true if recording feature is enabled or false otherwise.

 

pa_stream_loop(Enable, StartingPosition, EndingPosition) Enables or disables looping for Output Stream Buffer. When playback position reaches the EndingPosition inside the Output Stream Buffer, if you enable this feature the playback will start again from the StartingPosition. You can retrieve the current playback position with pa_stream_position() function.

Enable: enables (true) or disables (false) looping.
StartingPosition: loop starting position.
EndingPosition: loop ending position.

 

pa_stream_loop_start() Returns the loop starting position.

 

pa_stream_loop_end() Returns the loop ending position.

 

pa_stream_looping() Returns true if looping feature is enabled or false otherwise.

 

pa_stream_position() Returns the current playback position for Output Stream Buffer.

 

pa_stream_seek(Position) Sets the current playback position for Output Stream Buffer.

Position: playback position.

 

pa_stream_active() Returns 1 when the stream is active (ie playing or recording audio), 0 when not playing or an ErrorCode.

 

pa_stream_stopped() Returns 1 when the stream is stopped, 0 when the stream is running or an ErrorCode.

 

pa_stream_time() Determine the current time for the stream according to the same clock used to generate buffer timestamps. This time may be used for syncronising other events to the audio stream, for example synchronizing audio to MIDI.

 

pa_stream_cpu_load() Retrieve CPU usage information for the stream. The CPU Load is a fraction of total CPU time consumed by a callback stream’s audio processing routines including, but not limited to the client supplied stream callback.

 

pa_stream_sample_add(InputOrOutputBuffer, ChannelIndex, SampleIndex, Amplitude) Adds a sample to the specified Stream Buffer.

InputOrOutputBuffer: indicates the input (false) or output (true) buffer.
ChannelIndex:
channel index (from 0 to pa_stream_channel_count()-1).
SampleIndex:
sample index (or -1 to append data to the end of the buffer).
Amplitude:
amplitude value (from -1 to 1).

 

pa_stream_sample_delete(InputOrOutputBuffer, ChannelIndex, SampleIndex) Deletes a sample from the specified Stream Buffer.

InputOrOutputBuffer: indicates the input (false) or output (true) buffer.
ChannelIndex: channel index (from 0 to pa_stream_channel_count()-1).
SampleIndex: sample index (from 0 to pa_stream_sample_count()-1).

 

pa_stream_sample_set(InputOrOutputBuffer, ChannelIndex, SampleIndex, Amplitude) Sets the Amplitude of a sample in the specified Stream Buffer.

InputOrOutputBuffer: indicates the input (false) or output (true) buffer.
ChannelIndex:
channel index (from 0 to pa_stream_channel_count()-1).
SampleIndex:
sample index (from 0 to pa_stream_sample_count()-1).
Amplitude:
amplitude value (from -1 to 1).

 

pa_stream_sample_get(InputOrOutputBuffer, ChannelIndex, SampleIndex) Returns the Amplitude of a sample in the specified Stream Buffer.

InputOrOutputBuffer: indicates the input (false) or output (true) buffer.
ChannelIndex: channel index (from 0 to pa_stream_channel_count()-1).
SampleIndex: sample index (from 0 to pa_stream_sample_count()-1).

 

pa_stream_sample_count(InputOrOutputBuffer, ChannelIndex) Returns the total number of samples stored in the specified Stream Buffer.

InputOrOutputBuffer: indicates the input (false) or output (true) buffer.
ChannelIndex: channel index (from 0 to pa_stream_channel_count()-1).

 

pa_stream_sample_clear(InputOrOutputBuffer, ChannelIndex) Clears all the samples stored in the specified Stream Buffer.

InputOrOutputBuffer: indicates the input (false) or output (true) buffer.
ChannelIndex: channel index (from 0 to pa_stream_channel_count()-1).

 

pa_stream_channel_copy(InputOrOutputBufferFirst, ChannelIndexFirst, InputOrOutputBufferSecond, ChannelIndexSecond) Copies data from the second I/O Stream Buffer Channel and stores its Amplitudes values inside the first I/O Stream Buffer Channel. The function assigns new contents to the container, replacing its current contents, and modifying its size accordingly. Returns true if successful or false otherwise.

InputOrOutputBufferFirst: indicates the input (false) or output (true) buffer.
ChannelIndexFirst: channel index (from 0 to pa_stream_channel_count()-1).
InputOrOutputBufferSecond: indicates the input (false) or output (true) buffer.
ChannelIndexSecond: channel index (from 0 to pa_stream_channel_count()-1).

 

pa_stream_channel_swap(InputOrOutputBufferFirst, ChannelIndexFirst, InputOrOutputBufferSecond, ChannelIndexSecond) Swaps the first I/O Stream Buffer Channel with the second I/O Stream Buffer Channel. Returns true if successful or false otherwise.

InputOrOutputBufferFirst: indicates the input (false) or output (true) buffer.
ChannelIndexFirst: channel index (from 0 to pa_stream_channel_count()-1).
InputOrOutputBufferSecond: indicates the input (false) or output (true) buffer.
ChannelIndexSecond: channel index (from 0 to pa_stream_channel_count()-1).

 

pa_stream_channel_clear(InputOrOutputBuffer) Clears all the samples stored in the specified Stream Buffer.

InputOrOutputBuffer: indicates the input (false) or output (true) buffer.

 

pa_stream_channel_count(InputOrOutputBuffer) Returns the total number of channels (buffers) stored in the specified Stream Buffer.

InputOrOutputBuffer: indicates the input (false) or output (true) buffer.

 

pa_stream_channel_address(InputOrOutputBuffer, ChannelIndex) Returns a pointer to the raw Stream Buffer Channel address. Sample data pointed by the pointer is an array of float values. This function is very useful when you need to share data with different extensions. This feature is for advanced users only.

InputOrOutputBuffer: indicates the input (false) or output (true) buffer.
ChannelIndex: channel index (from 0 to pa_stream_channel_count()-1).

 

pa_stream_buffer_clear() Clears all the samples stored in the Stream Buffer.

 

Code Examples

 

Opening a Stream for an I/O Device

 

//OPENS THE DEFAULT I/O DEVICES WITH CUSTOM SETTINGS
pa_stream_open(
pa_device_input_default(), 2,
pa_device_info(pa_device_input_default(), PA_DEVICE_INFO_INPUT_LOW_LATENCY),
pa_device_output_default(), 2,
pa_device_info(pa_device_output_default(), PA_DEVICE_INFO_OUTPUT_LOW_LATENCY),
44100, 441, PA_STREAM_NO_FLAG);

 

Generating and Playing Sounds in Run-Time

 

//GENERATES A SQUARE WAVE FORM 441 HERTZ (A)
//100 Samples * 441 Hz = 44100 Hz (Sample Rate a.k.a. Samples Per Second)
for(hz=0; hz<441; hz++)
{

//Adds 50 samples with the specified Amplitude in the Output Stream Buffer
repeat(50)
{
pa_stream_sample_add(1,0,-1,0.3);//LEFT PHASE (CHANNEL 1)
pa_stream_sample_add(1,1,-1,0.3);//RIGHT PHASE (CHANNEL 2)
}

//Adds 50 samples with the specified Amplitude in the Output Stream Buffer
repeat(50)
{
pa_stream_sample_add(1,0,-1,-0.3);//LEFT PHASE (CHANNEL 1)
pa_stream_sample_add(1,1,-1,-0.3);//RIGHT PHASE (CHANNEL 2)
}

}
pa_stream_start();//Plays the Output Stream Buffer…

 

Recording Samples from an Input Device

 

Key Press Event ‘R’

 

pa_stream_record(!pa_stream_recording());//Enables or disables Recording Feature for Input Devices
if(pa_stream_recording() && !pa_stream_active()){ pa_stream_start(); }//Starts Recording

 

Closing a Stream

 

pa_stream_close();

 

I am Luigi Piscopo, also known as DJ GiDeejay / Producer / Remixer in the music world. I am a Producer with a lot of experience in photo editing, video editing, audio editing, graphic design, web design, programming and promotion.

Leave a Reply

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

*