-
Notifications
You must be signed in to change notification settings - Fork 0
/
Audio.h
103 lines (85 loc) · 2.83 KB
/
Audio.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
103
// Peaberry CW - Transceiver for Peaberry SDR
// Copyright (C) 2015 David Turnbull AE9RB
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef AUDIO_H
#define AUDIO_H
#include <array>
#include <string>
#include <vector>
#include <windows.h>
#include <AudioClient.h>
#include <mmdeviceapi.h>
#include "Config.h"
extern const IID IID_IAudioClient;
extern const IID IID_IAudioRenderClient;
class Audio
{
friend class AudioWorkerThread;
public:
explicit Audio() : m_hThread(nullptr), m_exit_thread(false) {}
~Audio() { destroy(); }
void init();
void destroy();
const std::string get_error() const { return error; }
void start();
void stop();
private:
virtual int mutePaddingFrames();
virtual double transmitPaddingSecs();
void findPeaberry(IMMDeviceEnumerator *pEnumerator,
bool isTransmit,
IMMDevice **device,
IAudioClient **audioClient,
WAVEFORMATEXTENSIBLE *wavex,
const std::string &errstr);
void setPeaberryVolume(IMMDevice *device,
const std::string &errstr);
void initializeAudioClient(IMMDevice *device,
WAVEFORMATEX* wfx,
IAudioClient **audioClient,
bool *callbackMode,
UINT32 *bufferSize,
const std::string &errstr);
private:
void run();
static DWORD WINAPI thread_function(LPVOID lpParam);
void doReceive();
void doTransmit(int frames);
std::string error;
IMMDevice *receiveDevice = nullptr;
IAudioClient *receiveAudioClient = nullptr;
IAudioCaptureClient *receiveCaptureClient = nullptr;
HANDLE receiveEvent = nullptr;
UINT32 receiveBufferFrames = 0;
bool receiveCallbackMode = false;
IMMDevice *transmitDevice = nullptr;
IAudioClient *transmitAudioClient = nullptr;
IAudioRenderClient *transmitRenderClient = nullptr;
HANDLE transmitEvent = nullptr;
UINT32 transmitBufferFrames = 0;
bool transmitCallbackMode = false;
// Current audio thread handle.
HANDLE m_hThread;
// Flag to indicate the audio thread to stop.
volatile bool m_exit_thread;
std::vector<float> m_receive_buffer;
std::vector<float> m_receive_buffer_prev;
size_t m_receive_buffer_cnt;
bool m_muted = false;
size_t m_mute_zeros = 0;
size_t m_unmute_cntr = 0;
std::vector<float> m_unmute_envelope;
};
#endif // AUDIO_H