-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathAudioCaptureImpl_SDL.h
102 lines (83 loc) · 3.2 KB
/
AudioCaptureImpl_SDL.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#pragma once
#include <SDL2/SDL.h>
#include <Poco/Logger.h>
#include <string>
#include <vector>
class projectm;
/**
* @brief SDL-based audio capturing thread.
*
* Uses SDL's audio API to capture PCM data from any supported drivers.
*/
class AudioCaptureImpl
{
public:
AudioCaptureImpl();
~AudioCaptureImpl();
/**
* @brief Returns a map of available recording devices.
* @return A vector of available audio device IDs and names.
*/
std::map<int, std::string> AudioDeviceList();
/**
* @brief Starts audio capturing with the first available device.
* @param projectMHandle projectM instance handle that will receive the captured data.
* @param audioDeviceIndex The initial audio device ID to capture from. Use -1 to select the implementation's
* default device.
*/
void StartRecording(projectm* projectMHandle, int audioDeviceIndex);
/**
* @brief Stops audio recording.
*/
void StopRecording();
/**
* @brief Switches to the next available audio recording device.
*/
void NextAudioDevice();
/**
* @brief Activates the audio device with the given idnex for recording.
* @param index The index, as listed by @a AudioDeviceList()
*/
void AudioDeviceIndex(int index);
/**
* @brief Returns the currently used Audio device index.
* @return The index of the current audio device, as listed by @a AudioDeviceList()
*/
int AudioDeviceIndex() const;
/**
* @brief Retrieves the current audio device name.
* @return The name of the currently selected audio recording device.
*/
std::string AudioDeviceName() const;
/**
* @brief Asks the capture client to fill projectM's audio buffer for the next frame.
*
* As of now, SDL uses async callbacks to directly fill projectM's audio buffer.
*
* @todo Store audio samples internally and push them to projectM when requested.
*/
void FillBuffer(){};
protected:
/**
* @brief Opens the SDL audio device with the currently selected index.
* @return
*/
bool OpenAudioDevice();
/**
* @brief SDL audio capture callback.
*
* Called everytime if there is new data available in the audio recording buffer.
*
* @param userData
* @param stream
* @param len
*/
static void AudioInputCallback(void* userData, unsigned char* stream, int len);
projectm* _projectMHandle{nullptr}; //!< Handle if the projectM instance that will receive the audio data.
int32_t _currentAudioDeviceIndex{-1}; //!< Currently selected audio device index.
SDL_AudioDeviceID _currentAudioDeviceID{0}; //!< Device ID of the currently opened audio device.
uint32_t _channels{2};
constexpr static uint32_t _requestedSampleFrequency{44100}; //!< Requested sample frequency. Currently hardcoded as 44100 Hz, as this is what the spectrum analyzer expects.
uint32_t _requestedSampleCount{44100U / 60U}; //!< Requested audio buffer size. Determines how often SDL will call AudioInputCallback() with new data, and how much data is delivered on each call.
Poco::Logger& _logger{Poco::Logger::get("AudioCapture.SDL")}; //!< The class logger.
};