Alsa snd_pcm_writei

I noticed that the sine generator in pcm.c and speaker-test.c generates a new sinus buffer in the loop. Therefore, it constantly recreates the same buffer. I wanted to play a buffer without creating it every time to save some CPU time. However, when I tried to run the code by first creating a buffer and then sending the same buffer to snd_pcm_writei, I get a little ticklish sound at the end of each buffer. However, when it is rebuilt each time and then sent to snd_pcm_writei, there is no small click at the end of the buffer. Why is it necessary to rebuild the sine buffer each time before playing it so as not to get a click noise?

Any help would be appreciated?

from pcm.c:

while (1) {
    generate_sine(areas, 0, period_size, &phase);
    ptr = samples;
    cptr = period_size;
+3
source share
1 answer

, , phase, , , .

"" .

. , 16, A H.

// Old way
  phase = 0        phase = 2        phase = 4
ABCDEFGHGFEDCBAB|CDEFGHGFEDCBABCD|EFGHGFEDCBABCDEF....

// New way
  phase = 0        phase = 0        phase = 0
ABCDEFGHGFEDCBAB|ABCDEFGHGFEDCBAB|ABCDEFGHGFEDCBAB....

, , "" (, AB|AB AB|CD). "" .

, phase , , , .

EDIT: generate_sine, , phase:

static void generate_sine(const snd_pcm_channel_area_t *areas, 
                          snd_pcm_uframes_t offset,
                          int count, double *_phase)
{
    static double max_phase = 2. * M_PI;
    double phase = *_phase;
    double step = max_phase*freq/(double)rate;

    [...]

             phase += step;
             if (phase >= max_phase)
                    phase -= max_phase;
     }
     *_phase = phase;
}

EDIT2: /:

enter image description here

+4

All Articles