Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6902913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:51:29+00:00 2026-05-27T07:51:29+00:00

I write an application, which plays a sound getting from Hardware (like a ring

  • 0

I write an application, which plays a sound getting from Hardware (like a ring buffer filled with a sinus wave with certain frequency). Everything works fine, and I can playback the created sound correctly except a periodical clicking (maybe at the end of buffer?) and noise.

I initialize and run the Buffer:

void Audiooutput::InitializeAudioParameters()
{
    Audio_DataWritten = 0;
    Audio_fragments = 4;
    Audio_channels = 2;
    Audio_BufferSize = 256;
    Audio_Samplerate = 8000;
    Audio_ResamplingFactor = 1;
    Audio_Framesize = 2;
    // (SND_PCM_FORMAT_S16_LE / 8);
    Audio_frames = Audio_BufferSize / Audio_Framesize * Audio_fragments;

    snd_pcm_uframes_t size;
    err = snd_pcm_hw_params_any(pcmPlaybackHandle, hw_params);
    err = snd_pcm_hw_params_set_rate_resample(pcmPlaybackHandle, hw_params, 1);
    //     qDebug()<<a1.sprintf(" % d \t snd_pcm_hw_params_set_rate: %s",Audio_Samplerate,snd_strerror(err));
    err =
        snd_pcm_hw_params_set_format(pcmPlaybackHandle, hw_params,
                     SND_PCM_FORMAT_S16_LE);
    err =
        snd_pcm_hw_params_set_channels(pcmPlaybackHandle, hw_params,
                       Audio_channels);
    err = snd_pcm_hw_params_set_rate_near(pcmPlaybackHandle, hw_params, &Audio_Samplerate, 0);
    //     qDebug()<<a1.sprintf(" % d \t snd_pcm_hw_params_set_rate: %s",Audio_Samplerate,snd_strerror(err));

    if ((err =
         snd_pcm_hw_params_set_periods_near(pcmPlaybackHandle, hw_params,
                        &Audio_fragments, 0)) < 0) {
        qDebug() << a1.sprintf("Error setting # fragments to %d: %s\n",
                       Audio_fragments, snd_strerror(err));
    } else
        qDebug() << a1.sprintf("setting # fragments to %d: %s\n",
                       Audio_fragments, snd_strerror(err));
    err = snd_pcm_hw_params_get_buffer_size(hw_params, &size);
    if ((err =
         snd_pcm_hw_params_set_buffer_size_near(pcmPlaybackHandle,
                            hw_params,
                            &Audio_frames)) < 0) {
        qDebug() << a1.
            sprintf("Error setting buffer_size %d frames: %s",
                Audio_frames, snd_strerror(err));
    } else
        qDebug() << a1.sprintf("setting Buffersize to %d --> %d: %s\n",
                       Audio_BufferSize, Audio_frames,
                       snd_strerror(err));
    Audio_BufferSize = Audio_frames;
    if ((err = snd_pcm_hw_params(pcmPlaybackHandle, hw_params)) < 0) {
        qDebug() << a1.sprintf("Error setting HW params: %s",
                       snd_strerror(err));
    }
    Q_ASSERT(err >= 0);
}

void Audiooutput::ProduceAudioOutput(int n, int mmodes, int totalMModeGates,
         short *sinusValue, short *cosinusValue)
{
    for (int audioSample = 0; audioSample < n;
         audioSample += Audio_ResamplingFactor) {
        currentposition =
            (int)(m_Audio.generalPos % (Audio_BufferSize / 2));
        if (currentposition == 0) {
            QueueAudioBuffer();
            m_Audio.currentPos = 0;
        }
        m_Audio.generalPos++;
        AudioData[currentposition * 2] =
            (short)(sinusValue[audioSample]);
        AudioData[currentposition * 2 + 1] =
            (short)(cosinusValue[audioSample]);
    }
}

void Audiooutput::QueueAudioBuffer()
{
    snd_pcm_prepare(pcmPlaybackHandle);
    Audio_DataWritten +=
        snd_pcm_writei(pcmPlaybackHandle, AudioData, Audio_BufferSize);
}

Changing the audiobuffer size or fragments changes also the clicking period.
Can anyone help me with this issue ?
I checked also the first and Last Values. Thy are always difference.

OS: Ubuntu 11

more detail.

the count of received data is dynamically, and changes depend of different parameters. But I play always a certain part e.g. 128 values or 256 or 512….

// I get the Audiodata from a hardware (in a Timerloop)

    audiobuffersize  = 256;
    short *AudioData = new short[256];
    int generalAudioSample = 0;

    void CollectDataFromHw()
{  
...  
        int n = 0;
        n = GetData(buf1,buf2);//buf1 = new short[MAX_SHRT]
        if(n > 0)
           FillAudioBuffer(n,buf1,buf2)
...
}
    -------------------------------------------
    void FillAudioBuffer(int n, short*buf1, short*buf2)
    {
      for(int audioSample = 0;audioSample < n; audioSample++){
         iCurrentAudioSample = (int)(generalAudioSample % (audiobuffersize/2)); 
         if(iCurrentAudioSample == 0)  {
            snd_pcm_writei(pcmPlaybackHandle,AudioData,audiobuffersize );
             memset(AudioData,0x00,audiobuffersize*sizeof(short));

         }
        generalAudioSample++;
        AudioData[iCurrentAudioSample * 2]   = (short)(buf1[audioSample];
        AudioData[iCurrentAudioSample * 2 +1]   = (short)(buf2[audioSample];

     }
    }

I changed the audiobuffersize also. If I set it to a bigger size, I have some Echo additional to clicks.

any Idea ?
//———————–
the Problem is

snd_pcm_prepare(pcmPlaybackHandle);

every call of this function produce a click in sound !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:51:30+00:00Added an answer on May 27, 2026 at 7:51 am

    solved
    buffer has been played several times before it was filled with the data.

    stupid error in the code.missing a parantez –> audio_buffersize/2 <-–
    and therefore the result was very often if(iCurrentAudioSample == 0) true !!!!!

    iCurrentAudioSample = (int)(generalAudioSample % (audio_buffersize/2));
    if(iCurrentAudioSample == 0)
    {
     writetoaudioStream(audiobuffer);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've write a web-application in PHP which has 30 tables+views. From time to time
I write application which will get data from received SMS. This is data only
I'm currently using QTCreator version 4.7.4 to write a GUI application which plays AVI
Basically I write an application which copies/moves files from the local file system to
I'm trying to write a Cocoa application which allows me to see what buttons
I need to write a java application which can merge docx files. Any suggestions?
I've been trying to write a small application which will work with mysql in
I'm trying to write a small wsgi application which will put some objects to
I've got a local application (which I didn't write, and can't change) that talks
I need to write a standalone Java application which will have a embedded HTTP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.