Continuous WASAPI Ring-Buffer Sampling











up vote
-2
down vote

favorite












How to use WASAPI (or something like it) to continuously sample audio into a (thread-safe) ring-buffer, so that a consumer thread can read from that buffer in an a set interval?



Currently we have a .sample() method that returns a chunk of samples after a set sampling interval, but this has quite the overhead due to memory allocation etc.. maybe this method could be optimized; I'm pretty sure we're doing it wrong.



std::vector<short> sampler2::sample()
{
// prepare header
waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

// insert a wave input buffer
waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

// commence sampling input
waveInStart(hWaveIn);

// sleep for the duration of a sample interval
std::this_thread::sleep_for(milliseconds(SAMPLE_INTERVAL));

// create vector
std::vector<short> samplesChunk(&waveIn[0], &waveIn[0] + NUMPTS);

// return vector
return samplesChunk;
}


GitHub Links: sampler2.h & sampler2.cpp



The code is very shitty and we have no clue how to properly use WASAPI. Our goal was to (quickly) create a sampler class that can leverage a sampling interval of >10 ms.










share|improve this question


























    up vote
    -2
    down vote

    favorite












    How to use WASAPI (or something like it) to continuously sample audio into a (thread-safe) ring-buffer, so that a consumer thread can read from that buffer in an a set interval?



    Currently we have a .sample() method that returns a chunk of samples after a set sampling interval, but this has quite the overhead due to memory allocation etc.. maybe this method could be optimized; I'm pretty sure we're doing it wrong.



    std::vector<short> sampler2::sample()
    {
    // prepare header
    waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

    // insert a wave input buffer
    waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

    // commence sampling input
    waveInStart(hWaveIn);

    // sleep for the duration of a sample interval
    std::this_thread::sleep_for(milliseconds(SAMPLE_INTERVAL));

    // create vector
    std::vector<short> samplesChunk(&waveIn[0], &waveIn[0] + NUMPTS);

    // return vector
    return samplesChunk;
    }


    GitHub Links: sampler2.h & sampler2.cpp



    The code is very shitty and we have no clue how to properly use WASAPI. Our goal was to (quickly) create a sampler class that can leverage a sampling interval of >10 ms.










    share|improve this question
























      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite











      How to use WASAPI (or something like it) to continuously sample audio into a (thread-safe) ring-buffer, so that a consumer thread can read from that buffer in an a set interval?



      Currently we have a .sample() method that returns a chunk of samples after a set sampling interval, but this has quite the overhead due to memory allocation etc.. maybe this method could be optimized; I'm pretty sure we're doing it wrong.



      std::vector<short> sampler2::sample()
      {
      // prepare header
      waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

      // insert a wave input buffer
      waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

      // commence sampling input
      waveInStart(hWaveIn);

      // sleep for the duration of a sample interval
      std::this_thread::sleep_for(milliseconds(SAMPLE_INTERVAL));

      // create vector
      std::vector<short> samplesChunk(&waveIn[0], &waveIn[0] + NUMPTS);

      // return vector
      return samplesChunk;
      }


      GitHub Links: sampler2.h & sampler2.cpp



      The code is very shitty and we have no clue how to properly use WASAPI. Our goal was to (quickly) create a sampler class that can leverage a sampling interval of >10 ms.










      share|improve this question













      How to use WASAPI (or something like it) to continuously sample audio into a (thread-safe) ring-buffer, so that a consumer thread can read from that buffer in an a set interval?



      Currently we have a .sample() method that returns a chunk of samples after a set sampling interval, but this has quite the overhead due to memory allocation etc.. maybe this method could be optimized; I'm pretty sure we're doing it wrong.



      std::vector<short> sampler2::sample()
      {
      // prepare header
      waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

      // insert a wave input buffer
      waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

      // commence sampling input
      waveInStart(hWaveIn);

      // sleep for the duration of a sample interval
      std::this_thread::sleep_for(milliseconds(SAMPLE_INTERVAL));

      // create vector
      std::vector<short> samplesChunk(&waveIn[0], &waveIn[0] + NUMPTS);

      // return vector
      return samplesChunk;
      }


      GitHub Links: sampler2.h & sampler2.cpp



      The code is very shitty and we have no clue how to properly use WASAPI. Our goal was to (quickly) create a sampler class that can leverage a sampling interval of >10 ms.







      c++ winapi audio sampling wasapi






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      Androvich

      123115




      123115
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Your sample uses waveout API. You can check MSDN for WASAPI reference and usage.
          Here is the basic description of WASAPI usage:
          The client calls the methods in the IAudioRenderClient interface to write rendering data to an endpoint buffer.To request an endpoint buffer of a particular size, the client calls the IAudioClient::Initialize method. To get the size of the allocated buffer, which might be different from the requested size, the client calls the IAudioClient::GetBufferSize method.
          To move a stream of rendering data through the endpoint buffer, the client alternately calls the IAudioRenderClient::GetBuffer method and the IAudioRenderClient::ReleaseBuffer method. The client accesses the data in the endpoint buffer as a series of data packets. The GetBuffer call retrieves the next packet so that the client can fill it with rendering data. After writing the data to the packet, the client calls ReleaseBuffer to add the completed packet to the rendering queue.
          There is also this Microsoft C++ WASAPI example.






          share|improve this answer





















          • What's the difference between the two? And thanks, I'll take a look!
            – Androvich
            yesterday






          • 1




            WaveOut is a legacy API set present in Windows. While WASAPI is a core audio API and is available since Vista. It needs a specific driver implementation.
            – VuVirt
            yesterday











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364127%2fcontinuous-wasapi-ring-buffer-sampling%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Your sample uses waveout API. You can check MSDN for WASAPI reference and usage.
          Here is the basic description of WASAPI usage:
          The client calls the methods in the IAudioRenderClient interface to write rendering data to an endpoint buffer.To request an endpoint buffer of a particular size, the client calls the IAudioClient::Initialize method. To get the size of the allocated buffer, which might be different from the requested size, the client calls the IAudioClient::GetBufferSize method.
          To move a stream of rendering data through the endpoint buffer, the client alternately calls the IAudioRenderClient::GetBuffer method and the IAudioRenderClient::ReleaseBuffer method. The client accesses the data in the endpoint buffer as a series of data packets. The GetBuffer call retrieves the next packet so that the client can fill it with rendering data. After writing the data to the packet, the client calls ReleaseBuffer to add the completed packet to the rendering queue.
          There is also this Microsoft C++ WASAPI example.






          share|improve this answer





















          • What's the difference between the two? And thanks, I'll take a look!
            – Androvich
            yesterday






          • 1




            WaveOut is a legacy API set present in Windows. While WASAPI is a core audio API and is available since Vista. It needs a specific driver implementation.
            – VuVirt
            yesterday















          up vote
          0
          down vote













          Your sample uses waveout API. You can check MSDN for WASAPI reference and usage.
          Here is the basic description of WASAPI usage:
          The client calls the methods in the IAudioRenderClient interface to write rendering data to an endpoint buffer.To request an endpoint buffer of a particular size, the client calls the IAudioClient::Initialize method. To get the size of the allocated buffer, which might be different from the requested size, the client calls the IAudioClient::GetBufferSize method.
          To move a stream of rendering data through the endpoint buffer, the client alternately calls the IAudioRenderClient::GetBuffer method and the IAudioRenderClient::ReleaseBuffer method. The client accesses the data in the endpoint buffer as a series of data packets. The GetBuffer call retrieves the next packet so that the client can fill it with rendering data. After writing the data to the packet, the client calls ReleaseBuffer to add the completed packet to the rendering queue.
          There is also this Microsoft C++ WASAPI example.






          share|improve this answer





















          • What's the difference between the two? And thanks, I'll take a look!
            – Androvich
            yesterday






          • 1




            WaveOut is a legacy API set present in Windows. While WASAPI is a core audio API and is available since Vista. It needs a specific driver implementation.
            – VuVirt
            yesterday













          up vote
          0
          down vote










          up vote
          0
          down vote









          Your sample uses waveout API. You can check MSDN for WASAPI reference and usage.
          Here is the basic description of WASAPI usage:
          The client calls the methods in the IAudioRenderClient interface to write rendering data to an endpoint buffer.To request an endpoint buffer of a particular size, the client calls the IAudioClient::Initialize method. To get the size of the allocated buffer, which might be different from the requested size, the client calls the IAudioClient::GetBufferSize method.
          To move a stream of rendering data through the endpoint buffer, the client alternately calls the IAudioRenderClient::GetBuffer method and the IAudioRenderClient::ReleaseBuffer method. The client accesses the data in the endpoint buffer as a series of data packets. The GetBuffer call retrieves the next packet so that the client can fill it with rendering data. After writing the data to the packet, the client calls ReleaseBuffer to add the completed packet to the rendering queue.
          There is also this Microsoft C++ WASAPI example.






          share|improve this answer












          Your sample uses waveout API. You can check MSDN for WASAPI reference and usage.
          Here is the basic description of WASAPI usage:
          The client calls the methods in the IAudioRenderClient interface to write rendering data to an endpoint buffer.To request an endpoint buffer of a particular size, the client calls the IAudioClient::Initialize method. To get the size of the allocated buffer, which might be different from the requested size, the client calls the IAudioClient::GetBufferSize method.
          To move a stream of rendering data through the endpoint buffer, the client alternately calls the IAudioRenderClient::GetBuffer method and the IAudioRenderClient::ReleaseBuffer method. The client accesses the data in the endpoint buffer as a series of data packets. The GetBuffer call retrieves the next packet so that the client can fill it with rendering data. After writing the data to the packet, the client calls ReleaseBuffer to add the completed packet to the rendering queue.
          There is also this Microsoft C++ WASAPI example.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          VuVirt

          1,329711




          1,329711












          • What's the difference between the two? And thanks, I'll take a look!
            – Androvich
            yesterday






          • 1




            WaveOut is a legacy API set present in Windows. While WASAPI is a core audio API and is available since Vista. It needs a specific driver implementation.
            – VuVirt
            yesterday


















          • What's the difference between the two? And thanks, I'll take a look!
            – Androvich
            yesterday






          • 1




            WaveOut is a legacy API set present in Windows. While WASAPI is a core audio API and is available since Vista. It needs a specific driver implementation.
            – VuVirt
            yesterday
















          What's the difference between the two? And thanks, I'll take a look!
          – Androvich
          yesterday




          What's the difference between the two? And thanks, I'll take a look!
          – Androvich
          yesterday




          1




          1




          WaveOut is a legacy API set present in Windows. While WASAPI is a core audio API and is available since Vista. It needs a specific driver implementation.
          – VuVirt
          yesterday




          WaveOut is a legacy API set present in Windows. While WASAPI is a core audio API and is available since Vista. It needs a specific driver implementation.
          – VuVirt
          yesterday


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364127%2fcontinuous-wasapi-ring-buffer-sampling%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Cypress Hill

          what are some tips for doing well in the interview? [on hold]

          How does a super-power salesman not get shut down for legal reasons?