2 #define _ALSA_INPUT_H 1
4 // ALSA sound input, running in a separate thread and sending audio back
7 // Note: “frame” here generally refers to the ALSA definition of frame,
8 // which is a set of samples, exactly one for each channel. The only exception
9 // is in frame_length, where it means the TIMEBASE length of the buffer
10 // as a whole, since that's what AudioMixer::add_audio() wants.
12 #include <alsa/asoundlib.h>
20 #include "bmusb/bmusb.h"
25 typedef std::function<void(const uint8_t *data, unsigned num_samples, bmusb::AudioFormat audio_format, int64_t frame_length)> audio_callback_t;
27 ALSAInput(const char *device, unsigned sample_rate, unsigned num_channels, audio_callback_t audio_callback);
30 // NOTE: Might very well be different from the sample rate given to the
31 // constructor, since the card might not support the one you wanted.
32 unsigned get_sample_rate() const { return sample_rate; }
34 void start_capture_thread();
35 void stop_capture_thread();
38 void capture_thread_func();
39 int64_t frames_to_pts(uint64_t n) const;
40 void die_on_error(const char *func_name, int err);
43 unsigned sample_rate, num_channels, num_periods;
44 snd_pcm_uframes_t period_size;
45 snd_pcm_uframes_t buffer_frames;
46 bmusb::AudioFormat audio_format;
47 audio_callback_t audio_callback;
49 snd_pcm_t *pcm_handle;
50 std::thread capture_thread;
51 std::atomic<bool> should_quit{false};
52 std::unique_ptr<uint8_t[]> buffer;
55 #endif // !defined(_ALSA_INPUT_H)