I am trying to record audio with following lines of 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", &ss, NULL, NULL, &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(&(buffer[i].write));
// Record data and save it to the buffer
if (pa_simple_read(s, buffer[i].buf, sizeof(buffer[i].buf), &error) < 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(&(buffer[i].read)); // open up for reading
}
As you can see, I am storing the bytes read in a struct called buffer which looks like this:
#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;
Another thread tries to read and play the bytes stored in the buffer:
// 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", &ss, NULL, NULL, &error))) {
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
return false;
}
}
if (pa_simple_write(stream, buf, (size_t) size, &error) < 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, &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
pa_simple_free(stream);
return false;
}
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.
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?
I am really stuck here. Hopefully you guys can help me with that.
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.