1

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.

Bryan
  • 11,398
  • 3
  • 53
  • 78
Schnecki
  • 11
  • 1
  • 3
  • How are you ensuring that the playback thread is reading from the buffer block that the recording thread just recorded to? – Bryan Sep 14 '12 at 20:05
  • I am of course also using the pthread_mutex_lock and unlock functions to ensure that the buffer was written before it gets read: pthread_mutex_lock(&(buffer[i].read)); // open up for reading // ... Play data via audio output with a function call like this: play_data(buffer[i].buf, size); // in this case size == BUFSIZE pthread_mutex_unlock(&(buffer[i].write)); (sorry but I can't format my answer at all) – Schnecki Sep 14 '12 at 22:01
  • ok; you also claim the read locks ahead of time? You took some of this code from the PulseAudio examples; did you get them to work as written? – Bryan Sep 14 '12 at 22:23
  • yeah the locks should be fine. If I compile the two files and pipe the recording process output to the audio playing process I can perfectly hear myself...So yes, those two files work perfectly. – Schnecki Sep 14 '12 at 22:37
  • Well, if it works with the bytes going into a file and coming out unaltered, then this answers your "Do I have to convert the bytes" question - no. – Bryan Sep 14 '12 at 22:50
  • Yeah, that's true...So I gotta check the code once more, I guess. – Schnecki Sep 14 '12 at 22:53
  • thanks Bryan I found the solution! – Schnecki Sep 15 '12 at 11:13

1 Answers1

0

Solution:

The two values of: #define BUFSIZE 44100 // Size of one element #define BUFNUMBER 16 // Number of elements

don't really matter. They just shouldn't be too small. Otherwise the program will stuck playing the audio.

It's important NOT!!! to call following code fragment everytime a buffer struct fragment gets played. The audio sound card has a buffer included and by default the buffer gets first filled and then played. That's also why there is a little latency in playing the audio.

/* 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;
}

The amount of latency can be found out by following code:

pa_usec_t latency;

if ((latency = pa_simple_get_latency(stream, &error)) == (pa_usec_t) -1) {
    fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
}

fprintf(stderr, "%0.0f usec  \r", (float)latency);

However the latency is not constant and changes all the time. By the way: The buffer size could be set when creating the playback stream if needed.

Oh and I used: PA_SAMPLE_S16LE, however you just need to use the same value for recording and playback, otherwise it sounds weird.

Hope that helps someone.

Schnecki
  • 11
  • 1
  • 3