title: Record audio, store in buffer and then write bytes to sound card via PulseAudio
body: <p>I am trying to record audio with following lines of code:</p>
  
  <pre><code>// The sample type to use
  static const pa_sample_spec ss = {
  .format = PA_SAMPLE_S32LE , //PA_SAMPLE_S16BE, ??? Which one to us here ??? BE...Big Endian
  .rate = 44100, // That are samples per second
  .channels = 2
  };
  
  // Create the recording stream
  // see: http://freedesktop.org/software/pulseaudio/doxygen/parec-simple_8c-example.html
  if (!(s = pa_simple_new(NULL, "Record", PA_STREAM_RECORD, NULL, "record", &amp;ss, NULL, NULL, &amp;error))) {
      fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
      pa_simple_free(s);
      exit(EXIT_FAILURE);
  }
  
  int i = -1;
  
  while (!exit_program) {
      i = (i+1) % BUFNUMBER;
  
      pthread_mutex_lock(&amp;(buffer[i].write));
      // Record data and save it to the buffer
      if (pa_simple_read(s, buffer[i].buf, sizeof(buffer[i].buf), &amp;error) &lt; 0) {
          fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
          pa_simple_free(s);
          exit(EXIT_FAILURE);
      }
  
      // unlock the reading mutex
      pthread_mutex_unlock(&amp;(buffer[i].read)); // open up for reading
  
  }
  </code></pre>
  
  <p>As you can see, I am storing the bytes read in a struct called buffer which looks like this:</p>
  
  <pre><code>#define BUFSIZE 44100  // Size of one element
  #define BUFNUMBER 16 // Number of elements
  #define AUDIO_BUFFER_FORMAT char
  
  // one element of the ringbuffer
  typedef struct ringbuf {
      AUDIO_BUFFER_FORMAT buf[BUFSIZE]; /* The buffer array */
      pthread_mutex_t read; /* indicates if block was read */
      pthread_mutex_t write; /* for locking writing */
  } ringbuffer_element;
  </code></pre>
  
  <p>Another thread tries to read and play the bytes stored in the buffer:</p>
  
  <pre><code>// The sample type to use
  static const pa_sample_spec ss = {
  .format = PA_SAMPLE_S32LE , //PA_SAMPLE_S16BE,
  .rate = 44100,
  .channels = 2
  };
  
  if (stream == NULL) {
      if (!(stream = pa_simple_new(NULL, "Stream", PA_STREAM_PLAYBACK, NULL, "playback", &amp;ss, NULL, NULL, &amp;error))) {
          fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
          return false;
      }
  }
  
  if (pa_simple_write(stream, buf, (size_t) size, &amp;error) &lt; 0) {
      fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
      pa_simple_free(stream);
      return false;
  }
  
  
  /* Make sure that every single sample was played */
  if (pa_simple_drain(stream, &amp;error) &lt; 0) {
      fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
      pa_simple_free(stream);
      return false;
  }
  </code></pre>
  
  <p>However, I tested the implementation of the buffer, which works perfectly fine. Nonetheless, the only thing that I can hear is noise. So I am wondering, if I need to convert the bytes before I can play them again so that it sounds like the recording. </p>
  
  <p>Furthermore I couldn't find any data sheets for my sound card etc. Do I have to convert the bytes or can I just play them as recorded? Did the format I am using break something? </p>
  
  <p>I am really stuck here. Hopefully you guys can help me with that.</p>
  
  <p>edit: One more question: Is it better if I use the ALSA API to get closer to the hardware for my purpose? Yes, I am totally new to sound programming.</p>