title: cpp raw audio to analog values
body: <p>A microphone or a sound generate different current. I would like to extract this information from a raw audio file.</p>
  
  <p>I get the raw data from the microphone with the following command:</p>
  
  <pre><code>arecord -t raw -f cd
  </code></pre>
  
  <p>Now I would like to analyse the raw data in order to extract the signal from 0V to 5V. But I have no idea about how to proceed. </p>
  
  <p>I tried a bit but it is absolutely not successful, I think I am very far from the solution.</p>
  
  <pre><code>#define BUFSIZE 8
  
  uint8_t analogRead() {
      uint8_t buf[BUFSIZE];
      //cout &lt;&lt; "analogRead" &lt;&lt; endl;
      read(STDIN_FILENO, buf, sizeof(buf));
  
  
      size_t size = sizeof(buf);
      double accum = 0;
      int const n_samples = size/2;
      for (int i = 0; i &lt; size; i += 2)
      {
          // put two bytes into one __signed__ integer
          uint8_t val = buf[i] + ((uint8_t)buf[i+1] &lt;&lt; 8);
  
          accum += val*val;
      }
      accum /= n_samples;
  
      cout &lt;&lt; accum &lt;&lt; endl;
  
      return accum;    
  } 
  
  int main(int argc, char** argv) {
      while(1) {
          cout &lt;&lt; analogRead() &lt;&lt; endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Then I run my test like this:</p>
  
  <pre><code>arecord -t raw -f cd | ./mytest
  </code></pre>