Skip to content

proposal: pa_linux_alsa.c: do we want to pass dir (direction) param to alsa_snd_pcm_hw_params_set_period_size_near? #1145

Description

@RossBencina

As part of our review of #524 we notice that GropeDevice is passing NULL for the alsa_snd_pcm_hw_params_set_period_size_near direction parameter (lower/higher/match). We're not sure about that. Was changed from uninitialized var to NULL in #268

Possibly we should change it to pass the address of an int set to zero (see patch below) -- rationale being to match what we do when opening the stream. However it is unclear whether this would give the same behavior. The ALSA docs don't mention passing NULL: https://www.alsa-project.org/alsa-doc/alsa-lib/pcm_2pcm_8c.html#a9162045265f283c532634506456cab09 however the test program linked by the ALSA docs does pass NULL (written as 0): https://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2latency_8c-example.html#a14

Note that alsa-lib seems to treat passing NULL the same as passing &int where int dir=0;

F:\PortAudio\portaudio>git diff
diff --git a/src/hostapi/alsa/pa_linux_alsa.c b/src/hostapi/alsa/pa_linux_alsa.c
index d642ba0..e23a423 100644
--- a/src/hostapi/alsa/pa_linux_alsa.c
+++ b/src/hostapi/alsa/pa_linux_alsa.c
@@ -850,6 +850,7 @@ static PaError GropeDevice( snd_pcm_t* pcm, int isPlug, StreamDirection mode, in
     PaError result = paNoError;
     snd_pcm_hw_params_t *hwParams;
     snd_pcm_uframes_t alsaBufferFrames, alsaPeriodFrames;
+    int nearDir = 0;
     unsigned int minChans = 0;
     unsigned int maxChans = 0;
     int* minChannels, * maxChannels;
@@ -960,7 +961,8 @@ static PaError GropeDevice( snd_pcm_t* pcm, int isPlug, StreamDirection mode, in
     alsaBufferFrames = 512;
     alsaPeriodFrames = 128;
     ENSURE_( alsa_snd_pcm_hw_params_set_buffer_size_near( pcm, hwParams, &alsaBufferFrames ), paUnanticipatedHostError );
-    ENSURE_( alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, NULL ), paUnanticipatedHostError );    
+    nearDir = 0;
+    ENSURE_( alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &nearDir ), paUnanticipatedHostError );
     *defaultLowLatency = (double) (alsaBufferFrames - alsaPeriodFrames) / defaultSr;

     /* Base the high latency case on values four times larger */
@@ -971,7 +973,8 @@ static PaError GropeDevice( snd_pcm_t* pcm, int isPlug, StreamDirection mode, in
     ENSURE_( SetApproximateSampleRate( pcm, hwParams, &approximateSampleRate ), paUnanticipatedHostError );
     defaultSr = (double) approximateSampleRate;
     ENSURE_( alsa_snd_pcm_hw_params_set_buffer_size_near( pcm, hwParams, &alsaBufferFrames ), paUnanticipatedHostError );
-    ENSURE_( alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, NULL ), paUnanticipatedHostError );    
+    nearDir = 0;
+    ENSURE_( alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &nearDir ), paUnanticipatedHostError );
     *defaultHighLatency = (double) (alsaBufferFrames - alsaPeriodFrames) / defaultSr;

     *minChannels = (int)minChans;

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Priority: Highsrc-alsaALSA Host API /src/hostapi/alsa

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions