gpu-screen-recorder/src/sound.cpp

90 lines
3.0 KiB
C++
Raw Normal View History

2020-03-29 07:10:24 +00:00
#include "../include/sound.hpp"
#include <stdlib.h>
#include <stdio.h>
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>
int sound_device_get_by_name(SoundDevice *device, const char *name, unsigned int num_channels, unsigned int period_frame_size) {
int rc;
snd_pcm_t *handle;
rc = snd_pcm_open(&handle, name, SND_PCM_STREAM_CAPTURE, 0);
if(rc < 0) {
fprintf(stderr, "unable to open pcm device 'default', reason: %s\n", snd_strerror(rc));
return rc;
}
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(&params);
// Fill the params with default values
snd_pcm_hw_params_any(handle, params);
// Interleaved mode
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
// Signed 16--bit little-endian format
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_channels(handle, params, num_channels);
2020-04-01 17:25:16 +00:00
// 48000 bits/second samling rate (DVD quality)
unsigned int val = 48000;
2020-03-29 07:10:24 +00:00
int dir;
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
snd_pcm_uframes_t frames = period_frame_size;
snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);
// Write the parmeters to the driver
rc = snd_pcm_hw_params(handle, params);
if(rc < 0) {
fprintf(stderr, "unable to set hw parameters, reason: %s\n", snd_strerror(rc));
snd_pcm_close(handle);
return rc;
}
// Use a buffer large enough to hold one period
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
int buffer_size = frames * 2 * num_channels; // 2 bytes/sample, @num_channels channels
2020-04-01 17:25:16 +00:00
void *buffer = malloc(buffer_size);
2020-03-29 07:10:24 +00:00
if(!buffer) {
fprintf(stderr, "failed to allocate buffer for audio\n");
snd_pcm_close(handle);
return -1;
}
device->handle = (void*)handle;
device->buffer = buffer;
device->buffer_size = buffer_size;
device->frames = frames;
return 0;
}
2020-03-29 15:48:17 +00:00
void sound_device_close(SoundDevice *device) {
2020-03-29 07:10:24 +00:00
/* TODO: Is this also needed in @sound_device_get_by_name on failure? */
2020-04-01 17:25:16 +00:00
// TODO: This has been commented out since it causes the thread to block forever. Why?
//snd_pcm_drain((snd_pcm_t*)device->handle);
2020-03-29 07:10:24 +00:00
snd_pcm_close((snd_pcm_t*)device->handle);
free(device->buffer);
}
2020-04-01 17:25:16 +00:00
int sound_device_read_next_chunk(SoundDevice *device, void **buffer) {
2020-03-29 07:10:24 +00:00
int rc = snd_pcm_readi((snd_pcm_t*)device->handle, device->buffer, device->frames);
if (rc == -EPIPE) {
/* overrun */
fprintf(stderr, "overrun occured\n");
snd_pcm_prepare((snd_pcm_t*)device->handle);
2020-04-01 17:25:16 +00:00
return rc;
2020-03-29 07:10:24 +00:00
} else if(rc < 0) {
fprintf(stderr, "failed to read from sound device, reason: %s\n", snd_strerror(rc));
return rc;
} else if (rc != (int)device->frames) {
fprintf(stderr, "short read, read %d frames\n", rc);
}
*buffer = device->buffer;
2020-04-01 17:25:16 +00:00
return rc;
}
int sound_device_get_buffer_size(SoundDevice *device) {
return device->buffer_size;
2020-03-29 07:10:24 +00:00
}